Solved Tracking display names on equipment

Discussion in 'Spigot Plugin Development' started by MineStein, Apr 17, 2018.

  1. I need some sort of effective and not hacky way to track display names on equipment such as armor. When a piece of armor is equipped the display name is nullified.
     
  2. md_5

    Administrator Developer

    Hmm, are you sure the display name is reset?
    Try testing on pure spigot/vanilla, I think you may have a plugin nulling it.
     
  3. This plugin is the only one running on the server.
     
  4. I'm still looking for a resolution to this issue. I'm worried that I might have to do some sort of hacky method wherein I track what equipment they are wearing, but that could get messy very fast.
     
  5. I created this bit of code,

    Code (Text):
    ItemStack item = new ItemStack(Material.DIAMOND_CHESTPLATE);
    ItemMeta im = item.getItemMeta();
    player.getInventory().setChestplate(item);
    player.sendMessage(player.getInventory().getChestplate().getItemMeta().getDisplayName()); // returns nothign
    im.setDisplayName("test")item.setItemMeta(im);
    player.getInventory().setChestplate(item);
     player.sendMessage(player.getInventory().getChestplate().getItemMeta().getDisplayName()); // returns test

    So if the displayname isnt set, it wont do anything, so i created this bit of code, this will send the displayname of every armor piece to the player
    Code (Text):
    ItemStack[] armor = player.getInventory().getArmorContents();
    for(ItemStack item: armor) {
         String displayname = "nothing here";
          if(item != null) {
               if(item.hasItemMeta()) {
                      if(item.getItemMeta().hasDisplayName()) {
                              displayname = item.getItemMeta().getDisplayName();
                       }else {
                               displayname = CraftItemStack.asNMSCopy(item).getName();
                        }                              
                }else {
                        displayname = CraftItemStack.asNMSCopy(item).getName();
                }                      
           }
           player.sendMessage(displayname);
    }
     
    #5 x313, Apr 17, 2018
    Last edited: Apr 17, 2018
  6. I have to agree this sounds pretty weird, you sure your plugin isn't doing it? This would be a bug others would have noticed I'm sure... seems ok when I try it:

    [​IMG]
     
  7. I should be clear that the display name still appears, but I debugged thoroughly and getting the display name of the item returns null.
     
  8. Can you show us your code?
     
  9. Code (Text):
    public class CosmeticTask extends BukkitRunnable {

        private ItemStack getColorArmor(Material m, Color c) {
            ItemStack i = new ItemStack(m, 1); {
                LeatherArmorMeta meta = (LeatherArmorMeta) i.getItemMeta();

                meta.setColor(c);

                i.setItemMeta(meta);
            }

            return i;
        }

        private String stripDisplayName(ItemStack i) {
            return ChatColor.stripColor(i.getItemMeta().getDisplayName());
        }

        @Override
        public void run() {
            Random r = ThreadLocalRandom.current();
            Color c = Color.fromRGB(r.nextInt(255), r.nextInt(255), r.nextInt(255));

            for (Player p : Bukkit.getServer().getOnlinePlayers()) {
                ItemStack helmet = p.getInventory().getHelmet();
                ItemStack chestplate = p.getInventory().getChestplate();
                ItemStack leggings = p.getInventory().getLeggings();
                ItemStack boots = p.getInventory().getBoots();

                if (helmet != null && helmet.getType().equals(Material.LEATHER_HELMET) && stripDisplayName(helmet).equalsIgnoreCase("Reward Helmet")) {
                    p.getInventory().setHelmet(getColorArmor(Material.LEATHER_HELMET, c));
                }

                if (chestplate != null && chestplate.getType().equals(Material.LEATHER_CHESTPLATE) && stripDisplayName(chestplate).equalsIgnoreCase("Reward Chestplate")) {
                    p.getInventory().setChestplate(getColorArmor(Material.LEATHER_CHESTPLATE, c));
                }

                if (leggings != null && leggings.getType().equals(Material.LEATHER_LEGGINGS) && stripDisplayName(chestplate).equalsIgnoreCase("Reward Leggings")) {
                    p.getInventory().setLeggings(getColorArmor(Material.LEATHER_LEGGINGS, c));
                }

                if (boots != null && boots.getType().equals(Material.LEATHER_BOOTS) && stripDisplayName(boots).equalsIgnoreCase("Reward Boots")) {
                    p.getInventory().setBoots(getColorArmor(Material.LEATHER_BOOTS, c));
                }
            }
        }
    }
     
  10. Every time you color a piece of armor, you are re-creating it:

    Code (Text):
    private ItemStack getColorArmor(Material m, Color c) {
            ItemStack i = new ItemStack(m, 1); {
                LeatherArmorMeta meta = (LeatherArmorMeta) i.getItemMeta();

                meta.setColor(c);

                i.setItemMeta(meta);
            }

            return i;
        }
    That's going to lose any metadata (like display name) on the item the player is wearing. Rather than re-creating and re-equipping items each time, I think you can just modify the one they are wearing.
     
  11. Oh shoot, you're right. Thank you!
     
    • Friendly Friendly x 1

Share This Page