Fixes of recipes deserialization from JSON

This commit is contained in:
Aleksey 2020-12-27 15:46:42 +03:00
parent 58124139d5
commit 0a82a8c4be
5 changed files with 99 additions and 26 deletions

View file

@ -5,6 +5,7 @@ import com.google.gson.JsonObject;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemConvertible;
@ -18,11 +19,12 @@ import net.minecraft.tag.Tag;
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 ru.betterend.BetterEnd;
import ru.betterend.recipe.EndRecipeManager;
import ru.betterend.registry.EndBlocks;
import ru.betterend.util.ItemUtil;
import ru.betterend.util.RecipeHelper;
public class AlloyingRecipe implements Recipe<Inventory> {
@ -236,12 +238,12 @@ public class AlloyingRecipe implements Recipe<Inventory> {
JsonArray ingredients = JsonHelper.getArray(json, "ingredients");
Ingredient primaryInput = Ingredient.fromJson(ingredients.get(0));
Ingredient secondaryInput = Ingredient.fromJson(ingredients.get(1));
String resultStr = JsonHelper.getString(json, "result");
JsonObject result = JsonHelper.getObject(json, "result");
String group = JsonHelper.getString(json, "group", "");
Identifier resultId = new Identifier(resultStr);
ItemStack output = new ItemStack(Registry.ITEM.getOrEmpty(resultId).orElseThrow(() -> {
return new IllegalStateException("Item: " + resultStr + " does not exists!");
}));
ItemStack output = ItemUtil.fromJsonRecipe(result);
if (output == null) {
throw new IllegalStateException("Output item does not exists!");
}
float experience = JsonHelper.getFloat(json, "experience", 0.0F);
int smeltTime = JsonHelper.getInt(json, "smelttime", 350);

View file

@ -19,11 +19,12 @@ import net.minecraft.tag.Tag;
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 ru.betterend.BetterEnd;
import ru.betterend.recipe.EndRecipeManager;
import ru.betterend.registry.EndTags;
import ru.betterend.util.ItemUtil;
import ru.betterend.util.RecipeHelper;
public class AnvilSmithingRecipe implements Recipe<Inventory> {
@ -208,12 +209,12 @@ public class AnvilSmithingRecipe implements Recipe<Inventory> {
public static class Serializer implements RecipeSerializer<AnvilSmithingRecipe> {
@Override
public AnvilSmithingRecipe read(Identifier id, JsonObject json) {
Ingredient input = Ingredient.fromJson(JsonHelper.getObject(json, "input"));
String resultStr = JsonHelper.getString(json, "result");
Identifier resultId = new Identifier(resultStr);
ItemStack output = new ItemStack(Registry.ITEM.getOrEmpty(resultId).orElseThrow(() -> {
return new IllegalStateException("Item: " + resultStr + " does not exists!");
}));
Ingredient input = Ingredient.fromJson(json.get("input"));
JsonObject result = JsonHelper.getObject(json, "result");
ItemStack output = ItemUtil.fromJsonRecipe(result);
if (output == null) {
throw new IllegalStateException("Output item does not exists!");
}
int level = JsonHelper.getInt(json, "level", 1);
int damage = JsonHelper.getInt(json, "damage", 1);

View file

@ -2,7 +2,6 @@ package ru.betterend.recipe.builders;
import java.util.Arrays;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.minecraft.item.ItemConvertible;
@ -15,11 +14,12 @@ import net.minecraft.recipe.RecipeType;
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 ru.betterend.BetterEnd;
import ru.betterend.recipe.EndRecipeManager;
import ru.betterend.rituals.InfusionRitual;
import ru.betterend.util.ItemUtil;
public class InfusionRecipe implements Recipe<InfusionRitual> {
@ -178,17 +178,31 @@ public class InfusionRecipe implements Recipe<InfusionRitual> {
public InfusionRecipe read(Identifier id, JsonObject json) {
InfusionRecipe recipe = new InfusionRecipe(id);
recipe.input = Ingredient.fromJson(json.get("input"));
Identifier outId = new Identifier(JsonHelper.getString(json, "output"));
recipe.output = new ItemStack(Registry.ITEM.getOrEmpty(outId).orElseThrow(() -> {
return new IllegalStateException("Item: " + outId + " does not exists!");
}));
recipe.time = JsonHelper.getInt(json, "time", 1);
JsonArray catalysts = JsonHelper.asArray(json, "catalysts");
for (int i = 0; i < catalysts.size(); i++) {
ItemStack stack = new ItemStack(Registry.ITEM.getOrEmpty(outId).orElse(null));
recipe.catalysts[i] = Ingredient.ofStacks(
Arrays.stream(new ItemStack[] { stack }));
JsonObject result = JsonHelper.getObject(json, "result");
recipe.output = ItemUtil.fromJsonRecipe(result);
if (recipe.output == null) {
throw new IllegalStateException("Output item does not exists!");
}
recipe.time = JsonHelper.getInt(json, "time", 1);
JsonObject catalysts = JsonHelper.asObject(json, "catalysts");
ItemStack catalyst = ItemUtil.fromStackString(JsonHelper.getString(catalysts, "north", ""));
recipe.catalysts[0] = Ingredient.ofStacks(Arrays.stream(new ItemStack[] { catalyst }));
catalyst = ItemUtil.fromStackString(JsonHelper.getString(catalysts, "north_east", ""));
recipe.catalysts[1] = Ingredient.ofStacks(Arrays.stream(new ItemStack[] { catalyst }));
catalyst = ItemUtil.fromStackString(JsonHelper.getString(catalysts, "east", ""));
recipe.catalysts[2] = Ingredient.ofStacks(Arrays.stream(new ItemStack[] { catalyst }));
catalyst = ItemUtil.fromStackString(JsonHelper.getString(catalysts, "south_east", ""));
recipe.catalysts[3] = Ingredient.ofStacks(Arrays.stream(new ItemStack[] { catalyst }));
catalyst = ItemUtil.fromStackString(JsonHelper.getString(catalysts, "south", ""));
recipe.catalysts[4] = Ingredient.ofStacks(Arrays.stream(new ItemStack[] { catalyst }));
catalyst = ItemUtil.fromStackString(JsonHelper.getString(catalysts, "south_west", ""));
recipe.catalysts[5] = Ingredient.ofStacks(Arrays.stream(new ItemStack[] { catalyst }));
catalyst = ItemUtil.fromStackString(JsonHelper.getString(catalysts, "west", ""));
recipe.catalysts[6] = Ingredient.ofStacks(Arrays.stream(new ItemStack[] { catalyst }));
catalyst = ItemUtil.fromStackString(JsonHelper.getString(catalysts, "north_west", ""));
recipe.catalysts[7] = Ingredient.ofStacks(Arrays.stream(new ItemStack[] { catalyst }));
return recipe;
}