From a034a379c4e588f385d6156a140fb21f3d4a9a1b Mon Sep 17 00:00:00 2001 From: pahimar Date: Wed, 20 Nov 2013 20:17:32 -0500 Subject: [PATCH] Something something more dark side --- .../com/pahimar/ee3/EquivalentExchange3.java | 1 - common/com/pahimar/ee3/emc/EmcValue.java | 49 ++++--- .../pahimar/ee3/item/CustomWrappedStack.java | 135 +++--------------- common/com/pahimar/ee3/item/OreStack.java | 37 +++++ common/com/pahimar/ee3/lib/Reference.java | 10 +- 5 files changed, 87 insertions(+), 145 deletions(-) diff --git a/common/com/pahimar/ee3/EquivalentExchange3.java b/common/com/pahimar/ee3/EquivalentExchange3.java index b63ab9f4..f4379d4e 100644 --- a/common/com/pahimar/ee3/EquivalentExchange3.java +++ b/common/com/pahimar/ee3/EquivalentExchange3.java @@ -24,7 +24,6 @@ import com.pahimar.ee3.core.helper.VersionHelper; import com.pahimar.ee3.core.proxy.CommonProxy; import com.pahimar.ee3.creativetab.CreativeTabEE3; import com.pahimar.ee3.emc.EmcRegistry; -import com.pahimar.ee3.emc.EmcValue; import com.pahimar.ee3.item.ModItems; import com.pahimar.ee3.item.crafting.RecipesAlchemicalBagDyes; import com.pahimar.ee3.lib.Reference; diff --git a/common/com/pahimar/ee3/emc/EmcValue.java b/common/com/pahimar/ee3/emc/EmcValue.java index e0ae6a74..041b27e6 100644 --- a/common/com/pahimar/ee3/emc/EmcValue.java +++ b/common/com/pahimar/ee3/emc/EmcValue.java @@ -29,7 +29,7 @@ public class EmcValue implements Comparable { // Gson serializer for serializing to/deserializing from json private static final Gson gsonSerializer = (new GsonBuilder()).registerTypeAdapter(EmcValue.class, new EmcValue().new EmcValueJsonSerializer()).create(); - + public final float[] components; public EmcValue() { @@ -51,11 +51,12 @@ public class EmcValue implements Comparable { this(value, component.type); } - + public EmcValue(int value, EmcType emcType) { + this((float) value, emcType); } - + public EmcValue(float value, EmcType emcType) { this.components = new float[EmcType.TYPES.length]; @@ -125,16 +126,16 @@ public class EmcValue implements Comparable { public String toString() { StringBuilder stringBuilder = new StringBuilder(); - + // TODO Intelligible output stringBuilder.append("["); - + for (EmcType emcType : EmcType.TYPES) { if (components[emcType.ordinal()] > 0) { stringBuilder.append(String.format(" %s:%s ", emcType, components[emcType.ordinal()])); } } - + stringBuilder.append("]"); return stringBuilder.toString(); @@ -163,31 +164,34 @@ public class EmcValue implements Comparable { return -1; } } - + /** * Deserializes an EmcValue object from the given serialized json String * - * @param jsonEmcValue Json encoded String representing a EmcValue object - * @return The EmcValue that was encoded as json, or null if a valid EmcValue could not be decoded from given String + * @param jsonEmcValue + * Json encoded String representing a EmcValue object + * @return The EmcValue that was encoded as json, or null if a valid + * EmcValue could not be decoded from given String */ public static EmcValue createFromJson(String jsonEmcValue) { - + try { return (EmcValue) gsonSerializer.fromJson(jsonEmcValue, EmcValue.class); } catch (JsonSyntaxException exception) { // TODO Log something regarding the failed parse } - + return null; } - + /** * Returns this EmcValue as a json serialized String * * @return Json serialized String of this EmcValue */ public String toJson() { + return gsonSerializer.toJson(this); } @@ -234,18 +238,19 @@ public class EmcValue implements Comparable { throw new ArrayIndexOutOfBoundsException(); } } - - public class EmcValueJsonSerializer implements JsonDeserializer, JsonSerializer { - + + private class EmcValueJsonSerializer + implements JsonDeserializer, JsonSerializer { + @Override public JsonElement serialize(EmcValue emcValue, Type type, JsonSerializationContext context) { JsonObject jsonEmcValue = new JsonObject(); - + for (EmcType emcType : EmcType.TYPES) { jsonEmcValue.addProperty(emcType.toString(), emcValue.components[emcType.ordinal()]); } - + return jsonEmcValue; } @@ -253,8 +258,8 @@ public class EmcValue implements Comparable { public EmcValue deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException { float[] emcValueComponents = new float[EmcType.TYPES.length]; - JsonObject jsonEmcValue = (JsonObject) jsonElement; - + JsonObject jsonEmcValue = (JsonObject) jsonElement; + for (EmcType emcType : EmcType.TYPES) { if ((jsonEmcValue.get(emcType.toString()) != null) && (jsonEmcValue.get(emcType.toString()).isJsonPrimitive())) { try { @@ -266,13 +271,13 @@ public class EmcValue implements Comparable { } } } - + EmcValue emcValue = new EmcValue(emcValueComponents); - + if (emcValue.getValue() > 0f) { return emcValue; } - + return null; } } diff --git a/common/com/pahimar/ee3/item/CustomWrappedStack.java b/common/com/pahimar/ee3/item/CustomWrappedStack.java index 200ed903..e0da9419 100644 --- a/common/com/pahimar/ee3/item/CustomWrappedStack.java +++ b/common/com/pahimar/ee3/item/CustomWrappedStack.java @@ -1,8 +1,6 @@ package com.pahimar.ee3.item; -import java.lang.reflect.Type; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import net.minecraft.block.Block; @@ -13,22 +11,13 @@ import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.oredict.OreDictionary; import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonParseException; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; import com.google.gson.JsonSyntaxException; import com.pahimar.ee3.core.helper.ItemHelper; -import com.pahimar.ee3.lib.Reference; -import com.pahimar.ee3.lib.Strings; public class CustomWrappedStack implements Comparable { // Gson serializer for serializing to/deserializing from json - private static final Gson gsonSerializer = (new GsonBuilder()).registerTypeAdapter(CustomWrappedStack.class, new CustomWrappedStack(null).new CustomWrappedStackJsonSerializer()).create(); + private static final Gson gsonSerializer = new Gson(); private int stackSize; private final ItemStack itemStack; @@ -36,6 +25,8 @@ public class CustomWrappedStack implements Comparable { private final EnergyStack energyStack; private final FluidStack fluidStack; + private static final int ORE_DICTIONARY_NOT_FOUND = -1; + /** * Creates a new CustomWrappedStack object which wraps the given input. * Valid inputs would be ItemStacks or OreStacks. If something other than an @@ -108,61 +99,6 @@ public class CustomWrappedStack implements Comparable { energyStack = null; fluidStack = null; } - else if (object instanceof String) { - - String objectString = (String) object; - - if (objectString.startsWith(Strings.NBT_ENCODED_ATTR_ORE_NAME + ":")) { - - String possibleOreName = objectString.substring(Strings.NBT_ENCODED_ATTR_ORE_NAME.length() + 1); - OreStack possibleOreStack = new OreStack(possibleOreName); - - if (possibleOreStack.oreId != -1) { - - itemStack = null; - oreStack = possibleOreStack; - energyStack = null; - fluidStack = null; - stackSize = possibleOreStack.stackSize; - } - else { - - itemStack = null; - oreStack = null; - energyStack = null; - fluidStack = null; - stackSize = -1; - } - } - else if (objectString.startsWith(Strings.NBT_ENCODED_ATTR_ENERGY_NAME + ":")) { - - String possibleEnergyName = objectString.substring(Strings.NBT_ENCODED_ATTR_ENERGY_NAME.length() + 1); - - if (possibleEnergyName.length() > 0) { - - itemStack = null; - oreStack = null; - energyStack = new EnergyStack(possibleEnergyName); - fluidStack = null; - stackSize = 1; - } - else { - - itemStack = null; - oreStack = null; - energyStack = null; - fluidStack = null; - stackSize = -1; - } - } - else { - itemStack = null; - oreStack = null; - energyStack = null; - fluidStack = null; - stackSize = -1; - } - } /* * Or we are given an EnergyStack to wrap */ @@ -327,41 +263,25 @@ public class CustomWrappedStack implements Comparable { public static boolean canBeWrapped(Object object) { - // Simple case - if (object instanceof CustomWrappedStack || object instanceof ItemStack || object instanceof OreStack || object instanceof EnergyStack || object instanceof FluidStack || object instanceof Item || object instanceof Block || object instanceof Fluid) { + if (object instanceof CustomWrappedStack) { + return true; + } + else if (object instanceof Item || object instanceof Block || object instanceof ItemStack) { + return true; + } + else if (object instanceof OreStack) { return true; } - // If it's a List, check to see if it could possibly be an OreStack else if (object instanceof List) { if (getOreStackFromList((List) object) != null) { return true; } } - // If it's a String, check to see if it could be the encoded name for a custom stack (OreStack, EnergyStack, etc) - // TODO Revisit this now that we aren't using NBTTagCompounds to encode objects - else if (object instanceof String) { - - String objectString = (String) object; - - if (objectString.startsWith(Strings.NBT_ENCODED_ATTR_ORE_NAME + ":")) { - - String possibleOreName = objectString.substring(Strings.NBT_ENCODED_ATTR_ORE_NAME.length() + 1); - List oreNames = Arrays.asList(OreDictionary.getOreNames()); - - for (String oreName : oreNames) { - if (oreName.equalsIgnoreCase(possibleOreName)) { - return true; - } - } - } - else if (objectString.startsWith(Strings.NBT_ENCODED_ATTR_ENERGY_NAME + ":")) { - - String possibleEnergyName = objectString.substring(Strings.NBT_ENCODED_ATTR_ENERGY_NAME.length() + 1); - - if (possibleEnergyName.length() > 0) { - return true; - } - } + else if (object instanceof EnergyStack) { + return true; + } + else if (object instanceof Fluid || object instanceof FluidStack) { + return true; } return false; @@ -437,7 +357,7 @@ public class CustomWrappedStack implements Comparable { if (listElement instanceof ItemStack) { ItemStack stack = (ItemStack) listElement; - if (OreDictionary.getOreID(stack) != Reference.ORE_DICTIONARY_NOT_FOUND) { + if (OreDictionary.getOreID(stack) != CustomWrappedStack.ORE_DICTIONARY_NOT_FOUND) { return new OreStack(stack); } } @@ -452,8 +372,8 @@ public class CustomWrappedStack implements Comparable { * * @param jsonEmcValue * Json encoded String representing a CustomWrappedStack object - * @return The EmcValue that was encoded as json, or null if a valid - * CustomWrappedStack could not be decoded from given String + * @return The CustomWrappedStack that was encoded as json, or null if a + * valid CustomWrappedStack could not be decoded from given String */ public static CustomWrappedStack createFromJson(String jsonCustomWrappedStack) { @@ -476,23 +396,4 @@ public class CustomWrappedStack implements Comparable { return gsonSerializer.toJson(this); } - - public class CustomWrappedStackJsonSerializer - implements JsonDeserializer, - JsonSerializer { - - @Override - public JsonElement serialize(CustomWrappedStack customWrappedStack, Type type, JsonSerializationContext context) { - - // TODO Auto-generated method stub - return null; - } - - @Override - public CustomWrappedStack deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException { - - // TODO Auto-generated method stub - return null; - } - } } diff --git a/common/com/pahimar/ee3/item/OreStack.java b/common/com/pahimar/ee3/item/OreStack.java index 41d5d131..b1c8a4b2 100644 --- a/common/com/pahimar/ee3/item/OreStack.java +++ b/common/com/pahimar/ee3/item/OreStack.java @@ -5,8 +5,14 @@ import java.util.ArrayList; import net.minecraft.item.ItemStack; import net.minecraftforge.oredict.OreDictionary; +import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; + public class OreStack implements Comparable { + // Gson serializer for serializing to/deserializing from json + private static final Gson gsonSerializer = new Gson(); + public int oreId; public String oreName; public int stackSize; @@ -139,4 +145,35 @@ public class OreStack implements Comparable { return 1; } } + + /** + * Deserializes a OreStack object from the given serialized json + * String + * + * @param jsonEmcValue + * Json encoded String representing a OreStack object + * @return The OreStack that was encoded as json, or null if a valid + * OreStack could not be decoded from given String + */ + public static OreStack createFromJson(String jsonOreStack) { + + try { + return (OreStack) gsonSerializer.fromJson(jsonOreStack, OreStack.class); + } + catch (JsonSyntaxException exception) { + // TODO Log something regarding the failed parse + } + + return null; + } + + /** + * Returns this OreStack as a json serialized String + * + * @return Json serialized String of this OreStack + */ + public String toJson() { + + return gsonSerializer.toJson(this); + } } diff --git a/common/com/pahimar/ee3/lib/Reference.java b/common/com/pahimar/ee3/lib/Reference.java index 63cb027d..29f2b39b 100644 --- a/common/com/pahimar/ee3/lib/Reference.java +++ b/common/com/pahimar/ee3/lib/Reference.java @@ -11,10 +11,7 @@ package com.pahimar.ee3.lib; */ public class Reference { - /* Debug Mode On-Off */ - public static final boolean DEBUG_MODE = false; - - /* General Mod related constants */ + // General Mod related constants public static final String MOD_ID = "EE3"; public static final String MOD_NAME = "Equivalent Exchange 3"; public static final String VERSION_NUMBER = "@VERSION@ (build @BUILD_NUMBER@)"; @@ -27,6 +24,9 @@ public class Reference { public static final String CLIENT_PROXY_CLASS = "com.pahimar.ee3.core.proxy.ClientProxy"; public static final int VERSION_CHECK_ATTEMPTS = 3; - public static final int ORE_DICTIONARY_NOT_FOUND = -1; + // Comparator stuff + public static final int SMALLER_THAN = -1; + public static final int EQUAL_TO = 0; + public static final int LARGER_THAN = 1; }