KyoriPowered / adventure

A user-interface library, formerly known as text, for Minecraft: Java Edition
https://docs.advntr.dev/
MIT License
706 stars 107 forks source link

item displayName problem #878

Closed twseer67875 closed 1 year ago

twseer67875 commented 1 year ago

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...

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

{"color":"white","hoverEvent":{"action":"show_item","contents":{"id":"minecraft:anvil"}},"translate":"chat.square_brackets","with":[{"extra":[{"translate":"block.minecraft.anvil"}],"text":""}]}

after renaming

{"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":""}]}
twseer67875 commented 1 year ago

Tried the following, but I get chat.square_brackets as a result whether or not the item has been renamed

System.out.println(((TranslatableComponent) item.displayName()).key());