11package xyz .xenondevs .invui .item ;
22
3+ import com .google .common .collect .Sets ;
34import io .papermc .paper .datacomponent .DataComponentBuilder ;
45import io .papermc .paper .datacomponent .DataComponentType ;
56import io .papermc .paper .datacomponent .DataComponentTypes ;
67import io .papermc .paper .datacomponent .item .ItemLore ;
8+ import io .papermc .paper .datacomponent .item .TooltipDisplay ;
79import it .unimi .dsi .fastutil .booleans .BooleanArrayList ;
810import it .unimi .dsi .fastutil .booleans .BooleanList ;
911import it .unimi .dsi .fastutil .floats .FloatArrayList ;
2426import org .bukkit .inventory .ItemStack ;
2527import org .jspecify .annotations .Nullable ;
2628import xyz .xenondevs .invui .i18n .Languages ;
29+ import xyz .xenondevs .invui .internal .util .ArrayUtils ;
2730import xyz .xenondevs .invui .internal .util .ComponentUtils ;
2831
2932import java .util .*;
@@ -433,7 +436,7 @@ public ItemBuilder setCustomName(Component customName) {
433436 this .customName = new DirectComponentHolder (customName );
434437 this .name = null ;
435438 unset (DataComponentTypes .ITEM_NAME );
436- unset ( DataComponentTypes . HIDE_TOOLTIP );
439+ hideTooltip ( false );
437440 return this ;
438441 }
439442
@@ -451,7 +454,7 @@ public ItemBuilder setCustomName(String customName) {
451454 this .customName = new MiniMessageComponentHolder (customName );
452455 this .name = null ;
453456 unset (DataComponentTypes .ITEM_NAME );
454- unset ( DataComponentTypes . HIDE_TOOLTIP );
457+ hideTooltip ( false );
455458 return this ;
456459 }
457460
@@ -511,7 +514,7 @@ public ItemBuilder setLore(List<Component> lore) {
511514 this .lore = lore .stream ()
512515 .map (DirectComponentHolder ::new )
513516 .collect (Collectors .toCollection (ArrayList ::new ));
514- unset ( DataComponentTypes . HIDE_TOOLTIP );
517+ hideTooltip ( false );
515518 return this ;
516519 }
517520
@@ -529,7 +532,7 @@ public ItemBuilder setLegacyLore(List<String> lore) {
529532 .map (line -> legacySection ().deserialize (line ))
530533 .map (DirectComponentHolder ::new )
531534 .collect (Collectors .toCollection (ArrayList ::new ));
532- unset ( DataComponentTypes . HIDE_TOOLTIP );
535+ hideTooltip ( false );
533536 return this ;
534537 }
535538
@@ -572,7 +575,7 @@ public ItemBuilder addLoreLines(Component... lines) {
572575 lore .add (new DirectComponentHolder (line ));
573576 }
574577
575- unset ( DataComponentTypes . HIDE_TOOLTIP );
578+ hideTooltip ( false );
576579
577580 return this ;
578581 }
@@ -594,7 +597,7 @@ public ItemBuilder addLoreLines(List<Component> lines) {
594597 lore .add (new DirectComponentHolder (line ));
595598 }
596599
597- unset ( DataComponentTypes . HIDE_TOOLTIP );
600+ hideTooltip ( false );
598601
599602 return this ;
600603 }
@@ -616,7 +619,7 @@ public ItemBuilder addMiniMessageLoreLines(List<String> lines) {
616619 lore .add (new MiniMessageComponentHolder (line ));
617620 }
618621
619- unset ( DataComponentTypes . HIDE_TOOLTIP );
622+ hideTooltip ( false );
620623
621624 return this ;
622625 }
@@ -638,7 +641,7 @@ public ItemBuilder addLegacyLoreLines(List<String> lines) {
638641 lore .add (new DirectComponentHolder (legacySection ().deserialize (line )));
639642 }
640643
641- unset ( DataComponentTypes . HIDE_TOOLTIP );
644+ hideTooltip ( false );
642645
643646 return this ;
644647 }
@@ -1058,11 +1061,69 @@ public ItemBuilder clearCustomModelData() {
10581061 public ItemBuilder hideTooltip (boolean hide ) {
10591062 buildCache .clear ();
10601063
1061- if (hide ) {
1062- itemStack .setData (DataComponentTypes .HIDE_TOOLTIP );
1063- } else {
1064- itemStack .unsetData (DataComponentTypes .HIDE_TOOLTIP );
1065- }
1064+ TooltipDisplay display = itemStack .getData (DataComponentTypes .TOOLTIP_DISPLAY );
1065+ itemStack .setData (
1066+ DataComponentTypes .TOOLTIP_DISPLAY ,
1067+ TooltipDisplay .tooltipDisplay ()
1068+ .hideTooltip (hide )
1069+ .hiddenComponents (display != null ? display .hiddenComponents () : Set .of ())
1070+ .build ()
1071+ );
1072+
1073+ return this ;
1074+ }
1075+
1076+ /**
1077+ * Hides the tooltip for the given data components.
1078+ *
1079+ * @param type The first data component type to hide the tooltip for
1080+ * @param types Additional data component types to hide the tooltip for
1081+ * @return The builder instance
1082+ * @see #showTooltip(DataComponentType, DataComponentType...)
1083+ * @see #hideTooltip(boolean)
1084+ */
1085+ public ItemBuilder hideTooltip (DataComponentType type , DataComponentType ... types ) {
1086+ buildCache .clear ();
1087+
1088+ TooltipDisplay display = itemStack .getData (DataComponentTypes .TOOLTIP_DISPLAY );
1089+ itemStack .setData (
1090+ DataComponentTypes .TOOLTIP_DISPLAY ,
1091+ TooltipDisplay .tooltipDisplay ()
1092+ .hideTooltip (display != null && display .hideTooltip ())
1093+ .hiddenComponents (display != null ? display .hiddenComponents () : Set .of ())
1094+ .hiddenComponents (Set .of (ArrayUtils .concat (DataComponentType []::new , type , types )))
1095+ .build ()
1096+ );
1097+
1098+ return this ;
1099+ }
1100+
1101+ /**
1102+ * Un-hides the tooltip for the given data components that were previously hidden.
1103+ *
1104+ * @param type The first data component type to un-hide the tooltip for
1105+ * @param types Additional data component types to un-hide the tooltip for
1106+ * @return The builder instance
1107+ * @see #hideTooltip(DataComponentType, DataComponentType...)
1108+ * @see #hideTooltip(boolean)
1109+ */
1110+ public ItemBuilder showTooltip (DataComponentType type , DataComponentType ... types ) {
1111+ TooltipDisplay display = itemStack .getData (DataComponentTypes .TOOLTIP_DISPLAY );
1112+ if (display == null )
1113+ return this ;
1114+
1115+ buildCache .clear ();
1116+
1117+ itemStack .setData (
1118+ DataComponentTypes .TOOLTIP_DISPLAY ,
1119+ TooltipDisplay .tooltipDisplay ()
1120+ .hideTooltip (display .hideTooltip ())
1121+ .hiddenComponents (Sets .difference (
1122+ display .hiddenComponents (),
1123+ Set .of (ArrayUtils .concat (DataComponentType []::new , type , types ))
1124+ ))
1125+ .build ()
1126+ );
10661127
10671128 return this ;
10681129 }
0 commit comments