Changing an item name in an other one IN COLOR

Discussion in 'Plugin Development' started by Tempetek, Nov 8, 2012.

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

    Tempetek

    Hello everyone,
    So I have a server and we had an awesome idea : Create a lot of new item and rename it with color like orange, green etc... For make a "RPG" side. So we have already the Anvil for rename the item and it appear to be easy to add a color to the item, but never created a bukkit plugin. I've tried a simple thing in 15 min but it finally seem too hard and so I post here for search help on the question :

    How can I add color to the renamed tool ? Just that

    Here is my try ( xD I know this is bad coded ) :
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            if(cmd.getName().equalsIgnoreCase("rename"))
            { 
                if (!(sender instanceof Player)) {
                    sender.sendMessage("This command can only be run by a player.");
                }
                else
                {
                    ChatColor color = ChatColor.getByChar(args[1]);
                    Player player = (Player) sender;
                    PlayerInventory inventory = player.getInventory();
                    ItemStack itemstack = inventory.getItemInHand();
               
                    if (itemstack != null)
                    {
                        String itemname = itemstack.toString();
                        String coloreditemname = color + itemname;
                        itemname = coloreditemname;
                    }
                }
                return true;
            }
            return false;
        }
     
  2. Offline

    Ewe Loon

    you might be able to by adding a color code
    eg "§f" = white "§4" = red
    § is types by holding alt and typing 0167 on the numpad
    any of "0123456789abcdef" can be used after §

    you could also use ChatColor.RED etc
     
  3. Offline

    cman1885

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
      if(cmd.getName().equalsIgnoreCase("rename")){
        if(sender instanceof Player){
          Player p = (Player)sender;
          if(p.hasPermission("Renamer.rename")||p.isOp()){
            if(p.getItemInHand()!=null&&args.length>0){
              StringBuilder sb = new StringBuilder();
              for(String s: args){
                sb.append(s).append(" ");
              }
              Map<Enchantment, Integer> enchants = new HashMap<Enchantment, Integer>();
              enchants = p.getItemInHand().getEnchantments();
              ItemStack iss = p.getItemInHand();
              CraftItemStack css = new CraftItemStack(iss);
              net.minecraft.server.ItemStack nms = css.getHandle();
              NBTTagCompound tag = nms.tag;
              tag = new NBTTagCompound();
              tag.setCompound("display", new NBTTagCompound());
              nms.tag = tag;
              tag = nms.tag.getCompound("display");
              tag.setString("Name", sb.toString().replaceAll("&", "§"));
              nms.tag.setCompound("display", tag);
              for(Enchantment e: enchants.keySet())
                css.addUnsafeEnchantment(e, enchants.get(e));
              p.getInventory().setItemInHand(css);
              returntrue;
            }else{
              p.sendMessage("§cYou must provide a name and be holding an item.");
            }
          }
        }
      }
    returntrue;
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  4. Offline

    Tempetek

    Thanks very much ! It work nice !
     
Thread Status:
Not open for further replies.

Share This Page