Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.halestormxv.mysterium.recipes;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonObject;
- 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.JSONUtils;
- import net.minecraft.util.ResourceLocation;
- import net.minecraft.util.registry.Registry;
- import net.minecraftforge.registries.ForgeRegistryEntry;
- public class LunarForgeRecipeSerializer<T extends LunarForgeRecipe> extends ForgeRegistryEntry<IRecipeSerializer<?>> implements IRecipeSerializer<T> {
- private final int cookTime;
- private final LunarForgeRecipeSerializer.IFactory<T> factory;
- public LunarForgeRecipeSerializer(LunarForgeRecipeSerializer.IFactory<T> factoryIn, int timeIn) {
- this.cookTime = timeIn;
- this.factory = factoryIn;
- }
- @Override
- public T read(ResourceLocation recipeId, JsonObject json) {
- JsonElement jsonelement = JSONUtils.isJsonArray(json, "ingredient") ? JSONUtils.getJsonArray(json, "ingredient") : JSONUtils.getJsonObject(json, "ingredient");
- Ingredient ingredient = Ingredient.deserialize(jsonelement);
- //RESULT
- if (!json.has("result"))
- throw new com.google.gson.JsonSyntaxException("Missing result, expected to find a string or object");
- ItemStack resultStack;
- if (json.get("result").isJsonObject())
- resultStack = ShapedRecipe.deserializeItem(JSONUtils.getJsonObject(json, "result"));
- else {
- String s1 = JSONUtils.getString(json, "result");
- ResourceLocation resourcelocation = new ResourceLocation(s1);
- resultStack = new ItemStack(Registry.ITEM.getValue(resourcelocation).orElseThrow(() -> new IllegalStateException("Item: " + s1 + " does not exist")));
- }
- float f = JSONUtils.getFloat(json, "experience", 0.0F);
- int i = JSONUtils.getInt(json, "cookingtime", this.cookTime);
- return this.factory.create(recipeId, ingredient, resultStack, f, i);
- }
- @Override
- public T read(ResourceLocation recipeId, PacketBuffer buffer) {
- Ingredient ingredient = Ingredient.read(buffer);
- ItemStack itemstack = buffer.readItemStack();
- float f = buffer.readFloat();
- int i = buffer.readVarInt();
- return this.factory.create(recipeId, ingredient, itemstack, f, i);
- }
- @Override
- public void write(PacketBuffer buffer, T recipe) {
- recipe.getInput().write(buffer);
- buffer.writeItemStack(recipe.getRecipeOutput());
- buffer.writeFloat(recipe.xp);
- buffer.writeVarInt(recipe.cookTime);
- }
- public interface IFactory<T extends LunarForgeRecipe> {
- T create(ResourceLocation id, Ingredient ingredientIn, ItemStack outputIn, float experienceIn, int timeIn);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement