1.14.4 Keeping Displayname Error

Discussion in 'Spigot Plugin Development' started by AVCK, Jan 13, 2020.

  1. I'm making a plugin that will keep the displayname color of an item, but this bit of code is not working. I've put in the debugging broadcast messages and they work fine, but the displayname at the end is still the uncolored version. Any help with this?
    Code (Java):
        @EventHandler
        public void onPrepareAnvil(PrepareAnvilEvent event) {
            //Get item in first slot
            ItemStack firstSlot = event.getInventory().getItem(0);
            Bukkit.broadcastMessage("1");
            //Get resulting item of inventory
            ItemStack resultItem = event.getResult();

            //Check if resulting item is valid (you should probably also do this for firstSlot)
            if(resultItem.getType() != Material.AIR && resultItem.hasItemMeta()) {
                ItemMeta meta = resultItem.getItemMeta();
                Bukkit.broadcastMessage("2");
                if(resultItem.getItemMeta().hasDisplayName()) {

                    //Set the display name
                    meta.setDisplayName(firstSlot.getItemMeta().getDisplayName());
                    Bukkit.broadcastMessage("3");
                    //Set the resulting item
                    event.setResult(resultItem);
                }
            }
        }
     
  2. You forgot set ItemMeta.


    Code (Text):
    if(resultItem.getType() != Material.AIR && resultItem.hasItemMeta()) {
                ItemMeta meta = resultItem.getItemMeta();
                Bukkit.broadcastMessage("2");
                if(resultItem.getItemMeta().hasDisplayName()) {

                    //Set the display name
                    meta.setDisplayName(firstSlot.getItemMeta().getDisplayName());
    resultitem.setItemMeta(meta);
                    Bukkit.broadcastMessage("3");
                    //Set the resulting item
                    event.setResult(resultItem);
                }
            }
    I edit without IDE so please check if syntax is fine.
     
  3. There is another problem with this. When I take the item out of the anvil, it reverts to the old name. How can i prevent this from happening?[​IMG]
    [​IMG]
     
    #3 AVCK, Jan 13, 2020
    Last edited: Jan 13, 2020
  4. To resultItem in anvil is setted item with colored name?
     
  5. It shows up with the colored name in the result slot properly, but when I finish the transaction by either repairing it or by renaming it, the name reverts back to the original name.
     
  6. Try use the scheduler in 1 sec after take the item out of the anvil
     
  7. Try in invetoryclickevent if player click into result slot in anvil menu - give that but with name