Jump to content

1.17.1 - Obtaining translation keys of a tooltip?


SpectralGerbil

Recommended Posts

Hello, I'll keep it short and sweet. I'm removing armor tooltips from armor for a custom defense system that's nearly finished.

I am using ItemTooltipEvent to obtain each tooltip and check if it contains the +X Armor tooltip, and then remove that tooltip. It works however I would like to make it work across languages. (Searching for the phrase "Armor" will not work in other languages). After some research I've found I need to obtain the translation keys for parts of these tooltips, and I can then iterate them and remove tooltips that contain the translation key "attribute.name.generic.armor". However I'm digging through 1.17 documentation and cannot find a way to obtain translation keys from the tooltips. I believe I could convert the Components retrieved from the tooltips into TranslatableComponents, but cannot find a method that does this.

Could someone with the proper knowledge tell me how I can achieve this?

Edit: For some context, yes I plan on updating to 1.18.1 soon but would like to figure this out first.

Edited by SpectralGerbil
context on 1.17
Link to comment
Share on other sites

The issue I'm experiencing there is that when I attempt to cast most TextComponents to a TranslatableComponent, the game crashes saying it cannot. I fixed this by running an instanceof check, but that is only returning a few translation keys, none of which are what I need. Most look like this:

item.modifiers.chest 
attribute.modifier.plus.0 
attribute.modifier.plus.0 
item.modifiers.mainhand 
attribute.modifier.plus.0 
attribute.modifier.take.0

When I tested using regular components, I saw names of every single item which verified that was working, however in this instance I am only seeing the strings listed above and no item ids, block ids, or "attribute.name.generic.armor" as I need.

I feel I may be doing something completely wrong. I'll post an excerpt of my code below from inside my ItemTooltipEvent listener (apologies for the messy variable names during testing)


		List<Component> tooltip = event.getToolTip();

		int size = tooltip.size();
		int i = 0;

		while (i < size) {
			Component ttcomp = tooltip.get(i);
			if (ttcomp instanceof TranslatableComponent) {
			TranslatableComponent trancomp = (TranslatableComponent) ttcomp;
			String ttkey = trancomp.getKey();
			if (ttkey.contains("attribute.name.generic.armor")) {
				event.getToolTip().remove(i);
				size -= 1;
				i -= 1;
			}
			}
			i += 1;
		}

Just to outline, the purpose of the code is to search / iterate incoming tooltips for the "attribute.name.generic.armor" key and then remove those lines from the tooltip.

Link to comment
Share on other sites

Hello again. Things seem to be working great so far now, thanks to your help; I've been able to dig into the arguments and found the "generic.armor" attribute tooltips. However, how do I remove them from the TranslatableComponent arguments? There isn't a method to edit the arguments.

Alternatively, if I need to redefine my own TranslatableComponent to replace the old one, how can I do so properly, passing the right amount of arguments and such?

Edited by SpectralGerbil
made a question clearer
Link to comment
Share on other sites

Sorry, but I'm a little confused on which component you're referring to as the parent. Do you mean the one a level above the attribute I want to remove, or are you saying I should remove the entire tooltip processed in the event? Is there a particular method to going about doing that?

Also, how would I view the full translation structure? I'm not familiar with doing so.

Link to comment
Share on other sites

It does! Thank you! I've figured out what I'm supposed to do now with the components. I just have one more question on how to finish up.

After running all my checks, how can I modify the resulting tooltip to apply my changes? I can't see an event.setTooltip method or anything of that nature. I have the finished, edited tooltip that I need, but how do I apply it over the one the event gives me?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.