Skip to content

Commit f9da023

Browse files
author
ben
committed
Add HideToolTip option for items
1 parent ebd2d15 commit f9da023

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

AdvancedCore/src/main/java/com/bencodez/advancedcore/api/item/ItemBuilder.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,12 @@ public ItemBuilder(ConfigurationSection data) {
372372
} else {
373373
closeGUISet = false;
374374
}
375+
376+
if (data.getBoolean("HideToolTip", false)) {
377+
setHideTooltipCompat(is, true);
378+
}
375379
}
380+
is.getItemMeta().setHideTooltip(chancePass);
376381

377382
} else {
378383
setBlank();
@@ -381,6 +386,42 @@ public ItemBuilder(ConfigurationSection data) {
381386
}
382387
}
383388

389+
/**
390+
* Hides (or shows) the item tooltip. On 1.20.5+ this uses the native
391+
* ItemMeta#setHideTooltip method; on older versions it simply adds all
392+
* available ItemFlags (so at least most tooltip lines are hidden).
393+
*
394+
* @param item the ItemStack to modify
395+
* @param hide true to hide, false to show
396+
*/
397+
public void setHideTooltipCompat(ItemStack item, boolean hide) {
398+
if (item == null)
399+
return;
400+
401+
ItemMeta meta = item.getItemMeta();
402+
if (meta == null)
403+
return;
404+
405+
try {
406+
// Try to call ItemMeta#setHideTooltip(boolean) reflectively
407+
Method m = meta.getClass().getMethod("setHideTooltip", boolean.class);
408+
m.invoke(meta, hide);
409+
} catch (NoSuchMethodException e) {
410+
// Older versions: no native hideTooltip
411+
if (hide) {
412+
// add all flags to hide as much as possible
413+
meta.addItemFlags(ItemFlag.values());
414+
} else {
415+
// remove all flags so tooltip shows again
416+
meta.removeItemFlags(ItemFlag.values());
417+
}
418+
} catch (ReflectiveOperationException ex) {
419+
// something went wrong with reflection; just ignore
420+
}
421+
422+
item.setItemMeta(meta);
423+
}
424+
384425
/**
385426
* Create a new ItemBuilder over an existing itemstack.
386427
*

0 commit comments

Comments
 (0)