From 31b9d45dfec2093a8887476acfc630ae70da1030 Mon Sep 17 00:00:00 2001 From: Pahimar Date: Thu, 12 May 2016 16:02:01 -0400 Subject: [PATCH] More serialization work, looking a lot cleaner than before but definitely still some improvements possible --- .../ee3/exchange/NewEnergyValueRegistry.java | 3 - .../util/serialize/FluidStackSerializer.java | 46 ++++++------- .../util/serialize/ItemStackSerializer.java | 57 +++++++++------- .../util/serialize/OreStackSerializer.java | 10 +-- .../serialize/WrappedStackSerializer.java | 68 +++++++------------ 5 files changed, 81 insertions(+), 103 deletions(-) diff --git a/src/main/java/com/pahimar/ee3/exchange/NewEnergyValueRegistry.java b/src/main/java/com/pahimar/ee3/exchange/NewEnergyValueRegistry.java index 4b8c7dbc..cfc91ec2 100644 --- a/src/main/java/com/pahimar/ee3/exchange/NewEnergyValueRegistry.java +++ b/src/main/java/com/pahimar/ee3/exchange/NewEnergyValueRegistry.java @@ -1,6 +1,5 @@ package com.pahimar.ee3.exchange; -import com.google.common.reflect.TypeToken; import com.pahimar.ee3.api.exchange.EnergyValue; import com.pahimar.ee3.util.LogHelper; import com.pahimar.ee3.util.SerializationHelper; @@ -13,7 +12,6 @@ import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.lang.reflect.Type; import java.util.Map; import java.util.TreeMap; @@ -21,7 +19,6 @@ public class NewEnergyValueRegistry { public static final Marker ENERGY_VALUE_MARKER = MarkerManager.getMarker("EE3_ENERGY_VALUE", LogHelper.MOD_MARKER); public static final NewEnergyValueRegistry INSTANCE = new NewEnergyValueRegistry(); - public static final Type ENERGY_VALUE_MAP_TYPE = new TypeToken>(){}.getType(); public final Map preCalculationMappings; public final Map postCalculationMappings; diff --git a/src/main/java/com/pahimar/ee3/util/serialize/FluidStackSerializer.java b/src/main/java/com/pahimar/ee3/util/serialize/FluidStackSerializer.java index 8544e260..05896064 100644 --- a/src/main/java/com/pahimar/ee3/util/serialize/FluidStackSerializer.java +++ b/src/main/java/com/pahimar/ee3/util/serialize/FluidStackSerializer.java @@ -13,39 +13,32 @@ import java.lang.reflect.Type; public class FluidStackSerializer implements JsonSerializer, JsonDeserializer { - // TODO String constants for property names + private static final String NAME = "name"; + private static final String TAG_COMPOUND = "tagCompound"; @Override public FluidStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + if (json.isJsonObject()) { - JsonObject jsonObject = (JsonObject) json; + JsonObject jsonObject = json.getAsJsonObject(); - String fluidName = null; - int fluidAmount = Integer.MIN_VALUE; - NBTTagCompound nbtTagCompound = null; + String name = null; + NBTTagCompound tagCompound = null; try { - if (jsonObject.get("fluidName").getAsJsonPrimitive().isString()) { - fluidName = jsonObject.get("fluidName").getAsString(); + if (jsonObject.get(NAME).getAsJsonPrimitive().isString()) { + name = jsonObject.get(NAME).getAsString(); } } catch (IllegalStateException exception) { } try { - if (jsonObject.get("fluidAmount").getAsJsonPrimitive().isNumber()) { - fluidAmount = jsonObject.get("fluidAmount").getAsInt(); - } - } - catch (IllegalStateException exception) { - } + if (jsonObject.has(TAG_COMPOUND) && jsonObject.get(TAG_COMPOUND).getAsJsonPrimitive().isString()) { - try { - if (jsonObject.get("fluidTagCompound").getAsJsonPrimitive().isString()) { - - NBTBase nbtBase = JsonToNBT.func_150315_a(jsonObject.get("fluidTagCompound").getAsString()); + NBTBase nbtBase = JsonToNBT.func_150315_a(jsonObject.get(TAG_COMPOUND).getAsString()); if (nbtBase instanceof NBTTagCompound) { - nbtTagCompound = (NBTTagCompound) nbtBase; + tagCompound = (NBTTagCompound) nbtBase; } } } @@ -54,14 +47,14 @@ public class FluidStackSerializer implements JsonSerializer, JsonDes catch (NBTException e) { } - if (fluidName != null) { - Fluid fluid = FluidRegistry.getFluid(fluidName); + if (name != null) { + Fluid fluid = FluidRegistry.getFluid(name); - if (fluid != null && fluidAmount >= 0) { - FluidStack fluidStack = new FluidStack(fluid, fluidAmount); + if (fluid != null) { + FluidStack fluidStack = new FluidStack(fluid, 1); - if (nbtTagCompound != null) { - fluidStack.tag = nbtTagCompound; + if (tagCompound != null) { + fluidStack.tag = tagCompound; } return fluidStack; @@ -78,10 +71,9 @@ public class FluidStackSerializer implements JsonSerializer, JsonDes if (src != null) { JsonObject jsonObject = new JsonObject(); - jsonObject.addProperty("fluidName", src.getFluid().getName()); - jsonObject.addProperty("fluidAmount", src.amount); + jsonObject.addProperty(NAME, src.getFluid().getName()); if (src.tag != null) { - jsonObject.addProperty("fluidTagCompound", src.tag.toString()); + jsonObject.addProperty(TAG_COMPOUND, src.tag.toString()); } return jsonObject; diff --git a/src/main/java/com/pahimar/ee3/util/serialize/ItemStackSerializer.java b/src/main/java/com/pahimar/ee3/util/serialize/ItemStackSerializer.java index 7a4f990d..55695888 100644 --- a/src/main/java/com/pahimar/ee3/util/serialize/ItemStackSerializer.java +++ b/src/main/java/com/pahimar/ee3/util/serialize/ItemStackSerializer.java @@ -12,60 +12,61 @@ import java.lang.reflect.Type; public class ItemStackSerializer implements JsonSerializer, JsonDeserializer { - // TODO String constants for property names + private static final String NAME = "name"; + private static final String META_VALUE = "metaValue"; + private static final String TAG_COMPOUND = "tagCompound"; @Override public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonObject()) { - JsonObject jsonObject = (JsonObject) json; + JsonObject jsonObject = json.getAsJsonObject(); - String itemName = null; - int itemDamage = Integer.MIN_VALUE; - NBTTagCompound nbtTagCompound = null; + String name = null; + int metaValue = 0; + NBTTagCompound tagCompound = null; try { - if (jsonObject.get("itemName").getAsJsonPrimitive().isString()) { - itemName = jsonObject.get("itemName").getAsString(); + if (jsonObject.get(NAME).getAsJsonPrimitive().isString()) { + name = jsonObject.get(NAME).getAsString(); } } catch (IllegalStateException exception) { + // TODO We could probably log here that an invalid piece of data was found } try { - if (jsonObject.get("itemDamage").getAsJsonPrimitive().isNumber()) { - itemDamage = jsonObject.get("itemDamage").getAsInt(); + if (jsonObject.has(META_VALUE) && jsonObject.get(META_VALUE).getAsJsonPrimitive().isNumber()) { + metaValue = jsonObject.get(META_VALUE).getAsInt(); } } catch (IllegalStateException exception) { + // TODO We could probably log here that an invalid piece of data was found } try { - if (jsonObject.get("itemTagCompound").getAsJsonPrimitive().isString()) { + if (jsonObject.has(TAG_COMPOUND) && jsonObject.get(TAG_COMPOUND).getAsJsonPrimitive().isString()) { - NBTBase nbtBase = JsonToNBT.func_150315_a(jsonObject.get("itemTagCompound").getAsString()); + NBTBase nbtBase = JsonToNBT.func_150315_a(jsonObject.get(TAG_COMPOUND).getAsString()); if (nbtBase instanceof NBTTagCompound) { - nbtTagCompound = (NBTTagCompound) nbtBase; + tagCompound = (NBTTagCompound) nbtBase; } } } - catch (IllegalStateException exception) { - } catch (NBTException e) { } + catch (IllegalStateException exception) { + // TODO We could probably log here that an invalid piece of data was found + } - if (itemName != null) { - Item item = (Item) Item.itemRegistry.getObject(itemName); + if (name != null) { + Item item = (Item) Item.itemRegistry.getObject(name); if (item != null) { - ItemStack itemStack = new ItemStack((Item) Item.itemRegistry.getObject(itemName)); + ItemStack itemStack = new ItemStack((Item) Item.itemRegistry.getObject(name), 1, metaValue); - if (itemDamage >= 0) { - itemStack.setItemDamage(itemDamage); - } - - if (nbtTagCompound != null) { - itemStack.setTagCompound(nbtTagCompound); + if (tagCompound != null) { + itemStack.setTagCompound(tagCompound); } return itemStack; @@ -82,10 +83,14 @@ public class ItemStackSerializer implements JsonSerializer, JsonDeser if (src != null) { JsonObject jsonObject = new JsonObject(); - jsonObject.addProperty("itemName", Item.itemRegistry.getNameForObject(src.getItem())); - jsonObject.addProperty("itemDamage", src.getItemDamage()); + jsonObject.addProperty(NAME, Item.itemRegistry.getNameForObject(src.getItem())); + + if (src.getItemDamage() != 0) { + jsonObject.addProperty(META_VALUE, src.getItemDamage()); + } + if (src.getTagCompound() != null) { - jsonObject.addProperty("itemTagCompound", src.getTagCompound().toString()); + jsonObject.addProperty(TAG_COMPOUND, src.getTagCompound().toString()); } return jsonObject; diff --git a/src/main/java/com/pahimar/ee3/util/serialize/OreStackSerializer.java b/src/main/java/com/pahimar/ee3/util/serialize/OreStackSerializer.java index b6d9f46e..0bc8146d 100644 --- a/src/main/java/com/pahimar/ee3/util/serialize/OreStackSerializer.java +++ b/src/main/java/com/pahimar/ee3/util/serialize/OreStackSerializer.java @@ -7,7 +7,7 @@ import java.lang.reflect.Type; public class OreStackSerializer implements JsonSerializer, JsonDeserializer { - // TODO String constants for property names + private static final String NAME = "name"; @Override public OreStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { @@ -16,9 +16,9 @@ public class OreStackSerializer implements JsonSerializer, JsonDeseria JsonObject jsonObject = (JsonObject) json; try { - if (jsonObject.get("oreName").getAsJsonPrimitive().isString()) { - String oreName = jsonObject.get("oreName").getAsString(); - return new OreStack(oreName); + if (jsonObject.get(NAME).getAsJsonPrimitive().isString()) { + String name = jsonObject.get(NAME).getAsString(); + return new OreStack(name); } } catch (IllegalStateException exception) { @@ -34,7 +34,7 @@ public class OreStackSerializer implements JsonSerializer, JsonDeseria if (src != null) { JsonObject jsonObject = new JsonObject(); - jsonObject.addProperty("oreName", src.oreName); + jsonObject.addProperty(NAME, src.oreName); return jsonObject; } diff --git a/src/main/java/com/pahimar/ee3/util/serialize/WrappedStackSerializer.java b/src/main/java/com/pahimar/ee3/util/serialize/WrappedStackSerializer.java index ba7f9a10..f15f731c 100644 --- a/src/main/java/com/pahimar/ee3/util/serialize/WrappedStackSerializer.java +++ b/src/main/java/com/pahimar/ee3/util/serialize/WrappedStackSerializer.java @@ -11,7 +11,11 @@ import java.lang.reflect.Type; public class WrappedStackSerializer implements JsonSerializer, JsonDeserializer { - // TODO String constants for property names + private static final String DATA = "data"; + private static final String TYPE = "type"; + private static final String TYPE_ITEMSTACK = "itemstack"; + private static final String TYPE_ORESTACK = "orestack"; + private static final String TYPE_FLUIDSTACK = "fluidstack"; @Override public WrappedStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { @@ -21,56 +25,39 @@ public class WrappedStackSerializer implements JsonSerializer, Jso JsonObject jsonObject = json.getAsJsonObject(); String type = null; - int stackSize = Integer.MIN_VALUE; - JsonObject data = null; try { - if (jsonObject.get("type").getAsJsonPrimitive().isString()) { - type = jsonObject.get("type").getAsString(); + 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) { - } + // TODO COME BACK HERE AND MAKE THIS CLEANER FOR THE SAKE OF PACK DEVS AND USERS ALIKE - try { - if (jsonObject.get("data").isJsonObject()) { - data = jsonObject.getAsJsonObject("data"); - } - } - catch (IllegalStateException exception) { - } + if (jsonObject.get(DATA).isJsonObject()) { + JsonObject data = jsonObject.getAsJsonObject(DATA); - if ("itemstack".equalsIgnoreCase(type) || "orestack".equalsIgnoreCase(type) || "fluidstack".equalsIgnoreCase(type)) { - if (stackSize >= 1) { + if (TYPE_ITEMSTACK.equalsIgnoreCase(type)) { + ItemStack itemStack = SerializationHelper.GSON.fromJson(data, ItemStack.class); - if ("itemstack".equalsIgnoreCase(type)) { - ItemStack itemStack = SerializationHelper.GSON.fromJson(data, ItemStack.class); - - if (itemStack != null) { - return WrappedStack.wrap(itemStack, stackSize); - } + if (itemStack != null) { + return WrappedStack.wrap(itemStack); } - else if ("orestack".equalsIgnoreCase(type)) { - OreStack oreStack = SerializationHelper.GSON.fromJson(data, OreStack.class); + } + else if (TYPE_ORESTACK.equalsIgnoreCase(type)) { + OreStack oreStack = SerializationHelper.GSON.fromJson(data, OreStack.class); - if (oreStack != null) { - return WrappedStack.wrap(oreStack, stackSize); - } + if (oreStack != null) { + return WrappedStack.wrap(oreStack); } - else if ("fluidstack".equalsIgnoreCase(type)) { - FluidStack fluidStack = SerializationHelper.GSON.fromJson(data, FluidStack.class); + } + else if (TYPE_FLUIDSTACK.equalsIgnoreCase(type)) { + FluidStack fluidStack = SerializationHelper.GSON.fromJson(data, FluidStack.class); - if (fluidStack != null) { - return WrappedStack.wrap(fluidStack, stackSize); - } + if (fluidStack != null) { + return WrappedStack.wrap(fluidStack); } } } @@ -84,11 +71,8 @@ public class WrappedStackSerializer implements JsonSerializer, Jso 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()); - + if (src != null && src.getWrappedObject() != null) { + jsonObject.add(src.getWrappedObject().getClass().getSimpleName().toLowerCase(), context.serialize(src.getWrappedObject())); return jsonObject; }