Added in support for reading in custom pre/post value assignments from json files. More coming documentation/command wise to support this
This commit is contained in:
parent
67c704870e
commit
87ad5984c5
6 changed files with 608 additions and 88 deletions
|
@ -1,12 +1,21 @@
|
|||
package com.pahimar.ee3.api;
|
||||
|
||||
import com.google.gson.*;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
public final class EnergyValue implements Comparable<EnergyValue>
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public final class EnergyValue implements Comparable<EnergyValue>, JsonDeserializer<EnergyValue>, JsonSerializer<EnergyValue>
|
||||
{
|
||||
private static final Gson jsonSerializer = (new GsonBuilder()).registerTypeAdapter(EnergyValue.class, new EnergyValue()).create();
|
||||
private final float energyValue;
|
||||
private final EnergyType energyType;
|
||||
|
||||
public EnergyValue()
|
||||
{
|
||||
this(0f, EnergyType.UNKNOWN);
|
||||
}
|
||||
|
||||
public EnergyValue(int energyValue)
|
||||
{
|
||||
this((float) energyValue);
|
||||
|
@ -102,6 +111,101 @@ public final class EnergyValue implements Comparable<EnergyValue>
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes an EmcValue object from the given serialized json String
|
||||
*
|
||||
* @param jsonEnergyValue 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
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static EnergyValue createFromJson(String jsonEnergyValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
return jsonSerializer.fromJson(jsonEnergyValue, EnergyValue.class);
|
||||
}
|
||||
catch (JsonSyntaxException exception)
|
||||
{
|
||||
exception.printStackTrace();
|
||||
}
|
||||
catch (JsonParseException exception)
|
||||
{
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
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("type") != null && jsonEnergyValue.get("type").isJsonPrimitive() && jsonEnergyValue.get("value") != null && jsonEnergyValue.get("value").isJsonPrimitive())
|
||||
{
|
||||
EnergyType energyType = EnergyType.getEnergyTypeFromOrdinal(jsonEnergyValue.get("type").getAsInt());
|
||||
float energyValue = jsonEnergyValue.get("value").getAsFloat();
|
||||
|
||||
if (Float.compare(energyValue, 0f) > 0)
|
||||
{
|
||||
return new EnergyValue(energyValue, energyType);
|
||||
}
|
||||
}
|
||||
|
||||
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("type", energyValueObject.energyType.ordinal());
|
||||
jsonEnergyValue.addProperty("value", energyValueObject.energyValue);
|
||||
|
||||
return jsonEnergyValue;
|
||||
}
|
||||
|
||||
public static enum EnergyType
|
||||
{
|
||||
UNKNOWN, CORPOREAL, KINETIC, TEMPORAL, ESSENTIA, AMORPHOUS, VOID, OMNI;
|
||||
|
|
|
@ -363,6 +363,10 @@ public class EnergyValueRegistry implements INBTTaggable
|
|||
*/
|
||||
stackValueMap.putAll(preAssignedMappings);
|
||||
|
||||
// Grab custom pre-assigned values from file
|
||||
Map<WrappedStack, EnergyValue> preAssignedValueMap = SerializationHelper.readEnergyValueStackMapFromJsonFile("preAssignedValues.json");
|
||||
stackValueMap.putAll(preAssignedValueMap);
|
||||
|
||||
/*
|
||||
* Auto-assignment
|
||||
*/
|
||||
|
@ -434,6 +438,10 @@ public class EnergyValueRegistry implements INBTTaggable
|
|||
postAssignedMappings = new HashMap<WrappedStack, EnergyValue>();
|
||||
}
|
||||
|
||||
// Grab custom post-assigned values from file
|
||||
Map<WrappedStack, EnergyValue> postAssignedValueMap = SerializationHelper.readEnergyValueStackMapFromJsonFile("postAssignedValues.json");
|
||||
stackValueMap.putAll(postAssignedValueMap);
|
||||
|
||||
/**
|
||||
* Finalize the stack to value map
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,123 @@
|
|||
package com.pahimar.ee3.exchange;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.pahimar.ee3.api.EnergyValue;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class EnergyValueStackMapping implements JsonSerializer<EnergyValueStackMapping>, JsonDeserializer<EnergyValueStackMapping>
|
||||
{
|
||||
private static final Gson jsonSerializer = (new GsonBuilder()).setPrettyPrinting().registerTypeAdapter(EnergyValueStackMapping.class, new EnergyValueStackMapping()).registerTypeAdapter(EnergyValue.class, new EnergyValue()).registerTypeAdapter(WrappedStack.class, new WrappedStack()).create();
|
||||
|
||||
public final WrappedStack wrappedStack;
|
||||
public final EnergyValue energyValue;
|
||||
|
||||
public EnergyValueStackMapping()
|
||||
{
|
||||
wrappedStack = null;
|
||||
energyValue = null;
|
||||
}
|
||||
|
||||
public EnergyValueStackMapping(WrappedStack wrappedStack, EnergyValue energyValue)
|
||||
{
|
||||
this.wrappedStack = wrappedStack;
|
||||
this.energyValue = energyValue;
|
||||
}
|
||||
|
||||
public static EnergyValueStackMapping createFromJson(String jsonStackValueMapping)
|
||||
{
|
||||
try
|
||||
{
|
||||
return jsonSerializer.fromJson(jsonStackValueMapping, EnergyValueStackMapping.class);
|
||||
}
|
||||
catch (JsonSyntaxException exception)
|
||||
{
|
||||
exception.printStackTrace();
|
||||
}
|
||||
catch (JsonParseException exception)
|
||||
{
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
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 EnergyValueStackMapping deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
if (!jsonElement.isJsonPrimitive())
|
||||
{
|
||||
JsonObject jsonStackValueMapping = (JsonObject) jsonElement;
|
||||
|
||||
WrappedStack wrappedStack = null;
|
||||
EnergyValue energyValue = null;
|
||||
|
||||
if (jsonStackValueMapping.get("wrappedStack") != null)
|
||||
{
|
||||
wrappedStack = new WrappedStack().deserialize(jsonStackValueMapping.get("wrappedStack").getAsJsonObject(), typeOfT, context);
|
||||
}
|
||||
|
||||
if (jsonStackValueMapping.get("energyValue") != null)
|
||||
{
|
||||
energyValue = new EnergyValue().deserialize(jsonStackValueMapping.get("energyValue").getAsJsonObject(), typeOfT, context);
|
||||
}
|
||||
|
||||
if (wrappedStack != null && energyValue != null)
|
||||
{
|
||||
return new EnergyValueStackMapping(wrappedStack, energyValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
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 src} object. However, you should never invoke it on the
|
||||
* {@code src} object itself since that will cause an infinite loop (Gson will call your
|
||||
* call-back method again).</p>
|
||||
*
|
||||
* @param energyValueStackMapping 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(EnergyValueStackMapping energyValueStackMapping, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
JsonObject jsonStackValueMapping = new JsonObject();
|
||||
|
||||
jsonStackValueMapping.add("wrappedStack", jsonSerializer.toJsonTree(energyValueStackMapping.wrappedStack));
|
||||
jsonStackValueMapping.add("energyValue", jsonSerializer.toJsonTree(energyValueStackMapping.energyValue));
|
||||
|
||||
return jsonStackValueMapping;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.pahimar.ee3.exchange;
|
||||
|
||||
public class JsonItemStack
|
||||
{
|
||||
public String itemName;
|
||||
public int itemDamage;
|
||||
public int stackSize;
|
||||
public byte[] compressedStackTagCompound;
|
||||
}
|
|
@ -1,91 +1,34 @@
|
|||
package com.pahimar.ee3.exchange;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.pahimar.ee3.util.FluidHelper;
|
||||
import com.pahimar.ee3.util.ItemHelper;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompressedStreamTools;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class WrappedStack implements Comparable<WrappedStack>
|
||||
public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<WrappedStack>, JsonSerializer<WrappedStack>
|
||||
{
|
||||
private static final Gson jsonSerializer = (new GsonBuilder()).setPrettyPrinting().registerTypeAdapter(WrappedStack.class, new WrappedStack()).create();
|
||||
|
||||
private final String objectType;
|
||||
private final Object wrappedStack;
|
||||
public static Comparator<WrappedStack> comparator = new Comparator<WrappedStack>()
|
||||
{
|
||||
|
||||
@Override
|
||||
public int compare(WrappedStack wrappedStack1, WrappedStack wrappedStack2)
|
||||
{
|
||||
|
||||
if (wrappedStack1.wrappedStack instanceof ItemStack)
|
||||
{
|
||||
if (wrappedStack2.wrappedStack instanceof ItemStack)
|
||||
{
|
||||
return ItemHelper.compare((ItemStack) wrappedStack1.wrappedStack, (ItemStack) wrappedStack2.wrappedStack);
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else if (wrappedStack1.wrappedStack instanceof OreStack)
|
||||
{
|
||||
if (wrappedStack2.wrappedStack instanceof ItemStack)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (wrappedStack2.wrappedStack instanceof OreStack)
|
||||
{
|
||||
return OreStack.compare((OreStack) wrappedStack1.wrappedStack, (OreStack) wrappedStack2.wrappedStack);
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else if (wrappedStack1.wrappedStack instanceof FluidStack)
|
||||
{
|
||||
if (wrappedStack2.wrappedStack instanceof ItemStack || wrappedStack2.wrappedStack instanceof OreStack)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (wrappedStack2.wrappedStack instanceof FluidStack)
|
||||
{
|
||||
return FluidHelper.compare((FluidStack) wrappedStack1.wrappedStack, (FluidStack) wrappedStack2.wrappedStack);
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else if (wrappedStack1.wrappedStack == null)
|
||||
{
|
||||
if (wrappedStack2.wrappedStack != null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
private int stackSize;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public WrappedStack()
|
||||
{
|
||||
objectType = null;
|
||||
stackSize = -1;
|
||||
wrappedStack = null;
|
||||
}
|
||||
|
@ -110,12 +53,14 @@ public class WrappedStack implements Comparable<WrappedStack>
|
|||
if (((ItemStack) object).getItem() != null)
|
||||
{
|
||||
ItemStack itemStack = ((ItemStack) object).copy();
|
||||
objectType = "itemstack";
|
||||
stackSize = itemStack.stackSize;
|
||||
itemStack.stackSize = 1;
|
||||
wrappedStack = itemStack;
|
||||
}
|
||||
else
|
||||
{
|
||||
objectType = null;
|
||||
stackSize = -1;
|
||||
wrappedStack = null;
|
||||
}
|
||||
|
@ -123,7 +68,7 @@ public class WrappedStack implements Comparable<WrappedStack>
|
|||
else if (object instanceof OreStack)
|
||||
{
|
||||
OreStack oreStack = (OreStack) object;
|
||||
|
||||
objectType = "orestack";
|
||||
stackSize = oreStack.stackSize;
|
||||
oreStack.stackSize = 1;
|
||||
wrappedStack = oreStack;
|
||||
|
@ -136,12 +81,14 @@ public class WrappedStack implements Comparable<WrappedStack>
|
|||
|
||||
if (possibleOreStack != null)
|
||||
{
|
||||
objectType = "orestack";
|
||||
stackSize = possibleOreStack.stackSize;
|
||||
possibleOreStack.stackSize = 1;
|
||||
wrappedStack = possibleOreStack;
|
||||
}
|
||||
else
|
||||
{
|
||||
objectType = null;
|
||||
stackSize = -1;
|
||||
wrappedStack = null;
|
||||
}
|
||||
|
@ -149,7 +96,7 @@ public class WrappedStack implements Comparable<WrappedStack>
|
|||
else if (object instanceof FluidStack)
|
||||
{
|
||||
FluidStack fluidStack = ((FluidStack) object).copy();
|
||||
|
||||
objectType = "fluidstack";
|
||||
stackSize = fluidStack.amount;
|
||||
fluidStack.amount = 1;
|
||||
wrappedStack = fluidStack;
|
||||
|
@ -160,28 +107,25 @@ public class WrappedStack implements Comparable<WrappedStack>
|
|||
|
||||
if (wrappedStackObject.getWrappedStack() != null)
|
||||
{
|
||||
this.objectType = wrappedStackObject.objectType;
|
||||
this.stackSize = wrappedStackObject.stackSize;
|
||||
this.wrappedStack = wrappedStackObject.wrappedStack;
|
||||
}
|
||||
else
|
||||
{
|
||||
objectType = null;
|
||||
stackSize = -1;
|
||||
wrappedStack = null;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
objectType = null;
|
||||
stackSize = -1;
|
||||
wrappedStack = null;
|
||||
}
|
||||
}
|
||||
|
||||
public Object getWrappedStack()
|
||||
{
|
||||
|
||||
return wrappedStack;
|
||||
}
|
||||
|
||||
public WrappedStack(Object object, int stackSize)
|
||||
{
|
||||
if (object instanceof Item)
|
||||
|
@ -200,7 +144,7 @@ public class WrappedStack implements Comparable<WrappedStack>
|
|||
if (object instanceof ItemStack)
|
||||
{
|
||||
ItemStack itemStack = ((ItemStack) object).copy();
|
||||
|
||||
objectType = "itemstack";
|
||||
this.stackSize = stackSize;
|
||||
itemStack.stackSize = 1;
|
||||
wrappedStack = itemStack;
|
||||
|
@ -208,7 +152,7 @@ public class WrappedStack implements Comparable<WrappedStack>
|
|||
else if (object instanceof OreStack)
|
||||
{
|
||||
OreStack oreStack = (OreStack) object;
|
||||
|
||||
objectType = "orestack";
|
||||
this.stackSize = stackSize;
|
||||
oreStack.stackSize = 1;
|
||||
wrappedStack = oreStack;
|
||||
|
@ -221,12 +165,14 @@ public class WrappedStack implements Comparable<WrappedStack>
|
|||
|
||||
if (possibleOreStack != null)
|
||||
{
|
||||
objectType = "orestack";
|
||||
this.stackSize = stackSize;
|
||||
possibleOreStack.stackSize = 1;
|
||||
wrappedStack = possibleOreStack;
|
||||
}
|
||||
else
|
||||
{
|
||||
objectType = null;
|
||||
this.stackSize = -1;
|
||||
wrappedStack = null;
|
||||
}
|
||||
|
@ -234,7 +180,7 @@ public class WrappedStack implements Comparable<WrappedStack>
|
|||
else if (object instanceof FluidStack)
|
||||
{
|
||||
FluidStack fluidStack = (FluidStack) object;
|
||||
|
||||
objectType = "fluidstack";
|
||||
this.stackSize = stackSize;
|
||||
fluidStack.amount = 1;
|
||||
wrappedStack = fluidStack;
|
||||
|
@ -245,22 +191,30 @@ public class WrappedStack implements Comparable<WrappedStack>
|
|||
|
||||
if (wrappedStackObject.getWrappedStack() != null)
|
||||
{
|
||||
this.objectType = wrappedStackObject.objectType;
|
||||
this.stackSize = stackSize;
|
||||
this.wrappedStack = wrappedStackObject.wrappedStack;
|
||||
}
|
||||
else
|
||||
{
|
||||
objectType = null;
|
||||
this.stackSize = -1;
|
||||
wrappedStack = null;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
objectType = null;
|
||||
this.stackSize = -1;
|
||||
wrappedStack = null;
|
||||
}
|
||||
}
|
||||
|
||||
public Object getWrappedStack()
|
||||
{
|
||||
return wrappedStack;
|
||||
}
|
||||
|
||||
public static boolean canBeWrapped(Object object)
|
||||
{
|
||||
if (object instanceof WrappedStack)
|
||||
|
@ -299,13 +253,11 @@ public class WrappedStack implements Comparable<WrappedStack>
|
|||
|
||||
public int getStackSize()
|
||||
{
|
||||
|
||||
return stackSize;
|
||||
}
|
||||
|
||||
public void setStackSize(int stackSize)
|
||||
{
|
||||
|
||||
this.stackSize = stackSize;
|
||||
}
|
||||
|
||||
|
@ -377,6 +329,186 @@ public class WrappedStack implements Comparable<WrappedStack>
|
|||
return new WrappedStack();
|
||||
}
|
||||
|
||||
public static WrappedStack createFromJson(String jsonWrappedObject) throws JsonParseException
|
||||
{
|
||||
try
|
||||
{
|
||||
return jsonSerializer.fromJson(jsonWrappedObject, WrappedStack.class);
|
||||
}
|
||||
catch (JsonSyntaxException exception)
|
||||
{
|
||||
exception.printStackTrace();
|
||||
}
|
||||
catch (JsonParseException exception)
|
||||
{
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
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 WrappedStack deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
if (!jsonElement.isJsonPrimitive())
|
||||
{
|
||||
JsonObject jsonWrappedStack = (JsonObject) jsonElement;
|
||||
|
||||
int stackSize = -1;
|
||||
String objectType = null;
|
||||
Object stackObject = null;
|
||||
|
||||
if (jsonWrappedStack.get("type") != null)
|
||||
{
|
||||
objectType = jsonWrappedStack.get("type").getAsString();
|
||||
}
|
||||
|
||||
if (jsonWrappedStack.get("stackSize") != null)
|
||||
{
|
||||
stackSize = jsonWrappedStack.get("stackSize").getAsInt();
|
||||
}
|
||||
|
||||
if (jsonWrappedStack.get("data") != null && !jsonWrappedStack.get("data").isJsonPrimitive())
|
||||
{
|
||||
if (objectType != null)
|
||||
{
|
||||
if (objectType.equalsIgnoreCase("ItemStack"))
|
||||
{
|
||||
JsonItemStack jsonItemStack = jsonSerializer.fromJson(jsonWrappedStack.get("data"), JsonItemStack.class);
|
||||
ItemStack itemStack = null;
|
||||
Item item = (Item) Item.itemRegistry.getObject(jsonItemStack.itemName);
|
||||
if (stackSize > 0 && item != null)
|
||||
{
|
||||
itemStack = new ItemStack(item, stackSize, jsonItemStack.itemDamage);
|
||||
if (jsonItemStack.compressedStackTagCompound != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
itemStack.stackTagCompound = CompressedStreamTools.readCompressed(new ByteArrayInputStream(jsonItemStack.compressedStackTagCompound));
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
stackObject = itemStack;
|
||||
}
|
||||
else if (objectType.equalsIgnoreCase("OreStack"))
|
||||
{
|
||||
OreStack oreStack = jsonSerializer.fromJson(jsonWrappedStack.get("data"), OreStack.class);
|
||||
|
||||
if (stackSize > 0)
|
||||
{
|
||||
oreStack.stackSize = stackSize;
|
||||
}
|
||||
stackObject = oreStack;
|
||||
}
|
||||
else if (objectType.equalsIgnoreCase("FluidStack"))
|
||||
{
|
||||
FluidStack fluidStack = jsonSerializer.fromJson(jsonWrappedStack.get("data"), FluidStack.class);
|
||||
|
||||
if (stackSize > 0)
|
||||
{
|
||||
fluidStack.amount = stackSize;
|
||||
}
|
||||
stackObject = fluidStack;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (stackObject != null)
|
||||
{
|
||||
return new WrappedStack(stackObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new JsonParseException(String.format("Unable to parse a wrappable stack object from the provided json: %s", jsonElement.toString()));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new JsonParseException(String.format("Unable to parse a wrappable stack object from the provided json: %s", jsonElement.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 wrappedStack} object. However, you should never invoke it on the
|
||||
* {@code wrappedStack} object itself since that will cause an infinite loop (Gson will call your
|
||||
* call-back method again).</p>
|
||||
*
|
||||
* @param wrappedStack 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(WrappedStack wrappedStack, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
JsonObject jsonWrappedStack = new JsonObject();
|
||||
|
||||
Gson gson = new Gson();
|
||||
|
||||
jsonWrappedStack.addProperty("type", wrappedStack.objectType);
|
||||
jsonWrappedStack.addProperty("stackSize", wrappedStack.stackSize);
|
||||
|
||||
if (wrappedStack.wrappedStack instanceof ItemStack)
|
||||
{
|
||||
JsonItemStack jsonItemStack = new JsonItemStack();
|
||||
jsonItemStack.itemName = Item.itemRegistry.getNameForObject(((ItemStack) wrappedStack.wrappedStack).getItem());
|
||||
jsonItemStack.itemDamage = ((ItemStack) wrappedStack.wrappedStack).getItemDamage();
|
||||
jsonItemStack.stackSize = ((ItemStack) wrappedStack.wrappedStack).stackSize;
|
||||
if (((ItemStack) wrappedStack.wrappedStack).stackTagCompound != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
jsonItemStack.compressedStackTagCompound = CompressedStreamTools.compress(((ItemStack) wrappedStack.wrappedStack).stackTagCompound);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
jsonWrappedStack.add("data", gson.toJsonTree(jsonItemStack, JsonItemStack.class));
|
||||
}
|
||||
else if (wrappedStack.wrappedStack instanceof OreStack)
|
||||
{
|
||||
jsonWrappedStack.add("data", gson.toJsonTree(wrappedStack.wrappedStack, OreStack.class));
|
||||
}
|
||||
else if (wrappedStack.wrappedStack instanceof FluidStack)
|
||||
{
|
||||
jsonWrappedStack.add("data", gson.toJsonTree(wrappedStack.wrappedStack, FluidStack.class));
|
||||
}
|
||||
|
||||
return jsonWrappedStack;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
@ -458,4 +590,68 @@ public class WrappedStack implements Comparable<WrappedStack>
|
|||
return "null";
|
||||
}
|
||||
}
|
||||
|
||||
public static Comparator<WrappedStack> comparator = new Comparator<WrappedStack>()
|
||||
{
|
||||
|
||||
@Override
|
||||
public int compare(WrappedStack wrappedStack1, WrappedStack wrappedStack2)
|
||||
{
|
||||
|
||||
if (wrappedStack1.wrappedStack instanceof ItemStack)
|
||||
{
|
||||
if (wrappedStack2.wrappedStack instanceof ItemStack)
|
||||
{
|
||||
return ItemHelper.compare((ItemStack) wrappedStack1.wrappedStack, (ItemStack) wrappedStack2.wrappedStack);
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else if (wrappedStack1.wrappedStack instanceof OreStack)
|
||||
{
|
||||
if (wrappedStack2.wrappedStack instanceof ItemStack)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (wrappedStack2.wrappedStack instanceof OreStack)
|
||||
{
|
||||
return OreStack.compare((OreStack) wrappedStack1.wrappedStack, (OreStack) wrappedStack2.wrappedStack);
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else if (wrappedStack1.wrappedStack instanceof FluidStack)
|
||||
{
|
||||
if (wrappedStack2.wrappedStack instanceof ItemStack || wrappedStack2.wrappedStack instanceof OreStack)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (wrappedStack2.wrappedStack instanceof FluidStack)
|
||||
{
|
||||
return FluidHelper.compare((FluidStack) wrappedStack1.wrappedStack, (FluidStack) wrappedStack2.wrappedStack);
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else if (wrappedStack1.wrappedStack == null)
|
||||
{
|
||||
if (wrappedStack2.wrappedStack != null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,6 +1,13 @@
|
|||
package com.pahimar.ee3.util;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import com.pahimar.ee3.api.EnergyValue;
|
||||
import com.pahimar.ee3.exchange.EnergyValueRegistry;
|
||||
import com.pahimar.ee3.exchange.EnergyValueStackMapping;
|
||||
import com.pahimar.ee3.exchange.WrappedStack;
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import cpw.mods.fml.common.Loader;
|
||||
import cpw.mods.fml.common.ModContainer;
|
||||
|
@ -8,16 +15,13 @@ import net.minecraft.nbt.CompressedStreamTools;
|
|||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class SerializationHelper
|
||||
{
|
||||
private static final Gson jsonSerializer = (new GsonBuilder()).setPrettyPrinting().registerTypeAdapter(EnergyValueStackMapping.class, new EnergyValueStackMapping()).registerTypeAdapter(EnergyValue.class, new EnergyValue()).registerTypeAdapter(WrappedStack.class, new WrappedStack()).create();
|
||||
|
||||
public static String getModListMD5()
|
||||
{
|
||||
List<String> modList = new ArrayList<String>();
|
||||
|
@ -112,4 +116,80 @@ public class SerializationHelper
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<WrappedStack, EnergyValue> readEnergyValueStackMapFromJsonFile(String fileName)
|
||||
{
|
||||
return readEnergyValueStackMapFromJsonFile(getFileInDataDirectory(fileName));
|
||||
}
|
||||
|
||||
public static Map<WrappedStack, EnergyValue> readEnergyValueStackMapFromJsonFile(File jsonFile)
|
||||
{
|
||||
Map<WrappedStack, EnergyValue> energyValueStackMap = new TreeMap<WrappedStack, EnergyValue>();
|
||||
JsonReader jsonReader;
|
||||
|
||||
try
|
||||
{
|
||||
jsonReader = new JsonReader(new FileReader(jsonFile));
|
||||
jsonReader.beginArray();
|
||||
while (jsonReader.hasNext())
|
||||
{
|
||||
EnergyValueStackMapping energyValueStackMapping = jsonSerializer.fromJson(jsonReader, EnergyValueStackMapping.class);
|
||||
energyValueStackMap.put(energyValueStackMapping.wrappedStack, energyValueStackMapping.energyValue);
|
||||
}
|
||||
jsonReader.endArray();
|
||||
jsonReader.close();
|
||||
}
|
||||
catch (FileNotFoundException e)
|
||||
{
|
||||
// TODO More intelligent log message
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return energyValueStackMap;
|
||||
}
|
||||
|
||||
public static void writeEnergyValueStackMapToJsonFile(String fileName, Map<WrappedStack, EnergyValue> energyValueMap)
|
||||
{
|
||||
writeEnergyValueStackMapToJsonFile(getFileInDataDirectory(fileName), energyValueMap);
|
||||
}
|
||||
|
||||
public static void writeEnergyValueStackMapToJsonFile(File jsonFile, Map<WrappedStack, EnergyValue> energyValueMap)
|
||||
{
|
||||
JsonWriter jsonWriter;
|
||||
|
||||
try
|
||||
{
|
||||
jsonWriter = new JsonWriter(new FileWriter(jsonFile));
|
||||
jsonWriter.setIndent(" ");
|
||||
jsonWriter.beginArray();
|
||||
for (WrappedStack wrappedStack : energyValueMap.keySet())
|
||||
{
|
||||
jsonSerializer.toJson(new EnergyValueStackMapping(wrappedStack, energyValueMap.get(wrappedStack)), EnergyValueStackMapping.class, jsonWriter);
|
||||
}
|
||||
|
||||
jsonWriter.endArray();
|
||||
jsonWriter.close();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static File getFileInDataDirectory(String fileName)
|
||||
{
|
||||
if (FMLCommonHandler.instance().getMinecraftServerInstance() != null && FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld() != null)
|
||||
{
|
||||
File dataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + "ee3");
|
||||
File dataFile = new File(dataDirectory, fileName);
|
||||
|
||||
return dataFile;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue