Start on recipe serde rewrite

This commit is contained in:
modmuss50 2021-12-19 13:44:46 +00:00
parent 5eab27bead
commit 47eea962a0
17 changed files with 411 additions and 229 deletions

View file

@ -38,47 +38,25 @@ import net.minecraft.util.collection.DefaultedList;
import net.minecraft.world.World;
import reborncore.common.crafting.RebornRecipe;
import reborncore.common.crafting.RebornRecipeType;
import reborncore.common.crafting.ingredient.DummyIngredient;
import reborncore.common.crafting.ingredient.RebornIngredient;
import java.util.Collections;
import java.util.List;
public class RollingMachineRecipe extends RebornRecipe {
private ShapedRecipe shapedRecipe;
private final ShapedRecipe shapedRecipe;
private final JsonObject shapedRecipeJson;
public RollingMachineRecipe(RebornRecipeType<?> type, Identifier name) {
super(type, name);
this(type, name, List.of(new DummyIngredient()), Collections.emptyList(), Integer.MAX_VALUE, Integer.MAX_VALUE, null, null);
this.dummy = true;
}
public RollingMachineRecipe(RebornRecipeType<?> type, Identifier name, ShapedRecipe recipe) {
super(type, name);
this.shapedRecipe = recipe;
}
@Override
public void deserialize(JsonObject jsonObject) {
if (jsonObject.has("shaped")) {
JsonObject json = JsonHelper.getObject(jsonObject, "shaped");
shapedRecipe = RecipeSerializer.SHAPED.read(getId(), json);
} else {
//This will be handled by the PacketByteBuf deserialize
}
}
@Override
public void serialize(PacketByteBuf byteBuf) {
String s = shapedRecipe.getId().toString();
byteBuf.writeInt(s.length());
byteBuf.writeString(s);
RecipeSerializer.SHAPED.write(byteBuf, shapedRecipe);
}
@Override
public void deserialize(PacketByteBuf byteBuf) {
Identifier identifier = new Identifier(byteBuf.readString(byteBuf.readInt()));
shapedRecipe = RecipeSerializer.SHAPED.read(identifier, byteBuf);
public RollingMachineRecipe(RebornRecipeType<?> type, Identifier name, List<RebornIngredient> ingredients, List<ItemStack> outputs, int power, int time, ShapedRecipe shapedRecipe, JsonObject shapedRecipeJson) {
super(type, name, ingredients, outputs, power, time);
this.shapedRecipe = shapedRecipe;
this.shapedRecipeJson = shapedRecipeJson;
}
@Override
@ -119,4 +97,8 @@ public class RollingMachineRecipe extends RebornRecipe {
public ShapedRecipe getShapedRecipe() {
return shapedRecipe;
}
public JsonObject getShapedRecipeJson() {
return shapedRecipeJson;
}
}

View file

@ -0,0 +1,36 @@
package techreborn.api.recipe.recipes;
import com.google.gson.JsonObject;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.ShapedRecipe;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import reborncore.common.crafting.RebornRecipeType;
import reborncore.common.crafting.ingredient.RebornIngredient;
import reborncore.common.crafting.serde.RebornRecipeSerde;
import java.util.List;
public class RollingMachineRecipeSerde extends RebornRecipeSerde<RollingMachineRecipe> {
@Override
protected RollingMachineRecipe fromJson(JsonObject jsonObject, RebornRecipeType<RollingMachineRecipe> type, Identifier name, List<RebornIngredient> ingredients, List<ItemStack> outputs, int power, int time) {
final JsonObject json = JsonHelper.getObject(jsonObject, "shaped");
final ShapedRecipe shapedRecipe = RecipeSerializer.SHAPED.read(name, json);
return new RollingMachineRecipe(type, name, ingredients, outputs, power, time, shapedRecipe, json);
}
@Override
public void toJson(RollingMachineRecipe recipe, JsonObject jsonObject) {
super.toJson(recipe, jsonObject);
if (recipe.getShapedRecipeJson() != null) {
jsonObject.add("shaped", recipe.getShapedRecipeJson());
}
}
@Override
public RollingMachineRecipe createDummy(RebornRecipeType<RollingMachineRecipe> type, Identifier name) {
return new RollingMachineRecipe(type, name);
}
}

View file

@ -33,27 +33,27 @@ import techreborn.api.recipe.recipes.*;
public class ModRecipes {
public static final RebornRecipeType<RebornRecipe> ALLOY_SMELTER = RecipeManager.newRecipeType(RebornRecipe::new, new Identifier("techreborn:alloy_smelter"));
public static final RebornRecipeType<RebornRecipe> ASSEMBLING_MACHINE = RecipeManager.newRecipeType(RebornRecipe::new, new Identifier("techreborn:assembling_machine"));
public static final RebornRecipeType<BlastFurnaceRecipe> BLAST_FURNACE = RecipeManager.newRecipeType(BlastFurnaceRecipe::new, new Identifier("techreborn:blast_furnace"));
public static final RebornRecipeType<RebornRecipe> CENTRIFUGE = RecipeManager.newRecipeType(RebornRecipe::new, new Identifier("techreborn:centrifuge"));
public static final RebornRecipeType<RebornRecipe> CHEMICAL_REACTOR = RecipeManager.newRecipeType(RebornRecipe::new, new Identifier("techreborn:chemical_reactor"));
public static final RebornRecipeType<RebornRecipe> COMPRESSOR = RecipeManager.newRecipeType(RebornRecipe::new, new Identifier("techreborn:compressor"));
public static final RebornRecipeType<RebornRecipe> DISTILLATION_TOWER = RecipeManager.newRecipeType(RebornRecipe::new, new Identifier("techreborn:distillation_tower"));
public static final RebornRecipeType<RebornRecipe> EXTRACTOR = RecipeManager.newRecipeType(RebornRecipe::new, new Identifier("techreborn:extractor"));
public static final RebornRecipeType<RebornRecipe> GRINDER = RecipeManager.newRecipeType(RebornRecipe::new, new Identifier("techreborn:grinder"));
public static final RebornRecipeType<RebornRecipe> IMPLOSION_COMPRESSOR = RecipeManager.newRecipeType(RebornRecipe::new, new Identifier("techreborn:implosion_compressor"));
public static final RebornRecipeType<RebornRecipe> INDUSTRIAL_ELECTROLYZER = RecipeManager.newRecipeType(RebornRecipe::new, new Identifier("techreborn:industrial_electrolyzer"));
public static final RebornRecipeType<IndustrialGrinderRecipe> INDUSTRIAL_GRINDER = RecipeManager.newRecipeType(IndustrialGrinderRecipe::new, new Identifier("techreborn:industrial_grinder"));
public static final RebornRecipeType<IndustrialSawmillRecipe> INDUSTRIAL_SAWMILL = RecipeManager.newRecipeType(IndustrialSawmillRecipe::new, new Identifier("techreborn:industrial_sawmill"));
public static final RebornRecipeType<RebornRecipe> RECYCLER = RecipeManager.newRecipeType(RebornRecipe::new, new Identifier("techreborn:recycler"));
public static final RebornRecipeType<RebornRecipe> SCRAPBOX = RecipeManager.newRecipeType(RebornRecipe::new, new Identifier("techreborn:scrapbox"));
public static final RebornRecipeType<RebornRecipe> VACUUM_FREEZER = RecipeManager.newRecipeType(RebornRecipe::new, new Identifier("techreborn:vacuum_freezer"));
public static final RebornRecipeType<FluidReplicatorRecipe> FLUID_REPLICATOR = RecipeManager.newRecipeType(FluidReplicatorRecipe::new, new Identifier("techreborn:fluid_replicator"));
public static final RebornRecipeType<FusionReactorRecipe> FUSION_REACTOR = RecipeManager.newRecipeType(FusionReactorRecipe::new, new Identifier("techreborn:fusion_reactor"));
public static final RebornRecipeType<RollingMachineRecipe> ROLLING_MACHINE = RecipeManager.newRecipeType(RollingMachineRecipe::new, new Identifier("techreborn:rolling_machine"));
public static final RebornRecipeType<RebornRecipe> SOLID_CANNING_MACHINE = RecipeManager.newRecipeType(RebornRecipe::new, new Identifier("techreborn:solid_canning_machine"));
public static final RebornRecipeType<RebornRecipe> WIRE_MILL = RecipeManager.newRecipeType(RebornRecipe::new, new Identifier("techreborn:wire_mill"));
public static final RebornRecipeType<RebornRecipe> ALLOY_SMELTER = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:alloy_smelter"));
public static final RebornRecipeType<RebornRecipe> ASSEMBLING_MACHINE = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:assembling_machine"));
public static final RebornRecipeType<BlastFurnaceRecipe> BLAST_FURNACE = RecipeManager.newRecipeType(BlastFurnaceRecipe::deserialize, new Identifier("techreborn:blast_furnace"));
public static final RebornRecipeType<RebornRecipe> CENTRIFUGE = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:centrifuge"));
public static final RebornRecipeType<RebornRecipe> CHEMICAL_REACTOR = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:chemical_reactor"));
public static final RebornRecipeType<RebornRecipe> COMPRESSOR = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:compressor"));
public static final RebornRecipeType<RebornRecipe> DISTILLATION_TOWER = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:distillation_tower"));
public static final RebornRecipeType<RebornRecipe> EXTRACTOR = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:extractor"));
public static final RebornRecipeType<RebornRecipe> GRINDER = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:grinder"));
public static final RebornRecipeType<RebornRecipe> IMPLOSION_COMPRESSOR = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:implosion_compressor"));
public static final RebornRecipeType<RebornRecipe> INDUSTRIAL_ELECTROLYZER = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:industrial_electrolyzer"));
public static final RebornRecipeType<IndustrialGrinderRecipe> INDUSTRIAL_GRINDER = RecipeManager.newRecipeType(IndustrialGrinderRecipe::deserialize, new Identifier("techreborn:industrial_grinder"));
public static final RebornRecipeType<IndustrialSawmillRecipe> INDUSTRIAL_SAWMILL = RecipeManager.newRecipeType(IndustrialSawmillRecipe::deserialize, new Identifier("techreborn:industrial_sawmill"));
public static final RebornRecipeType<RebornRecipe> RECYCLER = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:recycler"));
public static final RebornRecipeType<RebornRecipe> SCRAPBOX = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:scrapbox"));
public static final RebornRecipeType<RebornRecipe> VACUUM_FREEZER = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:vacuum_freezer"));
public static final RebornRecipeType<FluidReplicatorRecipe> FLUID_REPLICATOR = RecipeManager.newRecipeType(FluidReplicatorRecipe::deserialize, new Identifier("techreborn:fluid_replicator"));
public static final RebornRecipeType<FusionReactorRecipe> FUSION_REACTOR = RecipeManager.newRecipeType(FusionReactorRecipe::deserialize, new Identifier("techreborn:fusion_reactor"));
public static final RebornRecipeType<RollingMachineRecipe> ROLLING_MACHINE = RecipeManager.newRecipeType(RollingMachineRecipe::deserialize, new Identifier("techreborn:rolling_machine"));
public static final RebornRecipeType<RebornRecipe> SOLID_CANNING_MACHINE = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:solid_canning_machine"));
public static final RebornRecipeType<RebornRecipe> WIRE_MILL = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:wire_mill"));
public static RebornRecipeType<?> byName(Identifier identifier) {
return (RebornRecipeType<?>) Registry.RECIPE_SERIALIZER.get(identifier);