Skip to content

Commit

Permalink
Allow Adding Newlines in Crafting Recipe Output Tooltips
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Jul 29, 2024
1 parent 370c85f commit a87a9a9
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 11 deletions.
13 changes: 10 additions & 3 deletions src/main/groovy-tests/jeiTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,23 @@ addDescription(item('minecraft:apple'), translatableLiteral('An Ordinary Apple..
// Add a translated description page for a stack
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. */
/*
* Recipe Output Tooltips. These are tooltips that appear on CRAFTING TABLE recipes that output that stack.
*
* They appear BEFORE `Recipe By <MODID>` Tooltips, and AFTER any item tooltips.
*
* You can either PROVIDE or NOT PROVIDE a Resource Location of the Recipe Name. If you PROVIDE, that provided tooltip has
* a higher priority than tooltip of the same stack, but without Recipe Name provided.
*/

// Add a crafting recipe output tooltip for a stack
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'), 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)
// Add a crafting recipe output tooltip for a specific recipe for a stack (Higher Priority than wild recipe name)
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)
// Add a translated crafting recipe output tooltip for a specific recipe for a stack (Higher Priority than wild recipe name)
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/config/LabsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@ public static class ModIntegration {
@Config.RequiresMcRestart
public boolean enableTopAddonsIntegration = true;

@Config.Comment({
"Whether to add a Empty Line between any Crafting Recipe Output Tooltips in JEI.",
"Examples of Crafting Recipe Output Tooltips are `Recipe By <MOD_ID>` and `Recipe ID: <RECIPE_ID>`.",
"[default: true]",
})
@Config.LangKey("config.nomilabs.mod_integration.jei_crafting_output_empty_line")
public boolean addJEICraftingOutputEmptyLine = true;

public static class EffortlessBuildingIntegration {

@Config.Comment({ "Whether to enable Effortless Building Integration, which splits the parts of reach.",
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/com/nomiceu/nomilabs/integration/jei/JEIPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class JEIPlugin implements IModPlugin {
.create();
private static final List<Pair<ItemStack, Function<NBTTagCompound, Boolean>>> IGNORE_NBT_HIDE = new ArrayList<>();

private static Table<ItemTagMeta, ResourceLocation, List<Translatable>> COMPILED_RECIPE_OUTPUT_TOOLTIPS = null;

private static IIngredientRegistry itemRegistry;

@Override
Expand Down Expand Up @@ -133,18 +135,25 @@ private static void addRecipeOutputTooltip(Table<ItemTagMeta, ResourceLocation,
table.put(stack, recipeName, list);
}

public static List<String> getRecipeOutputTooltip(ItemStack stack, ResourceLocation recipeName) {
var tempTable = HashBasedTable.create(RECIPE_OUTPUT_TOOLTIPS);
private static void cacheRecipeOutputTooltip() {
if (COMPILED_RECIPE_OUTPUT_TOOLTIPS != null) return;

COMPILED_RECIPE_OUTPUT_TOOLTIPS = HashBasedTable.create(RECIPE_OUTPUT_TOOLTIPS);
GROOVY_RECIPE_OUTPUT_TOOLTIPS.cellSet()
.forEach((cell) -> addRecipeOutputTooltip(tempTable, Objects.requireNonNull(cell.getRowKey()),
.forEach((cell) -> addRecipeOutputTooltip(COMPILED_RECIPE_OUTPUT_TOOLTIPS,
Objects.requireNonNull(cell.getRowKey()),
cell.getColumnKey(),
(list) -> list.addAll(Objects.requireNonNull(cell.getValue()))));
}

public static List<String> getRecipeOutputTooltip(ItemStack stack, ResourceLocation recipeName) {
cacheRecipeOutputTooltip();

var itemTagMeta = new ItemTagMeta(stack);
var specific = tempTable.get(itemTagMeta, recipeName);
var specific = COMPILED_RECIPE_OUTPUT_TOOLTIPS.get(itemTagMeta, recipeName);
if (specific != null) return specific.stream().map(Translatable::translate).collect(Collectors.toList());

var wildcard = tempTable.get(itemTagMeta, WILDCARD_LOCATION);
var wildcard = COMPILED_RECIPE_OUTPUT_TOOLTIPS.get(itemTagMeta, WILDCARD_LOCATION);
if (wildcard != null) return wildcard.stream().map(Translatable::translate).collect(Collectors.toList());
return new ArrayList<>();
}
Expand All @@ -153,5 +162,6 @@ public static void onReload() {
GROOVY_DESCRIPTIONS.clear();
GROOVY_RECIPE_OUTPUT_TOOLTIPS.clear();
IGNORE_NBT_HIDE.clear();
COMPILED_RECIPE_OUTPUT_TOOLTIPS = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import java.util.List;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.FMLCommonHandler;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -13,10 +14,16 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.nomiceu.nomilabs.config.LabsConfig;
import com.nomiceu.nomilabs.integration.jei.JEIPlugin;
import com.nomiceu.nomilabs.util.LabsSide;

import mezz.jei.plugins.vanilla.crafting.CraftingRecipeCategory;

/**
* Allows adding Custom Recipe Output Tooltips, and adds a new line before any recipe output tooltip (including `Recipe
* By <modid>`)
*/
@Mixin(value = CraftingRecipeCategory.class, remap = false)
public class CraftingRecipeCategoryMixin {

Expand All @@ -27,7 +34,20 @@ public class CraftingRecipeCategoryMixin {
@Inject(method = "lambda$setRecipe$0", at = @At("HEAD"))
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;
tooltip.addAll(JEIPlugin.getRecipeOutputTooltip(stack, registryName));
if (LabsSide.isServer() || slotIndex != craftOutputSlot) return;
boolean modIdDifferent = false;
ResourceLocation itemRegistryName = stack.getItem().getRegistryName();
if (itemRegistryName != null) {
String itemModId = itemRegistryName.getNamespace();
modIdDifferent = !registryName.getNamespace().equals(itemModId);
}

boolean showAdvanced = Minecraft.getMinecraft().gameSettings.advancedItemTooltips || GuiScreen.isShiftKeyDown();

var outputTooltip = JEIPlugin.getRecipeOutputTooltip(stack, registryName);
if (LabsConfig.modIntegration.addJEICraftingOutputEmptyLine &&
(modIdDifferent || showAdvanced || !outputTooltip.isEmpty()))
tooltip.add("");
tooltip.addAll(outputTooltip);
}
}
1 change: 1 addition & 0 deletions src/main/resources/assets/nomilabs/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ config.nomilabs.mod_integration.ender_storage=Enable Ender Storage Integration
config.nomilabs.mod_integration.ender_io=Enable Ender IO Integration
config.nomilabs.mod_integration.ftb_utils=Enable FTB Utilities Integration
config.nomilabs.mod_integration.top_addons=Enable TOP Addons Integration
config.nomilabs.mod_integration.jei_crafting_output_empty_line=Add JEI Crafting Output Tooltip Empty Line

config.nomilabs.mod_integration.effortlessbuilding=Effortless Building Integration Settings
config.nomilabs.mod_integration.effortlessbuilding.enable=Enable Effortless Building Integration
Expand Down

0 comments on commit a87a9a9

Please sign in to comment.