Posted December 23, 20213 yr 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 December 23, 20213 yr by SpectralGerbil context on 1.17
December 23, 20213 yr Author 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.
December 23, 20213 yr Author 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 December 23, 20213 yr by SpectralGerbil made a question clearer
December 23, 20213 yr Author 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.
December 23, 20213 yr Author 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?
December 23, 20213 yr Author Oh! Simple enough. Thanks, things are working now! I've just got a few bits of tidying up to do with my code, to make everything work perfectly, but I've removed the tooltips I wanted. Massive props for the help!
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.