diff --git a/src/main/java/com/pahimar/ee3/EquivalentExchange3.java b/src/main/java/com/pahimar/ee3/EquivalentExchange3.java index 2bd20842..7f4ed795 100644 --- a/src/main/java/com/pahimar/ee3/EquivalentExchange3.java +++ b/src/main/java/com/pahimar/ee3/EquivalentExchange3.java @@ -1,10 +1,10 @@ package com.pahimar.ee3; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.pahimar.ee3.array.AlchemyArrayRegistry; import com.pahimar.ee3.command.CommandEE; -import com.pahimar.ee3.exchange.CachedOreDictionary; -import com.pahimar.ee3.exchange.EnergyValueRegistry; -import com.pahimar.ee3.exchange.NewEnergyValueRegistry; +import com.pahimar.ee3.exchange.*; import com.pahimar.ee3.handler.*; import com.pahimar.ee3.init.*; import com.pahimar.ee3.knowledge.AbilityRegistry; @@ -22,6 +22,10 @@ import com.pahimar.ee3.util.FluidHelper; import com.pahimar.ee3.util.LogHelper; import com.pahimar.ee3.util.SerializationHelper; import com.pahimar.ee3.util.TileEntityDataHelper; +import com.pahimar.ee3.util.serialize.FluidStackSerializer; +import com.pahimar.ee3.util.serialize.ItemStackSerializer; +import com.pahimar.ee3.util.serialize.OreStackSerializer; +import com.pahimar.ee3.util.serialize.WrappedStackSerializer; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.Mod.Instance; @@ -29,6 +33,8 @@ import cpw.mods.fml.common.SidedProxy; import cpw.mods.fml.common.event.*; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.registry.GameRegistry; +import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.FluidStack; import java.io.File; import java.io.IOException; @@ -42,6 +48,13 @@ public class EquivalentExchange3 @SidedProxy(clientSide = Reference.CLIENT_PROXY_CLASS, serverSide = Reference.SERVER_PROXY_CLASS) public static IProxy proxy; + public static final Gson GSON = new GsonBuilder() + .registerTypeAdapter(ItemStack.class, new ItemStackSerializer()) + .registerTypeAdapter(OreStack.class, new OreStackSerializer()) + .registerTypeAdapter(FluidStack.class, new FluidStackSerializer()) + .registerTypeAdapter(WrappedStack.class, new WrappedStackSerializer()) + .create(); + @EventHandler public void invalidFingerprint(FMLFingerprintViolationEvent event) { @@ -72,6 +85,7 @@ public class EquivalentExchange3 { ConfigurationHandler.init(event.getSuggestedConfigurationFile()); + Files.init(event); Files.Global.init(event); PacketHandler.init(); diff --git a/src/main/java/com/pahimar/ee3/exchange/NewEnergyValueRegistry.java b/src/main/java/com/pahimar/ee3/exchange/NewEnergyValueRegistry.java index 2e33ffd5..92e9493e 100644 --- a/src/main/java/com/pahimar/ee3/exchange/NewEnergyValueRegistry.java +++ b/src/main/java/com/pahimar/ee3/exchange/NewEnergyValueRegistry.java @@ -2,8 +2,8 @@ package com.pahimar.ee3.exchange; import com.google.common.reflect.TypeToken; import com.google.gson.*; +import com.pahimar.ee3.EquivalentExchange3; import com.pahimar.ee3.api.exchange.EnergyValue; -import com.pahimar.ee3.reference.Files; import com.pahimar.ee3.util.LogHelper; import net.minecraft.init.Items; import net.minecraft.nbt.NBTTagCompound; @@ -22,11 +22,14 @@ public class NewEnergyValueRegistry implements JsonSerializer>(){}.getType(); + public static final Type ENERGY_VALUE_MAP_TYPE = new TypeToken>(){}.getType(); private final Map preCalculationMappings; private final Map postCalculationMappings; + public static File energyValuesDataDirectory; + public static File energyValuesDataFile; + private NewEnergyValueRegistry() { preCalculationMappings = new TreeMap<>(); // Loading up some dummy values for testing serialization @@ -39,8 +42,7 @@ public class NewEnergyValueRegistry implements JsonSerializer, JsonDeserializer { + + // TODO String constants for property names + + @Override + public FluidStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + if (json.isJsonObject()) { + JsonObject jsonObject = (JsonObject) json; + + String fluidName = null; + int fluidAmount = Integer.MIN_VALUE; + NBTTagCompound nbtTagCompound = null; + + try { + if (jsonObject.get("fluidName").getAsJsonPrimitive().isString()) { + fluidName = jsonObject.get("fluidName").getAsString(); + } + } + catch (IllegalStateException exception) { + } + + try { + if (jsonObject.get("fluidAmount").getAsJsonPrimitive().isNumber()) { + fluidAmount = jsonObject.get("fluidAmount").getAsInt(); + } + } + catch (IllegalStateException exception) { + } + + try { + if (jsonObject.get("fluidTagCompound").getAsJsonPrimitive().isString()) { + + NBTBase nbtBase = JsonToNBT.func_150315_a(jsonObject.get("fluidTagCompound").getAsString()); + if (nbtBase instanceof NBTTagCompound) { + nbtTagCompound = (NBTTagCompound) nbtBase; + } + } + } + catch (IllegalStateException exception) { + } + catch (NBTException e) { + } + + if (fluidName != null) { + Fluid fluid = FluidRegistry.getFluid(fluidName); + + if (fluid != null && fluidAmount >= 0) { + FluidStack fluidStack = new FluidStack(fluid, fluidAmount); + + if (nbtTagCompound != null) { + fluidStack.tag = nbtTagCompound; + } + + return fluidStack; + } + } + } + + return null; + } + + @Override + public JsonElement serialize(FluidStack src, Type typeOfSrc, JsonSerializationContext context) { + + if (src != null) { + JsonObject jsonObject = new JsonObject(); + + jsonObject.addProperty("fluidName", src.getFluid().getName()); + jsonObject.addProperty("fluidAmount", src.amount); + if (src.tag != null) { + jsonObject.addProperty("fluidTagCompound", src.tag.toString()); + } + + return jsonObject; + } + + return null; + } +} diff --git a/src/main/java/com/pahimar/ee3/util/serialize/ItemStackSerializer.java b/src/main/java/com/pahimar/ee3/util/serialize/ItemStackSerializer.java new file mode 100644 index 00000000..7a4f990d --- /dev/null +++ b/src/main/java/com/pahimar/ee3/util/serialize/ItemStackSerializer.java @@ -0,0 +1,96 @@ +package com.pahimar.ee3.util.serialize; + +import com.google.gson.*; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.JsonToNBT; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTException; +import net.minecraft.nbt.NBTTagCompound; + +import java.lang.reflect.Type; + +public class ItemStackSerializer implements JsonSerializer, JsonDeserializer { + + // TODO String constants for property names + + @Override + public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + + if (json.isJsonObject()) { + JsonObject jsonObject = (JsonObject) json; + + String itemName = null; + int itemDamage = Integer.MIN_VALUE; + NBTTagCompound nbtTagCompound = null; + + try { + if (jsonObject.get("itemName").getAsJsonPrimitive().isString()) { + itemName = jsonObject.get("itemName").getAsString(); + } + } + catch (IllegalStateException exception) { + } + + try { + if (jsonObject.get("itemDamage").getAsJsonPrimitive().isNumber()) { + itemDamage = jsonObject.get("itemDamage").getAsInt(); + } + } + catch (IllegalStateException exception) { + } + + try { + if (jsonObject.get("itemTagCompound").getAsJsonPrimitive().isString()) { + + NBTBase nbtBase = JsonToNBT.func_150315_a(jsonObject.get("itemTagCompound").getAsString()); + if (nbtBase instanceof NBTTagCompound) { + nbtTagCompound = (NBTTagCompound) nbtBase; + } + } + } + catch (IllegalStateException exception) { + } + catch (NBTException e) { + } + + if (itemName != null) { + Item item = (Item) Item.itemRegistry.getObject(itemName); + + if (item != null) { + ItemStack itemStack = new ItemStack((Item) Item.itemRegistry.getObject(itemName)); + + if (itemDamage >= 0) { + itemStack.setItemDamage(itemDamage); + } + + if (nbtTagCompound != null) { + itemStack.setTagCompound(nbtTagCompound); + } + + return itemStack; + } + } + } + + return null; + } + + @Override + public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationContext context) { + + if (src != null) { + JsonObject jsonObject = new JsonObject(); + + jsonObject.addProperty("itemName", Item.itemRegistry.getNameForObject(src.getItem())); + jsonObject.addProperty("itemDamage", src.getItemDamage()); + if (src.getTagCompound() != null) { + jsonObject.addProperty("itemTagCompound", src.getTagCompound().toString()); + } + + return jsonObject; + } + + return null; + } +} diff --git a/src/main/java/com/pahimar/ee3/util/serialize/OreStackSerializer.java b/src/main/java/com/pahimar/ee3/util/serialize/OreStackSerializer.java new file mode 100644 index 00000000..b6d9f46e --- /dev/null +++ b/src/main/java/com/pahimar/ee3/util/serialize/OreStackSerializer.java @@ -0,0 +1,43 @@ +package com.pahimar.ee3.util.serialize; + +import com.google.gson.*; +import com.pahimar.ee3.exchange.OreStack; + +import java.lang.reflect.Type; + +public class OreStackSerializer implements JsonSerializer, JsonDeserializer { + + // TODO String constants for property names + + @Override + public OreStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + + if (json.isJsonObject()) { + JsonObject jsonObject = (JsonObject) json; + + try { + if (jsonObject.get("oreName").getAsJsonPrimitive().isString()) { + String oreName = jsonObject.get("oreName").getAsString(); + return new OreStack(oreName); + } + } + catch (IllegalStateException exception) { + // TODO We could probably log here that an invalid piece of data was found + } + } + + return null; + } + + @Override + public JsonElement serialize(OreStack src, Type typeOfSrc, JsonSerializationContext context) { + + if (src != null) { + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("oreName", src.oreName); + return jsonObject; + } + + return null; + } +} diff --git a/src/main/java/com/pahimar/ee3/util/serialize/WrappedStackSerializer.java b/src/main/java/com/pahimar/ee3/util/serialize/WrappedStackSerializer.java new file mode 100644 index 00000000..91c828fc --- /dev/null +++ b/src/main/java/com/pahimar/ee3/util/serialize/WrappedStackSerializer.java @@ -0,0 +1,77 @@ +package com.pahimar.ee3.util.serialize; + +import com.google.gson.*; +import com.pahimar.ee3.exchange.OreStack; +import com.pahimar.ee3.exchange.WrappedStack; +import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.FluidStack; + +import java.lang.reflect.Type; + +public class WrappedStackSerializer implements JsonSerializer, JsonDeserializer { + + // TODO String constants for property names + + @Override + public WrappedStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + + if (json.isJsonObject()) { + + JsonObject jsonObject = (JsonObject) json; + + String type = null; + int stackSize = Integer.MIN_VALUE; + String data = null; + + + try { + if (jsonObject.get("type").getAsJsonPrimitive().isString()) { + type = jsonObject.get("type").getAsString(); + } + } + catch (IllegalStateException exception) { + } + + try { + if (jsonObject.get("stackSize").getAsJsonPrimitive().isNumber()) { + stackSize = jsonObject.get("stackSize").getAsInt(); + } + } + catch (IllegalStateException exception) { + } + + try { + if (jsonObject.get("data").getAsJsonPrimitive().isString()) { + data = jsonObject.get("data").getAsString(); + } + } + catch (IllegalStateException exception) { + } + + if ("itemstack".equalsIgnoreCase(type) || "orestack".equalsIgnoreCase(type) || "fluidstack".equalsIgnoreCase(type)) { + if (stackSize >= 1) { + // TODO PICK UP HERE + // TODO ALSO LOOK INTO MOVING THESE ALL INTO SerializationHelper + } + } + } + + return null; + } + + @Override + public JsonElement serialize(WrappedStack src, Type typeOfSrc, JsonSerializationContext context) { + + final JsonObject jsonObject = new JsonObject(); + + if (src.getWrappedObject() instanceof ItemStack || src.getWrappedObject() instanceof OreStack || src.getWrappedObject() instanceof FluidStack) { + jsonObject.addProperty("type", src.getWrappedObject().getClass().getSimpleName().toLowerCase()); + jsonObject.add("data", context.serialize(src.getWrappedObject())); + jsonObject.addProperty("stackSize", src.getStackSize()); + + return jsonObject; + } + + return null; + } +}