Guest User

ItemRenamer

a guest
Mar 8th, 2016
127
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package rename;
  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.meta.ItemMeta;
  9. import org.bukkit.plugin.java.JavaPlugin;
  10.  
  11. public class ItemRenamer extends JavaPlugin{
  12.    
  13.     @Override
  14.     public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args){
  15.        
  16.         if(!(sender instanceof Player))
  17.             return true;
  18.        
  19.         Player player = (Player) sender;
  20.        
  21.         if(cmd.getName().equalsIgnoreCase("rename")){
  22.             if (!sender.hasPermission("rename.use")) return true;
  23.             if(args.length == 0)
  24.                 return false;
  25.            
  26.             String newName = "";
  27.            
  28.             for(String arg : args){
  29.                 if(newName.length() > 0)
  30.                     newName += "";
  31.                 newName+= arg;
  32.             }
  33.            
  34.             newName = ChatColor.translateAlternateColorCodes('&', newName);
  35.            
  36.             if(player.getItemInHand() == null || player.getItemInHand().getType() == Material.AIR){
  37.                 player.sendMessage(ChatColor.RED + "You must hold item that you want to rename!");
  38.                 return true;
  39.             }
  40.            
  41.             ItemMeta meta = player.getItemInHand().getItemMeta();
  42.             meta.setDisplayName(newName);
  43.             player.getItemInHand().setItemMeta(meta);
  44.            
  45.             player.sendMessage(ChatColor.DARK_AQUA + "Name changed to: " + ChatColor.RESET + newName);
  46.         }      
  47.         return true;
  48.     }
  49.  
  50. }
RAW Paste Data