From 1ba2f9aad26324489b43f4f1f803f467507527b5 Mon Sep 17 00:00:00 2001 From: Integer Limit <103940576+IntegerLimit@users.noreply.github.com> Date: Mon, 7 Oct 2024 12:43:44 +1100 Subject: [PATCH] Fix Warning Tooltips with Multiple Recipes with Same Output --- .../nomilabs/groovy/GroovyHelpers.java | 7 +++--- .../nomilabs/groovy/NBTClearingRecipe.java | 3 +-- .../nomilabs/tooltip/LabsTooltipHelper.java | 12 +++++----- .../nomilabs/tooltip/TooltipAdder.java | 22 +++++++++++++------ 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/nomiceu/nomilabs/groovy/GroovyHelpers.java b/src/main/java/com/nomiceu/nomilabs/groovy/GroovyHelpers.java index 54f5e6d3..27151f52 100644 --- a/src/main/java/com/nomiceu/nomilabs/groovy/GroovyHelpers.java +++ b/src/main/java/com/nomiceu/nomilabs/groovy/GroovyHelpers.java @@ -21,7 +21,6 @@ import net.minecraftforge.fml.common.Loader; import net.minecraftforge.fml.common.registry.ForgeRegistries; -import org.apache.commons.lang3.tuple.Pair; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -58,6 +57,7 @@ import gregtech.client.utils.TooltipHelper; import gregtech.integration.groovy.VirtualizedRecipeMap; import groovy.lang.Closure; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; /** * The interface for groovy to interact with. @@ -520,8 +520,9 @@ public static NBTClearingRecipe nbtClearingRecipe(ItemStack input, ItemStack exa exampleOutput.setTagCompound(null); var recipe = new NBTClearingRecipe(input, exampleOutput, clearer); - NBTClearingRecipe.NBT_CLEARERS.put(new ItemMeta(exampleOutput), - Pair.of(new ItemMeta(input), warningTooltip)); + NBTClearingRecipe.NBT_CLEARERS + .computeIfAbsent(new ItemMeta(exampleOutput), (key) -> new Object2ObjectOpenHashMap<>()) + .put(new ItemMeta(input), warningTooltip); ReloadableRegistryManager.addRegistryEntry(ForgeRegistries.RECIPES, name, recipe); TooltipHelpers.addTooltip(input, canClearTooltip); return recipe; diff --git a/src/main/java/com/nomiceu/nomilabs/groovy/NBTClearingRecipe.java b/src/main/java/com/nomiceu/nomilabs/groovy/NBTClearingRecipe.java index 900a64d7..df9f23d5 100644 --- a/src/main/java/com/nomiceu/nomilabs/groovy/NBTClearingRecipe.java +++ b/src/main/java/com/nomiceu/nomilabs/groovy/NBTClearingRecipe.java @@ -10,7 +10,6 @@ import net.minecraft.world.World; import net.minecraftforge.registries.IForgeRegistryEntry; -import org.apache.commons.lang3.tuple.Pair; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -22,7 +21,7 @@ public class NBTClearingRecipe extends IForgeRegistryEntry.Impl implements IRecipe { // Map of Output to Warning Tooltip + Input - public static final Map> NBT_CLEARERS = new Object2ObjectOpenHashMap<>(); + public static final Map> NBT_CLEARERS = new Object2ObjectOpenHashMap<>(); public static final LabsTranslate.Translatable WARNING_TOOLTIP = new LabsTranslate.Translatable( "tooltip.nomilabs.recipe.clearing"); public static final LabsTranslate.Translatable CAN_CLEAR_TOOLTIP = new LabsTranslate.Translatable( diff --git a/src/main/java/com/nomiceu/nomilabs/tooltip/LabsTooltipHelper.java b/src/main/java/com/nomiceu/nomilabs/tooltip/LabsTooltipHelper.java index ccb405d8..93cff9fc 100644 --- a/src/main/java/com/nomiceu/nomilabs/tooltip/LabsTooltipHelper.java +++ b/src/main/java/com/nomiceu/nomilabs/tooltip/LabsTooltipHelper.java @@ -27,7 +27,7 @@ public class LabsTooltipHelper { private static final Map> TOOLTIPS = new Object2ObjectOpenHashMap<>(); private static final Map> CACHED_TOOLTIPS = new Object2ObjectOpenHashMap<>(); - private static final Map CACHED_NBT_WARNINGS = new Object2ObjectOpenHashMap<>(); + private static final Map, String> CACHED_NBT_WARNINGS = new Object2ObjectOpenHashMap<>(); public static String DRAWER_UPDGRADE = LabsTranslate.translate("tooltip.nomilabs.drawers.upgrades"); @@ -94,12 +94,14 @@ public static List getTranslatableFromStack(ItemStack stack) { } public static String getTranslatedNBTClearer(ItemMeta outputItemMeta, - Pair retrievedPair) { - if (CACHED_NBT_WARNINGS.containsKey(outputItemMeta)) return CACHED_NBT_WARNINGS.get(outputItemMeta); + ItemMeta inputItemMeta, + LabsTranslate.Translatable tr) { + if (CACHED_NBT_WARNINGS.containsKey(Pair.of(outputItemMeta, inputItemMeta))) + return CACHED_NBT_WARNINGS.get(Pair.of(outputItemMeta, inputItemMeta)); - var translated = retrievedPair.getRight().translate(); + var translated = tr.translate(); - CACHED_NBT_WARNINGS.put(outputItemMeta, translated); + CACHED_NBT_WARNINGS.put(Pair.of(outputItemMeta, inputItemMeta), translated); return translated; } } diff --git a/src/main/java/com/nomiceu/nomilabs/tooltip/TooltipAdder.java b/src/main/java/com/nomiceu/nomilabs/tooltip/TooltipAdder.java index 69756436..49526540 100644 --- a/src/main/java/com/nomiceu/nomilabs/tooltip/TooltipAdder.java +++ b/src/main/java/com/nomiceu/nomilabs/tooltip/TooltipAdder.java @@ -5,6 +5,7 @@ import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.List; +import java.util.Map; import java.util.Objects; import net.minecraft.entity.player.EntityPlayer; @@ -18,6 +19,8 @@ import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; +import org.jetbrains.annotations.Nullable; + import com.cleanroommc.groovyscript.helper.ingredient.IngredientHelper; import com.enderio.core.client.handlers.SpecialTooltipHandler; import com.jaquadro.minecraft.storagedrawers.item.ItemCompDrawers; @@ -77,19 +80,24 @@ public static void addTooltipClearing(List tooltip, ItemStack stack, Ent var resultItemMeta = new ItemMeta(resultStack); if (!NBTClearingRecipe.NBT_CLEARERS.containsKey(resultItemMeta)) return; - var pair = NBTClearingRecipe.NBT_CLEARERS.get(resultItemMeta); - if (isNBTClearing(inv, pair.getLeft())) - tooltip.add(LabsTooltipHelper.getTranslatedNBTClearer(resultItemMeta, pair)); + var inputTooltips = NBTClearingRecipe.NBT_CLEARERS.get(resultItemMeta); + var input = isNBTClearing(inv, inputTooltips); + if (input != null) + tooltip.add(LabsTooltipHelper.getTranslatedNBTClearer(resultItemMeta, input, inputTooltips.get(input))); } - private static boolean isNBTClearing(InventoryCrafting inv, ItemMeta inputItemMeta) { - boolean found = false; + @Nullable + private static ItemMeta isNBTClearing(InventoryCrafting inv, Map pairs) { + ItemMeta found = null; for (int i = 0; i < inv.getSizeInventory(); i++) { var stack = inv.getStackInSlot(i); if (stack.isEmpty()) continue; - if (found || !inputItemMeta.compareWith(stack)) return false; - found = true; + if (found != null) return null; + + var itemMeta = new ItemMeta(stack); + if (pairs.containsKey(itemMeta)) found = itemMeta; + else return null; } return found; }