Skip to content

Commit

Permalink
Allow Specifying Warning Tooltip for NBT Clearing
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Oct 5, 2024
1 parent 317304d commit 24469eb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 22 deletions.
15 changes: 11 additions & 4 deletions src/main/groovy-tests/customRecipeClassTests.groovy
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import com.nomiceu.nomilabs.groovy.ShapedConversionRecipe
import com.nomiceu.nomilabs.groovy.ShapedDummyRecipe
import net.minecraft.util.text.TextFormatting

import static com.nomiceu.nomilabs.groovy.GroovyHelpers.NBTClearingRecipeHelpers.*
import static com.nomiceu.nomilabs.groovy.GroovyHelpers.TranslationHelpers.*

// Custom Recipe Classes. Goes in Post Init.

Expand Down Expand Up @@ -54,7 +56,12 @@ nbtClearingRecipe(item('storagedrawers:basicdrawers'), {
it.tagCompound = tag // Remember to Save!
})

// Different Input/Output Item, Custom NBT Clearer
nbtClearingRecipe(item('minecraft:water_bucket'), item('minecraft:bucket'), {
it.stackDisplayName = 'Old Water Bucket'
})
// Different Input/Output Item, Custom NBT Clearer, Custom CanClear/Warning Tooltips
nbtClearingRecipe(item('minecraft:water_bucket'), item('minecraft:bucket'),
{
it.stackDisplayName = 'Old Water Bucket'
},
translatableLiteral('Great Water').addFormat(TextFormatting.AQUA),
translatableLiteral('Warning: Great Water?').addFormat(TextFormatting.RED)
)

26 changes: 16 additions & 10 deletions src/main/java/com/nomiceu/nomilabs/groovy/GroovyHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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 @@ -463,39 +464,43 @@ public static void removeDraconicFusionRecipe(ItemStack catalyst, ItemStack resu
public static class NBTClearingRecipeHelpers {

public static NBTClearingRecipe nbtClearingRecipe(ItemStack item) {
return nbtClearingRecipe(item, item, (Consumer<ItemStack>) null);
return nbtClearingRecipe(item, item, null);
}

public static NBTClearingRecipe nbtClearingRecipe(ItemStack item, @Nullable Consumer<ItemStack> clearer) {
return nbtClearingRecipe(item, item, clearer);
}

public static NBTClearingRecipe nbtClearingRecipe(ItemStack input, ItemStack output) {
return nbtClearingRecipe(input, output, (Consumer<ItemStack>) null);
return nbtClearingRecipe(input, output, null);
}

public static NBTClearingRecipe nbtClearingRecipe(ItemStack input, ItemStack exampleOutput,
@Nullable Consumer<ItemStack> clearer) {
return nbtClearingRecipe(input, exampleOutput, clearer, NBTClearingRecipe.CAN_CLEAR_TOOLTIP);
return nbtClearingRecipe(input, exampleOutput, clearer, NBTClearingRecipe.CAN_CLEAR_TOOLTIP,
NBTClearingRecipe.WARNING_TOOLTIP);
}

public static NBTClearingRecipe nbtClearingRecipe(ItemStack item, LabsTranslate.Translatable tooltip) {
return nbtClearingRecipe(item, item, null, tooltip);
}

public static NBTClearingRecipe nbtClearingRecipe(ItemStack item, @Nullable Consumer<ItemStack> clearer,
LabsTranslate.Translatable tooltip) {
return nbtClearingRecipe(item, item, clearer, tooltip);
LabsTranslate.Translatable canClearTooltip,
LabsTranslate.Translatable warningTooltip) {
return nbtClearingRecipe(item, item, clearer, canClearTooltip, warningTooltip);
}

public static NBTClearingRecipe nbtClearingRecipe(ItemStack input, ItemStack output,
LabsTranslate.Translatable tooltip) {
return nbtClearingRecipe(input, output, null, tooltip);
LabsTranslate.Translatable canClearTooltip,
LabsTranslate.Translatable warningTooltip) {
return nbtClearingRecipe(input, output, null, canClearTooltip, warningTooltip);
}

public static NBTClearingRecipe nbtClearingRecipe(ItemStack input, ItemStack exampleOutput,
@Nullable Consumer<ItemStack> clearer,
LabsTranslate.Translatable tooltip) {
LabsTranslate.Translatable canClearTooltip,
LabsTranslate.Translatable warningTooltip) {
ResourceLocation name = RecipeName.generateRl("nomilabs_nbt_clearing");

GroovyLog.Msg msg = GroovyLog.msg("Error adding Minecraft Shaped Crafting recipe '{}'", name).error()
Expand All @@ -513,9 +518,10 @@ 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), new ItemMeta(input));
NBTClearingRecipe.NBT_CLEARERS.put(new ItemMeta(exampleOutput),
Pair.of(new ItemMeta(input), warningTooltip));
ReloadableRegistryManager.addRegistryEntry(ForgeRegistries.RECIPES, name, recipe);
TooltipHelpers.addTooltip(input, tooltip);
TooltipHelpers.addTooltip(input, canClearTooltip);
return recipe;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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 @@ -20,8 +21,8 @@

public class NBTClearingRecipe extends IForgeRegistryEntry.Impl<IRecipe> implements IRecipe {

// Map of Output to Input
public static final Map<ItemMeta, ItemMeta> NBT_CLEARERS = new Object2ObjectOpenHashMap<>();
// Map of Output to Warning Tooltip + Input
public static final Map<ItemMeta, Pair<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
17 changes: 14 additions & 3 deletions src/main/java/com/nomiceu/nomilabs/tooltip/LabsTooltipHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

import net.minecraft.item.ItemStack;

import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.input.Keyboard;

import com.cleanroommc.groovyscript.api.GroovyBlacklist;
import com.nomiceu.nomilabs.groovy.NBTClearingRecipe;
import com.nomiceu.nomilabs.util.ItemMeta;
import com.nomiceu.nomilabs.util.LabsTranslate;

Expand All @@ -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<>();
public static String CACHED_NBT_CLEARER = NBTClearingRecipe.WARNING_TOOLTIP.translate();
private static final Map<ItemMeta, String> CACHED_NBT_WARNINGS = new Object2ObjectOpenHashMap<>();

public static boolean isShiftDown() {
return Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT);
Expand Down Expand Up @@ -64,11 +64,12 @@ public static void clearAll() {
CLEARED.clear();
TOOLTIPS.clear();
CACHED_TOOLTIPS.clear();
CACHED_NBT_WARNINGS.clear();
}

public static void onLanguageChange() {
CACHED_TOOLTIPS.clear();
CACHED_NBT_CLEARER = NBTClearingRecipe.WARNING_TOOLTIP.translate();
CACHED_NBT_WARNINGS.clear();
}

public static boolean shouldClear(ItemStack stack) {
Expand All @@ -88,4 +89,14 @@ public static List<String> getTranslatableFromStack(ItemStack stack) {
.collect(Collectors.toList()));
return CACHED_TOOLTIPS.get(itemMeta);
}

public static String getTranslatedNBTClearer(ItemMeta outputItemMeta,
Pair<ItemMeta, LabsTranslate.Translatable> retrievedPair) {
if (CACHED_NBT_WARNINGS.containsKey(outputItemMeta)) return CACHED_NBT_WARNINGS.get(outputItemMeta);

var translated = retrievedPair.getRight().translate();

CACHED_NBT_WARNINGS.put(outputItemMeta, translated);
return translated;
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/nomiceu/nomilabs/tooltip/TooltipAdder.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public static void addTooltipClearing(List<String> tooltip, ItemStack stack, Ent
var resultItemMeta = new ItemMeta(resultStack);
if (!NBTClearingRecipe.NBT_CLEARERS.containsKey(resultItemMeta)) return;

var inputItemMeta = NBTClearingRecipe.NBT_CLEARERS.get(resultItemMeta);
if (isNBTClearing(inv, inputItemMeta))
tooltip.add(LabsTooltipHelper.CACHED_NBT_CLEARER);
var pair = NBTClearingRecipe.NBT_CLEARERS.get(resultItemMeta);
if (isNBTClearing(inv, pair.getLeft()))
tooltip.add(LabsTooltipHelper.getTranslatedNBTClearer(resultItemMeta, pair));
}

private static boolean isNBTClearing(InventoryCrafting inv, ItemMeta inputItemMeta) {
Expand Down

0 comments on commit 24469eb

Please sign in to comment.