diff --git a/src/main/java/com/pahimar/ee3/api/exchange/EnergyValue.java b/src/main/java/com/pahimar/ee3/api/exchange/EnergyValue.java index 1354a2bb..7662cce0 100644 --- a/src/main/java/com/pahimar/ee3/api/exchange/EnergyValue.java +++ b/src/main/java/com/pahimar/ee3/api/exchange/EnergyValue.java @@ -1,16 +1,15 @@ package com.pahimar.ee3.api.exchange; -import com.google.gson.*; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.ChatComponentText; import net.minecraft.util.IChatComponent; -import java.lang.reflect.Type; import java.math.BigDecimal; +import java.text.DecimalFormat; -public final class EnergyValue implements Comparable, JsonDeserializer, JsonSerializer { +public final class EnergyValue implements Comparable { - private static final Gson jsonSerializer = (new GsonBuilder()).registerTypeAdapter(EnergyValue.class, new EnergyValue()).create(); + private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("###,###,###,###,###.###"); private float energyValue; public EnergyValue() @@ -32,7 +31,7 @@ public final class EnergyValue implements Comparable, JsonDeseriali @Override public String toString() { - return String.format("%s", energyValue); + return DECIMAL_FORMAT.format(energyValue); } @Override @@ -80,69 +79,6 @@ public final class EnergyValue implements Comparable, JsonDeseriali return null; } - /** - * Returns this EmcValue as a json serialized String - * - * @return Json serialized String of this EmcValue - */ - public String toJson() { - return jsonSerializer.toJson(this); - } - - /** - * Gson invokes this call-back method during deserialization when it encounters a field of the - * specified type. - *

In the implementation of this call-back method, you should consider invoking - * {@link com.google.gson.JsonDeserializationContext#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type)} method to create objects - * for any non-trivial field of the returned object. However, you should never invoke it on the - * the same type passing {@code jsonElement} since that will cause an infinite loop (Gson will call your - * call-back method again). - * - * @param jsonElement The Json data being deserialized - * @param typeOfT The type of the Object to deserialize to - * @param context - * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} - * @throws com.google.gson.JsonParseException if jsonElement is not in the expected format of {@code typeofT} - */ - @Override - public EnergyValue deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - - JsonObject jsonEnergyValue = (JsonObject) jsonElement; - - if (jsonEnergyValue.get("value") != null && jsonEnergyValue.get("value").isJsonPrimitive()) { - float energyValue = jsonEnergyValue.get("value").getAsFloat(); - - if (Float.compare(energyValue, 0f) >= 0) { - return new EnergyValue(energyValue); - } - } - - return null; - } - - /** - * Gson invokes this call-back method during serialization when it encounters a field of the - * specified type. - *

- *

In the implementation of this call-back method, you should consider invoking - * {@link com.google.gson.JsonSerializationContext#serialize(Object, java.lang.reflect.Type)} method to create JsonElements for any - * non-trivial field of the {@code energyValueObject} object. However, you should never invoke it on the - * {@code energyValueObject} object itself since that will cause an infinite loop (Gson will call your - * call-back method again).

- * - * @param energyValueObject the object that needs to be converted to Json. - * @param typeOfSrc the actual type (fully genericized version) of the source object. - * @param context - * @return a JsonElement corresponding to the specified object. - */ - @Override - public JsonElement serialize(EnergyValue energyValueObject, Type typeOfSrc, JsonSerializationContext context) { - - JsonObject jsonEnergyValue = new JsonObject(); - jsonEnergyValue.addProperty("value", energyValueObject.energyValue); - return jsonEnergyValue; - } - public static EnergyValue factor(EnergyValue energyValue, Number factor) { if ((Float.compare(factor.floatValue(), 0f) != 0) && (energyValue != null)) { @@ -152,5 +88,4 @@ public final class EnergyValue implements Comparable, JsonDeseriali return null; } } - } diff --git a/src/main/java/com/pahimar/ee3/client/handler/ItemTooltipEventHandler.java b/src/main/java/com/pahimar/ee3/client/handler/ItemTooltipEventHandler.java index f8300e03..bdd32824 100644 --- a/src/main/java/com/pahimar/ee3/client/handler/ItemTooltipEventHandler.java +++ b/src/main/java/com/pahimar/ee3/client/handler/ItemTooltipEventHandler.java @@ -21,14 +21,11 @@ import net.minecraftforge.fluids.FluidContainerRegistry; import net.minecraftforge.fluids.FluidStack; import org.lwjgl.input.Keyboard; -import java.text.DecimalFormat; import java.util.UUID; @SideOnly(Side.CLIENT) public class ItemTooltipEventHandler { - private static DecimalFormat energyValueDecimalFormat = new DecimalFormat("###,###,###,###,###.###"); - @SubscribeEvent public void handleItemTooltipEvent(ItemTooltipEvent event) { @@ -36,26 +33,26 @@ public class ItemTooltipEventHandler { WrappedStack wrappedItemStack = WrappedStack.wrap(event.itemStack); EnergyValue energyValue = EnergyValueRegistryProxy.getEnergyValue(wrappedItemStack); + EnergyValue stackEnergyValue = EnergyValueRegistryProxy.getEnergyValueForStack(wrappedItemStack); if (energyValue != null && (BlacklistRegistryProxy.isExchangeable(wrappedItemStack) || BlacklistRegistryProxy.isLearnable(wrappedItemStack))) { if (wrappedItemStack.getStackSize() > 1) { - event.toolTip.add(String.format("Exchange Energy (Item): %s", energyValueDecimalFormat.format(energyValue.getValue()))); // TODO Localize - event.toolTip.add(String.format("Exchange Energy (Stack of %s): %s", event.itemStack.stackSize, energyValueDecimalFormat.format(wrappedItemStack.getStackSize() * energyValue.getValue()))); // TODO Localize + event.toolTip.add(String.format("Exchange Energy (Item): %s", energyValue)); // TODO Localize + event.toolTip.add(String.format("Exchange Energy (Stack of %s): %s", event.itemStack.stackSize, stackEnergyValue)); // TODO Localize } else { - event.toolTip.add(String.format("Exchange Energy: %s", energyValueDecimalFormat.format(wrappedItemStack.getStackSize() * energyValue.getValue()))); // TODO Localize + event.toolTip.add(String.format("Exchange Energy: %s", stackEnergyValue)); // TODO Localize if (FluidContainerRegistry.getFluidForFilledItem(event.itemStack) != null) { FluidStack fluidStack = FluidContainerRegistry.getFluidForFilledItem(event.itemStack); + EnergyValue fluidStackEnergyValue = EnergyValueRegistryProxy.getEnergyValueForStack(fluidStack); - if (EnergyValueRegistryProxy.getEnergyValueForStack(fluidStack) != null) { - - EnergyValue fluidStackEnergyValue = EnergyValueRegistryProxy.getEnergyValueForStack(fluidStack); - event.toolTip.add(String.format(" - Exchange Energy (%s): %s", fluidStack.getLocalizedName(), energyValueDecimalFormat.format(fluidStackEnergyValue.getValue()))); // TODO Localize - event.toolTip.add(String.format(" - Exchange Energy (Container): %s", energyValueDecimalFormat.format(energyValue.getValue() - fluidStackEnergyValue.getValue()))); // TODO Localize + if (fluidStackEnergyValue != null) { + event.toolTip.add(String.format(" - Exchange Energy (%smB of %s): %s", fluidStack.amount, fluidStack.getLocalizedName(), fluidStackEnergyValue)); // TODO Localize + event.toolTip.add(String.format(" - Exchange Energy (Container): %s", new EnergyValue(energyValue.getValue() - fluidStackEnergyValue.getValue()))); // TODO Localize } } } diff --git a/src/main/java/com/pahimar/ee3/exchange/OreStack.java b/src/main/java/com/pahimar/ee3/exchange/OreStack.java index 5e097d8f..ed96d1c3 100644 --- a/src/main/java/com/pahimar/ee3/exchange/OreStack.java +++ b/src/main/java/com/pahimar/ee3/exchange/OreStack.java @@ -50,6 +50,11 @@ public final class OreStack implements Comparable { this.stackSize = stackSize; } + public OreStack(OreStack oreStack) { + this.oreName = oreStack.oreName; + this.stackSize = oreStack.stackSize; + } + public static boolean compareOreNames(OreStack oreStack1, OreStack oreStack2) { if (oreStack1 != null && oreStack2 != null) { diff --git a/src/main/java/com/pahimar/ee3/exchange/WrappedStack.java b/src/main/java/com/pahimar/ee3/exchange/WrappedStack.java index 649eb1d6..d475904b 100644 --- a/src/main/java/com/pahimar/ee3/exchange/WrappedStack.java +++ b/src/main/java/com/pahimar/ee3/exchange/WrappedStack.java @@ -50,7 +50,7 @@ public final class WrappedStack implements Comparable { } else if (object instanceof OreStack) { - OreStack oreStack = (OreStack) object; + OreStack oreStack = new OreStack((OreStack) object); stackSize = oreStack.stackSize; oreStack.stackSize = 1; wrappedStack = oreStack; @@ -124,16 +124,14 @@ public final class WrappedStack implements Comparable { } else if (object instanceof OreStack) { - OreStack oreStack = (OreStack) object; + OreStack oreStack = new OreStack((OreStack) object); this.stackSize = stackSize; oreStack.stackSize = 1; wrappedStack = oreStack; } else if (object instanceof ArrayList) { - ArrayList objectList = (ArrayList) object; - - OreStack possibleOreStack = OreStack.getOreStackFrom(objectList); + OreStack possibleOreStack = OreStack.getOreStackFrom((ArrayList) object); if (possibleOreStack != null) { @@ -149,7 +147,7 @@ public final class WrappedStack implements Comparable { } else if (object instanceof FluidStack) { - FluidStack fluidStack = (FluidStack) object; + FluidStack fluidStack = ((FluidStack) object).copy(); this.stackSize = stackSize; fluidStack.amount = 1; wrappedStack = fluidStack;