1.14.4 Keeping Colored Names in Anvils

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

  1. I'm trying to make a plugin which, when an item has a colored name and wants to rename/repair using an anvil, it will keep the colored name and can rename it using the rename function in the anvil while not losing the colored name. I've tried InventoryClickEvent like so
    Code (Java):
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {

            Inventory inv = e.getInventory();


            ItemStack item3 = e.getCurrentItem();


            Player player = (Player) e.getWhoClicked();

            if (inv instanceof AnvilInventory) {
                ItemStack item1 = inv.getItem(0);
                ItemStack item2 = inv.getItem(1);


                if (item3 != null && item3.getType() != Material.AIR && e.getSlot() == 2 ) {

                    if (item3.getEnchantmentLevel(Enchantment.ARROW_DAMAGE) > 5
                            || item3.getEnchantmentLevel(Enchantment.DAMAGE_ALL) > 10
                            || item3.getEnchantmentLevel(Enchantment.PROTECTION_ENVIRONMENTAL) > 5
                            || item3.getEnchantmentLevel(Enchantment.DIG_SPEED) > 5) {

                        if (Bindenchant.globalMap.get(player) > 0){
                            if (item2.getType() == Material.ENCHANTED_BOOK){
                                e.setCancelled(true);
                                player.sendMessage(ChatColor.RED + "You cannot enchant what is forged by gods!");
                            }else{
                                int a = Bindenchant.globalMap.get(player) - 1;

                                Bindenchant.globalMap.put(player, a);
                                if (item1.hasItemMeta()){
                                    ItemMeta itemMeta = item1.getItemMeta();
                                    String itemName = itemMeta.getDisplayName();
                                    ItemMeta itemMetaResult = item3.getItemMeta();
                                    itemMetaResult.setDisplayName(itemName);
                                    item3.setItemMeta(itemMetaResult);
                                    player.sendMessage(ChatColor.GREEN + "Successfully repaired item! Your remaining repair credit is " + a + ".");

                                }else{
                                    player.sendMessage(ChatColor.GREEN + "Successfully repaired item! Your remaining repair credit is " + a + ".");

                                }

    but this doesn't work, and only returns the normal renamed item, without the color. There are also no errors in the console. Is it possible to fix my code so it works or do i have to do the whole thing over?
     
  2. Try setting the color on PrepareAnvilEvent, that worked for me.
     
  3. what would this look like?
     
  4. I would ideally not spoonfeed you, but this is the easiest way to explain this.
    Code (Java):
    @EventHandler
    public void onPrepareAnvilEvent(PrepareAnvilEvent event) {
        //Get item in first slot
        ItemStack firstSlot = event.getInventory().getItem(0);

        //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().equals(Material.AIR) && resultItem.hasItemMeta()) {
            ItemMeta meta = resultItem.getItemMeta();
            if(resultItem.getItemMeta().hasDisplayName()) {

                //Set the display name
                meta.setDisplayName(firstSlot.getItemMeta().getDisplayName());

                //Set the resulting item
                event.setResult(resultItem);
            }
        }
    }