codename_B's list of plugins under 50 lines of code AKA the under 50 challenge

Discussion in 'Resources' started by codename_B, Aug 23, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Ultimate_n00b

    xD
    Edited.
     
  2. Offline

    Digi

    Ultimate_n00b
    Not fixed, conditions are evaluated in order, and you're casting before you're checking.
     
  3. Offline

    Ultimate_n00b

    >_> I did it at like 2 a.m., I figured I made a few mistakes.
     
  4. Offline

    Digi

    Ultimate_n00b
    But also, the hasPermission() method is available for CommandSender as well, so casting to Player was not required at all.
     
  5. Offline

    Ultimate_n00b

    Hm, I actually did not know that. Probably saved me from a few errors before though.
     
  6. Offline

    RROD

    Smelta - Buttermilk Edition
    Functionality: Smelt ores using commands.
    Lines: 44
    Permission: smelta.perform

    Code:java
    1. package org.toastedtruth.smelta;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Smelta extends JavaPlugin {
    12. @Override
    13. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    14. if (!(sender instanceof Player)) {
    15. sender.sendMessage("You must be in game to perform that command. ");
    16. return true;
    17. }
    18. if (!sender.hasPermission("smelta.perform")) {
    19. sender.sendMessage(ChatColor.RED + "You don't have permission. ");
    20. return true;
    21. }
    22. ItemStack toSmelt = ((Player)sender).getItemInHand();
    23. Material type = toSmelt.getType();
    24. if (type.equals(Material.AIR)) {
    25. sender.sendMessage(ChatColor.RED + "You're not holding an item to smelt! ");
    26. return true;
    27. } else if (type.equals(Material.COAL_ORE)) toSmelt.setType(Material.COAL);
    28. else if (type.equals(Material.IRON_ORE)) toSmelt.setType(Material.IRON_INGOT);
    29. else if (type.equals(Material.LAPIS_ORE)) {
    30. toSmelt.setType(Material.INK_SACK);
    31. toSmelt.setDurability((short)4);
    32. }
    33. else if (type.equals(Material.GOLD_ORE)) toSmelt.setType(Material.GOLD_INGOT);
    34. else if (type.equals(Material.DIAMOND_ORE)) toSmelt.setType(Material.DIAMOND);
    35. else if (type.equals(Material.REDSTONE_ORE)) toSmelt.setType(Material.REDSTONE);
    36. else if (type.equals(Material.EMERALD_ORE)) toSmelt.setType(Material.EMERALD);
    37. else {
    38. sender.sendMessage(ChatColor.RED + "Can't smelt that item! ");
    39. return true;
    40. }
    41. sender.sendMessage(ChatColor.GOLD + "Magically smelted the item in your hand. ");
    42. return true;
    43. }
    44. }
     
  7. Offline

    Skyshayde

    Exploding Arrows, Always Edition
    40 lines, permission is arrow.explode

    Code:java
    1.  
    2. package com.gmail.skyshayde;
    3.  
    4. import org.bukkit.Material;
    5. import org.bukkit.entity.Arrow;
    6. import org.bukkit.entity.Entity;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.entity.ProjectileHitEvent;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class TNTArrows extends JavaPlugin implements Listener {
    15.  
    16. public void onEnable() {
    17. getServer().getPluginManager().registerEvents(this, this);
    18. }
    19.  
    20. @EventHandler
    21. void onArrowHit(ProjectileHitEvent event) {
    22. Entity entity = event.getEntity();
    23. if (entity instanceof Arrow) {
    24. Arrow arrow = (Arrow) entity;
    25. Entity shooter = arrow.getShooter();
    26. if (shooter instanceof Player) {
    27. Player player = (Player) shooter;
    28. if(player.hasPermission("arrow.explode"))
    29. if (player.getInventory().contains(Material.TNT)) {
    30. ItemStack tnt = player.getInventory().getItem(player.getInventory().first(Material.TNT));
    31. if (tnt.getAmount() > 1) {
    32. tnt.setAmount(tnt.getAmount() - 1);
    33. player.getWorld().createExplosion(arrow.getLocation(), 5.0F);
    34. arrow.remove();
    35. }
    36. }
    37.  
    38. }
    39. }
    40. }
    41. }
    42.  
     
  8. Offline

    Digi

    RROD
    Bad design, I suggest you retrieve the recipe for the ingredient instead as other plugins can add and even remove smelting recipes, not to mention the game itself, therefore it's a stuck and antisocial plugin :p
     
    RROD likes this.
  9. Offline

    Ultimate_n00b

    codename_B You ever going to update the first post?
     
  10. Offline

    jacklin213

    right , a few plugins on the first page is now outdated
     
  11. Offline

    Minecrell

    Just a simple plugin that replaces the color codes in books after editing. No commands needed.
    Requires latest dev build #2785 to work.

    Download: Coming soon

    Code:java
    1. import org.bukkit.ChatColor;
    2. import org.bukkit.entity.Player;
    3. import org.bukkit.event.EventHandler;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.event.player.PlayerEditBookEvent;
    6. import org.bukkit.inventory.meta.BookMeta;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class ColorBooks extends JavaPlugin implements Listener {
    10.  
    11. @Override
    12. public void onEnable() {
    13. this.getServer().getPluginManager().registerEvents(this, this);
    14. this.getLogger().info(this.getName() + " enabled.");
    15. }
    16.  
    17. @Override
    18. public void onDisable() {
    19. this.getLogger().info(this.getName() + " disabled.");
    20. }
    21.  
    22. @EventHandler
    23. public void onPlayerEditBook(final PlayerEditBookEvent event) {
    24. final Player player = event.getPlayer();
    25.  
    26. if (player.hasPermission("colorbooks.use")) {
    27. final BookMeta book = event.getNewBookMeta();
    28.  
    29. for (int i = 1; i <= book.getPageCount(); i++) {
    30. book.setPage(i, ChatColor.translateAlternateColorCodes('&', book.getPage(i)));
    31. }
    32.  
    33. event.setNewBookMeta(book);
    34. }
    35. }
    36.  
    37. }

    Edit: Maybe it is the first plugin using the new event? (I don't know) :p
     
  12. Offline

    YoFuzzy3

    Minecrell
    That can be compacted down so much. :p
    Code:java
    1. package com.fuzzoland.ColourBooks;
    2. import org.bukkit.event.*;
    3. import org.bukkit.event.player.PlayerEditBookEvent;
    4. import org.bukkit.inventory.meta.BookMeta;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6. public class ColourBooks extends JavaPlugin implements Listener{
    7. public void onEnable(){
    8. getServer().getPluginManager().registerEvents(this, this);
    9. }
    10. @EventHandler
    11. public void onPlayerEditBook(PlayerEditBookEvent event){
    12. if(event.getPlayer().hasPermission("ColourBooks.use")){
    13. BookMeta metaData = event.getNewBookMeta();
    14. for(int i = 1; i <= metaData.getPageCount(); i++){
    15. metaData.setPage(i, metaData.getPage(i).replaceAll("(&([a-f0-9l-or]))", "\u00A7$2"));
    16. }
    17. event.setNewBookMeta(metaData);
    18. }
    19. }
    20. }
     
    SupaHam likes this.
  13. Offline

    Minecrell

    It is under 50 lines so I don't care. I know I it can be compacted. :D
     
  14. Offline

    ACStache

    Skyshayde what happens if the player has only 1 tnt in their inv? :)
     
  15. Offline

    Skyshayde

    ACStache Nothing, I can probably fix it though. I'll take a look.
     
  16. Offline

    Skyost

  17. Offline

    LaxWasHere

  18. Offline

    Cirno

    Code:java
    1. package com.tenko;
    2.  
    3. import java.util.Iterator;
    4. import java.net.URL;
    5. import javax.imageio.ImageIO;
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandExecutor;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.map.MapCanvas;
    13. import org.bukkit.map.MapPalette;
    14. import org.bukkit.map.MapRenderer;
    15. import org.bukkit.map.MapView;
    16. import org.bukkit.map.MapView.Scale;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public class ImgMap extends JavaPlugin {
    20. @Override
    21. public void onEnable(){
    22. getCommand("map").setExecutor(new CommandExecutor(){
    23. @Override
    24. public boolean onCommand(final CommandSender cs, Command c, String l, final String[] args){
    25. if(!(cs instanceof Player) && ((Player)cs).getItemInHand().getType() == Material.MAP)
    26. cs.sendMessage("You must be a player wielding a map!");
    27. MapView view = Bukkit.getMap(((Player)cs).getItemInHand().getDurability());
    28. Iterator<MapRenderer> mr = view.getRenderers().iterator();
    29. while(mr.hasNext()){
    30. view.removeRenderer(mr.next());
    31. }
    32. view.setScale(Scale.FARTHEST);
    33. view.addRenderer(new MapRenderer(){
    34. boolean hasRendered = false;
    35. @Override
    36. public void render(MapView v, MapCanvas c, Player p) {
    37. if(!hasRendered) try {
    38. c.drawImage(0, 0, MapPalette.resizeImage(ImageIO.read(new URL(args[0]))));
    39. hasRendered = true;
    40. } catch (Exception e){
    41. cs.sendMessage("Failed to render image!");
    42. }
    43. }
    44.  
    45. });
    46. return true;
    47. }
    48. });
    49. }
    50. }


    ImgMap in 50 lines?
     
  19. Offline

    Skyost

    Okay :

    Code:java
    1. package com.skyost.pcl;
    2.  
    3. import java.util.logging.Handler;
    4. import java.util.logging.LogRecord;
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class ConsolePlayerListenerPlugin extends JavaPlugin {
    13. Handler han;
    14.  
    15. public boolean onCommand(final CommandSender sender, Command cmd, String label, String[] args){
    16. if(cmd.getName().equalsIgnoreCase("listen")) {
    17. if(sender instanceof Player) {
    18. if(sender.hasPermission("console.playerlistener.listen")) {
    19. if(args[0].equalsIgnoreCase("start") || args[0].equalsIgnoreCase("st")) {
    20. sender.sendMessage(ChatColor.GREEN + "Getting alls messages from the console...");
    21. han = new Handler() {
    22. @Override
    23. public void close() throws SecurityException { /* Do nothing... */ }
    24. @Override
    25. public void flush() { /* Do nothing... */ }
    26. @Override
    27. public void publish(LogRecord logRecord) { sender.sendMessage("[" + logRecord.getLevel().toString() + "]" + " - " + logRecord.getMessage()); }
    28. };
    29. Bukkit.getServer().getLogger().addHandler(han);
    30. }
    31. else if(args[0].equalsIgnoreCase("stop") || args[0].equalsIgnoreCase("sp")) {
    32. sender.sendMessage(ChatColor.RED + "Stop getting alls messages from the console...");
    33. Bukkit.getServer().getLogger().removeHandler(han);
    34. }
    35. else {
    36. return false;
    37. }
    38. }
    39. }
    40. else {
    41. sender.sendMessage(ChatColor.RED + "You don't have permission to do this !");
    42. }
    43. }
    44. else {
    45. sender.sendMessage(ChatColor.RED + "[ConsolePlayerListener] You can't do this from the console !");
    46. }
    47. return true;
    48. }
    49. }


    PasteBin link.
     
  20. Offline

    krazytraynz

    (If it's from a plugin we already have does it count?)

    Hardcore Mode plugin (Deletes .DAT file):
    Code:java
    1. package com.github.KrazyTraynz;
    2.  
    3. import java.io.File;
    4.  
    5. import org.bukkit.World;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.entity.PlayerDeathEvent;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class HardcoreMode extends JavaPlugin implements Listener{
    13.  
    14. public void onEnable(){
    15. getServer().getPluginManager().registerEvents(this, this);
    16. getLogger().info("Hardcore enabled!");
    17. }
    18.  
    19. public void onDisable(){
    20. getLogger().info("Hardcore disabled!");
    21. }
    22.  
    23. @EventHandler
    24. public void onPlayerDeath(PlayerDeathEvent e){
    25. World w = e.getEntity().getLocation().getWorld();
    26. File file = new File(w.getWorldFolder() + "/players/" + e.getEntity().getName() + ".dat");
    27. file.delete();
    28. }
    29. }
     
  21. Offline

    Hoolean Retired Staff

    krazytraynz

    You'd have to add the player.loadData() method to that wouldn't you, otherwise a Player could overwrite the file? :p
     
  22. Offline

    bobacadodl

    Leash unleashable mobs! (Creepers, Withers, Blazes, Villagers, and more!)
    Code:java
    1. package com.bobacadodl.followme;
    2.  
    3. import java.lang.reflect.Method;
    4.  
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Entity;
    7. import org.bukkit.entity.EntityType;
    8. import org.bukkit.entity.LivingEntity;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.EventPriority;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerInteractEntityEvent;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class FollowMe extends JavaPlugin implements Listener{
    17. public void onEnable(){
    18. getServer().getPluginManager().registerEvents(this, this);
    19. }
    20. @EventHandler(priority=EventPriority.HIGHEST)
    21. public void onRightClick(PlayerInteractEntityEvent e){
    22. Player p = e.getPlayer();
    23. if(p.getItemInHand().getType()==Material.LEASH){
    24. Entity c = e.getRightClicked();
    25. if(!(c instanceof LivingEntity) || (c.getType()==EntityType.PLAYER) || (c.getType()==EntityType.ENDER_DRAGON)) return;
    26. net.minecraft.server.v1_6_R2.Entity nmsPlayer = ((org.bukkit.craftbukkit.v1_6_R2.entity.CraftEntity) c).getHandle();
    27. net.minecraft.server.v1_6_R2.NBTTagCompound compound = new net.minecraft.server.v1_6_R2.NBTTagCompound();
    28. nmsPlayer.c(compound); //get tag
    29. compound.setByte("Leashed", (byte) 1); //edit tag
    30. net.minecraft.server.v1_6_R2.NBTTagCompound leashcompound = new net.minecraft.server.v1_6_R2.NBTTagCompound();
    31. leashcompound.setName("Leash");
    32. leashcompound.setLong("UUIDMost", p.getUniqueId().getMostSignificantBits());
    33. leashcompound.setLong("UUIDLeast", p.getUniqueId().getLeastSignificantBits());
    34. compound.setCompound("Leash", leashcompound);
    35. Method[] methods = net.minecraft.server.v1_6_R2.Entity.class.getMethods(); //save tag
    36. for (Method method : methods) {
    37. if (((method.getName() != "f") && (method.getName() != "a")) || (method.getParameterTypes().length != 1) || (method.getParameterTypes()[0] != net.minecraft.server.v1_6_R2.NBTTagCompound.class)) continue;
    38. try {
    39. method.setAccessible(true);
    40. method.invoke(nmsPlayer, new Object[] { compound });
    41. }
    42. catch (Exception ex) {
    43. ex.printStackTrace();
    44. return;
    45. }
    46. }
    47. e.setCancelled(true);
    48. }
    49. }
    50. }


    edit: for some reason, this makes you need to click with the leash twice to leash mobs :/ Oh well, it works.
     
    codename_B likes this.
  23. Offline

    Henzz

    Cheap chat logger lol
    PHP:
    package net.bigbadcraft.basicchatlogger;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.logging.Level;
     
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    public class 
    Main extends JavaPlugin implements Listener {
        
    File file;
        public 
    void onEnable() {
            
    this.file = new File(this.getDataFolder(), "chatlog.txt");
            
    this.getDataFolder().mkdir();
            if (!(
    file.exists())) {
                try {
                    
    getLogger().log(Level.WARNINGfile.getName() + " doesn't exist, creating one...");
                    
    file.createNewFile();
                    
    getLogger().log(Level.INFOfile.getName() + " has been successfully created!");
                } catch (
    IOException ex) {
                    
    getLogger().log(Level.SEVEREfile.getName() + " could not be created!");
                    
    ex.printStackTrace();
                }
            } else { 
    getLogger().log(Level.INFO"Successfully loaded " file.getName()); }
            
    getServer().getPluginManager().registerEvents(thisthis);
        }
        public 
    void onDisable(){}
        @
    EventHandler(priority EventPriority.HIGHEST)
        
    void onChat(AsyncPlayerChatEvent event) {
            
    Date date Calendar.getInstance().getTime();
            
    String name event.getPlayer().getName();
            
    String message event.getMessage();
            
    this.write(datenamemessage);
        }
        public 
    void write(Date dateString nameString message) {
            try(
    BufferedWriter out = new BufferedWriter(new FileWriter(this.filetrue))) {
                
    out.append("[" date "]" " " name ": " message);
                
    out.newLine();
                
    out.close();
            } catch (
    IOException e) {
                
    getLogger().log(Level.SEVERE"Could not write to " file.getName() + "!");
                
    e.printStackTrace();
            }
        }
    }
     
  24. Offline

    Hoolean Retired Staff

    Henzz

    Just a tip, if you're just monitoring an event and not doing anything else to it, I'd make your EventHandler annotation arguments look like this:
    Code:
    @EventHandler(priority = EventPriority.MONITOR,ignoreCancelled=true)
    This means that the event will be handled to your Listener after all plugins have had a chance to modify it (it won't change after you've recieved it). Additionally, ignoreCancelled means if a Player chats and it is cancelled it will not be logged ^_^
     
    Henzz likes this.
  25. Offline

    Henzz

    Hoolean
    I figured that, but thought it wouldn't be needed since it's just a resource thread. But cheers for the let know :)
     
    Hoolean likes this.
  26. Offline

    codename_B

    I'm so happy this is still going :D
     
  27. Offline

    Hoolean Retired Staff

    #codenameBlegacy
     
  28. Offline

    historio

    codename_B DeadlyPie (50 lines of code):

    Code:java
    1. package me.historio13.DeadlyPie;
    2. import java.util.ArrayList;
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.command.*;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.player.PlayerInteractEvent;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.inventory.meta.ItemMeta;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14. public class DeadlyPie extends JavaPlugin implements Listener {
    15. @Override
    16. public void onEnable() {
    17. getServer().getPluginManager().registerEvents(this, this);
    18. }
    19. @EventHandler
    20. public void onPlayerInteract(PlayerInteractEvent e) {
    21. Player player = e.getPlayer();
    22. if (e.getAction() == Action.RIGHT_CLICK_BLOCK
    23. || e.getAction() == Action.RIGHT_CLICK_AIR) {
    24. if (player.getItemInHand().getType() == Material.PUMPKIN_PIE) {
    25. player.setHealth(0);
    26. }
    27. }
    28. }
    29. }
    30. public boolean onCommand(CommandSender sender, Command cmd,
    31. String commandLabel, String[] args) {
    32. Player player = (Player) sender;
    33. if (commandLabel.equalsIgnoreCase("DeadlyPie")) {
    34. ItemStack DeadlyPie = new ItemStack(Material.PUMPKIN_PIE);
    35. ItemMeta DeadlyPieMeta = DeadlyPie.getItemMeta();
    36. ArrayList<String> description = new ArrayList<String>();
    37. description.add(ChatColor.RED + "I'm Deadly");
    38. DeadlyPieMeta.setLore(description);
    39. DeadlyPieMeta.setDisplayName(ChatColor.YELLOW + "DeadlyPie");
    40. DeadlyPie.setItemMeta(DeadlyPieMeta);
    41. player.sendMessage(ChatColor.BLACK + "[" + ChatColor.YELLOW
    42. + "DeadlyPie" + ChatColor.BLACK + "] " + ChatColor.DARK_RED
    43. + "Be very careful with the pie......");
    44. player.getInventory().addItem(DeadlyPie);
    45. }
    46. return false;
    47. }
    48. }
     
  29. Offline

    Hoolean Retired Staff

  30. Offline

    codename_B

    I like this one :)

    Oh nifty! :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
Thread Status:
Not open for further replies.

Share This Page