Skip to content

Commit

Permalink
Improve JEI Groovy Helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Jan 21, 2024
1 parent 4f3582f commit d98061d
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 30 deletions.
14 changes: 10 additions & 4 deletions src/main/groovy-tests/jeiTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
3 changes: 2 additions & 1 deletion src/main/java/com/nomiceu/nomilabs/block/BlockBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<String> tooltip, @NotNull ITooltipFlag flagIn) {
tooltip.addAll(Arrays.asList(description));
Collections.addAll(tooltip, description);
}

@Override
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/nomiceu/nomilabs/groovy/GroovyHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Pair<ItemStack, ItemMaterialInfo>> {
@Override
Expand All @@ -26,4 +28,11 @@ public void registerOre(ItemStack stack, ItemMaterialInfo info) {
OreDictUnifier.registerOre(stack, info);
}
}

public static class JEIManager extends VirtualizedRegistry<String> {
@Override
public void onReload() {
JEIPlugin.onReload();
}
}
}
77 changes: 64 additions & 13 deletions src/main/java/com/nomiceu/nomilabs/integration/jei/JEIPlugin.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,98 @@
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;
import mezz.jei.api.IModRegistry;
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<ItemStack, String[]> DESCRIPTIONS = new HashMap<>();
private static final Map<ItemTagMeta, String[]> RECIPE_OUTPUT_TOOLTIPS = new HashMap<>();

private static IModRegistry registry;
private static final ResourceLocation WILDCARD_LOCATION = new ResourceLocation("*", "*");
private static final Map<ItemTagMeta, List<String>> DESCRIPTIONS = new HashMap<>();
private static final Map<ItemTagMeta, List<String>> GROOVY_DESCRIPTIONS = new HashMap<>();
private static final Table<ItemTagMeta, ResourceLocation, List<String>> RECIPE_OUTPUT_TOOLTIPS = HashBasedTable.create();
private static final Table<ItemTagMeta, ResourceLocation, List<String>> 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<ItemTagMeta, List<String>> 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<ItemTagMeta, List<String>> map,
@NotNull ItemTagMeta stack, Consumer<List<String>> 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<ItemTagMeta, ResourceLocation, List<String>> table,
@NotNull ItemTagMeta stack, ResourceLocation recipeName, Consumer<List<String>> addToList){
var list = table.get(stack, recipeName);
if (list == null) list = new ArrayList<>();
addToList.accept(list);
table.put(stack, recipeName, list);
}

public static List<String> 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<ItemTagMeta, String[]> getRecipeOutputTooltips() {
return RECIPE_OUTPUT_TOOLTIPS;
public static void onReload() {
GROOVY_DESCRIPTIONS.clear();
GROOVY_RECIPE_OUTPUT_TOOLTIPS.clear();
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/nomiceu/nomilabs/item/ItemBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<String> tooltip, @NotNull ITooltipFlag flagIn) {
tooltip.addAll(Arrays.asList(description));
Collections.addAll(tooltip, description);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand All @@ -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<String> tooltip, CallbackInfo ci) {
private static void addRecipeOutputTooltip(ResourceLocation registryName, int slotIndex, boolean input,
ItemStack stack, List<String> 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));
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/util/ItemMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/util/ItemTagMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit d98061d

Please sign in to comment.