Centralize the energy value decimal formatter, change EnergyValue.toString to use the formatter, and fix two mistakes in wrapping objects where the object being wrapped is modified rather than the copy of the object

This commit is contained in:
Pahimar 2016-05-28 14:57:10 -04:00
parent c36adabdee
commit adc3a79490
4 changed files with 21 additions and 86 deletions

View File

@ -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<EnergyValue>, JsonDeserializer<EnergyValue>, JsonSerializer<EnergyValue> {
public final class EnergyValue implements Comparable<EnergyValue> {
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<EnergyValue>, 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<EnergyValue>, 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.
* <p>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.
* <p/>
* <p>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).</p>
*
* @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<EnergyValue>, JsonDeseriali
return null;
}
}
}

View File

@ -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
}
}
}

View File

@ -50,6 +50,11 @@ public final class OreStack implements Comparable<OreStack> {
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) {

View File

@ -50,7 +50,7 @@ public final class WrappedStack implements Comparable<WrappedStack> {
}
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<WrappedStack> {
}
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<WrappedStack> {
}
else if (object instanceof FluidStack) {
FluidStack fluidStack = (FluidStack) object;
FluidStack fluidStack = ((FluidStack) object).copy();
this.stackSize = stackSize;
fluidStack.amount = 1;
wrappedStack = fluidStack;