Skip to content

Commit

Permalink
jei explosion recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed Aug 20, 2023
1 parent 7869929 commit bef24e3
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.cleanroommc.groovyscript.api.GroovyBlacklist;
import com.cleanroommc.groovyscript.api.GroovyLog;
import com.cleanroommc.groovyscript.api.IIngredient;
import com.cleanroommc.groovyscript.compat.inworldcrafting.jei.ExplosionRecipeCategory;
import com.cleanroommc.groovyscript.compat.vanilla.VanillaModule;
import com.cleanroommc.groovyscript.helper.recipe.AbstractRecipeBuilder;
import com.cleanroommc.groovyscript.registry.VirtualizedRegistry;
Expand All @@ -12,15 +13,23 @@
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.fml.common.Optional;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Explosion extends VirtualizedRegistry<Explosion.Recipe> {

private final List<Recipe> recipes = new ArrayList<>();

@Optional.Method(modid = "jei")
@GroovyBlacklist
public List<ExplosionRecipeCategory.RecipeWrapper> getRecipeWrappers() {
return this.recipes.stream().map(ExplosionRecipeCategory.RecipeWrapper::new).collect(Collectors.toList());
}

@Override
public void onReload() {
this.recipes.addAll(getBackupRecipes());
Expand Down Expand Up @@ -62,6 +71,18 @@ public Recipe(IIngredient input, ItemStack output, float chance, Closure<Boolean
this.beforeRecipe = beforeRecipe;
}

public IIngredient getInput() {
return input;
}

public ItemStack getOutput() {
return output;
}

public float getChance() {
return chance;
}

private boolean tryRecipe(EntityItem entityItem, ItemStack itemStack) {
if (!this.input.test(itemStack)) return false;
if (this.beforeRecipe != null && !ClosureHelper.call(true, this.beforeRecipe, entityItem, itemStack)) return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.cleanroommc.groovyscript.compat.inworldcrafting.jei;

import com.cleanroommc.groovyscript.GroovyScript;
import com.cleanroommc.groovyscript.compat.inworldcrafting.Explosion;
import com.cleanroommc.groovyscript.compat.mods.jei.BaseCategory;
import mezz.jei.api.IGuiHelper;
import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.gui.IRecipeLayout;
import mezz.jei.api.ingredients.IIngredients;
import mezz.jei.api.ingredients.VanillaTypes;
import mezz.jei.api.recipe.IRecipeWrapper;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.resources.I18n;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ExplosionRecipeCategory extends BaseCategory<ExplosionRecipeCategory.RecipeWrapper> {

public static final String UID = GroovyScript.ID + ":explosion";

private final IDrawable icon;

public ExplosionRecipeCategory(IGuiHelper guiHelper) {
super(guiHelper, UID, 176, 56);
this.icon = guiHelper.createDrawableIngredient(new ItemStack(Blocks.TNT));
}

@Override
public void setRecipe(@NotNull IRecipeLayout recipeLayout, @NotNull RecipeWrapper recipeWrapper, @NotNull IIngredients ingredients) {
addItemSlot(recipeLayout, 0, true, 53, 25);
addItemSlot(recipeLayout, 1, false, 105, 25);

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

@Override
public void drawExtras(@NotNull Minecraft minecraft) {
minecraft.fontRenderer.drawSplitString(I18n.format("groovyscript.recipe.explosion"), 4, 4, 168, 0x404040);
GlStateManager.color(1f, 1f, 1f, 1f);
rightArrow.draw(minecraft, 76, 26);
float tntScale = 0.5f;
GlStateManager.pushMatrix();
GlStateManager.translate(80, 26, 0);
GlStateManager.translate(8, 8, 0);
GlStateManager.scale(tntScale, tntScale, 1);
GlStateManager.translate(-8, -8, 0);
this.icon.draw(minecraft);
GlStateManager.popMatrix();
}

@Nullable
@Override
public IDrawable getIcon() {
return icon;
}

public static class RecipeWrapper implements IRecipeWrapper {

private final Explosion.Recipe recipe;

public RecipeWrapper(Explosion.Recipe recipe) {
this.recipe = recipe;
}

@Override
public void getIngredients(IIngredients ingredients) {
ingredients.setInputLists(VanillaTypes.ITEM, Collections.singletonList(Arrays.asList(this.recipe.getInput().getMatchingStacks())));
ingredients.setOutput(VanillaTypes.ITEM, this.recipe.getOutput());
}

@Override
public void drawInfo(@NotNull Minecraft minecraft, int recipeWidth, int recipeHeight, int mouseX, int mouseY) {
float chance = this.recipe.getChance();
if (chance < 1) {
String chanceS = FluidRecipeCategory.numberFormat.format(chance);
int w = minecraft.fontRenderer.getStringWidth(chanceS);
int x = 88 - w / 2, y = 44;
minecraft.fontRenderer.drawString(chanceS, x, y, 0x404040);
}
}

@Override
public @NotNull List<String> getTooltipStrings(int mouseX, int mouseY) {
if (this.recipe.getChance() < 1 && mouseX >= 74 && mouseX <= 72 + 28 && mouseY >= 25 && mouseY <= 54) {
return Collections.singletonList(I18n.format("groovyscript.recipe.chance_produce", FluidRecipeCategory.numberFormat.format(this.recipe.getChance())));
}
return Collections.emptyList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,22 @@

public class FluidRecipeCategory extends BaseCategory<FluidRecipeCategory.RecipeWrapper> {

public static final String UID = "fluid_recipe";
private static final NumberFormat numberFormat = NumberFormat.getPercentInstance();
public static final String UID = GroovyScript.ID + ":fluid_recipe";
public static final NumberFormat numberFormat = NumberFormat.getPercentInstance();

private static final int inputY = 23;
public static final int outputY = 52;
public static final int outputX = 107;
public static final int outputX = 105;

public final IDrawable rightArrow;
public final IDrawable downRightArrow;
public final IDrawable slot;
public final IDrawable icon;

public FluidRecipeCategory(IGuiHelper guiHelper) {
super(guiHelper, UID, 176, 73);
rightArrow = guiHelper.drawableBuilder(new ResourceLocation(GroovyScript.ID, "textures/jei/arrow_right.png"), 0, 0, 24, 15)
.setTextureSize(24, 15)
.build();

downRightArrow = guiHelper.drawableBuilder(new ResourceLocation(GroovyScript.ID, "textures/jei/arrow_down_right.png"), 0, 0, 24, 18)
.setTextureSize(24, 18)
.build();
slot = guiHelper.getSlotDrawable();/*guiHelper.drawableBuilder(new ResourceLocation(GroovyScript.ID, "textures/jei/slot.png"), 0, 0, 18, 18)
.setTextureSize(18, 18)
.build();*/
icon = guiHelper.createDrawableIngredient(new ItemStack(Items.WATER_BUCKET));
}

Expand All @@ -59,7 +52,7 @@ public void setRecipe(@NotNull IRecipeLayout recipeLayout, @NotNull RecipeWrappe
for (int i = 0; i < 9; i++) {
addItemSlot(recipeLayout, i, true, 7 + 18 * i, inputY);
}
addFluidSlot(recipeLayout, 10, true, 55, outputY);
addFluidSlot(recipeLayout, 10, true, 53, outputY);
if (recipeWrapper.recipe.getClass() == FluidToFluid.Recipe.class) {
addFluidSlot(recipeLayout, 11, false, outputX, outputY);
} else {
Expand All @@ -68,8 +61,8 @@ public void setRecipe(@NotNull IRecipeLayout recipeLayout, @NotNull RecipeWrappe
recipeLayout.getItemStacks().set(ingredients);
recipeLayout.getFluidStacks().set(ingredients);

setBackgrounds(recipeLayout.getItemStacks(), this.slot);
setBackgrounds(recipeLayout.getFluidStacks(), this.slot);
setBackgrounds(recipeLayout.getItemStacks(), slot);
setBackgrounds(recipeLayout.getFluidStacks(), slot);

recipeLayout.getItemStacks().addTooltipCallback((slotIndex, input, ingredient, tooltip) -> {
if (slotIndex < 9) {
Expand Down Expand Up @@ -98,8 +91,8 @@ public static void getIngredients(FluidRecipe recipe, IIngredients ingredients)
public void drawExtras(@NotNull Minecraft minecraft) {
drawLine(minecraft, "groovyscript.recipe.fluid_recipe", 4, 4, 0x404040);
GlStateManager.color(1f, 1f, 1f, 1f);
this.downRightArrow.draw(minecraft, 25, outputY - 3);
this.rightArrow.draw(minecraft, 78, outputY + 1);
this.downRightArrow.draw(minecraft, 23, outputY - 3);
rightArrow.draw(minecraft, 76, outputY + 1);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,30 @@
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.recipe.IRecipeCategory;
import mezz.jei.api.recipe.IRecipeWrapper;
import mezz.jei.gui.ingredients.GuiIngredient;
import net.minecraft.client.resources.I18n;
import net.minecraft.util.ResourceLocation;
import org.jetbrains.annotations.NotNull;

public abstract class BaseCategory<T extends IRecipeWrapper> implements IRecipeCategory<T> {

private final String uid;
private final IDrawable background;
protected IRecipeLayout recipeLayout;

public static IDrawable rightArrow;
public static IDrawable slot;

public BaseCategory(IGuiHelper guiHelper, String uid, int width, int height) {
this.uid = uid;
this.background = guiHelper.createBlankDrawable(width, height);
if (slot == null) {
rightArrow = guiHelper.drawableBuilder(new ResourceLocation(GroovyScript.ID, "textures/jei/arrow_right.png"), 0, 0, 24, 15)
.setTextureSize(24, 15)
.build();
slot = guiHelper.getSlotDrawable();
}
}

@Override
Expand All @@ -44,11 +52,6 @@ public BaseCategory(IGuiHelper guiHelper, String uid, int width, int height) {
return this.background;
}

@Override
public void setRecipe(@NotNull IRecipeLayout recipeLayout, @NotNull T recipeWrapper, @NotNull IIngredients ingredients) {
this.recipeLayout = recipeLayout;
}

public static void addItemSlot(IRecipeLayout recipeLayout, int index, boolean input, int x, int y) {
recipeLayout.getItemStacks().init(index, input, x, y);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import com.cleanroommc.groovyscript.command.GSCommand;
import com.cleanroommc.groovyscript.command.SimpleCommand;
import com.cleanroommc.groovyscript.compat.inworldcrafting.FluidRecipe;
import com.cleanroommc.groovyscript.compat.inworldcrafting.jei.ExplosionRecipeCategory;
import com.cleanroommc.groovyscript.compat.inworldcrafting.jei.FluidRecipeCategory;
import com.cleanroommc.groovyscript.compat.vanilla.ShapedCraftingRecipe;
import com.cleanroommc.groovyscript.compat.vanilla.ShapelessCraftingRecipe;
import com.cleanroommc.groovyscript.compat.vanilla.VanillaModule;
import mezz.jei.Internal;
import mezz.jei.api.*;
import mezz.jei.api.ingredients.IIngredientHelper;
Expand Down Expand Up @@ -62,7 +64,9 @@ public void registerCategories(IRecipeCategoryRegistration registry) {
IngredientRegistry ingredientRegistry = Internal.getIngredientRegistry();
fluidRenderer = ingredientRegistry.getIngredientRenderer(VanillaTypes.FLUID);

registry.addRecipeCategories(new FluidRecipeCategory(registry.getJeiHelpers().getGuiHelper()));
IGuiHelper guiHelper = registry.getJeiHelpers().getGuiHelper();
registry.addRecipeCategories(new FluidRecipeCategory(guiHelper));
registry.addRecipeCategories(new ExplosionRecipeCategory(guiHelper));
}

@Override
Expand All @@ -82,6 +86,7 @@ public void register(IModRegistry registry) {
recipeWrappers.add(new FluidRecipeCategory.RecipeWrapper(fluidRecipe));
});
registry.addRecipes(recipeWrappers, FluidRecipeCategory.UID);
registry.addRecipes(VanillaModule.inWorldCrafting.explosion.getRecipeWrappers(), ExplosionRecipeCategory.UID);
}

@Override
Expand Down
7 changes: 5 additions & 2 deletions src/main/resources/assets/groovyscript/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ groovyscript.command.copy.copied_end=] to the clipboard
key.categories.groovyscript=GroovyScript
key.groovyscript.reload=Reload Scripts

groovyscript.jei.category.fluid_recipe.name=In world fluid recipes
groovyscript.jei.category.groovyscript:fluid_recipe.name=In world fluid recipes
groovyscript.jei.category.groovyscript:explosion.name=Explosion recipes

groovyscript.recipe.fluid_recipe=Drop items into fluid to transform fluid
groovyscript.recipe.chance=§2%s§7 chance to get consumed
groovyscript.recipe.explosion=Explode item with an explosion to transform item
groovyscript.recipe.chance_produce=§2%s§7 chance to produce output
groovyscript.recipe.chance_consume=§2%s§7 chance to get consumed

0 comments on commit bef24e3

Please sign in to comment.