Skip to content

Commit

Permalink
Better JEI compat (#142)
Browse files Browse the repository at this point in the history
* convert Ingredient into subcategory

* add category registry

* add description compat

* add catalyst modification compat

* remove outdated reload code

* add examples and lang keys

* properly comment out examples which error in default environment

* fix up description a bit

* add Sidebar to Ingredient aliases
  • Loading branch information
WaitingIdly authored Mar 24, 2024
1 parent 3f5c539 commit 6837ebf
Show file tree
Hide file tree
Showing 12 changed files with 619 additions and 140 deletions.
48 changes: 48 additions & 0 deletions examples/postInit/jei.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

// Auto generated groovyscript example file
// MODS_LOADED: jei

import mezz.jei.api.ingredients.VanillaTypes

println 'mod \'jei\' detected, running script'

// Category Catalysts:
// Modify the items shown on the left of JEI Categories which indicate where the recipe takes place

mods.jei.catalyst.remove('minecraft.smelting', item('minecraft:furnace'))
// mods.jei.catalyst.removeByType('minecraft.anvil')

mods.jei.catalyst.add('minecraft.smelting', item('minecraft:clay') * 8, item('minecraft:cobblestone'))

// Categories:
// Modify the Categories visible in JEI, each of which contain recipes and are associated with specific blocks, typically
// machines.

mods.jei.category.hideCategory('minecraft.fuel')
// mods.jei.category.hideAll()

// Description Category:
// Modify the description of the input items, where the description is a unique JEI tab containing text.

// mods.jei.description.remove(item('thaumcraft:triple_meat_treat'))

mods.jei.description.add(item('minecraft:clay'), ['wow', 'this', 'is', 'neat'])
mods.jei.description.add(item('minecraft:gold_ingot'), 'groovyscript.recipe.fluid_recipe')

// Ingredient Sidebar:
// Modify what ingredients show up in the search menu sidebar.

mods.jei.ingredient.hide(fluid('water'))
mods.jei.ingredient.hide(item('minecraft:stone:1'), item('minecraft:stone:3'))
mods.jei.ingredient.hide(VanillaTypes.ITEM, item('minecraft:bed:*'))
// mods.jei.ingredient.hide(mekanism.client.jei.MekanismJEI.TYPE_GAS, gas('tritium'))
// mods.jei.ingredient.hideByType(VanillaTypes.ITEM)
// mods.jei.ingredient.hideByType(VanillaTypes.FLUID)
// mods.jei.ingredient.hideByType(VanillaTypes.ENCHANT)
// mods.jei.ingredient.hideByType(mekanism.client.jei.MekanismJEI.TYPE_GAS)
// mods.jei.ingredient.hideByType(com.buuz135.thaumicjei.ThaumcraftJEIPlugin.ASPECT_LIST)
// mods.jei.ingredient.hideAll()

mods.jei.ingredient.add(item('minecraft:stone:1').withNbt([display:[Name:'Special Granite']]))
mods.jei.ingredient.add(VanillaTypes.ITEM, item('minecraft:bed').withNbt([display:[Name:'Beds come in 16 different colors!']]))

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.cleanroommc.groovyscript.compat.mods.jei;

import com.cleanroommc.groovyscript.api.GroovyBlacklist;
import com.cleanroommc.groovyscript.api.documentation.annotations.Example;
import com.cleanroommc.groovyscript.api.documentation.annotations.MethodDescription;
import com.cleanroommc.groovyscript.api.documentation.annotations.RegistryDescription;
import com.cleanroommc.groovyscript.core.mixin.jei.ModRegistryAccessor;
import com.cleanroommc.groovyscript.registry.VirtualizedRegistry;
import mezz.jei.api.IModRegistry;
import net.minecraft.item.ItemStack;
import org.apache.commons.lang3.tuple.Pair;

import java.util.Arrays;
import java.util.Collection;

@RegistryDescription(category = RegistryDescription.Category.ENTRIES)
public class Catalyst extends VirtualizedRegistry<Pair<String, ItemStack>> {

/**
* Called by {@link JeiPlugin#register}
*/
@GroovyBlacklist
public void applyChanges(IModRegistry modRegistry) {
for (var backupRecipe : getBackupRecipes()) {
((ModRegistryAccessor) modRegistry).getRecipeCatalysts().get(backupRecipe.getKey())
.removeIf(x -> backupRecipe.getValue() == null ||
x instanceof ItemStack &&
ItemStack.areItemStacksEqual((ItemStack) x, backupRecipe.getValue()));
}
for (var scriptedRecipe : getScriptedRecipes()) {
modRegistry.addRecipeCatalyst(scriptedRecipe.getValue(), scriptedRecipe.getKey());
}
}

@Override
public void onReload() {
restoreFromBackup();
removeScripted();
}

@MethodDescription(type = MethodDescription.Type.ADDITION)
public void add(String category, ItemStack item) {
addScripted(Pair.of(category, item));
}

@MethodDescription(type = MethodDescription.Type.ADDITION, example = @Example("'minecraft.smelting', item('minecraft:clay') * 8, item('minecraft:cobblestone')"))
public void add(String category, ItemStack... item) {
Arrays.stream(item).map(i -> Pair.of(category, i)).forEach(this::addScripted);
}

@MethodDescription(type = MethodDescription.Type.ADDITION)
public void add(String category, Collection<ItemStack> item) {
item.stream().map(i -> Pair.of(category, i)).forEach(this::addScripted);
}

@MethodDescription(example = @Example("'minecraft.smelting', item('minecraft:furnace')"))
public void remove(String category, ItemStack item) {
addBackup(Pair.of(category, item));
}

@MethodDescription
public void remove(String category, ItemStack... item) {
Arrays.stream(item).map(i -> Pair.of(category, i)).forEach(this::addBackup);
}

@MethodDescription
public void remove(String category, Collection<ItemStack> item) {
item.stream().map(i -> Pair.of(category, i)).forEach(this::addBackup);
}

@MethodDescription(example = @Example(value = "'minecraft.anvil'", commented = true))
public void removeByType(String category) {
addBackup(Pair.of(category, null));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.cleanroommc.groovyscript.compat.mods.jei;

import com.cleanroommc.groovyscript.api.GroovyBlacklist;
import com.cleanroommc.groovyscript.api.GroovyLog;
import com.cleanroommc.groovyscript.api.documentation.annotations.Admonition;
import com.cleanroommc.groovyscript.api.documentation.annotations.Example;
import com.cleanroommc.groovyscript.api.documentation.annotations.MethodDescription;
import com.cleanroommc.groovyscript.api.documentation.annotations.RegistryDescription;
import com.cleanroommc.groovyscript.registry.VirtualizedRegistry;
import mezz.jei.api.IRecipeRegistry;
import mezz.jei.api.recipe.IRecipeCategory;

@RegistryDescription(category = RegistryDescription.Category.ENTRIES,
admonition = @Admonition("groovyscript.wiki.jei.category.note0"))
public class Category extends VirtualizedRegistry<String> {

private boolean hideAllCategories;

/**
* Called by {@link JeiPlugin#onRuntimeAvailable}
*/
@GroovyBlacklist
public void applyChanges(IRecipeRegistry recipeRegistry) {
if (hideAllCategories) recipeRegistry.getRecipeCategories().stream().map(IRecipeCategory::getUid).forEach(this::addBackup);
getBackupRecipes().forEach(recipeRegistry::hideRecipeCategory);
}

@Override
public void onReload() {
restoreFromBackup();
hideAllCategories = false;
}

@MethodDescription(description = "groovyscript.wiki.jei.category.hideCategory")
public void remove(String category) {
if (category == null || category.isEmpty()) {
GroovyLog.msg("Error hiding category")
.add("category must not be empty")
.error()
.post();
return;
}
addBackup(category);
}

@MethodDescription(example = @Example("'minecraft.fuel'"))
public void hideCategory(String category) {
remove(category);
}

@MethodDescription(priority = 2000, example = @Example(commented = true))
public void hideAll() {
hideAllCategories = true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.cleanroommc.groovyscript.compat.mods.jei;

import com.cleanroommc.groovyscript.api.GroovyBlacklist;
import com.cleanroommc.groovyscript.api.IIngredient;
import com.cleanroommc.groovyscript.api.documentation.annotations.Example;
import com.cleanroommc.groovyscript.api.documentation.annotations.MethodDescription;
import com.cleanroommc.groovyscript.api.documentation.annotations.RegistryDescription;
import com.cleanroommc.groovyscript.core.mixin.jei.IngredientInfoRecipeAccessor;
import com.cleanroommc.groovyscript.registry.VirtualizedRegistry;
import mezz.jei.api.IModRegistry;
import mezz.jei.api.IRecipeRegistry;
import mezz.jei.api.ingredients.VanillaTypes;
import mezz.jei.api.recipe.VanillaRecipeCategoryUid;
import mezz.jei.plugins.jei.info.IngredientInfoRecipeCategory;
import net.minecraft.item.ItemStack;
import org.apache.commons.lang3.tuple.Pair;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RegistryDescription(category = RegistryDescription.Category.ENTRIES)
public class Description extends VirtualizedRegistry<Pair<List<IIngredient>, List<String>>> {

/**
* Called by {@link JeiPlugin#register}
*/
@GroovyBlacklist
public void applyAdditions(IModRegistry modRegistry) {
for (Pair<List<IIngredient>, List<String>> entry : this.getScriptedRecipes()) {
modRegistry.addIngredientInfo(entry.getLeft().stream().flatMap(x -> Stream.of(x.getMatchingStacks())).collect(Collectors.toList()),
// Currently, it is only possible to add VanillaTypes.ITEM. It may be desirable to add the ability to do other types.
VanillaTypes.ITEM,
entry.getRight().toArray(new String[0]));
}
}

/**
* Called by {@link JeiPlugin#onRuntimeAvailable}
*/
@GroovyBlacklist
public void applyRemovals(IRecipeRegistry recipeRegistry) {
IngredientInfoRecipeCategory category = (IngredientInfoRecipeCategory) recipeRegistry.getRecipeCategory(VanillaRecipeCategoryUid.INFORMATION);
if (category != null) {
recipeRegistry.getRecipeWrappers(category).forEach(wrapper -> {
IngredientInfoRecipeAccessor<?> accessor = (IngredientInfoRecipeAccessor<?>) wrapper;

// Currently, it is only possible to remove VanillaTypes.ITEM. It may be desirable to add the ability to do other types.
if (!VanillaTypes.ITEM.equals(accessor.getIngredientType())) return;

for (Pair<List<IIngredient>, List<String>> entry : this.getBackupRecipes()) {
if (entry.getKey().stream().anyMatch(x -> accessor.getIngredients().stream().anyMatch(a -> a instanceof ItemStack && x.test((ItemStack) a)))) {
recipeRegistry.hideRecipe(wrapper, VanillaRecipeCategoryUid.INFORMATION);
}
}
});
}
}

@Override
public void onReload() {
restoreFromBackup();
removeScripted();
}

@MethodDescription(type = MethodDescription.Type.ADDITION)
public void add(List<IIngredient> target, List<String> description) {
addScripted(Pair.of(target, description));
}

@MethodDescription(type = MethodDescription.Type.ADDITION)
public void add(List<IIngredient> target, String... description) {
addScripted(Pair.of(target, Arrays.asList(description)));
}

@MethodDescription(type = MethodDescription.Type.ADDITION, example = @Example("item('minecraft:clay'), ['wow', 'this', 'is', 'neat']"))
public void add(IIngredient target, List<String> description) {
addScripted(Pair.of(Collections.singletonList(target), description));
}

@MethodDescription(type = MethodDescription.Type.ADDITION, example = @Example("item('minecraft:gold_ingot'), 'groovyscript.recipe.fluid_recipe'"))
public void add(IIngredient target, String... description) {
addScripted(Pair.of(Collections.singletonList(target), Arrays.asList(description)));
}

@MethodDescription(example = @Example(value = "item('thaumcraft:triple_meat_treat')", commented = true))
public void remove(List<IIngredient> target) {
addBackup(Pair.of(target, null));
}

@MethodDescription
public void remove(IIngredient... target) {
addBackup(Pair.of(Arrays.asList(target), null));
}

}
Loading

0 comments on commit 6837ebf

Please sign in to comment.