Enchantment#displayName() returning empty Component

Hi, I wanted to use the displayName() method for enchantments but it’s returning a Component with content of " ", basically string with a whitespace. Is it like possible the functionality wasn’t implemented yet or what am I doing wrong?

It’s returning a a TranslatableComponent with a TextComponent and another TranslatableComponent as children. So if you are only trying to get the text out of it, yes, you would only get the " " (which is the space between the enchantment name and its level).

Okay, I see, although I don’t really see no content when printing the Component out. This is what comes out when i try to print out Enchantment#displayName(1)
TranslatableComponentImpl{key="enchantment.minecraft.knockback", args=[], style=StyleImpl{color=NamedTextColor{value="#aaaaaa"}, obfuscated=not_set, bold=not_set, strikethrough=not_set, underlined=not_set, italic=not_set, clickEvent=null, hoverEvent=null, insertion=null, font=null}, children=[TextComponentImpl{content=" ", style=StyleImpl{color=null, obfuscated=not_set, bold=not_set, strikethrough=not_set, underlined=not_set, italic=not_set, clickEvent=null, hoverEvent=null, insertion=null, font=null}, children=[]}, TranslatableComponentImpl{key="enchantment.level.1", args=[], style=StyleImpl{color=null, obfuscated=not_set, bold=not_set, strikethrough=not_set, underlined=not_set, italic=not_set, clickEvent=null, hoverEvent=null, insertion=null, font=null}, children=[]}]} Somwhere in there I should be seeing something like “Knockback I” no?

No. Because minecraft supports lots of languages. You are getting a translation key “enchantment.minecraft.knockback” which is sent to the client and based on whatever language the client has, displays the correct word.

Oh okay, so if I would want to put all enchantments of an item into its display name i would just append it with the translatable component and client would handle the rest? This whole new Component system is new for me but I kinda like what it can do.

It’s new to me too, and I haven’t worked with translatable components before, but that should work correctly.

The difficulty with translateable components comes if you want to manupulate the phrasing, but if you just want to put it somewhere else, the client will still recognize it as something that needs to be translated on the player’s screen and do so.

1 Like