Added CrusherRecipe and Factory

Signed-off-by: canitzp <canitzp@gmail.com>
This commit is contained in:
canitzp 2020-10-06 09:04:43 +02:00
parent d0363ed81c
commit 35ff26221a
No known key found for this signature in database
GPG key ID: A7641ECE126C50F2
3 changed files with 163 additions and 1 deletions

View file

@ -0,0 +1,66 @@
package de.ellpeck.actuallyadditions.common.recipes;
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.util.ResourceLocation;
import javax.annotation.Nonnull;
public class CrusherRecipe implements IDummyRecipe {
public static final IRecipeType<CrusherRecipe> CRUSHER_RECIPE_TYPE = IRecipeType.register("actuallyadditions:crusher");
@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 CrusherRecipeFactory.INSTANCE;
}
@Nonnull
@Override
public IRecipeType<?> getType(){
return CRUSHER_RECIPE_TYPE;
}
}

View file

@ -0,0 +1,95 @@
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

@ -3,6 +3,7 @@
"atomic_reconstructor": "de.ellpeck.actuallyadditions.common.recipes.AtomicReconstructorRecipeFactory",
"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"
"compost": "de.ellpeck.actuallyadditions.common.recipes.CompostRecipeFactory",
"crusher": "de.eppleck.actuallyadditions.common.recipes.CrusherRecipeFactory"
}
}