Added grinder and inscriber recipes. Improved JEI integration.

This commit is contained in:
Sebastian Hartte
2020-06-11 00:40:14 +02:00
parent 8ccc114f92
commit ecbcc56997
45 changed files with 614 additions and 1318 deletions
@@ -20,18 +20,21 @@ public class GrinderRecipe implements IRecipe<IInventory> {
private final ResourceLocation id;
private final String group;
private final Ingredient ingredient;
private final int ingredientCount;
private final ItemStack result;
private final List<GrinderOptionalResult> optionalResults;
private final int turns;
public GrinderRecipe(ResourceLocation id, String group, Ingredient ingredient, ItemStack result, int turns, List<GrinderOptionalResult> optionalResults) {
public GrinderRecipe(ResourceLocation id, String group, Ingredient ingredient, int ingredientCount, ItemStack result, int turns, List<GrinderOptionalResult> optionalResults) {
this.id = id;
this.group = group;
this.ingredient = ingredient;
this.ingredientCount = ingredientCount;
this.result = result;
this.turns = turns;
this.optionalResults = ImmutableList.copyOf(optionalResults);
}
@Override
public boolean matches(IInventory inv, World worldIn) {
return this.ingredient.test(inv.getStackInSlot(0));
@@ -72,6 +75,14 @@ public class GrinderRecipe implements IRecipe<IInventory> {
return ingredient;
}
public int getTurns() {
return turns;
}
public int getIngredientCount() {
return ingredientCount;
}
@Override
public NonNullList<Ingredient> getIngredients() {
NonNullList<Ingredient> nonnulllist = NonNullList.create();
@@ -1,5 +1,6 @@
package appeng.recipes.handlers;
import appeng.core.AEConfig;
import appeng.core.AppEng;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
@@ -32,7 +33,12 @@ public class GrinderRecipeSerializer extends ForgeRegistryEntry<IRecipeSerialize
@Override
public GrinderRecipe read(ResourceLocation recipeId, JsonObject json) {
String group = JSONUtils.getString(json, "group", "");
Ingredient ingredient = Ingredient.deserialize(JSONUtils.getJsonObject(json, "input"));
JsonObject inputObj = JSONUtils.getJsonObject(json, "input");
Ingredient ingredient = Ingredient.deserialize(inputObj);
int ingredientCount = 1;
if (inputObj.has("count")) {
ingredientCount = inputObj.get("count").getAsInt();
}
JsonObject result = JSONUtils.getJsonObject(json, "result");
ItemStack primaryResult = ShapedRecipe.deserializeItem(JSONUtils.getJsonObject(result, "primary"));
@@ -45,25 +51,28 @@ public class GrinderRecipeSerializer extends ForgeRegistryEntry<IRecipeSerialize
throw new IllegalStateException("Entry in optional result list should be an object.");
}
ItemStack optionalResultItem = ShapedRecipe.deserializeItem(optionalResultJson.getAsJsonObject());
float optionalChance = JSONUtils.getFloat(optionalResultJson.getAsJsonObject(), "chance", 1.0f);
float optionalChance = JSONUtils.getFloat(optionalResultJson.getAsJsonObject(), "percentageChance",
AEConfig.instance().getOreDoublePercentage()) / 100.0f;
optionalResults.add(new GrinderOptionalResult(optionalChance, optionalResultItem));
}
}
int turns = JSONUtils.getInt(json, "turns", 8);
return new GrinderRecipe(recipeId, group, ingredient, primaryResult, turns, optionalResults);
return new GrinderRecipe(recipeId, group, ingredient, ingredientCount, primaryResult, turns, optionalResults);
}
@Nullable
@Override
public GrinderRecipe read(ResourceLocation recipeId, PacketBuffer buffer) {
return null;
// FIXME NOT YET IMPLEMENTED
throw new IllegalStateException();
}
@Override
public void write(PacketBuffer buffer, GrinderRecipe recipe) {
// FIXME NOT YET IMPLEMENTED
throw new IllegalStateException();
}
}
@@ -0,0 +1,41 @@
package appeng.recipes.handlers;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.world.World;
import javax.annotation.Nullable;
public final class GrinderRecipes {
private GrinderRecipes() {
}
/**
* Search all available Grinder recipes for a recipe matching the given input or null;
*/
@Nullable
public static GrinderRecipe findForInput(World world, ItemStack input) {
for (IRecipe<IInventory> recipe : world.getRecipeManager().getRecipes(GrinderRecipe.TYPE).values()) {
GrinderRecipe grinderRecipe = (GrinderRecipe) recipe;
if (grinderRecipe.getIngredient().test(input) && input.getCount() >= grinderRecipe.getIngredientCount()) {
return grinderRecipe;
}
}
return null;
}
/**
* Checks if the given item stack is an ingredient in any grinder recipe, disregarding its current size.
*/
public static boolean isValidIngredient(World world, ItemStack stack) {
for (IRecipe<IInventory> recipe : world.getRecipeManager().getRecipes(GrinderRecipe.TYPE).values()) {
GrinderRecipe grinderRecipe = (GrinderRecipe) recipe;
if (grinderRecipe.getIngredient().test(stack)) {
return true;
}
}
return false;
}
}
@@ -1,56 +0,0 @@
package appeng.recipes.handlers;
import com.google.gson.JsonObject;
public class InscriberHandler
{
// @Override
// public void register( JsonObject json, JsonContext ctx )
// {
// ItemStack result = PartRecipeFactory.getResult( json, ctx );
// String mode = JSONUtils.getString( json, "mode" );
//
// JsonObject ingredients = JSONUtils.getJsonObject( json, "ingredients" );
//
// List<ItemStack> middle = Arrays.asList( CraftingHelper.getIngredient( ingredients.get( "middle" ), ctx ).getMatchingStacks() );
// ItemStack[] top = new ItemStack[] { null };
// if( ingredients.has( "top" ) )
// {
// top = CraftingHelper.getIngredient( JSONUtils.getJsonObject( ingredients, "top" ), ctx ).getMatchingStacks();
// }
//
// ItemStack[] bottom = new ItemStack[] { null };
// if( ingredients.has( "bottom" ) )
// {
// bottom = CraftingHelper.getIngredient( JSONUtils.getJsonObject( ingredients, "bottom" ), ctx ).getMatchingStacks();
// }
//
// final IInscriberRegistry reg = AEApi.instance().registries().inscriber();
// for( int i = 0; i < top.length; ++i )
// {
// for( int j = 0; j < bottom.length; ++j )
// {
// final IInscriberRecipeBuilder builder = reg.builder();
// builder.withOutput( result );
// builder.withProcessType( "press".equals( mode ) ? InscriberProcessType.PRESS : InscriberProcessType.INSCRIBE );
// builder.withInputs( middle );
//
// if( top[i] != null )
// {
// builder.withTopOptional( top[i] );
// }
// if( bottom[j] != null )
// {
// builder.withBottomOptional( bottom[j] );
// }
//
// reg.addRecipe( builder.build() );
// }
// }
// }
//
}
@@ -1,17 +1,17 @@
package appeng.recipes.handlers;
import appeng.api.features.InscriberProcessType;
import com.google.common.collect.ImmutableList;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import java.util.List;
import javax.annotation.Nullable;
public class InscriberRecipe implements IRecipe<IInventory> {
@@ -20,20 +20,20 @@ public class InscriberRecipe implements IRecipe<IInventory> {
private final ResourceLocation id;
private final String group;
private List<Ingredient> inputs;
private Ingredient middleInput;
private Ingredient topOptional;
private Ingredient bottomOptional;
private ItemStack output;
private ItemStack topOptional;
private ItemStack bottomOptional;
private InscriberProcessType type;
private InscriberProcessType processType;
public InscriberRecipe(ResourceLocation id, String group, List<Ingredient> inputs, ItemStack output, ItemStack topOptional, ItemStack bottomOptional, InscriberProcessType type) {
public InscriberRecipe(ResourceLocation id, String group, Ingredient middleInput, ItemStack output, Ingredient topOptional, Ingredient bottomOptional, InscriberProcessType processType) {
this.id = id;
this.group = group;
this.inputs = inputs;
this.middleInput = middleInput;
this.output = output;
this.topOptional = topOptional;
this.bottomOptional = bottomOptional;
this.type = type;
this.processType = processType;
}
@Override
@@ -43,27 +43,27 @@ public class InscriberRecipe implements IRecipe<IInventory> {
@Override
public ItemStack getCraftingResult(IInventory inv) {
return null;
return this.output.copy();
}
@Override
public boolean canFit(int width, int height) {
return false;
return true;
}
@Override
public ItemStack getRecipeOutput() {
return null;
return output;
}
@Override
public ResourceLocation getId() {
return null;
return id;
}
@Override
public IRecipeSerializer<?> getSerializer() {
return null;
return GrinderRecipeSerializer.INSTANCE;
}
@Override
@@ -71,20 +71,33 @@ public class InscriberRecipe implements IRecipe<IInventory> {
return TYPE;
}
public List<Ingredient> getInputs() {
return inputs;
@Override
public NonNullList<Ingredient> getIngredients() {
NonNullList<Ingredient> nonnulllist = NonNullList.create();
nonnulllist.add(this.topOptional);
nonnulllist.add(this.middleInput);
nonnulllist.add(this.bottomOptional);
return nonnulllist;
}
public Ingredient getMiddleInput() {
return middleInput;
}
public ItemStack getOutput() {
return output;
}
public ItemStack getTopOptional() {
public Ingredient getTopOptional() {
return topOptional;
}
public ItemStack getBottomOptional() {
public Ingredient getBottomOptional() {
return bottomOptional;
}
public InscriberProcessType getProcessType() {
return processType;
}
}
@@ -0,0 +1,79 @@
package appeng.recipes.handlers;
import appeng.api.features.InscriberProcessType;
import appeng.core.AppEng;
import com.google.gson.JsonObject;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.item.crafting.ShapedRecipe;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.JSONUtils;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.registries.ForgeRegistryEntry;
import javax.annotation.Nullable;
public class InscriberRecipeSerializer extends ForgeRegistryEntry<IRecipeSerializer<?>> implements IRecipeSerializer<InscriberRecipe> {
public static final InscriberRecipeSerializer INSTANCE = new InscriberRecipeSerializer();
static {
INSTANCE.setRegistryName(AppEng.MOD_ID, "inscriber");
}
private InscriberRecipeSerializer() {
}
private static InscriberProcessType getMode(JsonObject json) {
String mode = JSONUtils.getString( json, "mode", "inscribe" );
switch (mode) {
case "inscribe":
return InscriberProcessType.INSCRIBE;
case "press":
return InscriberProcessType.PRESS;
default:
throw new IllegalStateException("Unknown mode for inscriber recipe: " + mode);
}
}
@Override
public InscriberRecipe read(ResourceLocation recipeId, JsonObject json) {
InscriberProcessType mode = getMode(json);
String group = JSONUtils.getString(json, "group", "");
ItemStack result = ShapedRecipe.deserializeItem(JSONUtils.getJsonObject(json, "result"));
// Deserialize the three parts of the input
JsonObject ingredients = JSONUtils.getJsonObject( json, "ingredients" );
Ingredient middle = Ingredient.deserialize( ingredients.get( "middle" ) );
Ingredient top = Ingredient.EMPTY;
if( ingredients.has( "top" ) )
{
top = Ingredient.deserialize( ingredients.get( "top" ) );
}
Ingredient bottom = Ingredient.EMPTY;
if( ingredients.has( "bottom" ) )
{
bottom = Ingredient.deserialize( ingredients.get( "bottom" ) );
}
return new InscriberRecipe(recipeId, group, middle, result, top, bottom, mode);
}
@Nullable
@Override
public InscriberRecipe read(ResourceLocation recipeId, PacketBuffer buffer) {
// FIXME NOT YET IMPLEMENTED
throw new IllegalStateException();
}
@Override
public void write(PacketBuffer buffer, InscriberRecipe recipe) {
// FIXME NOT YET IMPLEMENTED
throw new IllegalStateException();
}
}