Question item displayName problem

tw_seer67875

New member
Mar 5, 2023
2
0
1
How do I get the translate key using adventure?
What I want to end up with is block.minecraft.anvil
At present, I can only use this stupid method to get it, and when the item is renamed, the Component of the two seems to be different, which makes me puzzled...

Java:
String json = GsonComponentSerializer.gson().serialize(item.displayName());
JSONObject jsonObject = new JSONObject(json);
String key = jsonObject     
        .getJSONArray("with")
        .getJSONObject(0)
        .getJSONArray("extra")
        .getJSONObject(0)  
        .getString("translate");

Use anvil and rename it to test as an example

before renaming

Code:
{"color":"white","hoverEvent":{"action":"show_item","contents":{"id":"minecraft:anvil"}},"translate":"chat.square_brackets","with":[{"extra":[{"translate":"block.minecraft.anvil"}],"text":""}]}
after renaming
Code:
 {"color":"white","hoverEvent":{"action":"show_item","contents":{"id":"minecraft:anvil","tag":"{display:{Name:\u0027{\"text\":\"test\"}\u0027}}"}},"translate":"chat.square_brackets","with":[{"italic":true,"extra":[{"text":"test"}],"text":""}]}
 
Version Output
This server is running Paper version git-Paper-307 (MC: 1.19.2) (Implementing API version 1.19.2-R0.1-SNAPSHOT) (Git: 476ef25)
Last edited:

Noah

Paper Developer
Staff member
Jan 4, 2022
44
6
16
8
The Netherlands
Haven't tested it, but something like this should probably work:
Java:
String key;
if (component instanceof TranslatableComponent translatable) {
    key = translatable.key();
}
 

tw_seer67875

New member
Mar 5, 2023
2
0
1
Haven't tested it, but something like this should probably work:
Java:
String key;
if (component instanceof TranslatableComponent translatable) {
    key = translatable.key();
}
I tried this case, but he always gets chat.square_brackets whether renamed or not...
 

Machine Maker

Paper Developer
Staff member
Dec 17, 2021
132
6
19
18
California
If you want the translation key for the itemstack, use ItemStack#translationKey.
Material#translationKey also exists, but it is different from ItemStack#translationKey. Some itemstacks in minecraft have different names depending on the NBT of the itemstack, like potions.

For future reference, ItemStack#displayName is NOT the same as ItemMeta#displayName()
 

BriaTyler

New member
Mar 14, 2023
1
0
1
First, I recommend making sure that the ItemDisplayNames plugin is installed and configured correctly. Also check that you are using the correct syntax for setting item names in the plugin's configuration file. If you are sure that the plugin settings are correct, it is possible that the problem may be related to the version of your server. Make sure you are using the latest version of Paper Minecraft and try updating the plugin to the latest version.