From d98061de122fe79289ecc20dd0f420446c258a7a Mon Sep 17 00:00:00 2001 From: Integer Limit <103940576+IntegerLimit@users.noreply.github.com> Date: Sun, 21 Jan 2024 23:50:17 +0800 Subject: [PATCH] Improve JEI Groovy Helpers --- src/main/groovy-tests/jeiTests.groovy | 14 +++- .../com/nomiceu/nomilabs/block/BlockBase.java | 3 +- .../nomilabs/groovy/GroovyHelpers.java | 9 ++- .../groovy/LabsVirtualizedRegistries.java | 9 +++ .../nomilabs/integration/jei/JEIPlugin.java | 77 +++++++++++++++---- .../com/nomiceu/nomilabs/item/ItemBase.java | 4 +- .../jei/CraftingRecipeCategoryMixin.java | 9 +-- .../com/nomiceu/nomilabs/util/ItemMeta.java | 4 + .../nomiceu/nomilabs/util/ItemTagMeta.java | 6 ++ 9 files changed, 105 insertions(+), 30 deletions(-) diff --git a/src/main/groovy-tests/jeiTests.groovy b/src/main/groovy-tests/jeiTests.groovy index 0e029fe8..eaa912bf 100644 --- a/src/main/groovy-tests/jeiTests.groovy +++ b/src/main/groovy-tests/jeiTests.groovy @@ -11,10 +11,16 @@ addDescription(item('minecraft:apple'), format("An Ordinary Apple... Not Poisone // Add a translated description page for a stack addDescription(item('minecraft:iron_ingot'), translate("tooltip.nomilabs.greenhouse.description"), translate("tooltip.nomilabs.dme_sim_chamber.description")) -/* Recipe Output Tooltips. These are tooltips that appear on recipes that output that stack. */ +/* Recipe Output Tooltips. These are tooltips that appear on CRAFTING recipes that output that stack. */ -// Add a recipe output tooltip for a stack -addRecipeOutputTooltip(item('minecraft:golden_apple'), format("An Ordinary Apple... Not Poisoned.", TextFormatting.GREEN), "Eat it!") +// Add a crafting recipe output tooltip for a stack +addRecipeOutputTooltip(item('minecraft:gold_ingot'), format("A Very Low Carrot Gold Ingot.", TextFormatting.GOLD), "Oops! Meant Carat!") -// Add a translated recipe output tooltip for a stack +// Add a translated crafting recipe output tooltip for a stack addRecipeOutputTooltip(item('minecraft:iron_ingot'), translate("tooltip.nomilabs.greenhouse.description"), translate("tooltip.nomilabs.dme_sim_chamber.description")) + +// Add a crafting recipe output tooltip for a specific recipe for a stack (Higher Priority than wild) +addRecipeOutputTooltip(item('minecraft:gold_ingot'), resource('minecraft:gold_ingot_from_block'), format("A Very High Carat Gold Ingot.", TextFormatting.GOLD), "Precious!") + +// Add a translated crafting recipe output tooltip for a specific recipe for a stack (Higher Priority than wild) +addRecipeOutputTooltip(item('minecraft:iron_ingot'), resource('minecraft:iron_ingot_from_nuggets'), translate("tooltip.nomilabs.universalnavigator.description"), translate("tooltip.nomilabs.greenhouse.description"), translate("tooltip.nomilabs.dme_sim_chamber.description")) diff --git a/src/main/java/com/nomiceu/nomilabs/block/BlockBase.java b/src/main/java/com/nomiceu/nomilabs/block/BlockBase.java index 7c73a693..3e54dda7 100644 --- a/src/main/java/com/nomiceu/nomilabs/block/BlockBase.java +++ b/src/main/java/com/nomiceu/nomilabs/block/BlockBase.java @@ -17,6 +17,7 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; public class BlockBase extends Block implements TOPInfoProvider { @@ -51,7 +52,7 @@ private void initialize(ResourceLocation rl, CreativeTabs tab, SoundType sound, @Override @SideOnly(Side.CLIENT) public void addInformation(@NotNull ItemStack stack, @Nullable World world, @NotNull List tooltip, @NotNull ITooltipFlag flagIn) { - tooltip.addAll(Arrays.asList(description)); + Collections.addAll(tooltip, description); } @Override diff --git a/src/main/java/com/nomiceu/nomilabs/groovy/GroovyHelpers.java b/src/main/java/com/nomiceu/nomilabs/groovy/GroovyHelpers.java index f0b65d34..bf2dd254 100644 --- a/src/main/java/com/nomiceu/nomilabs/groovy/GroovyHelpers.java +++ b/src/main/java/com/nomiceu/nomilabs/groovy/GroovyHelpers.java @@ -6,7 +6,6 @@ import com.cleanroommc.groovyscript.helper.ingredient.IngredientHelper; import com.cleanroommc.groovyscript.sandbox.ClosureHelper; import com.nomiceu.nomilabs.integration.jei.JEIPlugin; -import com.nomiceu.nomilabs.util.LabsSide; import com.nomiceu.nomilabs.util.LabsTranslate; import gregtech.api.unification.material.Material; import gregtech.api.util.GTUtility; @@ -54,11 +53,13 @@ public static String format(String str, LabsTranslate.Format... formats) { } public static class JEIHelpers { public static void addDescription(ItemStack stack, String... description) { - if (LabsSide.isClient()) - JEIPlugin.addDescription(stack, description); + JEIPlugin.addGroovyDescription(stack, description); } public static void addRecipeOutputTooltip(ItemStack stack, String... tooltip) { - JEIPlugin.addRecipeOutputTooltip(stack, tooltip); + JEIPlugin.addGroovyRecipeOutputTooltip(stack, tooltip); + } + public static void addRecipeOutputTooltip(ItemStack stack, ResourceLocation recipeName, String... tooltip) { + JEIPlugin.addGroovyRecipeOutputTooltip(stack, recipeName, tooltip); } } public static class MaterialHelpers { diff --git a/src/main/java/com/nomiceu/nomilabs/groovy/LabsVirtualizedRegistries.java b/src/main/java/com/nomiceu/nomilabs/groovy/LabsVirtualizedRegistries.java index 02b982a6..8cf2f019 100644 --- a/src/main/java/com/nomiceu/nomilabs/groovy/LabsVirtualizedRegistries.java +++ b/src/main/java/com/nomiceu/nomilabs/groovy/LabsVirtualizedRegistries.java @@ -1,6 +1,7 @@ package com.nomiceu.nomilabs.groovy; import com.cleanroommc.groovyscript.registry.VirtualizedRegistry; +import com.nomiceu.nomilabs.integration.jei.JEIPlugin; import gregtech.api.unification.OreDictUnifier; import gregtech.api.unification.stack.ItemMaterialInfo; import net.minecraft.item.ItemStack; @@ -9,6 +10,7 @@ @SuppressWarnings("unused") public class LabsVirtualizedRegistries { public static final ReplaceRecipeManager REPLACE_RECIPE_MANAGER = new ReplaceRecipeManager(); + public static final JEIManager JEI_MANAGER = new JEIManager(); public static class ReplaceRecipeManager extends VirtualizedRegistry> { @Override @@ -26,4 +28,11 @@ public void registerOre(ItemStack stack, ItemMaterialInfo info) { OreDictUnifier.registerOre(stack, info); } } + + public static class JEIManager extends VirtualizedRegistry { + @Override + public void onReload() { + JEIPlugin.onReload(); + } + } } diff --git a/src/main/java/com/nomiceu/nomilabs/integration/jei/JEIPlugin.java b/src/main/java/com/nomiceu/nomilabs/integration/jei/JEIPlugin.java index 68e752d4..823b63b0 100644 --- a/src/main/java/com/nomiceu/nomilabs/integration/jei/JEIPlugin.java +++ b/src/main/java/com/nomiceu/nomilabs/integration/jei/JEIPlugin.java @@ -1,5 +1,8 @@ package com.nomiceu.nomilabs.integration.jei; +import com.cleanroommc.groovyscript.api.GroovyBlacklist; +import com.google.common.collect.HashBasedTable; +import com.google.common.collect.Table; import com.nomiceu.nomilabs.groovy.PartialRecipe; import com.nomiceu.nomilabs.util.ItemTagMeta; import mezz.jei.api.IModPlugin; @@ -7,41 +10,89 @@ import mezz.jei.api.ingredients.VanillaTypes; import mezz.jei.api.recipe.VanillaRecipeCategoryUid; import net.minecraft.item.ItemStack; +import net.minecraft.util.ResourceLocation; import org.jetbrains.annotations.NotNull; -import java.util.HashMap; -import java.util.Map; +import java.util.*; +import java.util.function.Consumer; @mezz.jei.api.JEIPlugin @SuppressWarnings("unused") +@GroovyBlacklist public class JEIPlugin implements IModPlugin { - private static final Map DESCRIPTIONS = new HashMap<>(); - private static final Map RECIPE_OUTPUT_TOOLTIPS = new HashMap<>(); - - private static IModRegistry registry; + private static final ResourceLocation WILDCARD_LOCATION = new ResourceLocation("*", "*"); + private static final Map> DESCRIPTIONS = new HashMap<>(); + private static final Map> GROOVY_DESCRIPTIONS = new HashMap<>(); + private static final Table> RECIPE_OUTPUT_TOOLTIPS = HashBasedTable.create(); + private static final Table> GROOVY_RECIPE_OUTPUT_TOOLTIPS = HashBasedTable.create(); @Override public void register(IModRegistry registry) { - JEIPlugin.registry = registry; var jeiHelpers = registry.getJeiHelpers(); // JEI does not recognise Custom Recipe Classes on its own registry.handleRecipes(PartialRecipe.class, recipe -> new PartialRecipeWrapper(jeiHelpers, recipe), VanillaRecipeCategoryUid.CRAFTING); // Add Descriptions - DESCRIPTIONS.forEach(((itemStack, strings) -> - registry.addIngredientInfo(itemStack, VanillaTypes.ITEM, String.join("\n\n", strings)))); + Map> tempMap = new HashMap<>(DESCRIPTIONS); + GROOVY_DESCRIPTIONS.forEach(((key, value) -> addDescription(tempMap, key, (list) -> list.addAll(value)))); + tempMap.forEach(((itemTagMeta, strings) -> + registry.addIngredientInfo(itemTagMeta.toStack(), VanillaTypes.ITEM, String.join("\n\n", strings)))); } public static void addDescription(@NotNull ItemStack stack, String... description) { - DESCRIPTIONS.put(stack, description); + addDescription(DESCRIPTIONS, new ItemTagMeta(stack), (list) -> Collections.addAll(list, description)); + } + + public static void addGroovyDescription(@NotNull ItemStack stack, String... description) { + addDescription(GROOVY_DESCRIPTIONS, new ItemTagMeta(stack), (list) -> Collections.addAll(list, description)); + } + + private static void addDescription(Map> map, + @NotNull ItemTagMeta stack, Consumer> addToList) { + map.computeIfAbsent(stack, (k) -> new ArrayList<>()); + addToList.accept(map.get(stack)); } public static void addRecipeOutputTooltip(@NotNull ItemStack stack, String... tooltip) { - RECIPE_OUTPUT_TOOLTIPS.put(new ItemTagMeta(stack), tooltip); + addRecipeOutputTooltip(stack, WILDCARD_LOCATION, tooltip); + } + + public static void addRecipeOutputTooltip(@NotNull ItemStack stack, ResourceLocation recipeName, String... tooltip) { + addRecipeOutputTooltip(RECIPE_OUTPUT_TOOLTIPS, new ItemTagMeta(stack), recipeName, (list) -> Collections.addAll(list, tooltip)); + } + + public static void addGroovyRecipeOutputTooltip(@NotNull ItemStack stack, String... tooltip) { + addGroovyRecipeOutputTooltip(stack, WILDCARD_LOCATION, tooltip); + } + + public static void addGroovyRecipeOutputTooltip(@NotNull ItemStack stack, ResourceLocation recipeName, String... tooltip) { + addRecipeOutputTooltip(GROOVY_RECIPE_OUTPUT_TOOLTIPS, new ItemTagMeta(stack), recipeName, (list) -> Collections.addAll(list, tooltip)); + } + + private static void addRecipeOutputTooltip(Table> table, + @NotNull ItemTagMeta stack, ResourceLocation recipeName, Consumer> addToList){ + var list = table.get(stack, recipeName); + if (list == null) list = new ArrayList<>(); + addToList.accept(list); + table.put(stack, recipeName, list); + } + + public static List getRecipeOutputTooltip(ItemStack stack, ResourceLocation recipeName) { + var tempTable = HashBasedTable.create(RECIPE_OUTPUT_TOOLTIPS); + GROOVY_RECIPE_OUTPUT_TOOLTIPS.cellSet().forEach((cell) -> + addRecipeOutputTooltip(tempTable, Objects.requireNonNull(cell.getRowKey()), cell.getColumnKey(), + (list) -> list.addAll(Objects.requireNonNull(cell.getValue())))); + var itemTagMeta = new ItemTagMeta(stack); + var specific = tempTable.get(itemTagMeta, recipeName); + if (specific != null) return specific; + var wildcard = tempTable.get(itemTagMeta, WILDCARD_LOCATION); + if (wildcard != null) return wildcard; + return new ArrayList<>(); } - public static Map getRecipeOutputTooltips() { - return RECIPE_OUTPUT_TOOLTIPS; + public static void onReload() { + GROOVY_DESCRIPTIONS.clear(); + GROOVY_RECIPE_OUTPUT_TOOLTIPS.clear(); } } diff --git a/src/main/java/com/nomiceu/nomilabs/item/ItemBase.java b/src/main/java/com/nomiceu/nomilabs/item/ItemBase.java index 9a7b8111..312e6b2f 100644 --- a/src/main/java/com/nomiceu/nomilabs/item/ItemBase.java +++ b/src/main/java/com/nomiceu/nomilabs/item/ItemBase.java @@ -13,7 +13,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Arrays; +import java.util.Collections; import java.util.List; public class ItemBase extends Item { @@ -61,6 +61,6 @@ public IRarity getForgeRarity(@NotNull ItemStack stack) { @Override @SideOnly(Side.CLIENT) public void addInformation(@NotNull ItemStack stack, @Nullable World world, @NotNull List tooltip, @NotNull ITooltipFlag flagIn) { - tooltip.addAll(Arrays.asList(description)); + Collections.addAll(tooltip, description); } } diff --git a/src/main/java/com/nomiceu/nomilabs/mixin/jei/CraftingRecipeCategoryMixin.java b/src/main/java/com/nomiceu/nomilabs/mixin/jei/CraftingRecipeCategoryMixin.java index 6bf3b91f..470b967f 100644 --- a/src/main/java/com/nomiceu/nomilabs/mixin/jei/CraftingRecipeCategoryMixin.java +++ b/src/main/java/com/nomiceu/nomilabs/mixin/jei/CraftingRecipeCategoryMixin.java @@ -1,7 +1,6 @@ package com.nomiceu.nomilabs.mixin.jei; import com.nomiceu.nomilabs.integration.jei.JEIPlugin; -import com.nomiceu.nomilabs.util.ItemTagMeta; import mezz.jei.plugins.vanilla.crafting.CraftingRecipeCategory; import net.minecraft.item.ItemStack; import net.minecraft.util.ResourceLocation; @@ -13,7 +12,6 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import java.util.Arrays; import java.util.List; @Mixin(value = CraftingRecipeCategory.class, remap = false) @@ -23,10 +21,9 @@ public class CraftingRecipeCategoryMixin { private static int craftOutputSlot; @Inject(method = "lambda$setRecipe$0", at = @At("HEAD")) - private static void addRecipeOutputTooltip(ResourceLocation registryName, int slotIndex, boolean input, ItemStack ingredient, List tooltip, CallbackInfo ci) { + private static void addRecipeOutputTooltip(ResourceLocation registryName, int slotIndex, boolean input, + ItemStack stack, List tooltip, CallbackInfo ci) { if (FMLCommonHandler.instance().getEffectiveSide().isServer() || slotIndex != craftOutputSlot) return; - var itemTagMeta = new ItemTagMeta(ingredient); - if (!JEIPlugin.getRecipeOutputTooltips().containsKey(itemTagMeta)) return; - tooltip.addAll(Arrays.asList(JEIPlugin.getRecipeOutputTooltips().get(itemTagMeta))); + tooltip.addAll(JEIPlugin.getRecipeOutputTooltip(stack, registryName)); } } diff --git a/src/main/java/com/nomiceu/nomilabs/util/ItemMeta.java b/src/main/java/com/nomiceu/nomilabs/util/ItemMeta.java index 10fbd7dd..65c6ea15 100644 --- a/src/main/java/com/nomiceu/nomilabs/util/ItemMeta.java +++ b/src/main/java/com/nomiceu/nomilabs/util/ItemMeta.java @@ -35,4 +35,8 @@ public int hashCode() { public static boolean compare(ItemStack a, ItemStack b) { return a.getItem().equals(b.getItem()) && a.getMetadata() == b.getMetadata(); } + + public ItemStack toStack() { + return new ItemStack(item, 1, meta); + } } diff --git a/src/main/java/com/nomiceu/nomilabs/util/ItemTagMeta.java b/src/main/java/com/nomiceu/nomilabs/util/ItemTagMeta.java index 8e22a5d4..2ae18d7f 100644 --- a/src/main/java/com/nomiceu/nomilabs/util/ItemTagMeta.java +++ b/src/main/java/com/nomiceu/nomilabs/util/ItemTagMeta.java @@ -34,4 +34,10 @@ public int hashCode() { public static boolean compare(ItemStack a, ItemStack b) { return ItemMeta.compare(a, b) && Objects.equals(a.getTagCompound(), b.getTagCompound()); } + + public ItemStack toStack() { + var stack = super.toStack(); + if (tag != null) stack.setTagCompound(tag); + return stack; + } }