Solved How to change item name of result item in anvil?

  • After careful consideration and due to limited usage, we’ve made the decision to discontinue the PaperMC forums. Moving forward, we recommend using Hangar for plugin uploads, and for all other community discussions and support, please join us on Discord.

bambolumba

New member
Dec 14, 2024
6
0
1
I don't really understand how to do this correctly. DisplayName seems to be changing, but the name of item in result slot remains the same. Here is my code. Sorry if it`s dumb, this is the first time I've encountered this
Java:
AnvilInventory anvilInventory = event.getInventory();
        if (anvilInventory.getSecondItem() == null) {
            if (anvilInventory.getResult() != null) {
                ItemStack resultItem = anvilInventory.getResult();
                ItemMeta resultItemMeta = resultItem.getItemMeta();
                ProjectAnvils.getPlugin(ProjectAnvils.class).getLogger().info("Old display name " + resultItemMeta.displayName());
                resultItemMeta.displayName(Component.text("dog"));
                resultItem.setItemMeta(resultItemMeta);
                ProjectAnvils.getPlugin(ProjectAnvils.class).getLogger().info("New display name " + resultItemMeta.displayName());
                anvilInventory.setResult(resultItem);
                ProjectAnvils.getPlugin(ProjectAnvils.class).getLogger().info(String.valueOf(resultItem));
            }
        }

And here is what i get in console
[02:58:34 INFO]: [ProjectAnvils] Old display name TextComponentImpl{content="Трезубецa", style=StyleImpl{obfuscated=not_set, bold=not_set, strikethrough=not_set, underlined=not_set, italic=not_set, color=null, clickEvent=null, hoverEvent=null, insertion=null, font=null}, children=[]}
[02:58:34 INFO]: [ProjectAnvils] New display name TextComponentImpl{content="dog", style=StyleImpl{obfuscated=not_set, bold=not_set, strikethrough=not_set, underlined=not_set, italic=not_set, color=null, clickEvent=null, hoverEvent=null, insertion=null, font=null}, children=[]}
[02:58:34 INFO]: [ProjectAnvils] ItemStack{TRIDENT x 1, UNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name="dog"}}
 

bambolumba

New member
Dec 14, 2024
6
0
1
Ok, I should use event.setResult(), not anvilInventory.setResult();

So it should be like that:
Java:
        AnvilInventory anvilInventory = event.getInventory();
        AnvilView anvilView = event.getView();
        if (anvilInventory.getSecondItem() == null) {
            if (event.getResult() != null) {
                
                ItemStack resultItem = event.getResult();
                ItemMeta resultItemMeta = resultItem.getItemMeta();
                
                resultItemMeta.displayName(configManager.buildMessage(anvilView.getRenameText(), true));
                resultItem.setItemMeta(resultItemMeta);
                
                event.setResult(resultItem);
                
            }
        }