Hopefully fixes Gson related crashes with FluidStacks
This commit is contained in:
parent
dd69c6ced7
commit
eb2b16eec7
|
@ -1,6 +1,6 @@
|
|||
#
|
||||
#Sat Dec 28 00:14:08 EST 2013
|
||||
minecraft_version = 1.7.10
|
||||
forge_version = 10.13.2.1291
|
||||
forge_version = 10.13.3.1395-1710ls
|
||||
mod_version = 0.3
|
||||
release_type = beta
|
||||
|
|
|
@ -0,0 +1,111 @@
|
|||
package com.pahimar.ee3.exchange;
|
||||
|
||||
import com.google.gson.*;
|
||||
import net.minecraft.nbt.JsonToNBT;
|
||||
import net.minecraft.nbt.NBTBase;
|
||||
import net.minecraft.nbt.NBTException;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class JsonFluidStack implements JsonSerializer<JsonFluidStack>, JsonDeserializer<JsonFluidStack>
|
||||
{
|
||||
public static final Gson jsonSerializer = (new GsonBuilder()).registerTypeAdapter(JsonFluidStack.class, new JsonFluidStack()).create();
|
||||
|
||||
public int fluidID;
|
||||
public int amount;
|
||||
public NBTTagCompound tag;
|
||||
|
||||
public JsonFluidStack()
|
||||
{
|
||||
this.fluidID = Integer.MIN_VALUE;
|
||||
this.amount = 0;
|
||||
this.tag = null;
|
||||
}
|
||||
|
||||
public JsonFluidStack(FluidStack fluidStack)
|
||||
{
|
||||
this.fluidID = fluidStack.fluidID;
|
||||
this.amount = fluidStack.amount;
|
||||
this.tag = fluidStack.tag;
|
||||
}
|
||||
|
||||
public JsonFluidStack(int fluidID, int amount, NBTTagCompound tag)
|
||||
{
|
||||
this.fluidID = fluidID;
|
||||
this.amount = amount;
|
||||
this.tag = tag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonFluidStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
if (json.isJsonObject())
|
||||
{
|
||||
JsonObject jsonObject = (JsonObject) json;
|
||||
JsonFluidStack jsonFluidStack = new JsonFluidStack();
|
||||
|
||||
if (jsonObject.has("fluidID"))
|
||||
{
|
||||
jsonFluidStack.fluidID = jsonObject.get("fluidID").getAsInt();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new JsonParseException(""); // TODO Exception message
|
||||
}
|
||||
|
||||
if (jsonObject.has("amount"))
|
||||
{
|
||||
jsonFluidStack.amount = jsonObject.get("amount").getAsInt();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new JsonParseException(""); // TODO Exception message
|
||||
}
|
||||
|
||||
if (jsonObject.has("tag"))
|
||||
{
|
||||
try
|
||||
{
|
||||
NBTBase nbtBase = JsonToNBT.func_150315_a(jsonObject.get("tag").getAsString());
|
||||
|
||||
if (nbtBase instanceof NBTTagCompound)
|
||||
{
|
||||
jsonFluidStack.tag = (NBTTagCompound) nbtBase;
|
||||
}
|
||||
}
|
||||
catch (NBTException e)
|
||||
{
|
||||
throw new JsonParseException(e.getMessage(), e.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
return jsonFluidStack;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(JsonFluidStack src, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
|
||||
jsonObject.addProperty("fluidID", src.fluidID);
|
||||
jsonObject.addProperty("amount", src.amount);
|
||||
|
||||
if (src.tag != null)
|
||||
{
|
||||
jsonObject.addProperty("tag", src.tag.toString());
|
||||
}
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return String.format("fluidID: %s, amount: %s, tag: %s", fluidID, amount, tag);
|
||||
}
|
||||
}
|
|
@ -442,7 +442,8 @@ public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<
|
|||
}
|
||||
else if (objectType.equalsIgnoreCase("FluidStack"))
|
||||
{
|
||||
FluidStack fluidStack = jsonSerializer.fromJson(jsonWrappedStack.get("objectData"), FluidStack.class);
|
||||
JsonFluidStack jsonFluidStack = JsonFluidStack.jsonSerializer.fromJson(jsonWrappedStack.get("objectData"), JsonFluidStack.class);
|
||||
FluidStack fluidStack = new FluidStack(jsonFluidStack.fluidID, jsonFluidStack.amount, jsonFluidStack.tag);
|
||||
|
||||
if (stackSize > 0)
|
||||
{
|
||||
|
@ -510,7 +511,8 @@ public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<
|
|||
}
|
||||
else if (wrappedStack.wrappedStack instanceof FluidStack)
|
||||
{
|
||||
jsonWrappedStack.add("objectData", gson.toJsonTree(wrappedStack.wrappedStack, FluidStack.class));
|
||||
JsonFluidStack jsonFluidStack = new JsonFluidStack((FluidStack) wrappedStack.wrappedStack);
|
||||
jsonWrappedStack.add("objectData", JsonFluidStack.jsonSerializer.toJsonTree(jsonFluidStack, JsonFluidStack.class));
|
||||
}
|
||||
|
||||
return jsonWrappedStack;
|
||||
|
|
Loading…
Reference in New Issue