diff --git a/RebornCore/src/main/java/reborncore/common/RebornCoreCommands.java b/RebornCore/src/main/java/reborncore/common/RebornCoreCommands.java index d0b6648e8..45f91476f 100644 --- a/RebornCore/src/main/java/reborncore/common/RebornCoreCommands.java +++ b/RebornCore/src/main/java/reborncore/common/RebornCoreCommands.java @@ -48,7 +48,6 @@ import net.minecraft.text.LiteralText; import net.minecraft.util.registry.Registry; import net.minecraft.world.chunk.ChunkStatus; import reborncore.client.ItemStackRenderManager; -import reborncore.common.crafting.RecipeManager; import java.util.Collection; import java.util.Collections; @@ -79,17 +78,6 @@ public class RebornCoreCommands { dispatcher.register( literal("reborncore") - .then( - literal("recipes") - .then(literal("validate") - .requires(source -> source.hasPermissionLevel(3)) - .executes(ctx -> { - RecipeManager.validateRecipes(ctx.getSource().getWorld()); - return Command.SINGLE_SUCCESS; - }) - ) - ) - .then( literal("generate") .requires(source -> source.hasPermissionLevel(3)) diff --git a/RebornCore/src/main/java/reborncore/common/crafting/RebornFluidRecipe.java b/RebornCore/src/main/java/reborncore/common/crafting/RebornFluidRecipe.java index 42176c9ce..b0fc26443 100644 --- a/RebornCore/src/main/java/reborncore/common/crafting/RebornFluidRecipe.java +++ b/RebornCore/src/main/java/reborncore/common/crafting/RebornFluidRecipe.java @@ -27,27 +27,19 @@ package reborncore.common.crafting; import net.minecraft.block.entity.BlockEntity; import net.minecraft.item.ItemStack; import net.minecraft.util.Identifier; -import net.minecraft.util.collection.DefaultedList; import org.jetbrains.annotations.NotNull; import reborncore.common.crafting.ingredient.RebornIngredient; import reborncore.common.fluid.container.FluidInstance; import reborncore.common.util.Tank; +import java.util.List; + public abstract class RebornFluidRecipe extends RebornRecipe { - @NotNull - private FluidInstance fluidInstance = FluidInstance.EMPTY; + private final FluidInstance fluidInstance; - public RebornFluidRecipe(RebornRecipeType type, Identifier name) { - super(type, name); - } - - public RebornFluidRecipe(RebornRecipeType type, Identifier name, DefaultedList ingredients, DefaultedList outputs, int power, int time) { + public RebornFluidRecipe(RebornRecipeType type, Identifier name, List ingredients, List outputs, int power, int time, @NotNull FluidInstance fluidInstance) { super(type, name, ingredients, outputs, power, time); - } - - public RebornFluidRecipe(RebornRecipeType type, Identifier name, DefaultedList ingredients, DefaultedList outputs, int power, int time, FluidInstance fluidInstance) { - this(type, name, ingredients, outputs, power, time); this.fluidInstance = fluidInstance; } diff --git a/RebornCore/src/main/java/reborncore/common/crafting/RebornRecipe.java b/RebornCore/src/main/java/reborncore/common/crafting/RebornRecipe.java index 99a41fd04..87071e275 100644 --- a/RebornCore/src/main/java/reborncore/common/crafting/RebornRecipe.java +++ b/RebornCore/src/main/java/reborncore/common/crafting/RebornRecipe.java @@ -24,33 +24,20 @@ package reborncore.common.crafting; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; -import com.mojang.serialization.Dynamic; -import com.mojang.serialization.JsonOps; import io.github.cottonmc.libcd.api.CustomOutputRecipe; import net.minecraft.block.entity.BlockEntity; -import net.minecraft.data.server.recipe.RecipeJsonProvider; import net.minecraft.inventory.Inventory; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NbtOps; -import net.minecraft.network.PacketByteBuf; import net.minecraft.recipe.Ingredient; import net.minecraft.recipe.Recipe; import net.minecraft.recipe.RecipeSerializer; import net.minecraft.util.Identifier; -import net.minecraft.util.JsonHelper; import net.minecraft.util.collection.DefaultedList; -import net.minecraft.util.registry.Registry; import net.minecraft.world.World; -import org.apache.commons.lang3.Validate; import reborncore.api.recipe.IRecipeCrafterProvider; -import reborncore.common.crafting.ingredient.DummyIngredient; -import reborncore.common.crafting.ingredient.IngredientManager; import reborncore.common.crafting.ingredient.RebornIngredient; import reborncore.common.util.DefaultedListCollector; -import reborncore.common.util.serialization.SerializationUtil; import java.util.ArrayList; import java.util.Collection; @@ -58,7 +45,6 @@ import java.util.Collections; import java.util.List; public class RebornRecipe implements Recipe, CustomOutputRecipe { - private final RebornRecipeType type; private final Identifier name; @@ -67,8 +53,6 @@ public class RebornRecipe implements Recipe, CustomOutputRecipe { protected final int power; protected final int time; - protected boolean dummy = false; - public RebornRecipe(RebornRecipeType type, Identifier name, List ingredients, List outputs, int power, int time) { this.type = type; this.name = name; @@ -78,19 +62,6 @@ public class RebornRecipe implements Recipe, CustomOutputRecipe { this.time = time; } - public RebornRecipe(RebornRecipeType type, Identifier name) { - this(type, name, List.of(new DummyIngredient()), Collections.emptyList(), Integer.MAX_VALUE, Integer.MAX_VALUE); - this.dummy = true; - } - - public void serialize(PacketByteBuf byteBuf) { - throw new UnsupportedOperationException(); - } - - public void deserialize(PacketByteBuf byteBuf) { - throw new UnsupportedOperationException(); - } - @Override public Identifier getId() { return name; @@ -138,9 +109,6 @@ public class RebornRecipe implements Recipe, CustomOutputRecipe { * @return if true the recipe will craft, if false it will not */ public boolean canCraft(BlockEntity blockEntity) { - if (isDummy()) { - return false; - } if (blockEntity instanceof IRecipeCrafterProvider) { return ((IRecipeCrafterProvider) blockEntity).canCraft(this); } @@ -179,7 +147,7 @@ public class RebornRecipe implements Recipe, CustomOutputRecipe { @Deprecated @Override public ItemStack getOutput() { - if (isDummy() || outputs.isEmpty()) { + if (outputs.isEmpty()) { return ItemStack.EMPTY; } return outputs.get(0); @@ -196,10 +164,6 @@ public class RebornRecipe implements Recipe, CustomOutputRecipe { return true; } - public boolean isDummy() { - return dummy; - } - @Override public Collection getOutputItems() { List items = new ArrayList<>(); diff --git a/RebornCore/src/main/java/reborncore/common/crafting/RebornRecipeType.java b/RebornCore/src/main/java/reborncore/common/crafting/RebornRecipeType.java index c175c3b8b..4b429ece7 100644 --- a/RebornCore/src/main/java/reborncore/common/crafting/RebornRecipeType.java +++ b/RebornCore/src/main/java/reborncore/common/crafting/RebornRecipeType.java @@ -26,13 +26,11 @@ package reborncore.common.crafting; import com.google.gson.JsonObject; import net.minecraft.network.PacketByteBuf; -import net.minecraft.recipe.Recipe; import net.minecraft.recipe.RecipeSerializer; import net.minecraft.recipe.RecipeType; import net.minecraft.util.Identifier; import net.minecraft.util.JsonHelper; import net.minecraft.world.World; -import reborncore.RebornCore; import reborncore.common.crafting.serde.RecipeSerde; import reborncore.common.util.serialization.SerializationUtil; @@ -40,7 +38,7 @@ import java.util.List; public record RebornRecipeType( RecipeSerde recipeSerde, - Identifier name) implements RecipeType, RecipeSerializer { + Identifier name) implements RecipeType, RecipeSerializer { @Override public R read(Identifier recipeId, JsonObject json) { @@ -49,19 +47,10 @@ public record RebornRecipeType( throw new RuntimeException("RebornRecipe type not supported!"); } - try { - R recipe = newRecipe(json, recipeId); - - return recipe; - } catch (Throwable t) { - RebornCore.LOGGER.error("Failed to read recipe: " + recipeId, t); - // Make a new recipe - don't reuse the existing recipe object because it might be in an invalid state if an - // exception was thrown in the middle of its deserialization. - return recipeSerde.createDummy(this, recipeId); - } + return recipeSerde.fromJson(json, this, recipeId); } - public JsonObject toJson(R recipe) { + private JsonObject toJson(R recipe) { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("type", name.toString()); @@ -70,29 +59,17 @@ public record RebornRecipeType( return jsonObject; } - public R fromJson(Identifier recipeType, JsonObject json) { - return read(recipeType, json); - } - - private R newRecipe(JsonObject jsonObject, Identifier recipeId) { - return recipeSerde.fromJson(jsonObject, this, recipeId); - } - @Override public R read(Identifier recipeId, PacketByteBuf buffer) { - String input = buffer.readString(buffer.readInt()); - R r = read(recipeId, SerializationUtil.GSON_FLAT.fromJson(input, JsonObject.class)); - r.deserialize(buffer); - return r; + String jsonSize = buffer.readString(buffer.readInt()); + return read(recipeId, SerializationUtil.GSON_FLAT.fromJson(jsonSize, JsonObject.class)); } @Override - public void write(PacketByteBuf buffer, Recipe recipe) { - JsonObject jsonObject = toJson((R) recipe); - String output = SerializationUtil.GSON_FLAT.toJson(jsonObject); + public void write(PacketByteBuf buffer, R recipe) { + String output = SerializationUtil.GSON_FLAT.toJson(toJson(recipe)); buffer.writeInt(output.length()); buffer.writeString(output); - ((R) recipe).serialize(buffer); } public List getRecipes(World world) { diff --git a/RebornCore/src/main/java/reborncore/common/crafting/RecipeManager.java b/RebornCore/src/main/java/reborncore/common/crafting/RecipeManager.java index d5c44c1fe..842034cc5 100644 --- a/RebornCore/src/main/java/reborncore/common/crafting/RecipeManager.java +++ b/RebornCore/src/main/java/reborncore/common/crafting/RecipeManager.java @@ -24,28 +24,25 @@ package reborncore.common.crafting; -import io.netty.buffer.Unpooled; -import net.minecraft.item.ItemStack; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.recipe.Recipe; import net.minecraft.recipe.RecipeSerializer; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; -import net.minecraft.world.World; -import org.apache.commons.lang3.Validate; -import reborncore.common.crafting.ingredient.RebornIngredient; +import reborncore.common.crafting.serde.RebornRecipeSerde; import reborncore.common.crafting.serde.RecipeSerde; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.function.BiFunction; import java.util.stream.Collectors; public class RecipeManager { private static final Map> recipeTypes = new HashMap<>(); + public static RebornRecipeType newRecipeType(Identifier name) { + return newRecipeType(RebornRecipeSerde.BASIC, name); + } + public static RebornRecipeType newRecipeType(RecipeSerde recipeSerde, Identifier name) { if (recipeTypes.containsKey(name)) { throw new RuntimeException("RebornRecipe type with this name already registered"); @@ -68,64 +65,4 @@ public class RecipeManager { public static List getRecipeTypes(String namespace) { return recipeTypes.values().stream().filter(rebornRecipeType -> rebornRecipeType.name().getNamespace().equals(namespace)).collect(Collectors.toList()); } - - public static void validateRecipes(World world) { - //recipeTypes.forEach((key, value) -> validate(value, world)); - - System.out.println("Validating recipes"); - world.getRecipeManager().keys().forEach(identifier -> { - try { - Recipe recipe = world.getRecipeManager().get(identifier).get(); - RecipeSerializer recipeSerializer = recipe.getSerializer(); - PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); - recipeSerializer.write(buf, recipe); - - Recipe readback = recipeSerializer.read(identifier, buf); - } catch (Exception e) { - throw new RuntimeException("Failed to read " + identifier, e); - } - }); - System.out.println("Done"); - } - - private static void validate(RebornRecipeType rebornRecipeType, World world) { - List recipes = rebornRecipeType.getRecipes(world); - - for (RebornRecipe recipe1 : recipes) { - for (RebornRecipe recipe2 : recipes) { - if (recipe1 == recipe2) { - continue; - } - - Validate.isTrue(recipe1.getRebornIngredients().size() > 0, recipe1.getId() + " has no inputs"); - Validate.isTrue(recipe2.getRebornIngredients().size() > 0, recipe2.getId() + " has no inputs"); - Validate.isTrue(recipe1.getOutputs().size() > 0, recipe1.getId() + " has no outputs"); - Validate.isTrue(recipe2.getOutputs().size() > 0, recipe2.getId() + " has no outputs"); - - boolean hasAll = true; - - for (RebornIngredient recipe1Input : recipe1.getRebornIngredients()) { - boolean matches = false; - for (ItemStack testStack : recipe1Input.getPreviewStacks()) { - for (RebornIngredient recipe2Input : recipe2.getRebornIngredients()) { - if (recipe2Input.test(testStack)) { - matches = true; - } - } - } - - if (!matches) { - hasAll = false; - } - } - - if (hasAll) { - System.out.println(recipe1.getId() + " conflicts with " + recipe2.getId()); - } - - } - } - } - - } diff --git a/RebornCore/src/main/java/reborncore/common/crafting/RecipeUtils.java b/RebornCore/src/main/java/reborncore/common/crafting/RecipeUtils.java index 820eac7ae..72e69c6af 100644 --- a/RebornCore/src/main/java/reborncore/common/crafting/RecipeUtils.java +++ b/RebornCore/src/main/java/reborncore/common/crafting/RecipeUtils.java @@ -28,11 +28,13 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.mojang.serialization.Dynamic; import com.mojang.serialization.JsonOps; +import net.minecraft.inventory.Inventory; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtOps; +import net.minecraft.recipe.Recipe; import net.minecraft.util.Identifier; import net.minecraft.util.JsonHelper; import net.minecraft.util.collection.DefaultedList; @@ -42,15 +44,17 @@ import reborncore.common.util.DefaultedListCollector; import reborncore.common.util.serialization.SerializationUtil; import reborncore.mixin.common.AccessorRecipeManager; -import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.List; public class RecipeUtils { @SuppressWarnings("unchecked") - public static List getRecipes(World world, RebornRecipeType type) { - AccessorRecipeManager accessorRecipeManager = (AccessorRecipeManager) world.getRecipeManager(); + public static List getRecipes(World world, RebornRecipeType type) { + final AccessorRecipeManager accessorRecipeManager = (AccessorRecipeManager) world.getRecipeManager(); + final Collection> recipes = accessorRecipeManager.getAll(type).values(); //noinspection unchecked - return new ArrayList<>(accessorRecipeManager.getAll(type).values()); + return Collections.unmodifiableList((List) (Object) recipes); } public static DefaultedList deserializeItems(JsonElement jsonObject) { diff --git a/RebornCore/src/main/java/reborncore/common/crafting/serde/AbstractRecipeSerde.java b/RebornCore/src/main/java/reborncore/common/crafting/serde/AbstractRecipeSerde.java index 9831ca5bc..c0b8ea63e 100644 --- a/RebornCore/src/main/java/reborncore/common/crafting/serde/AbstractRecipeSerde.java +++ b/RebornCore/src/main/java/reborncore/common/crafting/serde/AbstractRecipeSerde.java @@ -1,3 +1,27 @@ +/* + * This file is part of TechReborn, licensed under the MIT License (MIT). + * + * Copyright (c) 2020 TechReborn + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package reborncore.common.crafting.serde; import com.google.gson.JsonArray; @@ -6,12 +30,9 @@ import com.mojang.serialization.Dynamic; import com.mojang.serialization.JsonOps; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtOps; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.util.Identifier; import net.minecraft.util.JsonHelper; import net.minecraft.util.registry.Registry; import reborncore.common.crafting.RebornRecipe; -import reborncore.common.crafting.RebornRecipeType; import reborncore.common.crafting.RecipeUtils; import reborncore.common.crafting.ingredient.IngredientManager; import reborncore.common.crafting.ingredient.RebornIngredient; @@ -21,31 +42,6 @@ import reborncore.common.util.serialization.SerializationUtil; import java.util.List; public abstract class AbstractRecipeSerde implements RecipeSerde { - @Override - public void writeToPacket(PacketByteBuf buffer, R recipe) { - final JsonObject jsonObject = new JsonObject(); - toJson(recipe, jsonObject); - - final String jsonStr = SerializationUtil.GSON_FLAT.toJson(jsonObject); - - buffer.writeInt(jsonStr.length()); - buffer.writeString(jsonStr); - - recipe.serialize(buffer); - } - - @Override - public R fromPacket(PacketByteBuf buffer, RebornRecipeType type) { - final Identifier identifier = new Identifier(buffer.readString(buffer.readInt())); - final String input = buffer.readString(buffer.readInt()); - final JsonObject jsonObject = SerializationUtil.GSON_FLAT.fromJson(input, JsonObject.class); - - final R r = fromJson(jsonObject, type, identifier); - r.deserialize(buffer); - - return r; - } - protected int getPower(JsonObject jsonObject) { return JsonHelper.getInt(jsonObject, "power"); } diff --git a/RebornCore/src/main/java/reborncore/common/crafting/serde/RebornFluidRecipeSerde.java b/RebornCore/src/main/java/reborncore/common/crafting/serde/RebornFluidRecipeSerde.java index 650cf28cb..94d741357 100644 --- a/RebornCore/src/main/java/reborncore/common/crafting/serde/RebornFluidRecipeSerde.java +++ b/RebornCore/src/main/java/reborncore/common/crafting/serde/RebornFluidRecipeSerde.java @@ -1,3 +1,27 @@ +/* + * This file is part of TechReborn, licensed under the MIT License (MIT). + * + * Copyright (c) 2020 TechReborn + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package reborncore.common.crafting.serde; import com.google.gson.JsonObject; @@ -6,7 +30,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.util.Identifier; import net.minecraft.util.JsonHelper; import net.minecraft.util.registry.Registry; -import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.NotNull; import reborncore.common.crafting.RebornFluidRecipe; import reborncore.common.crafting.RebornRecipeType; import reborncore.common.crafting.ingredient.RebornIngredient; @@ -16,7 +40,7 @@ import reborncore.common.fluid.container.FluidInstance; import java.util.List; public abstract class RebornFluidRecipeSerde extends RebornRecipeSerde { - protected abstract R fromJson(JsonObject jsonObject, RebornRecipeType type, Identifier name, List ingredients, List outputs, int power, int time, @Nullable FluidInstance fluidInstance); + protected abstract R fromJson(JsonObject jsonObject, RebornRecipeType type, Identifier name, List ingredients, List outputs, int power, int time, @NotNull FluidInstance fluidInstance); @Override protected final R fromJson(JsonObject jsonObject, RebornRecipeType type, Identifier name, List ingredients, List outputs, int power, int time) { @@ -35,13 +59,24 @@ public abstract class RebornFluidRecipeSerde extend } @Override - public void toJson(R recipe, JsonObject jsonObject) { - super.toJson(recipe, jsonObject); - + public void collectJsonData(R recipe, JsonObject jsonObject) { final JsonObject tankObject = new JsonObject(); tankObject.addProperty("fluid", Registry.FLUID.getId(recipe.getFluidInstance().getFluid()).toString()); tankObject.addProperty("value", recipe.getFluidInstance().getAmount().getRawValue()); jsonObject.add("tank", tankObject); } + + public static RebornFluidRecipeSerde create(SimpleFluidRecipeFactory factory) { + return new RebornFluidRecipeSerde<>() { + @Override + protected R fromJson(JsonObject jsonObject, RebornRecipeType type, Identifier name, List ingredients, List outputs, int power, int time, @NotNull FluidInstance fluidInstance) { + return factory.create(type, name, ingredients, outputs, power, time, fluidInstance); + } + }; + } + + public interface SimpleFluidRecipeFactory { + R create(RebornRecipeType type, Identifier name, List ingredients, List outputs, int power, int time, @NotNull FluidInstance fluidInstance); + } } diff --git a/RebornCore/src/main/java/reborncore/common/crafting/serde/RebornRecipeSerde.java b/RebornCore/src/main/java/reborncore/common/crafting/serde/RebornRecipeSerde.java index 4a71d2f63..5d16f31f7 100644 --- a/RebornCore/src/main/java/reborncore/common/crafting/serde/RebornRecipeSerde.java +++ b/RebornCore/src/main/java/reborncore/common/crafting/serde/RebornRecipeSerde.java @@ -1,3 +1,27 @@ +/* + * This file is part of TechReborn, licensed under the MIT License (MIT). + * + * Copyright (c) 2020 TechReborn + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package reborncore.common.crafting.serde; import com.google.gson.JsonObject; @@ -10,26 +34,12 @@ import reborncore.common.crafting.ingredient.RebornIngredient; import java.util.List; public abstract class RebornRecipeSerde extends AbstractRecipeSerde { - public static final RebornRecipeSerde BASIC = new RebornRecipeSerde<>() { - @Override - protected RebornRecipe fromJson(JsonObject jsonObject, RebornRecipeType type, Identifier name, List ingredients, List outputs, int power, int time) { - return new RebornRecipe(type, name, ingredients, outputs, power, time); - } - - @Override - public RebornRecipe createDummy(RebornRecipeType type, Identifier name) { - return new RebornRecipe(type, name); - } - }; + public static final RebornRecipeSerde BASIC = create(RebornRecipe::new); protected abstract R fromJson(JsonObject jsonObject, RebornRecipeType type, Identifier name, List ingredients, List outputs, int power, int time); @Override public final R fromJson(JsonObject jsonObject, RebornRecipeType type, Identifier name) { - if (jsonObject.has("dummy")) { - return createDummy(type, name); - } - final int power = getPower(jsonObject); final int time = getTime(jsonObject); final List ingredients = getIngredients(jsonObject); @@ -38,16 +48,32 @@ public abstract class RebornRecipeSerde extends Abstract return fromJson(jsonObject, type, name, ingredients, outputs, power, time); } - @Override - public void toJson(R recipe, JsonObject jsonObject) { - if (recipe.isDummy()) { - jsonObject.addProperty("dummy", true); - return; - } + protected abstract void collectJsonData(R recipe, JsonObject jsonObject); + @Override + public final void toJson(R recipe, JsonObject jsonObject) { writePower(recipe, jsonObject); writeTime(recipe, jsonObject); writeIngredients(recipe, jsonObject); writeOutputs(recipe, jsonObject); + + collectJsonData(recipe, jsonObject); + } + + public static RebornRecipeSerde create(SimpleRecipeFactory factory) { + return new RebornRecipeSerde() { + @Override + protected R fromJson(JsonObject jsonObject, RebornRecipeType type, Identifier name, List ingredients, List outputs, int power, int time) { + return factory.create(type, name, ingredients, outputs, power, time); + } + + @Override + protected void collectJsonData(R recipe, JsonObject jsonObject) { + } + }; + } + + public interface SimpleRecipeFactory { + R create(RebornRecipeType type, Identifier name, List ingredients, List outputs, int power, int time); } } diff --git a/RebornCore/src/main/java/reborncore/common/crafting/serde/RecipeSerde.java b/RebornCore/src/main/java/reborncore/common/crafting/serde/RecipeSerde.java index b78113008..6f84ab6b1 100644 --- a/RebornCore/src/main/java/reborncore/common/crafting/serde/RecipeSerde.java +++ b/RebornCore/src/main/java/reborncore/common/crafting/serde/RecipeSerde.java @@ -1,7 +1,30 @@ +/* + * This file is part of TechReborn, licensed under the MIT License (MIT). + * + * Copyright (c) 2020 TechReborn + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package reborncore.common.crafting.serde; import com.google.gson.JsonObject; -import net.minecraft.network.PacketByteBuf; import net.minecraft.util.Identifier; import reborncore.common.crafting.RebornRecipe; import reborncore.common.crafting.RebornRecipeType; @@ -10,10 +33,4 @@ public interface RecipeSerde { R fromJson(JsonObject jsonObject, RebornRecipeType type, Identifier name); void toJson(R recipe, JsonObject jsonObject); - - void writeToPacket(PacketByteBuf buffer, R recipe); - - R fromPacket(PacketByteBuf buffer, RebornRecipeType type); - - R createDummy(RebornRecipeType type, Identifier name); } diff --git a/src/main/java/techreborn/api/recipe/recipes/BlastFurnaceRecipe.java b/src/main/java/techreborn/api/recipe/recipes/BlastFurnaceRecipe.java index 003541664..b37d779e5 100644 --- a/src/main/java/techreborn/api/recipe/recipes/BlastFurnaceRecipe.java +++ b/src/main/java/techreborn/api/recipe/recipes/BlastFurnaceRecipe.java @@ -24,26 +24,20 @@ package techreborn.api.recipe.recipes; -import com.google.gson.JsonObject; import net.minecraft.block.entity.BlockEntity; import net.minecraft.item.ItemStack; import net.minecraft.util.Identifier; -import net.minecraft.util.JsonHelper; -import net.minecraft.util.collection.DefaultedList; import reborncore.common.crafting.RebornRecipe; import reborncore.common.crafting.RebornRecipeType; import reborncore.common.crafting.ingredient.RebornIngredient; import techreborn.blockentity.machine.multiblock.IndustrialBlastFurnaceBlockEntity; +import java.util.List; + public class BlastFurnaceRecipe extends RebornRecipe { + private final int heat; - private int heat; - - public BlastFurnaceRecipe(RebornRecipeType type, Identifier name) { - super(type, name); - } - - public BlastFurnaceRecipe(RebornRecipeType type, Identifier name, DefaultedList ingredients, DefaultedList outputs, int power, int time, int heat) { + public BlastFurnaceRecipe(RebornRecipeType type, Identifier name, List ingredients, List outputs, int power, int time, int heat) { super(type, name, ingredients, outputs, power, time); this.heat = heat; } @@ -52,18 +46,6 @@ public class BlastFurnaceRecipe extends RebornRecipe { return heat; } - @Override - public void deserialize(JsonObject jsonObject) { - super.deserialize(jsonObject); - heat = JsonHelper.getInt(jsonObject, "heat"); - } - - @Override - public void serialize(JsonObject jsonObject) { - super.serialize(jsonObject); - jsonObject.addProperty("heat", heat); - } - @Override public boolean canCraft(final BlockEntity blockEntity) { if (blockEntity instanceof final IndustrialBlastFurnaceBlockEntity blastFurnace) { diff --git a/src/main/java/techreborn/api/recipe/recipes/FluidReplicatorRecipe.java b/src/main/java/techreborn/api/recipe/recipes/FluidReplicatorRecipe.java index 1009278f4..e4a01dbae 100644 --- a/src/main/java/techreborn/api/recipe/recipes/FluidReplicatorRecipe.java +++ b/src/main/java/techreborn/api/recipe/recipes/FluidReplicatorRecipe.java @@ -29,27 +29,19 @@ import net.minecraft.fluid.Fluid; import net.minecraft.fluid.Fluids; import net.minecraft.item.ItemStack; import net.minecraft.util.Identifier; -import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.math.BlockPos; import reborncore.common.crafting.RebornFluidRecipe; import reborncore.common.crafting.RebornRecipeType; import reborncore.common.crafting.ingredient.RebornIngredient; +import reborncore.common.fluid.FluidUtils; import reborncore.common.fluid.container.FluidInstance; import reborncore.common.util.Tank; import techreborn.blockentity.machine.multiblock.FluidReplicatorBlockEntity; -import reborncore.common.fluid.FluidUtils; + +import java.util.List; public class FluidReplicatorRecipe extends RebornFluidRecipe { - - public FluidReplicatorRecipe(RebornRecipeType type, Identifier name) { - super(type, name); - } - - public FluidReplicatorRecipe(RebornRecipeType type, Identifier name, DefaultedList ingredients, DefaultedList outputs, int power, int time) { - super(type, name, ingredients, outputs, power, time); - } - - public FluidReplicatorRecipe(RebornRecipeType type, Identifier name, DefaultedList ingredients, DefaultedList outputs, int power, int time, FluidInstance fluid) { + public FluidReplicatorRecipe(RebornRecipeType type, Identifier name, List ingredients, List outputs, int power, int time, FluidInstance fluid) { super(type, name, ingredients, outputs, power, time, fluid); } diff --git a/src/main/java/techreborn/api/recipe/recipes/FusionReactorRecipe.java b/src/main/java/techreborn/api/recipe/recipes/FusionReactorRecipe.java index cef8e9f32..767b6d5e6 100644 --- a/src/main/java/techreborn/api/recipe/recipes/FusionReactorRecipe.java +++ b/src/main/java/techreborn/api/recipe/recipes/FusionReactorRecipe.java @@ -24,30 +24,24 @@ package techreborn.api.recipe.recipes; -import com.google.gson.JsonObject; import net.minecraft.block.entity.BlockEntity; import net.minecraft.item.ItemStack; import net.minecraft.util.Identifier; -import net.minecraft.util.JsonHelper; -import net.minecraft.util.collection.DefaultedList; import reborncore.common.crafting.RebornRecipe; import reborncore.common.crafting.RebornRecipeType; import reborncore.common.crafting.ingredient.RebornIngredient; import techreborn.blockentity.machine.multiblock.FusionControlComputerBlockEntity; +import java.util.List; + /** * @author drcrazy */ public class FusionReactorRecipe extends RebornRecipe { + private final int startE; + private final int minSize; - private int startE; - private int minSize; - - public FusionReactorRecipe(RebornRecipeType type, Identifier name) { - super(type, name); - } - - public FusionReactorRecipe(RebornRecipeType type, Identifier name, DefaultedList ingredients, DefaultedList outputs, int power, int time, int startE, int minSize) { + public FusionReactorRecipe(RebornRecipeType type, Identifier name, List ingredients, List outputs, int power, int time, int startE, int minSize) { super(type, name, ingredients, outputs, power, time); this.startE = startE; this.minSize = minSize; @@ -57,18 +51,8 @@ public class FusionReactorRecipe extends RebornRecipe { return startE; } - @Override - public void deserialize(JsonObject jsonObject) { - super.deserialize(jsonObject); - startE = JsonHelper.getInt(jsonObject, "start-power"); - minSize = JsonHelper.getInt(jsonObject, "min-size"); - } - - @Override - public void serialize(JsonObject jsonObject) { - super.serialize(jsonObject); - jsonObject.addProperty("start-power", startE); - jsonObject.addProperty("min-size", minSize); + public int getMinSize() { + return minSize; } @Override diff --git a/src/main/java/techreborn/api/recipe/recipes/IndustrialGrinderRecipe.java b/src/main/java/techreborn/api/recipe/recipes/IndustrialGrinderRecipe.java index 38a5d9b89..e568eed52 100644 --- a/src/main/java/techreborn/api/recipe/recipes/IndustrialGrinderRecipe.java +++ b/src/main/java/techreborn/api/recipe/recipes/IndustrialGrinderRecipe.java @@ -27,7 +27,6 @@ package techreborn.api.recipe.recipes; import net.minecraft.block.entity.BlockEntity; import net.minecraft.item.ItemStack; import net.minecraft.util.Identifier; -import net.minecraft.util.collection.DefaultedList; import reborncore.common.crafting.RebornFluidRecipe; import reborncore.common.crafting.RebornRecipeType; import reborncore.common.crafting.ingredient.RebornIngredient; @@ -35,17 +34,10 @@ import reborncore.common.fluid.container.FluidInstance; import reborncore.common.util.Tank; import techreborn.blockentity.machine.multiblock.IndustrialGrinderBlockEntity; +import java.util.List; + public class IndustrialGrinderRecipe extends RebornFluidRecipe { - - public IndustrialGrinderRecipe(RebornRecipeType type, Identifier name) { - super(type, name); - } - - public IndustrialGrinderRecipe(RebornRecipeType type, Identifier name, DefaultedList ingredients, DefaultedList outputs, int power, int time) { - super(type, name, ingredients, outputs, power, time); - } - - public IndustrialGrinderRecipe(RebornRecipeType type, Identifier name, DefaultedList ingredients, DefaultedList outputs, int power, int time, FluidInstance fluid) { + public IndustrialGrinderRecipe(RebornRecipeType type, Identifier name, List ingredients, List outputs, int power, int time, FluidInstance fluid) { super(type, name, ingredients, outputs, power, time, fluid); } diff --git a/src/main/java/techreborn/api/recipe/recipes/IndustrialSawmillRecipe.java b/src/main/java/techreborn/api/recipe/recipes/IndustrialSawmillRecipe.java index 25827bd46..84720612d 100644 --- a/src/main/java/techreborn/api/recipe/recipes/IndustrialSawmillRecipe.java +++ b/src/main/java/techreborn/api/recipe/recipes/IndustrialSawmillRecipe.java @@ -27,7 +27,6 @@ package techreborn.api.recipe.recipes; import net.minecraft.block.entity.BlockEntity; import net.minecraft.item.ItemStack; import net.minecraft.util.Identifier; -import net.minecraft.util.collection.DefaultedList; import reborncore.common.crafting.RebornFluidRecipe; import reborncore.common.crafting.RebornRecipeType; import reborncore.common.crafting.ingredient.RebornIngredient; @@ -35,17 +34,10 @@ import reborncore.common.fluid.container.FluidInstance; import reborncore.common.util.Tank; import techreborn.blockentity.machine.multiblock.IndustrialSawmillBlockEntity; +import java.util.List; + public class IndustrialSawmillRecipe extends RebornFluidRecipe { - - public IndustrialSawmillRecipe(RebornRecipeType type, Identifier name) { - super(type, name); - } - - public IndustrialSawmillRecipe(RebornRecipeType type, Identifier name, DefaultedList ingredients, DefaultedList outputs, int power, int time) { - super(type, name, ingredients, outputs, power, time); - } - - public IndustrialSawmillRecipe(RebornRecipeType type, Identifier name, DefaultedList ingredients, DefaultedList outputs, int power, int time, FluidInstance fluid) { + public IndustrialSawmillRecipe(RebornRecipeType type, Identifier name, List ingredients, List outputs, int power, int time, FluidInstance fluid) { super(type, name, ingredients, outputs, power, time, fluid); } diff --git a/src/main/java/techreborn/api/recipe/recipes/RollingMachineRecipe.java b/src/main/java/techreborn/api/recipe/recipes/RollingMachineRecipe.java index 74042db85..1b3ed279e 100644 --- a/src/main/java/techreborn/api/recipe/recipes/RollingMachineRecipe.java +++ b/src/main/java/techreborn/api/recipe/recipes/RollingMachineRecipe.java @@ -28,17 +28,13 @@ import com.google.gson.JsonObject; import net.minecraft.inventory.CraftingInventory; import net.minecraft.inventory.Inventory; import net.minecraft.item.ItemStack; -import net.minecraft.network.PacketByteBuf; import net.minecraft.recipe.Ingredient; -import net.minecraft.recipe.RecipeSerializer; import net.minecraft.recipe.ShapedRecipe; import net.minecraft.util.Identifier; -import net.minecraft.util.JsonHelper; 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; @@ -48,11 +44,6 @@ public class RollingMachineRecipe extends RebornRecipe { private final ShapedRecipe shapedRecipe; private final JsonObject shapedRecipeJson; - public RollingMachineRecipe(RebornRecipeType type, Identifier 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, List ingredients, List outputs, int power, int time, ShapedRecipe shapedRecipe, JsonObject shapedRecipeJson) { super(type, name, ingredients, outputs, power, time); this.shapedRecipe = shapedRecipe; diff --git a/src/main/java/techreborn/api/recipe/recipes/RollingMachineRecipeSerde.java b/src/main/java/techreborn/api/recipe/recipes/RollingMachineRecipeSerde.java deleted file mode 100644 index b5dc696da..000000000 --- a/src/main/java/techreborn/api/recipe/recipes/RollingMachineRecipeSerde.java +++ /dev/null @@ -1,36 +0,0 @@ -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 { - @Override - protected RollingMachineRecipe fromJson(JsonObject jsonObject, RebornRecipeType type, Identifier name, List ingredients, List 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 type, Identifier name) { - return new RollingMachineRecipe(type, name); - } -} diff --git a/src/main/java/techreborn/api/recipe/recipes/serde/BlastFurnaceRecipeSerde.java b/src/main/java/techreborn/api/recipe/recipes/serde/BlastFurnaceRecipeSerde.java new file mode 100644 index 000000000..af3efc237 --- /dev/null +++ b/src/main/java/techreborn/api/recipe/recipes/serde/BlastFurnaceRecipeSerde.java @@ -0,0 +1,49 @@ +/* + * This file is part of TechReborn, licensed under the MIT License (MIT). + * + * Copyright (c) 2020 TechReborn + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package techreborn.api.recipe.recipes.serde; + +import com.google.gson.JsonObject; +import net.minecraft.item.ItemStack; +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 techreborn.api.recipe.recipes.BlastFurnaceRecipe; + +import java.util.List; + +public class BlastFurnaceRecipeSerde extends RebornRecipeSerde { + @Override + protected BlastFurnaceRecipe fromJson(JsonObject jsonObject, RebornRecipeType type, Identifier name, List ingredients, List outputs, int power, int time) { + final int heat = JsonHelper.getInt(jsonObject, "heat"); + return new BlastFurnaceRecipe(type, name, ingredients, outputs, power, time, heat); + } + + @Override + public void collectJsonData(BlastFurnaceRecipe recipe, JsonObject jsonObject) { + jsonObject.addProperty("heat", recipe.getHeat()); + } +} diff --git a/src/main/java/techreborn/api/recipe/recipes/serde/FusionReactorRecipeSerde.java b/src/main/java/techreborn/api/recipe/recipes/serde/FusionReactorRecipeSerde.java new file mode 100644 index 000000000..35a71600d --- /dev/null +++ b/src/main/java/techreborn/api/recipe/recipes/serde/FusionReactorRecipeSerde.java @@ -0,0 +1,51 @@ +/* + * This file is part of TechReborn, licensed under the MIT License (MIT). + * + * Copyright (c) 2020 TechReborn + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package techreborn.api.recipe.recipes.serde; + +import com.google.gson.JsonObject; +import net.minecraft.item.ItemStack; +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 techreborn.api.recipe.recipes.FusionReactorRecipe; + +import java.util.List; + +public class FusionReactorRecipeSerde extends RebornRecipeSerde { + @Override + protected FusionReactorRecipe fromJson(JsonObject jsonObject, RebornRecipeType type, Identifier name, List ingredients, List outputs, int power, int time) { + final int startE = JsonHelper.getInt(jsonObject, "start-power"); + final int minSize = JsonHelper.getInt(jsonObject, "min-size"); + return new FusionReactorRecipe(type, name, ingredients, outputs, power, time, startE, minSize); + } + + @Override + protected void collectJsonData(FusionReactorRecipe recipe, JsonObject jsonObject) { + jsonObject.addProperty("start-power", recipe.getStartEnergy()); + jsonObject.addProperty("min-size", recipe.getMinSize()); + } +} diff --git a/src/main/java/techreborn/api/recipe/recipes/serde/RollingMachineRecipeSerde.java b/src/main/java/techreborn/api/recipe/recipes/serde/RollingMachineRecipeSerde.java new file mode 100644 index 000000000..6e29cdcfa --- /dev/null +++ b/src/main/java/techreborn/api/recipe/recipes/serde/RollingMachineRecipeSerde.java @@ -0,0 +1,49 @@ +package techreborn.api.recipe.recipes.serde; + +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 techreborn.api.recipe.recipes.RollingMachineRecipe; + +import java.util.Collections; +import java.util.List; + +public class RollingMachineRecipeSerde extends RebornRecipeSerde { + @Override + protected RollingMachineRecipe fromJson(JsonObject jsonObject, RebornRecipeType type, Identifier name, List ingredients, List outputs, int power, int time) { + final JsonObject shapedRecipeJson = JsonHelper.getObject(jsonObject, "shaped"); + final ShapedRecipe shapedRecipe = RecipeSerializer.SHAPED.read(name, shapedRecipeJson); + return new RollingMachineRecipe(type, name, ingredients, outputs, power, time, shapedRecipe, shapedRecipeJson); + } + + @Override + public void collectJsonData(RollingMachineRecipe recipe, JsonObject jsonObject) { + jsonObject.add("shaped", recipe.getShapedRecipeJson()); + } + + @Override + protected List getIngredients(JsonObject jsonObject) { + // Inputs are handled by the shaped recipe. + return Collections.emptyList(); + } + + @Override + protected List getOutputs(JsonObject jsonObject) { + // Outputs are handled by the shaped recipe. + return Collections.emptyList(); + } + + @Override + protected void writeIngredients(RollingMachineRecipe recipe, JsonObject jsonObject) { + } + + @Override + protected void writeOutputs(RollingMachineRecipe recipe, JsonObject jsonObject) { + } +} diff --git a/src/main/java/techreborn/blockentity/machine/tier1/RollingMachineBlockEntity.java b/src/main/java/techreborn/blockentity/machine/tier1/RollingMachineBlockEntity.java index 38d5e386b..e401bfe48 100644 --- a/src/main/java/techreborn/blockentity/machine/tier1/RollingMachineBlockEntity.java +++ b/src/main/java/techreborn/blockentity/machine/tier1/RollingMachineBlockEntity.java @@ -126,7 +126,7 @@ public class RollingMachineBlockEntity extends PowerAcceptorBlockEntity } if (!currentRecipeOutput.isEmpty() && canMake(craftMatrix)) { - if (tickTime >= Math.max((int) (TechRebornConfig.rollingMachineRunTime * (1.0 - getSpeedMultiplier())), 1)) { + if (tickTime >= Math.max((int) (currentRecipe.getTime() * (1.0 - getSpeedMultiplier())), 1)) { currentRecipeOutput = findMatchingRecipeOutput(craftMatrix, world); if (!currentRecipeOutput.isEmpty()) { boolean hasCrafted = false; @@ -159,10 +159,10 @@ public class RollingMachineBlockEntity extends PowerAcceptorBlockEntity tickTime = 0; } if (!currentRecipeOutput.isEmpty()) { - if (getStored() > getEuPerTick(TechRebornConfig.rollingMachineEnergyPerTick) - && tickTime < Math.max((int) (TechRebornConfig.rollingMachineRunTime * (1.0 - getSpeedMultiplier())), 1) + if (getStored() > getEuPerTick(currentRecipe.getPower()) + && tickTime < Math.max((int) (currentRecipe.getTime() * (1.0 - getSpeedMultiplier())), 1) && canMake(craftMatrix)) { - useEnergy(getEuPerTick(TechRebornConfig.rollingMachineEnergyPerTick)); + useEnergy(getEuPerTick(currentRecipe.getPower())); tickTime++; } else { setIsActive(false); @@ -375,10 +375,10 @@ public class RollingMachineBlockEntity extends PowerAcceptorBlockEntity } public int getBurnTimeRemainingScaled(final int scale) { - if (tickTime == 0 || Math.max((int) (TechRebornConfig.rollingMachineRunTime * (1.0 - getSpeedMultiplier())), 1) == 0) { + if (tickTime == 0 || Math.max((int) (currentRecipe.getTime() * (1.0 - getSpeedMultiplier())), 1) == 0) { return 0; } - return tickTime * scale / Math.max((int) (TechRebornConfig.rollingMachineRunTime * (1.0 - getSpeedMultiplier())), 1); + return tickTime * scale / Math.max((int) (currentRecipe.getTime() * (1.0 - getSpeedMultiplier())), 1); } @Override @@ -405,8 +405,8 @@ public class RollingMachineBlockEntity extends PowerAcceptorBlockEntity } public int getProgressScaled(final int scale) { - if (tickTime != 0 && Math.max((int) (TechRebornConfig.rollingMachineRunTime * (1.0 - getSpeedMultiplier())), 1) != 0) { - return tickTime * scale / Math.max((int) (TechRebornConfig.rollingMachineRunTime * (1.0 - getSpeedMultiplier())), 1); + if (tickTime != 0 && Math.max((int) (currentRecipe.getTime() * (1.0 - getSpeedMultiplier())), 1) != 0) { + return tickTime * scale / Math.max((int) (currentRecipe.getTime() * (1.0 - getSpeedMultiplier())), 1); } return 0; } diff --git a/src/main/java/techreborn/config/TechRebornConfig.java b/src/main/java/techreborn/config/TechRebornConfig.java index ebdc917ba..9e60115da 100644 --- a/src/main/java/techreborn/config/TechRebornConfig.java +++ b/src/main/java/techreborn/config/TechRebornConfig.java @@ -366,12 +366,6 @@ public class TechRebornConfig { @Config(config = "machines", category = "rolling_machine", key = "RollingMachineMaxInput", comment = "Rolling Machine Max Input (Energy per tick)") public static int rollingMachineMaxInput = 32; - @Config(config = "machines", category = "rolling_machine", key = "RollingMachineEnergyPerTick", comment = "Rolling Machine Energy usage (Energy per tick)") - public static int rollingMachineEnergyPerTick = 5; - - @Config(config = "machines", category = "rolling_machine", key = "RollingMachineEnergyRunTime", comment = "Rolling Machine Run Time") - public static int rollingMachineRunTime = 250; - @Config(config = "machines", category = "rolling_machine", key = "RollingMachineMaxEnergy", comment = "Rolling Machine Max Energy") public static int rollingMachineMaxEnergy = 10000; diff --git a/src/main/java/techreborn/init/ModRecipes.java b/src/main/java/techreborn/init/ModRecipes.java index 8dbcd9fd3..6a820d6e2 100644 --- a/src/main/java/techreborn/init/ModRecipes.java +++ b/src/main/java/techreborn/init/ModRecipes.java @@ -29,31 +29,42 @@ import net.minecraft.util.registry.Registry; import reborncore.common.crafting.RebornRecipe; import reborncore.common.crafting.RebornRecipeType; import reborncore.common.crafting.RecipeManager; +import reborncore.common.crafting.serde.RebornFluidRecipeSerde; +import reborncore.common.crafting.serde.RebornRecipeSerde; import techreborn.api.recipe.recipes.*; +import techreborn.api.recipe.recipes.serde.BlastFurnaceRecipeSerde; +import techreborn.api.recipe.recipes.serde.FusionReactorRecipeSerde; +import techreborn.api.recipe.recipes.serde.RollingMachineRecipeSerde; public class ModRecipes { + public static final BlastFurnaceRecipeSerde BLAST_FURNACE_RECIPE_SERDE = new BlastFurnaceRecipeSerde(); + public static final RebornFluidRecipeSerde INDUSTRIAL_GRINDER_RECIPE_SERDE = RebornFluidRecipeSerde.create(IndustrialGrinderRecipe::new); + public static final RebornFluidRecipeSerde INDUSTRIAL_SAWILL_RECIPE_SERDE = RebornFluidRecipeSerde.create(IndustrialSawmillRecipe::new); + public static final RebornFluidRecipeSerde FLUID_REPLICATOR_RECIPE_SERDE = RebornFluidRecipeSerde.create(FluidReplicatorRecipe::new); + public static final FusionReactorRecipeSerde FUSION_REACTOR_RECIPE_SERDE = new FusionReactorRecipeSerde(); + public static final RollingMachineRecipeSerde ROLLING_MACHINE_RECIPE_SERDE = new RollingMachineRecipeSerde(); - public static final RebornRecipeType ALLOY_SMELTER = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:alloy_smelter")); - public static final RebornRecipeType ASSEMBLING_MACHINE = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:assembling_machine")); - public static final RebornRecipeType BLAST_FURNACE = RecipeManager.newRecipeType(BlastFurnaceRecipe::deserialize, new Identifier("techreborn:blast_furnace")); - public static final RebornRecipeType CENTRIFUGE = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:centrifuge")); - public static final RebornRecipeType CHEMICAL_REACTOR = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:chemical_reactor")); - public static final RebornRecipeType COMPRESSOR = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:compressor")); - public static final RebornRecipeType DISTILLATION_TOWER = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:distillation_tower")); - public static final RebornRecipeType EXTRACTOR = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:extractor")); - public static final RebornRecipeType GRINDER = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:grinder")); - public static final RebornRecipeType IMPLOSION_COMPRESSOR = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:implosion_compressor")); - public static final RebornRecipeType INDUSTRIAL_ELECTROLYZER = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:industrial_electrolyzer")); - public static final RebornRecipeType INDUSTRIAL_GRINDER = RecipeManager.newRecipeType(IndustrialGrinderRecipe::deserialize, new Identifier("techreborn:industrial_grinder")); - public static final RebornRecipeType INDUSTRIAL_SAWMILL = RecipeManager.newRecipeType(IndustrialSawmillRecipe::deserialize, new Identifier("techreborn:industrial_sawmill")); - public static final RebornRecipeType RECYCLER = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:recycler")); - public static final RebornRecipeType SCRAPBOX = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:scrapbox")); - public static final RebornRecipeType VACUUM_FREEZER = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:vacuum_freezer")); - public static final RebornRecipeType FLUID_REPLICATOR = RecipeManager.newRecipeType(FluidReplicatorRecipe::deserialize, new Identifier("techreborn:fluid_replicator")); - public static final RebornRecipeType FUSION_REACTOR = RecipeManager.newRecipeType(FusionReactorRecipe::deserialize, new Identifier("techreborn:fusion_reactor")); - public static final RebornRecipeType ROLLING_MACHINE = RecipeManager.newRecipeType(RollingMachineRecipe::deserialize, new Identifier("techreborn:rolling_machine")); - public static final RebornRecipeType SOLID_CANNING_MACHINE = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:solid_canning_machine")); - public static final RebornRecipeType WIRE_MILL = RecipeManager.newRecipeType(RebornRecipe::deserialize, new Identifier("techreborn:wire_mill")); + public static final RebornRecipeType ALLOY_SMELTER = RecipeManager.newRecipeType(new Identifier("techreborn:alloy_smelter")); + public static final RebornRecipeType ASSEMBLING_MACHINE = RecipeManager.newRecipeType(new Identifier("techreborn:assembling_machine")); + public static final RebornRecipeType BLAST_FURNACE = RecipeManager.newRecipeType(BLAST_FURNACE_RECIPE_SERDE, new Identifier("techreborn:blast_furnace")); + public static final RebornRecipeType CENTRIFUGE = RecipeManager.newRecipeType(new Identifier("techreborn:centrifuge")); + public static final RebornRecipeType CHEMICAL_REACTOR = RecipeManager.newRecipeType(new Identifier("techreborn:chemical_reactor")); + public static final RebornRecipeType COMPRESSOR = RecipeManager.newRecipeType(new Identifier("techreborn:compressor")); + public static final RebornRecipeType DISTILLATION_TOWER = RecipeManager.newRecipeType(new Identifier("techreborn:distillation_tower")); + public static final RebornRecipeType EXTRACTOR = RecipeManager.newRecipeType(new Identifier("techreborn:extractor")); + public static final RebornRecipeType GRINDER = RecipeManager.newRecipeType(new Identifier("techreborn:grinder")); + public static final RebornRecipeType IMPLOSION_COMPRESSOR = RecipeManager.newRecipeType(new Identifier("techreborn:implosion_compressor")); + public static final RebornRecipeType INDUSTRIAL_ELECTROLYZER = RecipeManager.newRecipeType(new Identifier("techreborn:industrial_electrolyzer")); + public static final RebornRecipeType INDUSTRIAL_GRINDER = RecipeManager.newRecipeType(INDUSTRIAL_GRINDER_RECIPE_SERDE, new Identifier("techreborn:industrial_grinder")); + public static final RebornRecipeType INDUSTRIAL_SAWMILL = RecipeManager.newRecipeType(INDUSTRIAL_SAWILL_RECIPE_SERDE, new Identifier("techreborn:industrial_sawmill")); + public static final RebornRecipeType RECYCLER = RecipeManager.newRecipeType(new Identifier("techreborn:recycler")); + public static final RebornRecipeType SCRAPBOX = RecipeManager.newRecipeType(new Identifier("techreborn:scrapbox")); + public static final RebornRecipeType VACUUM_FREEZER = RecipeManager.newRecipeType(new Identifier("techreborn:vacuum_freezer")); + public static final RebornRecipeType FLUID_REPLICATOR = RecipeManager.newRecipeType(FLUID_REPLICATOR_RECIPE_SERDE, new Identifier("techreborn:fluid_replicator")); + public static final RebornRecipeType FUSION_REACTOR = RecipeManager.newRecipeType(FUSION_REACTOR_RECIPE_SERDE, new Identifier("techreborn:fusion_reactor")); + public static final RebornRecipeType ROLLING_MACHINE = RecipeManager.newRecipeType(ROLLING_MACHINE_RECIPE_SERDE, new Identifier("techreborn:rolling_machine")); + public static final RebornRecipeType SOLID_CANNING_MACHINE = RecipeManager.newRecipeType(new Identifier("techreborn:solid_canning_machine")); + public static final RebornRecipeType WIRE_MILL = RecipeManager.newRecipeType(new Identifier("techreborn:wire_mill")); public static RebornRecipeType byName(Identifier identifier) { return (RebornRecipeType) Registry.RECIPE_SERIALIZER.get(identifier); diff --git a/src/main/resources/data/techreborn/recipes/rolling_machine/activator_rail.json b/src/main/resources/data/techreborn/recipes/rolling_machine/activator_rail.json index 3ab7e12ef..ccf356e7e 100644 --- a/src/main/resources/data/techreborn/recipes/rolling_machine/activator_rail.json +++ b/src/main/resources/data/techreborn/recipes/rolling_machine/activator_rail.json @@ -21,5 +21,7 @@ "item": "minecraft:activator_rail", "count": 8 } - } + }, + "power": 5, + "time": 250 } \ No newline at end of file diff --git a/src/main/resources/data/techreborn/recipes/rolling_machine/bucket.json b/src/main/resources/data/techreborn/recipes/rolling_machine/bucket.json index 0485d82b7..17254148c 100644 --- a/src/main/resources/data/techreborn/recipes/rolling_machine/bucket.json +++ b/src/main/resources/data/techreborn/recipes/rolling_machine/bucket.json @@ -15,5 +15,7 @@ "item": "minecraft:bucket", "count": 2 } - } + }, + "power": 5, + "time": 250 } \ No newline at end of file diff --git a/src/main/resources/data/techreborn/recipes/rolling_machine/cupronickel_heating_coil.json b/src/main/resources/data/techreborn/recipes/rolling_machine/cupronickel_heating_coil.json index 76a859aab..6df9d47cc 100644 --- a/src/main/resources/data/techreborn/recipes/rolling_machine/cupronickel_heating_coil.json +++ b/src/main/resources/data/techreborn/recipes/rolling_machine/cupronickel_heating_coil.json @@ -18,5 +18,7 @@ "item": "techreborn:cupronickel_heating_coil", "count": 3 } - } + }, + "power": 5, + "time": 250 } \ No newline at end of file diff --git a/src/main/resources/data/techreborn/recipes/rolling_machine/detector_rail.json b/src/main/resources/data/techreborn/recipes/rolling_machine/detector_rail.json index a790ea07d..172832221 100644 --- a/src/main/resources/data/techreborn/recipes/rolling_machine/detector_rail.json +++ b/src/main/resources/data/techreborn/recipes/rolling_machine/detector_rail.json @@ -21,5 +21,7 @@ "item": "minecraft:detector_rail", "count": 8 } - } + }, + "power": 5, + "time": 250 } \ No newline at end of file diff --git a/src/main/resources/data/techreborn/recipes/rolling_machine/heavy_pressure_plate.json b/src/main/resources/data/techreborn/recipes/rolling_machine/heavy_pressure_plate.json index 29b9821ad..2e5c1ae5b 100644 --- a/src/main/resources/data/techreborn/recipes/rolling_machine/heavy_pressure_plate.json +++ b/src/main/resources/data/techreborn/recipes/rolling_machine/heavy_pressure_plate.json @@ -13,5 +13,7 @@ "item": "minecraft:heavy_weighted_pressure_plate", "count": 2 } - } + }, + "power": 5, + "time": 250 } \ No newline at end of file diff --git a/src/main/resources/data/techreborn/recipes/rolling_machine/iron_bars.json b/src/main/resources/data/techreborn/recipes/rolling_machine/iron_bars.json index d78ab1338..02e044950 100644 --- a/src/main/resources/data/techreborn/recipes/rolling_machine/iron_bars.json +++ b/src/main/resources/data/techreborn/recipes/rolling_machine/iron_bars.json @@ -14,5 +14,7 @@ "item": "minecraft:iron_bars", "count": 24 } - } + }, + "power": 5, + "time": 250 } \ No newline at end of file diff --git a/src/main/resources/data/techreborn/recipes/rolling_machine/iron_door.json b/src/main/resources/data/techreborn/recipes/rolling_machine/iron_door.json index 644d0f435..1c744672c 100644 --- a/src/main/resources/data/techreborn/recipes/rolling_machine/iron_door.json +++ b/src/main/resources/data/techreborn/recipes/rolling_machine/iron_door.json @@ -15,5 +15,7 @@ "item": "minecraft:iron_door", "count": 4 } - } + }, + "power": 5, + "time": 250 } \ No newline at end of file diff --git a/src/main/resources/data/techreborn/recipes/rolling_machine/light_pressure_plate.json b/src/main/resources/data/techreborn/recipes/rolling_machine/light_pressure_plate.json index ce89c696a..f7418bcfd 100644 --- a/src/main/resources/data/techreborn/recipes/rolling_machine/light_pressure_plate.json +++ b/src/main/resources/data/techreborn/recipes/rolling_machine/light_pressure_plate.json @@ -13,5 +13,7 @@ "item": "minecraft:light_weighted_pressure_plate", "count": 2 } - } + }, + "power": 5, + "time": 250 } \ No newline at end of file diff --git a/src/main/resources/data/techreborn/recipes/rolling_machine/magnalium_plate.json b/src/main/resources/data/techreborn/recipes/rolling_machine/magnalium_plate.json index 08d828e8d..737f4eccc 100644 --- a/src/main/resources/data/techreborn/recipes/rolling_machine/magnalium_plate.json +++ b/src/main/resources/data/techreborn/recipes/rolling_machine/magnalium_plate.json @@ -18,5 +18,7 @@ "item": "techreborn:magnalium_plate", "count": 3 } - } + }, + "power": 5, + "time": 250 } \ No newline at end of file diff --git a/src/main/resources/data/techreborn/recipes/rolling_machine/minecart.json b/src/main/resources/data/techreborn/recipes/rolling_machine/minecart.json index bdfa7075f..9c632fff7 100644 --- a/src/main/resources/data/techreborn/recipes/rolling_machine/minecart.json +++ b/src/main/resources/data/techreborn/recipes/rolling_machine/minecart.json @@ -14,5 +14,7 @@ "item": "minecraft:minecart", "count": 1 } - } + }, + "power": 5, + "time": 250 } \ No newline at end of file diff --git a/src/main/resources/data/techreborn/recipes/rolling_machine/nichrome_heating_coil.json b/src/main/resources/data/techreborn/recipes/rolling_machine/nichrome_heating_coil.json index 46a9dc8fb..9f76006f6 100644 --- a/src/main/resources/data/techreborn/recipes/rolling_machine/nichrome_heating_coil.json +++ b/src/main/resources/data/techreborn/recipes/rolling_machine/nichrome_heating_coil.json @@ -18,5 +18,7 @@ "item": "techreborn:nichrome_heating_coil", "count": 2 } - } + }, + "power": 5, + "time": 250 } \ No newline at end of file diff --git a/src/main/resources/data/techreborn/recipes/rolling_machine/powered_rail.json b/src/main/resources/data/techreborn/recipes/rolling_machine/powered_rail.json index 52b620a5f..ea2460ca0 100644 --- a/src/main/resources/data/techreborn/recipes/rolling_machine/powered_rail.json +++ b/src/main/resources/data/techreborn/recipes/rolling_machine/powered_rail.json @@ -21,5 +21,7 @@ "item": "minecraft:powered_rail", "count": 8 } - } + }, + "power": 5, + "time": 250 } \ No newline at end of file diff --git a/src/main/resources/data/techreborn/recipes/rolling_machine/rail.json b/src/main/resources/data/techreborn/recipes/rolling_machine/rail.json index 64ea2c899..cff23d8b7 100644 --- a/src/main/resources/data/techreborn/recipes/rolling_machine/rail.json +++ b/src/main/resources/data/techreborn/recipes/rolling_machine/rail.json @@ -18,5 +18,7 @@ "item": "minecraft:rail", "count": 24 } - } + }, + "power": 5, + "time": 250 } \ No newline at end of file