Skip to content

Commit

Permalink
Add Clear Tooltip Helper
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Jul 12, 2024
1 parent 870d1e3 commit b4b4593
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/groovy-tests/tooltipTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import static com.nomiceu.nomilabs.groovy.GroovyHelpers.TooltipHelpers.*
// Note that tooltips apply to all stacks of that Item and Meta, regardless of NBT Tag.
// If meta is not provided, tooltip only applies to item of meta 0.

// Clearing Tooltips (through `clearTooltip`) always is applied before any `addTooltip` calls!

// Add a tooltip (Uses Translatable, see `jeiTests.groovy`)
addTooltip(item('minecraft:sand'), translatable('item.material.oreprefix.gemPerfect', 'World'))

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/groovy/GroovyHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ public static void addTooltip(ItemStack item, LabsTranslate.Translatable tr) {
list.add(tr);
LabsTooltipHelper.addTooltip(new ItemMeta(item), list);
}

public static void clearTooltip(ItemStack item) {
LabsTooltipHelper.clearTooltip(new ItemMeta(item));
}
}

public static class SafeMethodHelpers {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/tooltip/LabsTooltipHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import net.minecraft.item.ItemStack;
Expand All @@ -14,12 +15,15 @@
import com.nomiceu.nomilabs.util.LabsTranslate;

import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import mcjty.theoneprobe.api.IProbeInfo;

@SuppressWarnings("unused")
@GroovyBlacklist
public class LabsTooltipHelper {

private static final Set<ItemMeta> CLEARED = new ObjectOpenHashSet<>();

private static final Map<ItemMeta, List<LabsTranslate.Translatable>> TOOLTIPS = new Object2ObjectOpenHashMap<>();
private static final Map<ItemMeta, List<String>> CACHED_TOOLTIPS = new Object2ObjectOpenHashMap<>();

Expand All @@ -35,6 +39,15 @@ public static String getTOPFormat(String str) {
return IProbeInfo.STARTLOC + str + IProbeInfo.ENDLOC;
}

/**
* For use in GroovyScript ONLY.
* <p>
* If you want to add a tooltip in Labs, add a function to {@link TooltipAdder}.
*/
public static void clearTooltip(ItemMeta itemMeta) {
CLEARED.add(itemMeta);
}

/**
* For use in GroovyScript ONLY.
* <p>
Expand All @@ -46,6 +59,7 @@ public static void addTooltip(ItemMeta itemMeta, List<LabsTranslate.Translatable
}

public static void clearAll() {
CLEARED.clear();
TOOLTIPS.clear();
CACHED_TOOLTIPS.clear();
}
Expand All @@ -54,6 +68,10 @@ public static void onLanguageChange() {
CACHED_TOOLTIPS.clear();
}

public static boolean shouldClear(ItemStack stack) {
return CLEARED.contains(new ItemMeta(stack));
}

@Nullable
public static List<String> getTranslatableFromStack(ItemStack stack) {
if (stack.isEmpty()) return null;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/tooltip/TooltipAdder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
public class TooltipAdder {

public static void addTooltipNormal(List<String> tooltip, ItemStack stack) {
if (LabsTooltipHelper.shouldClear(stack))
tooltip.clear();

var groovyTooltips = LabsTooltipHelper.getTranslatableFromStack(stack);
if (groovyTooltips != null)
tooltip.addAll(groovyTooltips);
Expand Down

0 comments on commit b4b4593

Please sign in to comment.