ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/common/recipes/CrusherRecipe.java
canitzp 395b6d0491
Recipe Factory changes
Signed-off-by: canitzp <canitzp@gmail.com>
2020-10-06 12:10:09 +02:00

117 lines
4 KiB
Java

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> RECIPE_TYPE = IRecipeType.register("actuallyadditions:crusher");
public static final CrusherRecipeFactory FACTORY = IRecipeSerializer.register("actuallyadditions:crusher", new CrusherRecipeFactory());
@Nonnull private final ResourceLocation recipeId;
@Nonnull private final Ingredient input;
@Nonnull private final ItemStack output;
@Nonnull private final ItemStack secondaryOutput;
private final int outputChance;
public CrusherRecipe(@Nonnull ResourceLocation recipeId, @Nonnull Ingredient input, @Nonnull ItemStack output, @Nonnull ItemStack secondaryOutput, int outputChance){
this.recipeId = recipeId;
this.input = input;
this.output = output;
this.secondaryOutput = secondaryOutput;
this.outputChance = outputChance;
}
@Nonnull
public Ingredient getInput(){
return input;
}
@Nonnull
public ItemStack getOutput(){
return output;
}
@Nonnull
public ItemStack getSecondaryOutput(){
return secondaryOutput;
}
public int getOutputChance(){
return outputChance;
}
@Nonnull
@Override
public ResourceLocation getId(){
return this.recipeId;
}
@Nonnull
@Override
public IRecipeSerializer<?> getSerializer(){
return FACTORY;
}
@Nonnull
@Override
public IRecipeType<?> getType(){
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());
}
}
}