diff --git a/src/main/java/knightminer/inspirations/common/network/CauldronContentUpatePacket.java b/src/main/java/knightminer/inspirations/common/network/CauldronContentUpatePacket.java index cede8ee5..ee5dc3a1 100644 --- a/src/main/java/knightminer/inspirations/common/network/CauldronContentUpatePacket.java +++ b/src/main/java/knightminer/inspirations/common/network/CauldronContentUpatePacket.java @@ -30,7 +30,7 @@ public CauldronContentUpatePacket(PacketBuffer buffer) { @Override public void encode(PacketBuffer buffer) { buffer.writeBlockPos(pos); - CauldronContentTypes.write(contents, buffer); + contents.write(buffer); } @Override diff --git a/src/main/java/knightminer/inspirations/library/recipe/cauldron/CauldronContentTypes.java b/src/main/java/knightminer/inspirations/library/recipe/cauldron/CauldronContentTypes.java index 4c78aa6f..256cf345 100644 --- a/src/main/java/knightminer/inspirations/library/recipe/cauldron/CauldronContentTypes.java +++ b/src/main/java/knightminer/inspirations/library/recipe/cauldron/CauldronContentTypes.java @@ -25,13 +25,14 @@ import net.minecraftforge.common.util.Lazy; import javax.annotation.Nullable; +import java.util.function.BiFunction; /** * Registry that helps with registering, serializing, and deserializing cauldron properties */ public class CauldronContentTypes { private static final ResourceLocation UNREGISTERED = Inspirations.getResource("null"); - private static final String KEY_TYPE = "type"; + public static final String KEY_TYPE = "type"; private static final BiMap> TYPES = HashBiMap.create(); /* Public constants */ @@ -102,45 +103,34 @@ public static ResourceLocation getName(CauldronContentType type) { } /** - * Converts the given contents to JSON - * @param contents Contents - * @return JSON + * Simple helper function to make the generics work out + * @param type Content type + * @param data Data being read + * @param parser Parses data into contents */ - public static JsonObject toJson(ICauldronContents contents) { - JsonObject json = new JsonObject(); - CauldronContentType type = contents.getType(); - json.addProperty(KEY_TYPE, getName(type).toString()); - type.write(contents, json); - return json; + private static ICauldronContents read(CauldronContentType type, D data, BiFunction,D,T> parser) { + T value = parser.apply(type, data); + if (value == null) { + return DEFAULT.get(); + } + return type.of(value); } /** * Reads the cauldron contents from JSON * @param json JSON to read * @return Cauldron contents + * @throws JsonSyntaxException If the type is missing or the data invalid */ public static ICauldronContents read(JsonObject json) { ResourceLocation location = new ResourceLocation(JSONUtils.getString(json, KEY_TYPE)); CauldronContentType type = get(location); if (type != null) { - return type.read(json); + return read(type, json, CauldronContentType::read); } throw new JsonSyntaxException("Invalid cauldron content type '" + location + "'"); } - /** - * Writes the given contents to NBT - * @param contents Contents to write - * @return Contents written to NBT - */ - public static CompoundNBT toNbt(ICauldronContents contents) { - CompoundNBT nbt = new CompoundNBT(); - CauldronContentType type = contents.getType(); - nbt.putString(KEY_TYPE, getName(type).toString()); - type.write(contents, nbt); - return nbt; - } - /** * Reads the given contents from NBT * @param nbt NBT contents @@ -151,30 +141,17 @@ public static ICauldronContents read(CompoundNBT nbt) { ResourceLocation location = new ResourceLocation(nbt.getString(KEY_TYPE)); CauldronContentType type = get(location); if (type != null) { - ICauldronContents contents = type.read(nbt); - if (contents != null) { - return contents; - } + return read(type, nbt, CauldronContentType::read); } } return CauldronContentTypes.DEFAULT.get(); } - /** - * Writes the given contents to NBT - * @param contents Contents to write - * @param buffer Buffer instance - */ - public static void write(ICauldronContents contents, PacketBuffer buffer) { - CauldronContentType type = contents.getType(); - buffer.writeResourceLocation(getName(type)); - type.write(contents, buffer); - } - /** * Reads the given contents from NBT * @param buffer Buffer instance * @return Cauldron contents + * @throws DecoderException if the type is missing or the data invalids */ public static ICauldronContents read(PacketBuffer buffer) { ResourceLocation name = buffer.readResourceLocation(); @@ -182,6 +159,6 @@ public static ICauldronContents read(PacketBuffer buffer) { if (type == null) { throw new DecoderException("Invalid type name '" + name + "'"); } - return type.read(buffer); + return read(type, buffer, CauldronContentType::read); } } diff --git a/src/main/java/knightminer/inspirations/library/recipe/cauldron/contents/CauldronContentType.java b/src/main/java/knightminer/inspirations/library/recipe/cauldron/contents/CauldronContentType.java index 65b6418a..510214b6 100644 --- a/src/main/java/knightminer/inspirations/library/recipe/cauldron/contents/CauldronContentType.java +++ b/src/main/java/knightminer/inspirations/library/recipe/cauldron/contents/CauldronContentType.java @@ -1,19 +1,18 @@ package knightminer.inspirations.library.recipe.cauldron.contents; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.google.gson.JsonSyntaxException; +import com.google.gson.JsonPrimitive; import io.netty.handler.codec.DecoderException; -import io.netty.handler.codec.EncoderException; import knightminer.inspirations.Inspirations; import knightminer.inspirations.library.recipe.cauldron.CauldronContentTypes; import knightminer.inspirations.recipes.recipe.cauldron.contents.CauldronContents; import net.minecraft.client.util.ITooltipFlag; import net.minecraft.nbt.CompoundNBT; import net.minecraft.network.PacketBuffer; -import net.minecraft.util.JSONUtils; import net.minecraft.util.ResourceLocation; import net.minecraft.util.text.ITextComponent; -import net.minecraftforge.common.util.Constants.NBT; +import slimeknights.mantle.util.JsonHelper; import javax.annotation.Nullable; import java.util.HashMap; @@ -142,15 +141,7 @@ public String getModId(T value) { public abstract String getName(T value); /** - * Gets the entry for a given value - * @param name Name - * @return Entry, or null if missing - */ - @Nullable - public abstract T getEntry(String name); - - /** - * Gets the key used for JSON and NBT + * Gets the base key used for ingredient JSON * @return JSON and NBT key */ public String getKey() { @@ -163,77 +154,67 @@ public String getKey() { * @return Read value */ @Nullable - public ICauldronContents read(CompoundNBT tag) { - if (tag.contains(getKey(), NBT.TAG_STRING)) { - T value = getEntry(tag.getString(getKey())); - if (value != null) { - return of(value); - } - } - return null; - } + public abstract T read(CompoundNBT tag); /** - * Reads the given type from JSON - * @param json JSON object - * @return Read value= - * @throws com.google.gson.JsonSyntaxException if the JSON is invalid + * Writes the given type to NBT + * @param tag NBT tag */ - public ICauldronContents read(JsonObject json) { - String name = JSONUtils.getString(json, getKey()); - T value = getEntry(name); - if (value == null) { - throw new JsonSyntaxException("Invalid name '" + name + "' for type '" + CauldronContentTypes.getName(this) + "'"); - } - return of(value); + public void write(T value, CompoundNBT tag) { + tag.putString(getKey(), getName(value)); } /** - * Reads the given type from the packet buffer - * @param buffer Packet buffer - * @return Read value - * @throws DecoderException if the type is invalid + * Gets the value from a JSON element + * @param element JSON element + * @param key Key to get + * @return Value */ - public ICauldronContents read(PacketBuffer buffer) { - String name = buffer.readString(Short.MAX_VALUE); - T value = getEntry(name); - if (value != null) { - return of(value); - } - throw new DecoderException("Invalid name '" + name + "' for type " + this); + public abstract T getValue(JsonElement element, String key); + + /** + * Writes the given type into a standalone JSON element + * @param value Value to write + * @return JsonElement representing this value + */ + public JsonElement toJson(T value) { + return new JsonPrimitive(getName(value)); } /** - * Writes the given type to NBT - * @param contents Contents to write - * @param tag NBT tag + * Reads the given type from JSON + * @param json JSON object + * @return Read value + * @throws com.google.gson.JsonSyntaxException if the JSON is invalid */ - public void write(ICauldronContents contents, CompoundNBT tag) { - contents.get(this).ifPresent(val -> tag.putString(getKey(), getName(val))); + public T read(JsonObject json) { + String key = getKey(); + return getValue(JsonHelper.getElement(json, key), key); } /** * Writes the given type to JSON - * @param contents Contents to write - * @param json JSON object + * @param value Value to write + * @param json JSON object */ - public void write(ICauldronContents contents, JsonObject json) { - contents.get(this).ifPresent(val -> json.addProperty(getKey(), getName(val))); + public void write(T value, JsonObject json) { + json.addProperty(getKey(), getName(value)); } + /** + * Reads the given type from the packet buffer + * @param buffer Packet buffer + * @return Read value + * @throws DecoderException if the type is invalid + */ + public abstract T read(PacketBuffer buffer); + /** * Writes the given type to the packet buffer - * @param contents Contents to write + * @param value Value to write * @param buffer Packet buffer */ - public void write(ICauldronContents contents, PacketBuffer buffer) { - Optional value = contents.get(this); - if (value.isPresent()) { - buffer.writeString(getName(value.get())); - } else { - throw new EncoderException("Invalid type for contents argument"); - } - } + public abstract void write(T value, PacketBuffer buffer); @Override public String toString() { diff --git a/src/main/java/knightminer/inspirations/library/recipe/cauldron/contents/ICauldronContents.java b/src/main/java/knightminer/inspirations/library/recipe/cauldron/contents/ICauldronContents.java index 8ef78daa..ca28b2a9 100644 --- a/src/main/java/knightminer/inspirations/library/recipe/cauldron/contents/ICauldronContents.java +++ b/src/main/java/knightminer/inspirations/library/recipe/cauldron/contents/ICauldronContents.java @@ -1,8 +1,11 @@ package knightminer.inspirations.library.recipe.cauldron.contents; +import com.google.gson.JsonObject; import knightminer.inspirations.library.recipe.cauldron.CauldronContentTypes; import net.minecraft.client.util.ITooltipFlag; import net.minecraft.fluid.Fluids; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.network.PacketBuffer; import net.minecraft.util.ResourceLocation; import net.minecraft.util.text.ITextComponent; @@ -67,12 +70,33 @@ default String getModId() { } /** - * Gets the unique name relative to the ingredient type + * Gets the unique name relative to the ingredient type, for differentiating it in JEI * @return Resource searching name */ String getName(); + /* Serializing */ + + /** + * Writes the given contents to JSON + * @return JSON contents + */ + JsonObject toJson(); + + /** + * Writes the given contents to NBT + * @return NBT contents + */ + CompoundNBT toNBT(); + + /** + * Writes the given contents to the packet buffer + * @param buffer Buffer instance + */ + void write(PacketBuffer buffer); + + /* Mapping */ /** diff --git a/src/main/java/knightminer/inspirations/library/recipe/cauldron/contents/NamedContentType.java b/src/main/java/knightminer/inspirations/library/recipe/cauldron/contents/NamedContentType.java index ac475778..e5e8b04f 100644 --- a/src/main/java/knightminer/inspirations/library/recipe/cauldron/contents/NamedContentType.java +++ b/src/main/java/knightminer/inspirations/library/recipe/cauldron/contents/NamedContentType.java @@ -1,25 +1,19 @@ package knightminer.inspirations.library.recipe.cauldron.contents; +import com.google.gson.JsonElement; +import com.google.gson.JsonSyntaxException; +import net.minecraft.nbt.CompoundNBT; import net.minecraft.util.IStringSerializable; +import net.minecraft.util.JSONUtils; +import net.minecraftforge.common.util.Constants.NBT; import javax.annotation.Nullable; -import java.util.function.Function; /** - * Cauldron content type based on an {@link IStringSerializable} object, typically an enum + * Cauldron content type based on {@link IStringSerializable}, typically an enum * @param Type class */ public abstract class NamedContentType extends CauldronContentType { - private final Function lookup; - - /** - * Creates a new type instance - * @param lookup Function to lookup a value from a string - */ - public NamedContentType(Function lookup) { - this.lookup = lookup; - } - /** * Gets the name of the given value for writing to JSON and NBT * @param value Value @@ -31,13 +25,35 @@ public String getName(T value) { } /** - * Gets the entry for a given value - * @param name Name - * @return Entry, or null if missing + * Gets an enum value from a string + * @param name String + * @return Value, or null if the name does not match + */ + @Nullable + protected abstract T getValue(String name); + + /** + * Gets the value from a JSON element + * @param element JSON element + * @param key Key to get + * @return Value */ @Override + public T getValue(JsonElement element, String key) { + String name = JSONUtils.getString(element, key); + T value = getValue(name); + if (value == null) { + throw new JsonSyntaxException("Invalid value '" + name + "' for enum"); + } + return value; + } + @Nullable - public T getEntry(String name) { - return lookup.apply(name); + @Override + public T read(CompoundNBT tag) { + if (tag.contains(getKey(), NBT.TAG_STRING)) { + return getValue(tag.getString(getKey())); + } + return null; } } diff --git a/src/main/java/knightminer/inspirations/library/recipe/cauldron/contents/RegistryContentType.java b/src/main/java/knightminer/inspirations/library/recipe/cauldron/contents/RegistryContentType.java index 7b2fa570..d7bc81c5 100644 --- a/src/main/java/knightminer/inspirations/library/recipe/cauldron/contents/RegistryContentType.java +++ b/src/main/java/knightminer/inspirations/library/recipe/cauldron/contents/RegistryContentType.java @@ -1,11 +1,19 @@ package knightminer.inspirations.library.recipe.cauldron.contents; +import com.google.gson.JsonElement; +import com.google.gson.JsonSyntaxException; +import io.netty.handler.codec.DecoderException; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.network.PacketBuffer; +import net.minecraft.util.JSONUtils; import net.minecraft.util.ResourceLocation; +import net.minecraftforge.common.util.Constants.NBT; import net.minecraftforge.registries.IForgeRegistry; import net.minecraftforge.registries.IForgeRegistryEntry; import javax.annotation.Nullable; import java.util.Objects; +import java.util.function.Function; /** * Content type that mirrors a Forge registry @@ -27,23 +35,62 @@ public String getName(T value) { return Objects.requireNonNull(value.getRegistryName()).toString(); } + @Override + public String getModId(T value) { + return Objects.requireNonNull(value.getRegistryName()).getNamespace(); + } + + + /* Serializing and deserializing */ + + /** + * Gets a value from a registry name + * @param name Value to fetch + * @return Registry value, null if missing + */ @Nullable + private T getValue(ResourceLocation name) { + if (registry.containsKey(name)) { + return registry.getValue(name); + } + return null; + } + + /** + * Gets a nonnull value from a registry name, throwing the exception if missing + * @param name Value to fetch + * @param exception Exception function + * @return Registry value + */ + private T getValue(ResourceLocation name, Function exception) { + T value = getValue(name); + if (value != null) { + return value; + } + throw exception.apply("Invalid value '" + name + "' for registry " + registry.getRegistryName()); + } + @Override - public T getEntry(String name) { - ResourceLocation location = ResourceLocation.tryCreate(name); - if (location != null) { - // swap default value for null - T value = registry.getValue(location); - ResourceLocation defaultKey = registry.getDefaultKey(); - if (value != null && defaultKey != null && !defaultKey.equals(value.getRegistryName())) { - return value; - } + public T getValue(JsonElement element, String key) { + return getValue(new ResourceLocation(JSONUtils.getString(element, key)), JsonSyntaxException::new); + } + + @Nullable + @Override + public T read(CompoundNBT tag) { + if (tag.contains(getKey(), NBT.TAG_STRING)) { + return getValue(new ResourceLocation(tag.getString(getKey()))); } return null; } @Override - public String getModId(T value) { - return Objects.requireNonNull(value.getRegistryName()).getNamespace(); + public T read(PacketBuffer buffer) { + return getValue(buffer.readResourceLocation(), DecoderException::new); + } + + @Override + public void write(T value, PacketBuffer buffer) { + buffer.writeResourceLocation(Objects.requireNonNull(value.getRegistryName())); } } diff --git a/src/main/java/knightminer/inspirations/library/recipe/cauldron/ingredient/ContentMatchIngredient.java b/src/main/java/knightminer/inspirations/library/recipe/cauldron/ingredient/ContentMatchIngredient.java index feeb86fd..cc162930 100644 --- a/src/main/java/knightminer/inspirations/library/recipe/cauldron/ingredient/ContentMatchIngredient.java +++ b/src/main/java/knightminer/inspirations/library/recipe/cauldron/ingredient/ContentMatchIngredient.java @@ -5,11 +5,9 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonSyntaxException; -import io.netty.handler.codec.DecoderException; import knightminer.inspirations.library.recipe.cauldron.contents.CauldronContentType; import knightminer.inspirations.library.recipe.cauldron.contents.ICauldronContents; import net.minecraft.network.PacketBuffer; -import net.minecraft.util.JSONUtils; import slimeknights.mantle.util.JsonHelper; import java.util.ArrayList; @@ -79,13 +77,13 @@ protected boolean testValue(T value) { @Override protected void write(JsonObject json) { - json.addProperty(serializer.type.getKey(), serializer.type.getName(value)); + json.add(serializer.type.getKey(), serializer.type.toJson(value)); } @Override protected void write(PacketBuffer buffer) { buffer.writeVarInt(1); - buffer.writeString(serializer.type.getName(value)); + serializer.type.write(value, buffer); } @Override @@ -115,7 +113,7 @@ protected boolean testValue(T value) { protected void write(JsonObject json) { JsonArray array = new JsonArray(); for (T value : values) { - array.add(serializer.type.getName(value)); + array.add(serializer.type.toJson(value)); } json.add(serializer.type.getKey(), array); } @@ -124,7 +122,7 @@ protected void write(JsonObject json) { protected void write(PacketBuffer buffer) { buffer.writeVarInt(values.size()); for (T value : values) { - buffer.writeString(serializer.type.getName(value)); + serializer.type.write(value, buffer); } } @@ -173,34 +171,12 @@ public ContentMatchIngredient of(Collection values) { return new Multi<>(this, ImmutableSet.copyOf(values)); } - /** - * Helper to get a single value from a string - * @param name Value name - * @return Content match ingredient - */ - private ContentMatchIngredient getSingle(String name) { - T value = type.getEntry(name); - if (value == null) { - throw new JsonSyntaxException("Invalid value '" + name + "' for type " + type); - } - return of(value); - } - /** * Helper to get a list of values from a string - * @param names Value names - * @param exception Exception function + * @param values List of values * @return Content match ingredient */ - private ContentMatchIngredient getList(List names, Function exception) { - List values = new ArrayList<>(); - for (String name : names) { - T value = type.getEntry(name); - if (value == null) { - throw exception.apply("Invalid value '" + name + "' for type " + type); - } - values.add(value); - } + private ContentMatchIngredient getList(List values) { // if a single element, save effort if (values.size() == 1) { return of(values.get(0)); @@ -211,17 +187,18 @@ private ContentMatchIngredient getList(List names, Function read(JsonObject json) { // actual element can be a string or array - JsonElement element = JsonHelper.getElement(json, this.type.getKey()); + String key = this.type.getKey(); + JsonElement element = JsonHelper.getElement(json, key); // single name if (element.isJsonPrimitive()) { - return getSingle(element.getAsString()); + return of(type.getValue(element, key)); } // array of names if (element.isJsonArray()) { - List names = JsonHelper.parseList(element.getAsJsonArray(), "names", JSONUtils::getString, Function.identity()); - return getList(names, JsonSyntaxException::new); + List values = JsonHelper.parseList(element.getAsJsonArray(), "names", type::getValue, Function.identity()); + return getList(values); } // error @@ -237,13 +214,13 @@ public void write(ContentMatchIngredient ingredient, JsonObject json) { public ContentMatchIngredient read(PacketBuffer buffer) { // read all values from the buffer int size = buffer.readVarInt(); - List names = new ArrayList<>(size); + List values = new ArrayList<>(size); for (int i = 0; i < size; i++) { - names.add(buffer.readString()); + values.add(type.read(buffer)); } // parse list - return getList(names, DecoderException::new); + return getList(values); } @Override diff --git a/src/main/java/knightminer/inspirations/library/recipe/cauldron/recipe/CauldronRecipe.java b/src/main/java/knightminer/inspirations/library/recipe/cauldron/recipe/CauldronRecipe.java index c22960bc..8bab6dc0 100644 --- a/src/main/java/knightminer/inspirations/library/recipe/cauldron/recipe/CauldronRecipe.java +++ b/src/main/java/knightminer/inspirations/library/recipe/cauldron/recipe/CauldronRecipe.java @@ -247,7 +247,7 @@ public void write(PacketBuffer buffer, CauldronRecipe recipe) { buffer.writeBoolean(recipe.copyNBT); if (recipe.outputContents != null) { buffer.writeBoolean(true); - CauldronContentTypes.write(recipe.outputContents, buffer); + recipe.outputContents.write(buffer); } else { buffer.writeBoolean(false); } diff --git a/src/main/java/knightminer/inspirations/library/recipe/cauldron/recipe/CauldronRecipeBuilder.java b/src/main/java/knightminer/inspirations/library/recipe/cauldron/recipe/CauldronRecipeBuilder.java index 56289327..c6dd2d97 100644 --- a/src/main/java/knightminer/inspirations/library/recipe/cauldron/recipe/CauldronRecipeBuilder.java +++ b/src/main/java/knightminer/inspirations/library/recipe/cauldron/recipe/CauldronRecipeBuilder.java @@ -375,7 +375,7 @@ public void serialize(JsonObject json) { } } if (newContents != null) { - outputJson.add("contents", CauldronContentTypes.toJson(newContents)); + outputJson.add("contents", newContents.toJson()); } if (levelUpdate != LevelUpdate.IDENTITY) { outputJson.add("level", levelUpdate.toJson()); diff --git a/src/main/java/knightminer/inspirations/library/recipe/cauldron/recipe/CauldronTransform.java b/src/main/java/knightminer/inspirations/library/recipe/cauldron/recipe/CauldronTransform.java index bc099ff8..8dc08fe7 100644 --- a/src/main/java/knightminer/inspirations/library/recipe/cauldron/recipe/CauldronTransform.java +++ b/src/main/java/knightminer/inspirations/library/recipe/cauldron/recipe/CauldronTransform.java @@ -167,7 +167,8 @@ public void write(PacketBuffer buffer, CauldronTransform recipe) { CauldronIngredients.write(recipe.ingredient, buffer); buffer.writeEnumValue(recipe.temperature); recipe.level.write(buffer); - CauldronContentTypes.write(recipe.outputContents == null ? CauldronContentTypes.DEFAULT.get() : recipe.outputContents, buffer); + ICauldronContents contents = recipe.outputContents == null ? CauldronContentTypes.DEFAULT.get() : recipe.outputContents; + contents.write(buffer); buffer.writeVarInt(recipe.time); buffer.writeResourceLocation(Objects.requireNonNull(recipe.sound.getRegistryName())); } diff --git a/src/main/java/knightminer/inspirations/library/recipe/cauldron/recipe/CauldronTransformBuilder.java b/src/main/java/knightminer/inspirations/library/recipe/cauldron/recipe/CauldronTransformBuilder.java index 783fdbaa..0df52eac 100644 --- a/src/main/java/knightminer/inspirations/library/recipe/cauldron/recipe/CauldronTransformBuilder.java +++ b/src/main/java/knightminer/inspirations/library/recipe/cauldron/recipe/CauldronTransformBuilder.java @@ -2,7 +2,6 @@ import com.google.gson.JsonObject; import knightminer.inspirations.library.recipe.RecipeSerializers; -import knightminer.inspirations.library.recipe.cauldron.CauldronContentTypes; import knightminer.inspirations.library.recipe.cauldron.CauldronIngredients; import knightminer.inspirations.library.recipe.cauldron.contents.ICauldronContents; import knightminer.inspirations.library.recipe.cauldron.ingredient.ICauldronIngredient; @@ -174,7 +173,7 @@ public void serialize(JsonObject json) { if (temperature != TemperaturePredicate.ANY) { json.addProperty("temperature", temperature.getName()); } - json.add("output", CauldronContentTypes.toJson(output)); + json.add("output", output.toJson()); json.addProperty("time", time); if (sound != null) { json.addProperty("sound", Objects.requireNonNull(sound.getRegistryName()).toString()); diff --git a/src/main/java/knightminer/inspirations/recipes/recipe/cauldron/contents/CauldronContents.java b/src/main/java/knightminer/inspirations/recipes/recipe/cauldron/contents/CauldronContents.java index 79d4428f..514151e7 100644 --- a/src/main/java/knightminer/inspirations/recipes/recipe/cauldron/contents/CauldronContents.java +++ b/src/main/java/knightminer/inspirations/recipes/recipe/cauldron/contents/CauldronContents.java @@ -1,8 +1,12 @@ package knightminer.inspirations.recipes.recipe.cauldron.contents; +import com.google.gson.JsonObject; +import knightminer.inspirations.library.recipe.cauldron.CauldronContentTypes; import knightminer.inspirations.library.recipe.cauldron.contents.CauldronContentType; import knightminer.inspirations.library.recipe.cauldron.contents.ICauldronContents; import net.minecraft.client.util.ITooltipFlag; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.network.PacketBuffer; import net.minecraft.util.ResourceLocation; import net.minecraft.util.text.ITextComponent; @@ -43,6 +47,30 @@ public CauldronContentType getType() { } + /* Serializing */ + + @Override + public JsonObject toJson() { + JsonObject json = new JsonObject(); + json.addProperty(CauldronContentTypes.KEY_TYPE, CauldronContentTypes.getName(type).toString()); + type.write(value, json); + return json; + } + + @Override + public CompoundNBT toNBT() { + CompoundNBT nbt = new CompoundNBT(); + nbt.putString(CauldronContentTypes.KEY_TYPE, CauldronContentTypes.getName(type).toString()); + type.write(value, nbt); + return nbt; + } + + @Override + public void write(PacketBuffer buffer) { + buffer.writeResourceLocation(CauldronContentTypes.getName(type)); + type.write(value, buffer); + } + /* Display */ @Override diff --git a/src/main/java/knightminer/inspirations/recipes/recipe/cauldron/contents/ColorContentType.java b/src/main/java/knightminer/inspirations/recipes/recipe/cauldron/contents/ColorContentType.java index 2d436d01..e42b5874 100644 --- a/src/main/java/knightminer/inspirations/recipes/recipe/cauldron/contents/ColorContentType.java +++ b/src/main/java/knightminer/inspirations/recipes/recipe/cauldron/contents/ColorContentType.java @@ -1,20 +1,22 @@ package knightminer.inspirations.recipes.recipe.cauldron.contents; -import io.netty.handler.codec.DecoderException; +import com.google.gson.JsonElement; +import com.google.gson.JsonSyntaxException; import knightminer.inspirations.Inspirations; import knightminer.inspirations.library.recipe.cauldron.contents.CauldronContentType; -import knightminer.inspirations.library.recipe.cauldron.contents.ICauldronContents; import net.minecraft.client.util.ITooltipFlag; +import net.minecraft.nbt.CompoundNBT; import net.minecraft.network.PacketBuffer; +import net.minecraft.util.JSONUtils; import net.minecraft.util.ResourceLocation; import net.minecraft.util.Util; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TextFormatting; import net.minecraft.util.text.TranslationTextComponent; +import net.minecraftforge.common.util.Constants.NBT; import javax.annotation.Nullable; import java.util.List; -import java.util.Optional; /** * Content type for colors in the cauldron @@ -54,11 +56,15 @@ public String getName(Integer value) { return getColorString(value); } + /** + * Gets an color from a string + * @param str Color string + * @return Color integer + */ @Nullable - @Override - public Integer getEntry(String name) { + private Integer getValue(String str) { try { - return Integer.parseInt(name, 16); + return Integer.parseInt(str, 16); } catch (NumberFormatException e) { return null; @@ -66,20 +72,35 @@ public Integer getEntry(String name) { } @Override - public ICauldronContents read(PacketBuffer buffer) { - return of(buffer.readInt()); + public Integer getValue(JsonElement element, String key) { + String text = JSONUtils.getString(element, key); + Integer value = getValue(text); + if (value != null) { + return value; + } + throw new JsonSyntaxException("Invalid color value '" + text + "'"); } + @Nullable @Override - public void write(ICauldronContents contents, PacketBuffer buffer) { - Optional optional = contents.get(this); - if (optional.isPresent()) { - buffer.writeInt(optional.get()); - } else { - throw new DecoderException("Invalid class type for cauldron contents"); + public Integer read(CompoundNBT tag) { + if (tag.contains(getKey(), NBT.TAG_STRING)) { + return getValue(tag.getString(getKey())); } + return null; + } + + @Override + public Integer read(PacketBuffer buffer) { + return buffer.readInt(); + } + + @Override + public void write(Integer color, PacketBuffer buffer) { + buffer.writeInt(color); } + /* Display */ @Override diff --git a/src/main/java/knightminer/inspirations/recipes/recipe/cauldron/contents/CustomContentType.java b/src/main/java/knightminer/inspirations/recipes/recipe/cauldron/contents/CustomContentType.java index 4f63f112..586d0686 100644 --- a/src/main/java/knightminer/inspirations/recipes/recipe/cauldron/contents/CustomContentType.java +++ b/src/main/java/knightminer/inspirations/recipes/recipe/cauldron/contents/CustomContentType.java @@ -1,10 +1,16 @@ package knightminer.inspirations.recipes.recipe.cauldron.contents; +import com.google.gson.JsonElement; +import com.google.gson.JsonSyntaxException; import knightminer.inspirations.library.recipe.cauldron.contents.CauldronContentType; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.network.PacketBuffer; +import net.minecraft.util.JSONUtils; import net.minecraft.util.ResourceLocation; import net.minecraft.util.Util; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TranslationTextComponent; +import net.minecraftforge.common.util.Constants.NBT; import javax.annotation.Nullable; @@ -27,14 +33,39 @@ public String getModId(ResourceLocation value) { return value.getNamespace(); } + /* Serializing */ + @Override public String getName(ResourceLocation value) { return value.toString(); } + @Override + public ResourceLocation getValue(JsonElement element, String key) { + String name = JSONUtils.getString(element, key); + ResourceLocation location = ResourceLocation.tryCreate(name); + if (location != null) { + return location; + } + throw new JsonSyntaxException("Invalid resource location '" + name + "'"); + } + @Nullable @Override - public ResourceLocation getEntry(String name) { - return ResourceLocation.tryCreate(name); + public ResourceLocation read(CompoundNBT tag) { + if (tag.contains(getKey(), NBT.TAG_STRING)) { + return ResourceLocation.tryCreate(tag.getString(getKey())); + } + return null; + } + + @Override + public ResourceLocation read(PacketBuffer buffer) { + return buffer.readResourceLocation(); + } + + @Override + public void write(ResourceLocation value, PacketBuffer buffer) { + buffer.writeResourceLocation(value); } } diff --git a/src/main/java/knightminer/inspirations/recipes/recipe/cauldron/contents/DyeContentType.java b/src/main/java/knightminer/inspirations/recipes/recipe/cauldron/contents/DyeContentType.java index 81ac07a4..3cd17626 100644 --- a/src/main/java/knightminer/inspirations/recipes/recipe/cauldron/contents/DyeContentType.java +++ b/src/main/java/knightminer/inspirations/recipes/recipe/cauldron/contents/DyeContentType.java @@ -4,11 +4,13 @@ import knightminer.inspirations.library.recipe.cauldron.contents.NamedContentType; import net.minecraft.client.util.ITooltipFlag; import net.minecraft.item.DyeColor; +import net.minecraft.network.PacketBuffer; import net.minecraft.util.ResourceLocation; import net.minecraft.util.Util; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TranslationTextComponent; +import javax.annotation.Nullable; import java.util.List; /** @@ -18,14 +20,6 @@ public class DyeContentType extends NamedContentType { private static final ResourceLocation TEXTURE_NAME = Inspirations.getResource("dye"); private static final String TRANSLATION_KEY = Util.makeTranslationKey("cauldron_contents", TEXTURE_NAME); - /** - * Creates a new type instance - */ - @SuppressWarnings("ConstantConditions") - public DyeContentType() { - super(name -> DyeColor.byTranslationKey(name, null)); - } - @Override public ResourceLocation getTexture(DyeColor value) { return TEXTURE_NAME; @@ -47,4 +41,24 @@ public void addInformation(DyeColor value, List tooltip, IToolti tooltip.add(ColorContentType.getColorTooltip(value.getColorValue())); } } + + + /* Serializing and deserializing */ + + @SuppressWarnings("ConstantConditions") + @Nullable + @Override + protected DyeColor getValue(String name) { + return DyeColor.byTranslationKey(name, null); + } + + @Override + public DyeColor read(PacketBuffer buffer) { + return buffer.readEnumValue(DyeColor.class); + } + + @Override + public void write(DyeColor value, PacketBuffer buffer) { + buffer.writeEnumValue(value); + } } diff --git a/src/main/java/knightminer/inspirations/recipes/tileentity/CauldronTileEntity.java b/src/main/java/knightminer/inspirations/recipes/tileentity/CauldronTileEntity.java index 5fa77599..553ee3af 100644 --- a/src/main/java/knightminer/inspirations/recipes/tileentity/CauldronTileEntity.java +++ b/src/main/java/knightminer/inspirations/recipes/tileentity/CauldronTileEntity.java @@ -700,7 +700,7 @@ public CompoundNBT getUpdateTag() { @Override public CompoundNBT write(CompoundNBT tags) { tags = super.write(tags); - tags.put(TAG_CONTENTS, CauldronContentTypes.toNbt(getContents())); + tags.put(TAG_CONTENTS, getContents().toNBT()); // write transform if present, or transform name if we somehow wrote before world is set if (currentTransform != null) { tags.putString(TAG_TRANSFORM, currentTransform.getId().toString());