Compost Recipe Factory

This commit is contained in:
canitzp 2020-09-25 09:00:03 +02:00
parent 2c57a7641a
commit 8b22fcfa6b
3 changed files with 164 additions and 3 deletions

View file

@ -0,0 +1,59 @@
package de.ellpeck.actuallyadditions.common.recipes;
import net.minecraft.block.BlockState;
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;
public class CompostRecipe implements IDummyRecipe {
public static final IRecipeType<CompostRecipe> COMPOST_RECIPE_TYPE = IRecipeType.register("actuallyadditions:compost");
private final ResourceLocation recipeId;
private final Ingredient input;
private final ItemStack output;
private final BlockState inputDisplay;
private final BlockState outputDisplay;
public CompostRecipe(ResourceLocation recipeId, Ingredient input, ItemStack output, BlockState inputDisplay, BlockState outputDisplay){
this.recipeId = recipeId;
this.input = input;
this.output = output;
this.inputDisplay = inputDisplay;
this.outputDisplay = outputDisplay;
}
public Ingredient getInput(){
return this.input;
}
public ItemStack getOutput(){
return this.output;
}
public BlockState getInputDisplay(){
return this.inputDisplay;
}
public BlockState getOutputDisplay(){
return this.outputDisplay;
}
@Override
public ResourceLocation getId(){
return this.recipeId;
}
@Override
public IRecipeSerializer<?> getSerializer(){
return CompostRecipeFactory.INSTANCE;
}
@Override
public IRecipeType<?> getType(){
return COMPOST_RECIPE_TYPE;
}
}

View file

@ -0,0 +1,101 @@
package de.ellpeck.actuallyadditions.common.recipes;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.mojang.datafixers.Dynamic;
import com.mojang.datafixers.types.JsonOps;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
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.nbt.*;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.registries.ForgeRegistryEntry;
import javax.annotation.Nullable;
public class CompostRecipeFactory extends ForgeRegistryEntry<IRecipeSerializer<?>> implements IRecipeSerializer<CompostRecipe> {
public static final CompostRecipeFactory INSTANCE = IRecipeSerializer.register("actuallyadditions:compost", new CompostRecipeFactory());
@Override
public CompostRecipe read(ResourceLocation recipeId, JsonObject json){
Ingredient input;
ItemStack output;
BlockState inputDisplay;
BlockState outputDisplay;
if(json.has("input")){
input = Ingredient.deserialize(json.get("input"));
} else {
throw new JsonParseException("CompostRecipe needs a input ingredient json element!");
}
if(json.has("output")){
JsonElement outputJsonElement = json.get("output");
if(outputJsonElement.isJsonObject()){
output = ShapedRecipe.deserializeItem(outputJsonElement.getAsJsonObject());
} else {
throw new JsonParseException("CompostRecipe output element is not a json object!");
}
} else {
throw new JsonParseException("CompostRecipe needs a output itemstack json object!");
}
if(json.has("display")){
JsonElement displayJsonElement = json.get("display");
if(displayJsonElement.isJsonObject()){
JsonObject displayJsonObject = displayJsonElement.getAsJsonObject();
if(displayJsonObject.has("input")){
inputDisplay = BlockState.deserialize(new Dynamic<>(JsonOps.INSTANCE, displayJsonObject.get("input")));
} else {
throw new JsonParseException("CompostRecipe display needs an input json element!");
}
if(displayJsonObject.has("output")){
outputDisplay = BlockState.deserialize(new Dynamic<>(JsonOps.INSTANCE, displayJsonObject.get("output")));
} else {
throw new JsonParseException("CompostRecipe display needs an output json element!");
}
} else {
throw new JsonParseException("CompostRecipe display has to be a json object!");
}
} else {
// no exception but default values
inputDisplay = Blocks.DIRT.getDefaultState();
outputDisplay = Blocks.COARSE_DIRT.getDefaultState();
}
return new CompostRecipe(recipeId, input, output, inputDisplay, outputDisplay);
}
@Nullable
@Override
public CompostRecipe read(ResourceLocation recipeId, PacketBuffer buffer){
Ingredient input = Ingredient.read(buffer);
ItemStack output = buffer.readItemStack();
CompoundNBT inputNBT = buffer.readCompoundTag();
CompoundNBT outputNBT = buffer.readCompoundTag();
BlockState inputDisplay = BlockState.deserialize(new Dynamic<>(NBTDynamicOps.INSTANCE, inputNBT));
BlockState outputDisplay = BlockState.deserialize(new Dynamic<>(NBTDynamicOps.INSTANCE, outputNBT));
return new CompostRecipe(recipeId, input, output, inputDisplay, outputDisplay);
}
@Override
public void write(PacketBuffer buffer, CompostRecipe recipe){
recipe.getInput().write(buffer);
buffer.writeItemStack(recipe.getOutput());
INBT inputNBT = BlockState.serialize(NBTDynamicOps.INSTANCE, recipe.getInputDisplay()).getValue();
buffer.writeCompoundTag((CompoundNBT) inputNBT); // if it isn't a compound than something real big is wrong and a crash should be the best way to handle it
INBT outputNBT = BlockState.serialize(NBTDynamicOps.INSTANCE, recipe.getOutputDisplay()).getValue();
buffer.writeCompoundTag((CompoundNBT) outputNBT);
}
}

View file

@ -1,7 +1,8 @@
{
"recipes": {
"atomic_reconstructor": "de.ellpeck.actuallyadditions.recipes.AtomicReconstructorRecipeFactory",
"ball_of_fur": "de.ellpeck.actuallyadditions.recipes.BallOfFurRecipeFactory",
"coffee_machine_ingredient": "de.ellpeck.actuallyadditions.recipes.CoffeeMachineIngredientFactory"
"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"
}
}