Skip to content

Commit

Permalink
Fix Warning Tooltips with Multiple Recipes with Same Output
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Oct 7, 2024
1 parent 465b58b commit 1ba2f9a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
7 changes: 4 additions & 3 deletions src/main/java/com/nomiceu/nomilabs/groovy/GroovyHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,7 +21,7 @@
public class NBTClearingRecipe extends IForgeRegistryEntry.Impl<IRecipe> implements IRecipe {

// Map of Output to Warning Tooltip + Input
public static final Map<ItemMeta, Pair<ItemMeta, LabsTranslate.Translatable>> NBT_CLEARERS = new Object2ObjectOpenHashMap<>();
public static final Map<ItemMeta, Map<ItemMeta, LabsTranslate.Translatable>> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class LabsTooltipHelper {

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

public static String DRAWER_UPDGRADE = LabsTranslate.translate("tooltip.nomilabs.drawers.upgrades");

Expand Down Expand Up @@ -94,12 +94,14 @@ public static List<String> getTranslatableFromStack(ItemStack stack) {
}

public static String getTranslatedNBTClearer(ItemMeta outputItemMeta,
Pair<ItemMeta, LabsTranslate.Translatable> 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;
}
}
22 changes: 15 additions & 7 deletions src/main/java/com/nomiceu/nomilabs/tooltip/TooltipAdder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -77,19 +80,24 @@ public static void addTooltipClearing(List<String> 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<ItemMeta, Translatable> 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;
}
Expand Down

0 comments on commit 1ba2f9a

Please sign in to comment.