SHARE
TWEET

HeadComponent.java

Darker666 Sep 21st, 2013 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * CommandBook plugin for /head command
  3.  *
  4.  */
  5.  
  6. package com.sk89q.commandbook;
  7.  
  8. import org.bukkit.ChatColor;
  9. import org.bukkit.command.CommandSender;
  10. import org.bukkit.entity.Player;
  11.  
  12. //These seem to be necessary
  13. import com.sk89q.commandbook.util.ItemUtil;
  14. import com.sk89q.worldedit.blocks.ItemType;
  15. import org.bukkit.inventory.Inventory;
  16. import org.bukkit.inventory.ItemStack;
  17. import org.bukkit.inventory.meta.ItemMeta;
  18. import org.bukkit.inventory.meta.SkullMeta;
  19. import org.bukkit.Material;
  20. import static com.sk89q.commandbook.CommandBookUtil.giveItem;
  21.  
  22. import com.sk89q.commandbook.util.PlayerUtil;
  23. import com.sk89q.minecraft.util.commands.Command;
  24. import com.sk89q.minecraft.util.commands.CommandContext;
  25. import com.sk89q.minecraft.util.commands.CommandException;
  26. import com.sk89q.minecraft.util.commands.CommandPermissions;
  27. import com.zachsthings.libcomponents.ComponentInformation;
  28. import com.zachsthings.libcomponents.bukkit.BukkitComponent;
  29.  
  30. @ComponentInformation(friendlyName = "Mob head", desc = "Mob head experiment")
  31. public class HeadComponent extends BukkitComponent {
  32.     @Override
  33.     public void enable() {
  34.         registerCommands(Commands.class);
  35.     }
  36.  
  37.     public class Commands {
  38.  
  39.         @Command(aliases = {"head", "gethead"},
  40.                         usage = "[player]", desc = "Get a player's head",
  41.                         flags = "c", min = 0, max = 64)
  42.         @CommandPermissions({"commandbook.head"})
  43.         public void head(CommandContext args,CommandSender sender) throws CommandException {    
  44.             Iterable<Player> targets = null;
  45.             CommandBook.inst().checkPermission(sender, "commandbook.head");
  46.            
  47.             if (args.argsLength() == 0) {
  48.                 //Use current player
  49.                 targets = PlayerUtil.matchPlayers(PlayerUtil.checkPlayer(sender));
  50.             } else if (args.argsLength() == 1) {
  51.                 try {
  52.                   //Use matched players
  53.                   targets = PlayerUtil.matchPlayers(sender, args.getString(0));
  54.                 }  
  55.                 catch (CommandException e) {
  56.                   //use exact string if no players matched
  57.                   String name = args.getString(0);
  58.                   String displayName = "";
  59.                   //Only allow valid minecraft usernames
  60.                   if(!name.matches("^[a-zA-Z0-9_]{3,16}$")) {
  61.                     throw new CommandException("Invalid username. Head cannot be created.");
  62.                   }
  63.                   ItemStack head = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
  64.                   SkullMeta meta = (SkullMeta)head.getItemMeta();
  65.                   meta.setOwner(name);
  66.                  
  67.                   //Special coloring for known mob-heads
  68.                   if(name.equals("MHF_Pig"))
  69.                     displayName = ChatColor.RESET.toString()+ChatColor.LIGHT_PURPLE.toString() + "Pig's"+ChatColor.RESET.toString();
  70.                   else if(name.equals("MHF_PigZombie"))
  71.                     displayName = (ChatColor.RESET.toString()+ChatColor.GREEN.toString() + "Zombie pig's"+ChatColor.RESET.toString());
  72.                   else if(name.equals("MHF_Sheep"))
  73.                     displayName = (ChatColor.RESET.toString() + "Sheep's");
  74.                   else if(name.equals("MHF_Villager"))
  75.                     displayName = (ChatColor.RESET.toString()+ChatColor.DARK_RED.toString() + "Villager's"+ChatColor.RESET.toString());
  76.                   else if(name.equals("MHF_Blaze"))
  77.                     displayName = (ChatColor.RESET.toString()+ChatColor.GOLD.toString() + "Blaze's"+ChatColor.RESET.toString());
  78.                   else if(name.equals("MHF_Herobrine"))
  79.                     displayName = (ChatColor.RESET.toString()+ChatColor.YELLOW.toString()+ChatColor.BOLD.toString() + "Herobrine's"+ChatColor.RESET.toString());
  80.                   else
  81.                     displayName = (ChatColor.RESET.toString() + name);
  82.                   //Set display name
  83.                   meta.setDisplayName(displayName+" head");
  84.                   head.setItemMeta(meta);
  85.                   PlayerUtil.checkPlayer(sender).getInventory().addItem(head);
  86.                   sender.sendMessage(ChatColor.YELLOW.toString() + "Giving you "+displayName+ChatColor.YELLOW+" head.");
  87.                   return;
  88.                 }
  89.             }
  90.             ItemStack head = null;
  91.             int count = 0;
  92.             //Is it fine to add items in loop?
  93.            
  94.             for (Player player : targets) {
  95.               //head = ItemStack.makeHead(player.getName());//(Material.SKULL_ITEM, 1, (short) 3);
  96.               head = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
  97.               SkullMeta meta = (SkullMeta)head.getItemMeta();
  98.               meta.setOwner(player.getName());
  99.               meta.setDisplayName(ChatColor.RESET + player.getDisplayName() + "'s Head");
  100.               head.setItemMeta(meta);
  101.               //head.makeHead(player.getName());  
  102.               PlayerUtil.checkPlayer(sender).getInventory().addItem(head);
  103.               count++;
  104.               //CommandBookUtil.giveItem(sender, head, 1, sender, PlayerComponent.this);
  105.             }  
  106.             sender.sendMessage(ChatColor.YELLOW.toString() + Integer.toString(count)+" heads given.");
  107.         }
  108.     }
  109. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top