Recipe Factory changes

Signed-off-by: canitzp <canitzp@gmail.com>
This commit is contained in:
canitzp 2020-10-06 12:10:09 +02:00
parent 35ff26221a
commit 395b6d0491
No known key found for this signature in database
GPG Key ID: A7641ECE126C50F2
4 changed files with 136 additions and 99 deletions

View File

@ -1,16 +1,21 @@
package de.ellpeck.actuallyadditions.common.recipes;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.ResourceLocation;
import javax.annotation.Nonnull;
public class CrusherRecipe implements IDummyRecipe {
public static final IRecipeType<CrusherRecipe> CRUSHER_RECIPE_TYPE = IRecipeType.register("actuallyadditions:crusher");
public static final IRecipeType<CrusherRecipe> RECIPE_TYPE = IRecipeType.register("actuallyadditions:crusher");
public static final CrusherRecipeFactory FACTORY = IRecipeSerializer.register("actuallyadditions:crusher", new CrusherRecipeFactory());
@Nonnull private final ResourceLocation recipeId;
@ -55,12 +60,57 @@ public class CrusherRecipe implements IDummyRecipe {
@Nonnull
@Override
public IRecipeSerializer<?> getSerializer(){
return CrusherRecipeFactory.INSTANCE;
return FACTORY;
}
@Nonnull
@Override
public IRecipeType<?> getType(){
return CRUSHER_RECIPE_TYPE;
return RECIPE_TYPE;
}
static class CrusherRecipeFactory extends RecipeFactoryBase<CrusherRecipe> {
@Nonnull
@Override
public CrusherRecipe read(@Nonnull ResourceLocation recipeId, @Nonnull JsonObject json){
Ingredient input = this.readIngredient(json, "input");
ItemStack firstOutput = this.readItemStack(json, "output");
ItemStack secondOutput = ItemStack.EMPTY;
int secondaryOutputChance = 0;
if(json.has("secondary")){ // Optional
JsonElement secondaryElement = json.get("secondary");
if(secondaryElement.isJsonObject()){
JsonObject secondary = secondaryElement.getAsJsonObject();
secondOutput = this.readItemStack(secondary, "output", ItemStack.EMPTY);
secondaryOutputChance = this.readInt(secondary, "chance", 0);
} else {
throw new JsonSyntaxException("Secondary is not valid!");
}
}
return new CrusherRecipe(recipeId, input, firstOutput, secondOutput, secondaryOutputChance);
}
@Nonnull
@Override
public CrusherRecipe read(@Nonnull ResourceLocation recipeId, @Nonnull PacketBuffer buffer){
Ingredient input = Ingredient.read(buffer);
ItemStack output = buffer.readItemStack();
ItemStack secondaryOutput = buffer.readItemStack();
int chance = buffer.readVarInt();
return new CrusherRecipe(recipeId, input, output, secondaryOutput, chance);
}
@Override
public void write(@Nonnull PacketBuffer buffer, @Nonnull CrusherRecipe recipe){
recipe.getInput().write(buffer);
buffer.writeItemStack(recipe.getOutput());
buffer.writeItemStack(recipe.getSecondaryOutput());
buffer.writeVarInt(recipe.getOutputChance());
}
}
}

View File

@ -1,95 +0,0 @@
package de.ellpeck.actuallyadditions.common.recipes;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
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.ResourceLocation;
import net.minecraftforge.registries.ForgeRegistryEntry;
import javax.annotation.Nonnull;
public class CrusherRecipeFactory extends ForgeRegistryEntry<IRecipeSerializer<?>> implements IRecipeSerializer<CrusherRecipe> {
public static final CrusherRecipeFactory INSTANCE = IRecipeSerializer.register("actuallyadditions:crusher", new CrusherRecipeFactory());
@Nonnull
@Override
public CrusherRecipe read(@Nonnull ResourceLocation recipeId, @Nonnull JsonObject json){
Ingredient input;
ItemStack firstOutput;
ItemStack secondOutput = ItemStack.EMPTY;
int secondaryOutputChance = 0;
if(json.has("input")){
input = Ingredient.deserialize(json.get("input"));
} else {
throw new JsonSyntaxException("Input is not given for the recipe!");
}
if(json.has("output")){
JsonElement outputElement = json.get("output");
if(outputElement.isJsonObject()){
firstOutput = ShapedRecipe.deserializeItem(outputElement.getAsJsonObject());
} else {
throw new JsonSyntaxException("Output is not valid!");
}
} else {
throw new JsonSyntaxException("Output is not given for the recipe!");
}
if(json.has("secondary")){ // Optional
JsonElement secondaryElement = json.get("secondary");
if(secondaryElement.isJsonObject()){
JsonObject secondary = secondaryElement.getAsJsonObject();
if(secondary.has("output")){
JsonElement outputElement = json.get("output");
if(outputElement.isJsonObject()){
secondOutput = ShapedRecipe.deserializeItem(outputElement.getAsJsonObject());
} else {
throw new JsonSyntaxException("Secondary output is not valid!");
}
} else {
throw new JsonSyntaxException("Secondary output is not given for the recipe!");
}
if(secondary.has("chance")){
JsonElement chanceElement = secondary.get("chance");
if(chanceElement.isJsonPrimitive()){
secondaryOutputChance = chanceElement.getAsInt();
} else {
throw new JsonSyntaxException("Secondary chance is not valid!");
}
} else {
throw new JsonSyntaxException("Secondary chance is not given for the recipe!");
}
} else {
throw new JsonSyntaxException("Secondary is not valid!");
}
}
return new CrusherRecipe(recipeId, input, firstOutput, secondOutput, secondaryOutputChance);
}
@Nonnull
@Override
public CrusherRecipe read(@Nonnull ResourceLocation recipeId, @Nonnull PacketBuffer buffer){
Ingredient input = Ingredient.read(buffer);
ItemStack output = buffer.readItemStack();
ItemStack secondaryOutput = buffer.readItemStack();
int chance = buffer.readVarInt();
return new CrusherRecipe(recipeId, input, output, secondaryOutput, chance);
}
@Override
public void write(@Nonnull PacketBuffer buffer, @Nonnull CrusherRecipe recipe){
recipe.getInput().write(buffer);
buffer.writeItemStack(recipe.getOutput());
buffer.writeItemStack(recipe.getSecondaryOutput());
buffer.writeVarInt(recipe.getOutputChance());
}
}

View File

@ -0,0 +1,82 @@
package de.ellpeck.actuallyadditions.common.recipes;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
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.util.ResourceLocation;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.ForgeRegistryEntry;
public abstract class RecipeFactoryBase<T extends IDummyRecipe> extends ForgeRegistryEntry<IRecipeSerializer<?>> implements IRecipeSerializer<T> {
protected Ingredient readIngredient(JsonObject json, String key){
if(json.has(key)){
return Ingredient.deserialize(json.get(key));
} else {
throw new JsonSyntaxException(String.format("Json element does not contain any element with the key: '%s'", key));
}
}
protected Ingredient readIngredient(JsonObject json, String key, Ingredient alternative){
try{
return this.readIngredient(json, key);
} catch(JsonSyntaxException e){
return alternative;
}
}
protected ItemStack readItemStack(JsonObject json, String key){
if(json.has(key)){
if(json.get(key).isJsonObject()){
return ShapedRecipe.deserializeItem(json.get(key).getAsJsonObject());
} else if(json.get(key).isJsonPrimitive()){
ResourceLocation itemKey = new ResourceLocation(json.get(key).getAsString());
if(ForgeRegistries.ITEMS.containsKey(itemKey)){
return new ItemStack(ForgeRegistries.ITEMS.getValue(itemKey));
} else {
throw new JsonSyntaxException(String.format("Item with the key: '%s' is not registered!", key));
}
} else {
throw new JsonSyntaxException(String.format("Json element with the key: '%s' is neither a object nor a string!", key));
}
} else {
throw new JsonSyntaxException(String.format("Json element does not contain any element with the key: '%s'!", key));
}
}
protected ItemStack readItemStack(JsonObject json, String key, ItemStack alternative){
try{
return this.readItemStack(json, key);
} catch(JsonSyntaxException e){
return alternative;
}
}
protected int readInt(JsonObject json, String key){
if(json.has(key)){
if(json.get(key).isJsonPrimitive()){
try{
return json.get(key).getAsJsonPrimitive().getAsInt();
} catch(NumberFormatException e){
throw new JsonSyntaxException(String.format("Json element '%s' is not a valid integer!", key), e);
}
} else {
throw new JsonSyntaxException(String.format("Json element '%s' is not a valid integer!", key));
}
} else {
throw new JsonSyntaxException(String.format("Json element '%s' does not exist!", key));
}
}
protected int readInt(JsonObject json, String key, int alternative){
try{
return this.readInt(json, key);
} catch(Exception e){
return alternative;
}
}
}

View File

@ -4,6 +4,6 @@
"ball_of_fur": "de.ellpeck.actuallyadditions.common.recipes.BallOfFurRecipeFactory",
"coffee_machine_ingredient": "de.ellpeck.actuallyadditions.common.recipes.CoffeeMachineIngredientFactory",
"compost": "de.ellpeck.actuallyadditions.common.recipes.CompostRecipeFactory",
"crusher": "de.eppleck.actuallyadditions.common.recipes.CrusherRecipeFactory"
"crusher": "de.ellpeck.actuallyadditions.common.recipes.CrusherRecipe$CrusherRecipeFactory"
}
}