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

@ -114,7 +114,7 @@ public class EndPortalBlock extends NetherPortalBlock implements IRenderTypeable
float rotation = entity.applyRotation(BlockRotation.CLOCKWISE_90); float rotation = entity.applyRotation(BlockRotation.CLOCKWISE_90);
entity.yaw = rotation; entity.yaw = rotation;
} else { } else {
offStep = entity.getMovementDirection() == Direction.NORTH ? 1 : -1; offStep = entity.getMovementDirection() == Direction.NORTH ? -1 : 1;
} }
return checkPos.add(0, 0, offStep); return checkPos.add(0, 0, offStep);
} else { } else {

View file

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

View file

@ -2,7 +2,6 @@ package ru.betterend.recipe.builders;
import java.util.Arrays; import java.util.Arrays;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import net.minecraft.item.ItemConvertible; import net.minecraft.item.ItemConvertible;
@ -15,11 +14,12 @@ import net.minecraft.recipe.RecipeType;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper; import net.minecraft.util.JsonHelper;
import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World; import net.minecraft.world.World;
import ru.betterend.BetterEnd; import ru.betterend.BetterEnd;
import ru.betterend.recipe.EndRecipeManager; import ru.betterend.recipe.EndRecipeManager;
import ru.betterend.rituals.InfusionRitual; import ru.betterend.rituals.InfusionRitual;
import ru.betterend.util.ItemUtil;
public class InfusionRecipe implements Recipe<InfusionRitual> { public class InfusionRecipe implements Recipe<InfusionRitual> {
@ -178,17 +178,31 @@ public class InfusionRecipe implements Recipe<InfusionRitual> {
public InfusionRecipe read(Identifier id, JsonObject json) { public InfusionRecipe read(Identifier id, JsonObject json) {
InfusionRecipe recipe = new InfusionRecipe(id); InfusionRecipe recipe = new InfusionRecipe(id);
recipe.input = Ingredient.fromJson(json.get("input")); recipe.input = Ingredient.fromJson(json.get("input"));
Identifier outId = new Identifier(JsonHelper.getString(json, "output")); JsonObject result = JsonHelper.getObject(json, "result");
recipe.output = new ItemStack(Registry.ITEM.getOrEmpty(outId).orElseThrow(() -> { recipe.output = ItemUtil.fromJsonRecipe(result);
return new IllegalStateException("Item: " + outId + " does not exists!"); if (recipe.output == null) {
})); throw new IllegalStateException("Output item 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 }));
} }
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; return recipe;
} }

View file

@ -0,0 +1,56 @@
package ru.betterend.util;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.google.gson.JsonObject;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.registry.Registry;
import ru.betterend.BetterEnd;
public class ItemUtil {
public static String toStackString(@NotNull ItemStack stack) {
try {
if (stack == null) {
throw new IllegalStateException("Stack can't be null!");
}
Item item = stack.getItem();
return Registry.ITEM.getId(item) + ":" + stack.getCount();
} catch (Exception ex) {
BetterEnd.LOGGER.catching(ex);
}
return "";
}
@Nullable
public static ItemStack fromStackString(String stackString) {
String[] parts = stackString.split(":");
Identifier itemId = new Identifier(parts[0], parts[1]);
Item item = Registry.ITEM.getOrEmpty(itemId).orElse(null);
if (item == null) return null;
if (parts.length > 2) {
return new ItemStack(item, Integer.valueOf(parts[2]));
}
return new ItemStack(item);
}
@Nullable
public static ItemStack fromJsonRecipe(JsonObject recipe) {
if (!recipe.has("item")) return null;
try {
Identifier itemId = new Identifier(JsonHelper.getString(recipe, "item"));
Item item = Registry.ITEM.getOrEmpty(itemId).orElse(null);
if (item == null) return null;
int count = JsonHelper.getInt(recipe, "count", 1);
return new ItemStack(item, count);
} catch (Exception ex) {
BetterEnd.LOGGER.catching(ex);
}
return null;
}
}