Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JEI Categories and related improvements #201

Merged
merged 7 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions examples/classes/GenericRecipeCategory.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package classes

import classes.SimpleConversionRecipe
import mezz.jei.api.gui.IDrawable
import mezz.jei.api.gui.IGuiIngredient
import mezz.jei.api.gui.IGuiIngredientGroup
import mezz.jei.api.gui.IRecipeLayout
import mezz.jei.api.ingredients.IIngredients
import mezz.jei.api.ingredients.VanillaTypes
import mezz.jei.api.recipe.IRecipeCategory
import mezz.jei.api.recipe.IRecipeWrapper
import mezz.jei.gui.ingredients.GuiIngredient
import net.minecraft.client.Minecraft
import net.minecraft.client.renderer.GlStateManager
import net.minecraft.client.resources.I18n

import java.util.stream.Collectors

/**
* An example of a IRecipeCategory from JEI, used in {@code /postInit/jei.groovy}.
* Will only appear if {@code SimpleConversionRecipe.recipes} contains recipes.
*/
class GenericRecipeCategory implements IRecipeCategory<RecipeWrapper> {

static final String UID = "${getPackId()}:generic"

private final def icon

private final def uid
private final def background

static def rightArrow
static def slot

GenericRecipeCategory(guiHelper) {
this(guiHelper, UID, 176, 67)
}

GenericRecipeCategory(guiHelper, uid, width, height) {
this.uid = uid
this.background = guiHelper.createBlankDrawable(width, height)
if (slot == null) {
rightArrow = guiHelper.drawableBuilder(resource('groovyscript:textures/jei/arrow_right.png'), 0, 0, 24, 15)
.setTextureSize(24, 15)
.build()
slot = guiHelper.getSlotDrawable()
}
this.icon = guiHelper.createDrawableIngredient(item('minecraft:clay'))
}

static def getRecipeWrappers() {
SimpleConversionRecipe.recipes.collect RecipeWrapper.&new
}

void setRecipe(IRecipeLayout recipeLayout, IRecipeWrapper recipeWrapper, IIngredients ingredients) {
addItemSlot(recipeLayout, 0, true, 53, 25)
addItemSlot(recipeLayout, 1, false, 105, 25)

recipeLayout.getItemStacks().set(ingredients)
setBackgrounds(recipeLayout.getItemStacks(), slot)
}

void drawExtras(Minecraft minecraft) {
GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f)
rightArrow.draw(minecraft, 76, 26)
}

IDrawable getIcon() {
icon
}

String getUid() {
this.uid
}

String getTitle() {
I18n.format("jei.category.${this.uid}.name")
}

String getModName() {
getPackName()
}

IDrawable getBackground() {
this.background
}

private static void addItemSlot(IRecipeLayout recipeLayout, int index, boolean input, int x, int y) {
recipeLayout.getItemStacks().init(index, input, x, y)
}

private static void setBackgrounds(IGuiIngredientGroup<?> ingredientGroup, IDrawable drawable) {
for (IGuiIngredient<?> ingredient : ingredientGroup.getGuiIngredients().values()) {
((GuiIngredient<?>) ingredient).setBackground(drawable)
}
}

static class RecipeWrapper implements IRecipeWrapper {

private final SimpleConversionRecipe recipe

RecipeWrapper(SimpleConversionRecipe recipe) {
this.recipe = recipe
}

void getIngredients(IIngredients ingredients) {
ingredients.setInput(VanillaTypes.ITEM, this.recipe.getInput())
ingredients.setOutput(VanillaTypes.ITEM, this.recipe.getOutput())
}
}
}
40 changes: 40 additions & 0 deletions examples/classes/GenericRecipeReloading.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package classes

/**
* A simple example of reloadable compat for a single registry, in this case {@code SimpleConversionRecipe}.<br>
*
* To use, call {@link GenericRecipeReloading#onReload()} in an event listener listening to {@code GroovyReloadEvent},
* and manipulate recipes by calling {@link GenericRecipeReloading#add()} or {@link GenericRecipeReloading#remove()}.<br>
*
* Note that {@link GenericRecipeReloading#onReload()} should only be called when GroovyScript is reloading,
* so you will want to have it called by listening to {@code GroovyReloadEvent} in something like this:<br>
* <pre>
* eventManager.listen(com.cleanroommc.groovyscript.event.GroovyReloadEvent) {
* GenericRecipeReloading.instance.onReload()
* }
* </pre>
*/
class GenericRecipeReloading {

static def instance = new GenericRecipeReloading()

def scripted = []
def backup = []

void onReload() {
scripted.each { SimpleConversionRecipe.recipes.remove(it) }
scripted.clear()
backup.each { SimpleConversionRecipe.recipes.add(it) }
backup.clear()
}

void add(recipe) {
scripted << recipe
SimpleConversionRecipe.recipes.add(recipe)
}

void remove(recipe) {
backup << recipe
SimpleConversionRecipe.recipes.remove(recipe)
}
}
17 changes: 17 additions & 0 deletions examples/classes/SimpleConversionRecipe.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package classes

/**
* A simple recipe and recipe holder to demonstrate both classes in GroovyScript and custom reloading compat.
*/
class SimpleConversionRecipe {
static final List<SimpleConversionRecipe> recipes = []

ItemStack input
ItemStack output

SimpleConversionRecipe(input, output) {
this.input = input
this.output = output
}

}
30 changes: 30 additions & 0 deletions examples/postInit/custom/reloading_compat.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

// this file demonstrates custom reloading compat
// also demonstrating some event manager interactions

import classes.GenericRecipeReloading
import classes.SimpleConversionRecipe
import net.minecraft.util.text.TextComponentString
import net.minecraftforge.event.world.BlockEvent
import com.cleanroommc.groovyscript.event.GroovyReloadEvent

// add the example recipe
GenericRecipeReloading.instance.add(new SimpleConversionRecipe(item('minecraft:clay'), item('minecraft:gold_ingot')))

// reload via an event
eventManager.listen(GroovyReloadEvent) {
GenericRecipeReloading.instance.onReload()
}

// use an event to demonstrate the recipe in-game
eventManager.listen(BlockEvent.BreakEvent) {
// get the block drop if it was silk touched
def drop = it.getState().getBlock().getSilkTouchDrop(it.getState())
// find if any of the recipes have an input that match the drop
def found = SimpleConversionRecipe.recipes.find { it.input in drop }
if (found != null) {
// send the player a pair of messages to demonstrate the recipe
it.player.sendMessage(new TextComponentString("You broke a ${it.getState().getBlock().getLocalizedName()} Block!"))
it.player.sendMessage(new TextComponentString("A custom recipe marks a ${found.output.getDisplayName()} as its output."))
}
}
9 changes: 9 additions & 0 deletions examples/postInit/jei.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Auto generated groovyscript example file
// MODS_LOADED: jei

import classes.GenericRecipeCategory
import mezz.jei.api.ingredients.VanillaTypes

println 'mod \'jei\' detected, running script'
Expand All @@ -21,6 +22,14 @@ mods.jei.catalyst.add('minecraft.smelting', item('minecraft:clay') * 8, item('mi
mods.jei.category.hideCategory('minecraft.fuel')
// mods.jei.category.hideAll()

/*mods.jei.category.categoryBuilder()
.id(GenericRecipeCategory.UID) // Note that `GenericRecipeCategory` must be defined elsewhere, and this example presumes certain fields and methods exist.
.category(guiHelper -> new GenericRecipeCategory(guiHelper))
.catalyst(item('minecraft:clay'))
.wrapper(GenericRecipeCategory.getRecipeWrappers())
.register()*/


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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.cleanroommc.groovyscript.compat.inworldcrafting.jei;

import com.cleanroommc.groovyscript.api.GroovyBlacklist;
import com.cleanroommc.groovyscript.compat.inworldcrafting.FluidRecipe;
import com.cleanroommc.groovyscript.compat.vanilla.VanillaModule;
import mezz.jei.api.IGuiHelper;
import mezz.jei.api.IModRegistry;
import mezz.jei.api.recipe.IRecipeCategoryRegistration;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;

import java.util.ArrayList;
import java.util.List;

@GroovyBlacklist
public class InWorldCraftingJeiPlugin {

public static void registerCategories(IRecipeCategoryRegistration registry) {
IGuiHelper guiHelper = registry.getJeiHelpers().getGuiHelper();
registry.addRecipeCategories(new FluidRecipeCategory(guiHelper));
registry.addRecipeCategories(new ExplosionRecipeCategory(guiHelper));
registry.addRecipeCategories(new BurningRecipeCategory(guiHelper));
registry.addRecipeCategories(new PistonPushRecipeCategory(guiHelper));
}

public static void register(IModRegistry registry) {
// add catalyst items
registry.addRecipeCatalyst(new ItemStack(Items.WATER_BUCKET), FluidRecipeCategory.UID);
registry.addRecipeCatalyst(new ItemStack(Items.LAVA_BUCKET), FluidRecipeCategory.UID);
registry.addRecipeCatalyst(new ItemStack(Blocks.TNT), ExplosionRecipeCategory.UID);
//registry.addRecipeCatalyst(new ItemStack(Blocks.FIRE), BurningRecipeCategory.UID);
registry.addRecipeCatalyst(new ItemStack(Items.FLINT_AND_STEEL), BurningRecipeCategory.UID);
registry.addRecipeCatalyst(new ItemStack(Blocks.PISTON), PistonPushRecipeCategory.UID);
registry.addRecipeCatalyst(new ItemStack(Blocks.STICKY_PISTON), PistonPushRecipeCategory.UID);

// add recipe wrappers
List<FluidRecipeCategory.RecipeWrapper> recipeWrappers = new ArrayList<>();
FluidRecipe.forEach(fluidRecipe -> recipeWrappers.add(new FluidRecipeCategory.RecipeWrapper(fluidRecipe)));
registry.addRecipes(recipeWrappers, FluidRecipeCategory.UID);
registry.addRecipes(VanillaModule.inWorldCrafting.explosion.getRecipeWrappers(), ExplosionRecipeCategory.UID);
registry.addRecipes(VanillaModule.inWorldCrafting.burning.getRecipeWrappers(), BurningRecipeCategory.UID);
registry.addRecipes(VanillaModule.inWorldCrafting.pistonPush.getRecipeWrappers(), PistonPushRecipeCategory.UID);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class Catalyst extends VirtualizedRegistry<Pair<String, ItemStack>> {

/**
* Called by {@link JeiPlugin#register}
* Called by {@link JeiPlugin#afterRegister()}
*/
@GroovyBlacklist
public void applyChanges(IModRegistry modRegistry) {
Expand Down
Loading