2014-07-07 21:31:10 +02:00
|
|
|
package com.pahimar.ee3.exchange;
|
2014-06-14 21:40:45 +02:00
|
|
|
|
2014-09-12 22:11:18 +02:00
|
|
|
import com.google.gson.*;
|
2014-06-14 21:40:45 +02:00
|
|
|
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;
|
2014-08-29 22:25:31 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2014-06-14 21:40:45 +02:00
|
|
|
import net.minecraftforge.fluids.Fluid;
|
|
|
|
import net.minecraftforge.fluids.FluidStack;
|
|
|
|
|
2014-09-12 22:11:18 +02:00
|
|
|
import java.lang.reflect.Type;
|
2014-06-14 21:40:45 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.List;
|
|
|
|
|
2014-09-12 22:11:18 +02:00
|
|
|
public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<WrappedStack>, JsonSerializer<WrappedStack>
|
2014-06-14 21:40:45 +02:00
|
|
|
{
|
2015-02-08 05:12:25 +01:00
|
|
|
public static final Gson jsonSerializer = (new GsonBuilder()).setPrettyPrinting().registerTypeAdapter(WrappedStack.class, new WrappedStack()).create();
|
2014-06-14 21:40:45 +02:00
|
|
|
|
2014-09-12 22:11:18 +02:00
|
|
|
private final String objectType;
|
|
|
|
private final Object wrappedStack;
|
2014-06-14 21:40:45 +02:00
|
|
|
private int stackSize;
|
|
|
|
|
|
|
|
public WrappedStack()
|
|
|
|
{
|
2014-09-12 22:11:18 +02:00
|
|
|
objectType = null;
|
2014-06-14 21:40:45 +02:00
|
|
|
stackSize = -1;
|
|
|
|
wrappedStack = null;
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:01:35 +02:00
|
|
|
private WrappedStack(Object object)
|
2014-06-14 21:40:45 +02:00
|
|
|
{
|
|
|
|
if (object instanceof Item)
|
|
|
|
{
|
|
|
|
object = new ItemStack((Item) object);
|
|
|
|
}
|
|
|
|
else if (object instanceof Block)
|
|
|
|
{
|
|
|
|
object = new ItemStack((Block) object);
|
|
|
|
}
|
|
|
|
else if (object instanceof Fluid)
|
|
|
|
{
|
|
|
|
object = new FluidStack((Fluid) object, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object instanceof ItemStack)
|
|
|
|
{
|
2014-07-21 15:59:40 +02:00
|
|
|
if (((ItemStack) object).getItem() != null)
|
|
|
|
{
|
2015-04-09 18:40:19 +02:00
|
|
|
ItemStack itemStackObject = (ItemStack) object;
|
|
|
|
ItemStack itemStack = new ItemStack(itemStackObject.getItem(), itemStackObject.stackSize, itemStackObject.getItemDamage());
|
|
|
|
if (itemStackObject.stackTagCompound != null)
|
|
|
|
{
|
|
|
|
itemStack.stackTagCompound = (NBTTagCompound) itemStackObject.stackTagCompound.copy();
|
|
|
|
}
|
2014-09-12 22:11:18 +02:00
|
|
|
objectType = "itemstack";
|
2014-07-21 15:59:40 +02:00
|
|
|
stackSize = itemStack.stackSize;
|
|
|
|
itemStack.stackSize = 1;
|
|
|
|
wrappedStack = itemStack;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-12 22:11:18 +02:00
|
|
|
objectType = null;
|
2014-07-21 15:59:40 +02:00
|
|
|
stackSize = -1;
|
|
|
|
wrappedStack = null;
|
|
|
|
}
|
2014-06-14 21:40:45 +02:00
|
|
|
}
|
|
|
|
else if (object instanceof OreStack)
|
|
|
|
{
|
|
|
|
OreStack oreStack = (OreStack) object;
|
2014-09-12 22:11:18 +02:00
|
|
|
objectType = "orestack";
|
2014-06-14 21:40:45 +02:00
|
|
|
stackSize = oreStack.stackSize;
|
|
|
|
oreStack.stackSize = 1;
|
|
|
|
wrappedStack = oreStack;
|
|
|
|
}
|
|
|
|
else if (object instanceof ArrayList)
|
|
|
|
{
|
|
|
|
ArrayList<?> objectList = (ArrayList<?>) object;
|
|
|
|
|
|
|
|
OreStack possibleOreStack = OreStack.getOreStackFromList(objectList);
|
|
|
|
|
|
|
|
if (possibleOreStack != null)
|
|
|
|
{
|
2014-09-12 22:11:18 +02:00
|
|
|
objectType = "orestack";
|
2014-06-14 21:40:45 +02:00
|
|
|
stackSize = possibleOreStack.stackSize;
|
|
|
|
possibleOreStack.stackSize = 1;
|
|
|
|
wrappedStack = possibleOreStack;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-12 22:11:18 +02:00
|
|
|
objectType = null;
|
2014-06-14 21:40:45 +02:00
|
|
|
stackSize = -1;
|
|
|
|
wrappedStack = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (object instanceof FluidStack)
|
|
|
|
{
|
|
|
|
FluidStack fluidStack = ((FluidStack) object).copy();
|
2014-09-12 22:11:18 +02:00
|
|
|
objectType = "fluidstack";
|
2014-06-14 21:40:45 +02:00
|
|
|
stackSize = fluidStack.amount;
|
|
|
|
fluidStack.amount = 1;
|
|
|
|
wrappedStack = fluidStack;
|
|
|
|
}
|
|
|
|
else if (object instanceof WrappedStack)
|
|
|
|
{
|
|
|
|
WrappedStack wrappedStackObject = (WrappedStack) object;
|
|
|
|
|
2015-04-14 04:27:11 +02:00
|
|
|
if (wrappedStackObject.getWrappedObject() != null)
|
2014-06-14 21:40:45 +02:00
|
|
|
{
|
2014-09-12 22:11:18 +02:00
|
|
|
this.objectType = wrappedStackObject.objectType;
|
2014-06-14 21:40:45 +02:00
|
|
|
this.stackSize = wrappedStackObject.stackSize;
|
|
|
|
this.wrappedStack = wrappedStackObject.wrappedStack;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-12 22:11:18 +02:00
|
|
|
objectType = null;
|
2014-06-14 21:40:45 +02:00
|
|
|
stackSize = -1;
|
|
|
|
wrappedStack = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-12 22:11:18 +02:00
|
|
|
objectType = null;
|
2014-06-14 21:40:45 +02:00
|
|
|
stackSize = -1;
|
|
|
|
wrappedStack = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:01:35 +02:00
|
|
|
private WrappedStack(Object object, int stackSize)
|
2014-06-14 21:40:45 +02:00
|
|
|
{
|
|
|
|
if (object instanceof Item)
|
|
|
|
{
|
|
|
|
object = new ItemStack((Item) object);
|
|
|
|
}
|
|
|
|
else if (object instanceof Block)
|
|
|
|
{
|
|
|
|
object = new ItemStack((Block) object);
|
|
|
|
}
|
|
|
|
else if (object instanceof Fluid)
|
|
|
|
{
|
|
|
|
object = new FluidStack((Fluid) object, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object instanceof ItemStack)
|
|
|
|
{
|
|
|
|
ItemStack itemStack = ((ItemStack) object).copy();
|
2014-09-12 22:11:18 +02:00
|
|
|
objectType = "itemstack";
|
2014-06-14 21:40:45 +02:00
|
|
|
this.stackSize = stackSize;
|
|
|
|
itemStack.stackSize = 1;
|
|
|
|
wrappedStack = itemStack;
|
|
|
|
}
|
|
|
|
else if (object instanceof OreStack)
|
|
|
|
{
|
|
|
|
OreStack oreStack = (OreStack) object;
|
2014-09-12 22:11:18 +02:00
|
|
|
objectType = "orestack";
|
2014-06-14 21:40:45 +02:00
|
|
|
this.stackSize = stackSize;
|
|
|
|
oreStack.stackSize = 1;
|
|
|
|
wrappedStack = oreStack;
|
|
|
|
}
|
|
|
|
else if (object instanceof ArrayList)
|
|
|
|
{
|
|
|
|
ArrayList<?> objectList = (ArrayList<?>) object;
|
|
|
|
|
|
|
|
OreStack possibleOreStack = OreStack.getOreStackFromList(objectList);
|
|
|
|
|
|
|
|
if (possibleOreStack != null)
|
|
|
|
{
|
2014-09-12 22:11:18 +02:00
|
|
|
objectType = "orestack";
|
2014-06-14 21:40:45 +02:00
|
|
|
this.stackSize = stackSize;
|
|
|
|
possibleOreStack.stackSize = 1;
|
|
|
|
wrappedStack = possibleOreStack;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-12 22:11:18 +02:00
|
|
|
objectType = null;
|
2014-06-14 21:40:45 +02:00
|
|
|
this.stackSize = -1;
|
|
|
|
wrappedStack = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (object instanceof FluidStack)
|
|
|
|
{
|
|
|
|
FluidStack fluidStack = (FluidStack) object;
|
2014-09-12 22:11:18 +02:00
|
|
|
objectType = "fluidstack";
|
2014-06-14 21:40:45 +02:00
|
|
|
this.stackSize = stackSize;
|
|
|
|
fluidStack.amount = 1;
|
|
|
|
wrappedStack = fluidStack;
|
|
|
|
}
|
|
|
|
else if (object instanceof WrappedStack)
|
|
|
|
{
|
|
|
|
WrappedStack wrappedStackObject = (WrappedStack) object;
|
|
|
|
|
2015-04-14 04:27:11 +02:00
|
|
|
if (wrappedStackObject.getWrappedObject() != null)
|
2014-06-14 21:40:45 +02:00
|
|
|
{
|
2014-09-12 22:11:18 +02:00
|
|
|
this.objectType = wrappedStackObject.objectType;
|
2014-06-14 21:40:45 +02:00
|
|
|
this.stackSize = stackSize;
|
|
|
|
this.wrappedStack = wrappedStackObject.wrappedStack;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-12 22:11:18 +02:00
|
|
|
objectType = null;
|
2014-06-14 21:40:45 +02:00
|
|
|
this.stackSize = -1;
|
|
|
|
wrappedStack = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-12 22:11:18 +02:00
|
|
|
objectType = null;
|
2014-06-14 21:40:45 +02:00
|
|
|
this.stackSize = -1;
|
|
|
|
wrappedStack = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-14 04:27:11 +02:00
|
|
|
public Object getWrappedObject()
|
2014-09-12 22:11:18 +02:00
|
|
|
{
|
|
|
|
return wrappedStack;
|
|
|
|
}
|
|
|
|
|
2014-06-14 21:40:45 +02:00
|
|
|
public static boolean canBeWrapped(Object object)
|
|
|
|
{
|
|
|
|
if (object instanceof WrappedStack)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2014-07-21 15:59:40 +02:00
|
|
|
else if (object instanceof Item || object instanceof Block)
|
2014-06-14 21:40:45 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2014-07-21 15:59:40 +02:00
|
|
|
else if (object instanceof ItemStack)
|
|
|
|
{
|
2014-08-29 22:25:31 +02:00
|
|
|
if (((ItemStack) object).getItem() != null)
|
2014-07-21 15:59:40 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-06-14 21:40:45 +02:00
|
|
|
else if (object instanceof OreStack)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (object instanceof List)
|
|
|
|
{
|
|
|
|
if (OreStack.getOreStackFromList((List<?>) object) != null)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (object instanceof Fluid || object instanceof FluidStack)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getStackSize()
|
|
|
|
{
|
|
|
|
return stackSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setStackSize(int stackSize)
|
|
|
|
{
|
|
|
|
this.stackSize = stackSize;
|
|
|
|
}
|
|
|
|
|
2014-08-29 22:25:31 +02:00
|
|
|
public static NBTTagCompound toNBTTagCompound(WrappedStack wrappedStack)
|
|
|
|
{
|
2015-04-14 04:27:11 +02:00
|
|
|
if (wrappedStack != null && wrappedStack.getWrappedObject() != null)
|
2014-08-29 22:25:31 +02:00
|
|
|
{
|
|
|
|
NBTTagCompound wrappedStackTagCompound = new NBTTagCompound();
|
2015-04-14 04:27:11 +02:00
|
|
|
if (wrappedStack.getWrappedObject() instanceof ItemStack)
|
2014-08-29 22:25:31 +02:00
|
|
|
{
|
|
|
|
NBTTagCompound wrappedItemTagCompound = new NBTTagCompound();
|
2015-04-14 04:27:11 +02:00
|
|
|
((ItemStack) wrappedStack.getWrappedObject()).writeToNBT(wrappedItemTagCompound);
|
2015-02-08 05:12:25 +01:00
|
|
|
wrappedStackTagCompound.setInteger("wrappedStack_type", 0);
|
|
|
|
wrappedStackTagCompound.setTag("wrappedStack_data", wrappedItemTagCompound);
|
|
|
|
wrappedStackTagCompound.setInteger("wrappedStack_stackSize", wrappedStack.getStackSize());
|
2014-08-29 22:25:31 +02:00
|
|
|
return wrappedStackTagCompound;
|
|
|
|
}
|
2015-04-14 04:27:11 +02:00
|
|
|
else if (wrappedStack.getWrappedObject() instanceof OreStack)
|
2014-08-29 22:25:31 +02:00
|
|
|
{
|
|
|
|
NBTTagCompound wrappedOreTagCompound = new NBTTagCompound();
|
2015-04-14 04:27:11 +02:00
|
|
|
((OreStack) wrappedStack.getWrappedObject()).writeToNBT(wrappedOreTagCompound);
|
2015-02-08 05:12:25 +01:00
|
|
|
wrappedStackTagCompound.setInteger("wrappedStack_type", 1);
|
|
|
|
wrappedStackTagCompound.setTag("wrappedStack_data", wrappedOreTagCompound);
|
|
|
|
wrappedStackTagCompound.setInteger("wrappedStack_stackSize", wrappedStack.getStackSize());
|
2014-08-29 22:25:31 +02:00
|
|
|
return wrappedStackTagCompound;
|
|
|
|
}
|
2015-04-14 04:27:11 +02:00
|
|
|
else if (wrappedStack.getWrappedObject() instanceof FluidStack)
|
2014-08-29 22:25:31 +02:00
|
|
|
{
|
|
|
|
NBTTagCompound wrappedFluidTagCompound = new NBTTagCompound();
|
2015-04-14 04:27:11 +02:00
|
|
|
((FluidStack) wrappedStack.getWrappedObject()).writeToNBT(wrappedFluidTagCompound);
|
2015-02-08 05:12:25 +01:00
|
|
|
wrappedStackTagCompound.setInteger("wrappedStack_type", 2);
|
|
|
|
wrappedStackTagCompound.setTag("wrappedStack_data", wrappedFluidTagCompound);
|
|
|
|
wrappedStackTagCompound.setInteger("wrappedStack_stackSize", wrappedStack.getStackSize());
|
2014-08-29 22:25:31 +02:00
|
|
|
return wrappedStackTagCompound;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static WrappedStack fromNBTTagCompound(NBTTagCompound nbtTagCompound)
|
|
|
|
{
|
2015-02-08 05:12:25 +01:00
|
|
|
if (nbtTagCompound.hasKey("wrappedStack_type") && nbtTagCompound.hasKey("wrappedStack_data") && nbtTagCompound.hasKey("wrappedStack_stackSize"))
|
2014-08-29 22:25:31 +02:00
|
|
|
{
|
2015-02-08 05:12:25 +01:00
|
|
|
int objectType = nbtTagCompound.getInteger("wrappedStack_type");
|
|
|
|
int stackSize = nbtTagCompound.getInteger("wrappedStack_stackSize");
|
2014-08-29 22:25:31 +02:00
|
|
|
|
|
|
|
if (objectType == 0)
|
|
|
|
{
|
2015-02-08 05:12:25 +01:00
|
|
|
ItemStack itemStack = ItemStack.loadItemStackFromNBT(nbtTagCompound.getCompoundTag("wrappedStack_data"));
|
2014-08-29 22:25:31 +02:00
|
|
|
return new WrappedStack(itemStack, stackSize);
|
|
|
|
}
|
|
|
|
else if (objectType == 1)
|
|
|
|
{
|
2015-02-08 05:12:25 +01:00
|
|
|
OreStack oreStack = OreStack.loadOreStackFromNBT(nbtTagCompound.getCompoundTag("wrappedStack_data"));
|
2014-08-29 22:25:31 +02:00
|
|
|
return new WrappedStack(oreStack, stackSize);
|
|
|
|
}
|
|
|
|
else if (objectType == 2)
|
|
|
|
{
|
2015-02-08 05:12:25 +01:00
|
|
|
FluidStack fluidStack = FluidStack.loadFluidStackFromNBT(nbtTagCompound.getCompoundTag("wrappedStack_data"));
|
2014-08-29 22:25:31 +02:00
|
|
|
return new WrappedStack(fluidStack, stackSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return new WrappedStack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new WrappedStack();
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:01:35 +02:00
|
|
|
public static WrappedStack wrap(Object object)
|
|
|
|
{
|
|
|
|
if (canBeWrapped(object))
|
|
|
|
{
|
|
|
|
return new WrappedStack(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static WrappedStack wrap(Object object, int stackSize)
|
|
|
|
{
|
|
|
|
if (canBeWrapped(object))
|
|
|
|
{
|
|
|
|
return new WrappedStack(object, stackSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-09-12 22:11:18 +02:00
|
|
|
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).
|
|
|
|
*
|
2014-10-21 21:59:00 +02:00
|
|
|
* @param jsonElement The Json data being deserialized
|
|
|
|
* @param typeOfT The type of the Object to deserialize to
|
2014-09-12 22:11:18 +02:00
|
|
|
* @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;
|
|
|
|
|
2015-02-08 05:12:25 +01:00
|
|
|
int stackSize = 1;
|
2014-09-12 22:11:18 +02:00
|
|
|
String objectType = null;
|
|
|
|
Object stackObject = null;
|
|
|
|
|
2015-02-08 05:12:25 +01:00
|
|
|
if (jsonWrappedStack.get("wrappedStack_type") != null)
|
2014-09-12 22:11:18 +02:00
|
|
|
{
|
2015-02-08 05:12:25 +01:00
|
|
|
objectType = jsonWrappedStack.get("wrappedStack_type").getAsString();
|
2014-09-12 22:11:18 +02:00
|
|
|
}
|
|
|
|
|
2015-02-08 05:12:25 +01:00
|
|
|
if (jsonWrappedStack.get("wrappedStack_stackSize") != null)
|
2014-09-12 22:11:18 +02:00
|
|
|
{
|
2015-02-08 05:12:25 +01:00
|
|
|
stackSize = jsonWrappedStack.get("wrappedStack_stackSize").getAsInt();
|
2014-09-12 22:11:18 +02:00
|
|
|
}
|
|
|
|
|
2015-02-08 05:12:25 +01:00
|
|
|
if (jsonWrappedStack.get("wrappedStack_data") != null && !jsonWrappedStack.get("wrappedStack_data").isJsonPrimitive())
|
2014-09-12 22:11:18 +02:00
|
|
|
{
|
|
|
|
if (objectType != null)
|
|
|
|
{
|
|
|
|
if (objectType.equalsIgnoreCase("ItemStack"))
|
|
|
|
{
|
2015-04-09 18:40:19 +02:00
|
|
|
JsonItemStack jsonItemStack = JsonItemStack.jsonSerializer.fromJson(jsonWrappedStack.get("wrappedStack_data"), JsonItemStack.class);
|
2014-09-12 22:11:18 +02:00
|
|
|
ItemStack itemStack = null;
|
|
|
|
Item item = (Item) Item.itemRegistry.getObject(jsonItemStack.itemName);
|
|
|
|
if (stackSize > 0 && item != null)
|
|
|
|
{
|
|
|
|
itemStack = new ItemStack(item, stackSize, jsonItemStack.itemDamage);
|
2015-02-01 05:47:45 +01:00
|
|
|
if (jsonItemStack.itemNBTTagCompound != null)
|
2014-09-12 22:11:18 +02:00
|
|
|
{
|
2015-02-01 05:47:45 +01:00
|
|
|
itemStack.stackTagCompound = jsonItemStack.itemNBTTagCompound;
|
2014-09-12 22:11:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
stackObject = itemStack;
|
|
|
|
}
|
|
|
|
else if (objectType.equalsIgnoreCase("OreStack"))
|
|
|
|
{
|
2015-02-08 05:12:25 +01:00
|
|
|
OreStack oreStack = jsonSerializer.fromJson(jsonWrappedStack.get("wrappedStack_data"), OreStack.class);
|
2014-09-12 22:11:18 +02:00
|
|
|
|
|
|
|
if (stackSize > 0)
|
|
|
|
{
|
|
|
|
oreStack.stackSize = stackSize;
|
|
|
|
}
|
|
|
|
stackObject = oreStack;
|
|
|
|
}
|
|
|
|
else if (objectType.equalsIgnoreCase("FluidStack"))
|
|
|
|
{
|
2015-02-08 05:12:25 +01:00
|
|
|
FluidStack fluidStack = jsonSerializer.fromJson(jsonWrappedStack.get("wrappedStack_data"), FluidStack.class);
|
2014-09-12 22:11:18 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2015-02-08 05:12:25 +01:00
|
|
|
jsonWrappedStack.addProperty("wrappedStack_type", wrappedStack.objectType);
|
|
|
|
jsonWrappedStack.addProperty("wrappedStack_stackSize", wrappedStack.stackSize);
|
2014-09-12 22:11:18 +02:00
|
|
|
|
|
|
|
if (wrappedStack.wrappedStack instanceof ItemStack)
|
|
|
|
{
|
|
|
|
JsonItemStack jsonItemStack = new JsonItemStack();
|
|
|
|
jsonItemStack.itemName = Item.itemRegistry.getNameForObject(((ItemStack) wrappedStack.wrappedStack).getItem());
|
|
|
|
jsonItemStack.itemDamage = ((ItemStack) wrappedStack.wrappedStack).getItemDamage();
|
|
|
|
if (((ItemStack) wrappedStack.wrappedStack).stackTagCompound != null)
|
|
|
|
{
|
2015-02-01 05:47:45 +01:00
|
|
|
jsonItemStack.itemNBTTagCompound = ((ItemStack) wrappedStack.wrappedStack).stackTagCompound;
|
2014-09-12 22:11:18 +02:00
|
|
|
}
|
2015-04-09 18:40:19 +02:00
|
|
|
jsonWrappedStack.add("wrappedStack_data", JsonItemStack.jsonSerializer.toJsonTree(jsonItemStack, JsonItemStack.class));
|
2014-09-12 22:11:18 +02:00
|
|
|
}
|
|
|
|
else if (wrappedStack.wrappedStack instanceof OreStack)
|
|
|
|
{
|
2015-02-08 05:12:25 +01:00
|
|
|
jsonWrappedStack.add("wrappedStack_data", gson.toJsonTree(wrappedStack.wrappedStack, OreStack.class));
|
2014-09-12 22:11:18 +02:00
|
|
|
}
|
|
|
|
else if (wrappedStack.wrappedStack instanceof FluidStack)
|
|
|
|
{
|
2015-02-08 05:12:25 +01:00
|
|
|
jsonWrappedStack.add("wrappedStack_data", gson.toJsonTree(wrappedStack.wrappedStack, FluidStack.class));
|
2014-09-12 22:11:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return jsonWrappedStack;
|
|
|
|
}
|
|
|
|
|
2014-06-14 21:40:45 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int hashCode()
|
|
|
|
{
|
|
|
|
int hashCode = 1;
|
|
|
|
hashCode = (37 * hashCode) + stackSize;
|
|
|
|
|
|
|
|
if (wrappedStack instanceof ItemStack)
|
|
|
|
{
|
2014-06-19 21:56:00 +02:00
|
|
|
hashCode = (37 * hashCode) + Item.getIdFromItem(((ItemStack) wrappedStack).getItem());
|
2014-06-14 21:40:45 +02:00
|
|
|
hashCode = (37 * hashCode) + ((ItemStack) wrappedStack).getItemDamage();
|
|
|
|
|
|
|
|
if (((ItemStack) wrappedStack).getTagCompound() != null)
|
|
|
|
{
|
|
|
|
hashCode = (37 * hashCode) + ((ItemStack) wrappedStack).getTagCompound().hashCode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (wrappedStack instanceof OreStack)
|
|
|
|
{
|
|
|
|
if (((OreStack) wrappedStack).oreName != null)
|
|
|
|
{
|
|
|
|
hashCode = (37 * hashCode) + ((OreStack) wrappedStack).oreName.hashCode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (wrappedStack instanceof FluidStack)
|
|
|
|
{
|
|
|
|
hashCode = (37 * hashCode) + wrappedStack.hashCode();
|
|
|
|
|
|
|
|
if (((FluidStack) wrappedStack).tag != null)
|
|
|
|
{
|
|
|
|
hashCode = (37 * hashCode) + ((FluidStack) wrappedStack).tag.hashCode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hashCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object object)
|
|
|
|
{
|
2014-07-07 17:22:21 +02:00
|
|
|
return object instanceof WrappedStack && (this.compareTo((WrappedStack) object) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sort order (class-wise) goes ItemStack, OreStack, EnergyStack,
|
|
|
|
* FluidStack, null
|
|
|
|
* @see java.lang.Comparable#compareTo(java.lang.Object)
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int compareTo(WrappedStack wrappedStack)
|
|
|
|
{
|
|
|
|
return comparator.compare(this, wrappedStack);
|
2014-06-14 21:40:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return a string representation of the object.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String toString()
|
|
|
|
{
|
|
|
|
if (wrappedStack instanceof ItemStack)
|
|
|
|
{
|
2015-04-19 21:01:35 +02:00
|
|
|
ItemStack itemStack = (ItemStack) wrappedStack;
|
|
|
|
if (itemStack.hasTagCompound())
|
|
|
|
{
|
|
|
|
return String.format("%sxitemStack[%s@%s:%s]", stackSize, itemStack.getUnlocalizedName(), itemStack.getItemDamage(), itemStack.getTagCompound());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return String.format("%sxitemStack[%s@%s]", stackSize, itemStack.getUnlocalizedName(), itemStack.getItemDamage());
|
|
|
|
}
|
2014-06-14 21:40:45 +02:00
|
|
|
}
|
|
|
|
else if (wrappedStack instanceof OreStack)
|
|
|
|
{
|
|
|
|
OreStack oreStack = (OreStack) wrappedStack;
|
|
|
|
return String.format("%sxoreStack.%s", stackSize, oreStack.oreName);
|
|
|
|
}
|
|
|
|
else if (wrappedStack instanceof FluidStack)
|
|
|
|
{
|
|
|
|
FluidStack fluidStack = (FluidStack) wrappedStack;
|
|
|
|
return String.format("%sxfluidStack.%s", stackSize, fluidStack.getFluid().getName());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-19 21:01:35 +02:00
|
|
|
return "null-wrappedstack";
|
2014-06-14 21:40:45 +02:00
|
|
|
}
|
|
|
|
}
|
2014-09-12 22:11:18 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
2014-06-14 21:40:45 +02:00
|
|
|
}
|