Skip to content

Commit

Permalink
Revamp JEI Helpers, Revamp Information Item
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Jul 9, 2024
1 parent 477d343 commit d7f2a52
Show file tree
Hide file tree
Showing 17 changed files with 343 additions and 65 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ modGroup = com.nomiceu.nomilabs

# Version of your mod.
# This field can be left empty if you want your mod's version to be determined by the latest git tag instead.
modVersion = 0.6.29
modVersion = 0.7

# Whether to use the old jar naming structure (modid-mcversion-version) instead of the new version (modid-version)
includeMCVersionJar = false
Expand Down
46 changes: 39 additions & 7 deletions src/main/groovy-tests/jeiTests.groovy
Original file line number Diff line number Diff line change
@@ -1,28 +1,60 @@
import gregtech.client.utils.TooltipHelper
import net.minecraft.util.text.TextFormatting

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

// JEI Helpers. (Goes in Post Init)
// JEI and Translation Helpers. (Goes in Post Init)

// There are two types of translations: translate and translatable.
// Translate translates the input NOW, whilst translatable translates the input when it is needed.
// All JEI Pages use Translatable, so that the language can be changed on-the-fly.

// Translate: Instantly Translate Something, on Server or Client, with Parameters
// Use TranslateFormat to Translate with Parameters and a GT Format Code wrapped around the string!
// (Note, normal format codes should be included in the lang itself, and thus are not available with `translateFormat`)
// (Note, normal format codes are still available with `format`)
println('Translated Hand Framing Tool Side: ' + translate('tooltip.nomilabs.hand_framing_tool.side', format('Hi', TextFormatting.AQUA)))
println('Translated Hand Framing Tool Main: ' + translateFormat('tooltip.nomilabs.hand_framing_tool.not_set', TooltipHelper.RAINBOW))

// Translatable: Save something to be translated later
var translatableObj = translatable('tooltip.nomilabs.excitationcoil.description')

// You can add formatting (TextFormatting, GTFormatCode, or Format)
translatableObj.addFormat(TextFormatting.AQUA)

// You can append other translatable objects
// Note that appended objects will be concatenated with no seperator, and in this order:
// If translatable1 has translated2 and translated3 appended in that order, and translated2 has p1 and p2 appended in that order:
// transtable1 + translated2 + p1 + p2 + translated3
translatableObj.append(translatable('tooltip.nomilabs.hand_framing_tool.front', format('Hello World!', TextFormatting.RED)))

// Very flexible, you can even append literal strings
translatableObj.append(translatableLiteral('Hello World!'))

// Call `toString` or `translate` to retrieve the translated and concatenated string
println('Translatable Object: ' + translatableObj.translate())

/* JEI Pages. Each Method requires Translatable Objects. */

/* Description Pages. Each entry is seperated by double new lines. */

// Add a description page for a stack
addDescription(item('minecraft:apple'), format("An Ordinary Apple... Not Poisoned.", TextFormatting.DARK_GREEN), "Eat it!")
addDescription(item('minecraft:apple'), translatableLiteral('An Ordinary Apple... Not Poisoned.').addFormat(TextFormatting.DARK_GREEN), translatableLiteral('Eat it!'))

// 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"))
addDescription(item('minecraft:iron_ingot'), translatable('tooltip.nomilabs.greenhouse.description'), translatable('tooltip.nomilabs.dme_sim_chamber.description'))

/* Recipe Output Tooltips. These are tooltips that appear on CRAFTING recipes that output that stack. */

// 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!")
addRecipeOutputTooltip(item('minecraft:gold_ingot'), translatableLiteral('A Very Low Carrot Gold Ingot.').addFormat(TextFormatting.GOLD))

// 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"))
addRecipeOutputTooltip(item('minecraft:iron_ingot'), translatable('tooltip.nomilabs.greenhouse.description'), translatable('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!")
addRecipeOutputTooltip(item('minecraft:gold_ingot'), resource('minecraft:gold_ingot_from_block'), translatableLiteral('A Very High Carrot Gold Ingot.').addFormat(TextFormatting.GOLD))

// 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"))
addRecipeOutputTooltip(item('minecraft:iron_ingot'), resource('minecraft:iron_ingot_from_nuggets'), translatable('tooltip.nomilabs.universalnavigator.description'), translatable('tooltip.nomilabs.greenhouse.description'), translatable('tooltip.nomilabs.dme_sim_chamber.description'))
8 changes: 8 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/event/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraftforge.fml.relauncher.SideOnly;

import com.cleanroommc.groovyscript.event.ScriptRunEvent;
import com.cleanroommc.groovyscript.registry.ReloadableRegistryManager;
import com.nomiceu.nomilabs.LabsValues;
import com.nomiceu.nomilabs.NomiLabs;
import com.nomiceu.nomilabs.fluid.registry.LabsFluids;
Expand Down Expand Up @@ -55,6 +56,13 @@ public static void addTooltipNormal(ItemTooltipEvent event) {
TooltipAdder.addTooltipNormal(event.getToolTip(), event.getItemStack());
}

@SubscribeEvent
public static void languageChanged(LabsLanguageChangedEvent event) {
// Reload JEI to refresh description text
// noinspection UnstableApiUsage
ReloadableRegistryManager.reloadJei(false);
}

@SubscribeEvent
public static void afterScriptLoad(ScriptRunEvent.Post event) {
NomiLabs.LOGGER.info("Reloading Options File.");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.nomiceu.nomilabs.event;

import net.minecraft.client.resources.Language;
import net.minecraftforge.fml.common.eventhandler.Event;

/**
* An event that is called AFTER language is changed, and resources are refreshed.
* <p>
* Called Client Side ONLY.
*/
public class LabsLanguageChangedEvent extends Event {

public final Language oldLanguage;
public final Language newLanguage;

public LabsLanguageChangedEvent(Language oldLanguage, Language newLanguage) {
this.oldLanguage = oldLanguage;
this.newLanguage = newLanguage;
}
}
15 changes: 12 additions & 3 deletions src/main/java/com/nomiceu/nomilabs/groovy/GroovyHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ public static String format(String str, TooltipHelper.GTFormatCode... formats) {
public static String format(String str, LabsTranslate.Format... formats) {
return LabsTranslate.format(str, formats);
}

public static LabsTranslate.Translatable translatable(String key, Object... params) {
return LabsTranslate.translatable(key, params);
}

public static LabsTranslate.Translatable translatableLiteral(String text) {
return LabsTranslate.translatableLiteral(text);
}
}

public static class SafeMethodHelpers {
Expand Down Expand Up @@ -134,15 +142,16 @@ public static Object callMethod(Class<?> clazz, @Nullable Object caller, @NotNul
public static class JEIHelpers {

/* Description + Tooltip */
public static void addDescription(ItemStack stack, String... description) {
public static void addDescription(ItemStack stack, LabsTranslate.Translatable... description) {
JEIPlugin.addGroovyDescription(stack, description);
}

public static void addRecipeOutputTooltip(ItemStack stack, String... tooltip) {
public static void addRecipeOutputTooltip(ItemStack stack, LabsTranslate.Translatable... tooltip) {
JEIPlugin.addGroovyRecipeOutputTooltip(stack, tooltip);
}

public static void addRecipeOutputTooltip(ItemStack stack, ResourceLocation recipeName, String... tooltip) {
public static void addRecipeOutputTooltip(ItemStack stack, ResourceLocation recipeName,
LabsTranslate.Translatable... tooltip) {
JEIPlugin.addGroovyRecipeOutputTooltip(stack, recipeName, tooltip);
}

Expand Down
54 changes: 36 additions & 18 deletions src/main/java/com/nomiceu/nomilabs/integration/jei/JEIPlugin.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.nomiceu.nomilabs.integration.jei;

import static com.nomiceu.nomilabs.util.LabsTranslate.Translatable;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
Expand All @@ -19,10 +22,13 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Table;
import com.nomiceu.nomilabs.groovy.PartialRecipe;
import com.nomiceu.nomilabs.item.registry.LabsItems;
import com.nomiceu.nomilabs.util.ItemTagMeta;

import mezz.jei.api.IJeiRuntime;
import mezz.jei.api.IModPlugin;
import mezz.jei.api.IModRegistry;
import mezz.jei.api.ingredients.IIngredientRegistry;
import mezz.jei.api.ingredients.VanillaTypes;
import mezz.jei.api.recipe.VanillaRecipeCategoryUid;

Expand All @@ -32,27 +38,37 @@
public class JEIPlugin implements IModPlugin {

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
private static final Map<ItemTagMeta, List<Translatable>> DESCRIPTIONS = new HashMap<>();
private static final Map<ItemTagMeta, List<Translatable>> GROOVY_DESCRIPTIONS = new HashMap<>();
private static final Table<ItemTagMeta, ResourceLocation, List<Translatable>> RECIPE_OUTPUT_TOOLTIPS = HashBasedTable
.create();
private static final Table<ItemTagMeta, ResourceLocation, List<String>> GROOVY_RECIPE_OUTPUT_TOOLTIPS = HashBasedTable
private static final Table<ItemTagMeta, ResourceLocation, List<Translatable>> GROOVY_RECIPE_OUTPUT_TOOLTIPS = HashBasedTable
.create();
private static final List<Pair<ItemStack, Function<NBTTagCompound, Boolean>>> IGNORE_NBT_HIDE = new ArrayList<>();

private static IIngredientRegistry itemRegistry;

@Override
public void register(IModRegistry registry) {
var jeiHelpers = registry.getJeiHelpers();
itemRegistry = registry.getIngredientRegistry();

// JEI does not recognise Custom Recipe Classes on its own
registry.handleRecipes(PartialRecipe.class, recipe -> new PartialRecipeWrapper(jeiHelpers, recipe),
VanillaRecipeCategoryUid.CRAFTING);

// Add Descriptions
Map<ItemTagMeta, List<String>> tempMap = new HashMap<>(DESCRIPTIONS);
Map<ItemTagMeta, List<Translatable>> 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))));
strings.stream().map(Translatable::translate).collect(Collectors.joining("\n\n")))));
}

@Override
public void onRuntimeAvailable(@NotNull IJeiRuntime jeiRuntime) {
// Remove Info Item from JEI
itemRegistry.removeIngredientsAtRuntime(VanillaTypes.ITEM,
Collections.singletonList(new ItemStack(LabsItems.INFO_ITEM)));
}

public static void hideItemNBTMatch(ItemStack itemStack, Function<NBTTagCompound, Boolean> condition) {
Expand All @@ -74,43 +90,43 @@ public static List<Pair<ItemStack, Function<NBTTagCompound, Boolean>>> getIgnore
return ImmutableList.copyOf(IGNORE_NBT_HIDE);
}

public static void addDescription(@NotNull ItemStack stack, String... description) {
public static void addDescription(@NotNull ItemStack stack, Translatable... description) {
addDescription(DESCRIPTIONS, new ItemTagMeta(stack), (list) -> Collections.addAll(list, description));
}

public static void addGroovyDescription(@NotNull ItemStack stack, String... description) {
public static void addGroovyDescription(@NotNull ItemStack stack, Translatable... 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) {
private static void addDescription(Map<ItemTagMeta, List<Translatable>> map,
@NotNull ItemTagMeta stack, Consumer<List<Translatable>> addToList) {
map.computeIfAbsent(stack, (k) -> new ArrayList<>());
addToList.accept(map.get(stack));
}

public static void addRecipeOutputTooltip(@NotNull ItemStack stack, String... tooltip) {
public static void addRecipeOutputTooltip(@NotNull ItemStack stack, Translatable... tooltip) {
addRecipeOutputTooltip(stack, WILDCARD_LOCATION, tooltip);
}

public static void addRecipeOutputTooltip(@NotNull ItemStack stack, ResourceLocation recipeName,
String... tooltip) {
Translatable... tooltip) {
addRecipeOutputTooltip(RECIPE_OUTPUT_TOOLTIPS, new ItemTagMeta(stack), recipeName,
(list) -> Collections.addAll(list, tooltip));
}

public static void addGroovyRecipeOutputTooltip(@NotNull ItemStack stack, String... tooltip) {
public static void addGroovyRecipeOutputTooltip(@NotNull ItemStack stack, Translatable... tooltip) {
addGroovyRecipeOutputTooltip(stack, WILDCARD_LOCATION, tooltip);
}

public static void addGroovyRecipeOutputTooltip(@NotNull ItemStack stack, ResourceLocation recipeName,
String... tooltip) {
Translatable... 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,
private static void addRecipeOutputTooltip(Table<ItemTagMeta, ResourceLocation, List<Translatable>> table,
@NotNull ItemTagMeta stack, ResourceLocation recipeName,
Consumer<List<String>> addToList) {
Consumer<List<Translatable>> addToList) {
var list = table.get(stack, recipeName);
if (list == null) list = new ArrayList<>();
addToList.accept(list);
Expand All @@ -123,11 +139,13 @@ public static List<String> getRecipeOutputTooltip(ItemStack stack, ResourceLocat
.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;
if (specific != null) return specific.stream().map(Translatable::translate).collect(Collectors.toList());

var wildcard = tempTable.get(itemTagMeta, WILDCARD_LOCATION);
if (wildcard != null) return wildcard;
if (wildcard != null) return wildcard.stream().map(Translatable::translate).collect(Collectors.toList());
return new ArrayList<>();
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/nomiceu/nomilabs/item/ItemHandFramingTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,11 @@ private static NBTTagCompound getMaterialTag(@Nonnull ItemStack stack) {

public void addDescription() {
JEIPlugin.addDescription(new ItemStack(this),
translate("item.nomilabs.hand_framing_tool.desc1"),
translate("item.nomilabs.hand_framing_tool.desc2"),
translate("item.nomilabs.hand_framing_tool.desc3"),
translate("item.nomilabs.hand_framing_tool.desc4"),
translate("item.nomilabs.hand_framing_tool.desc5"),
translate("item.nomilabs.hand_framing_tool.desc6"));
translatable("item.nomilabs.hand_framing_tool.desc1"),
translatable("item.nomilabs.hand_framing_tool.desc2"),
translatable("item.nomilabs.hand_framing_tool.desc3"),
translatable("item.nomilabs.hand_framing_tool.desc4"),
translatable("item.nomilabs.hand_framing_tool.desc5"),
translatable("item.nomilabs.hand_framing_tool.desc6"));
}
}
80 changes: 80 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/item/ItemInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.nomiceu.nomilabs.item;

import static com.nomiceu.nomilabs.util.LabsTranslate.*;

import java.util.*;

import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.common.IRarity;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* An item specifically designed to show information, via the tooltip.
* <p>
* Note that this item is not in any creative tabs or JEI!
*/
public class ItemInfo extends Item {

private final EnumRarity rarity;
private static final Map<Integer, Translatable[]> TOOLTIP_MAP;

public static final int AE2_STUFF_REMAP_INFO = 1;

public ItemInfo(ResourceLocation rl, EnumRarity rarity) {
setRegistryName(rl);
setMaxStackSize(64);
setHasSubtypes(true);

this.rarity = rarity;
}

/**
* Does not include the item with no meta.
*/
public Set<Integer> getSubMetas() {
return TOOLTIP_MAP.keySet();
}

@Override
protected boolean isInCreativeTab(@NotNull CreativeTabs targetTab) {
return false;
}

@Override
public @NotNull IRarity getForgeRarity(@NotNull ItemStack stack) {
return rarity;
}

@Override
public void addInformation(ItemStack stack, @Nullable World worldIn, @NotNull List<String> tooltip,
@NotNull ITooltipFlag flagIn) {
if (!TOOLTIP_MAP.containsKey(stack.getMetadata())) return;

var tooltipList = TOOLTIP_MAP.get(stack.getMetadata());
for (var line : tooltipList) {
tooltip.add(line.translate());
}
}

static {
TOOLTIP_MAP = new HashMap<>();
// Leave Meta 0 as Nothing, so its almost a normal item
TOOLTIP_MAP.put(AE2_STUFF_REMAP_INFO, new Translatable[] {
translatable("tooltip.nomilabs.info.ae2-stuff.1"),
translatableLiteral(""),
translatable("tooltip.nomilabs.info.ae2-stuff.2"),
translatableLiteral(""),
translatable("tooltip.nomilabs.info.ae2-stuff.3"),
translatable("tooltip.nomilabs.info.ae2-stuff.4"),
translatable("tooltip.nomilabs.info.ae2-stuff.5")
});
}
}
Loading

0 comments on commit d7f2a52

Please sign in to comment.