2013-08-23 16:59:50 +02:00
|
|
|
package com.pahimar.ee3.item;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2013-11-08 02:37:44 +01:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
2013-08-23 16:59:50 +02:00
|
|
|
|
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraftforge.oredict.OreDictionary;
|
|
|
|
|
2013-10-18 02:53:08 +02:00
|
|
|
import com.pahimar.ee3.core.helper.ItemHelper;
|
2013-08-23 16:59:50 +02:00
|
|
|
import com.pahimar.ee3.lib.Reference;
|
2013-11-08 02:37:44 +01:00
|
|
|
import com.pahimar.ee3.lib.Strings;
|
2013-08-23 16:59:50 +02:00
|
|
|
|
2013-09-06 04:21:55 +02:00
|
|
|
public class CustomWrappedStack implements Comparable<CustomWrappedStack> {
|
2013-08-23 16:59:50 +02:00
|
|
|
|
|
|
|
private int stackSize;
|
2013-09-06 19:50:17 +02:00
|
|
|
private final ItemStack itemStack;
|
|
|
|
private final OreStack oreStack;
|
|
|
|
private final EnergyStack energyStack;
|
2013-08-23 16:59:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new CustomWrappedStack object which wraps the given input.
|
|
|
|
* Valid inputs would be ItemStacks or OreStacks. If something other than an
|
|
|
|
* ItemStack or an OreStack is used as input, nothing is wrapped and the
|
|
|
|
* size of the wrapped stack is set to -1 to indicate an invalid wrapped
|
|
|
|
* stack.
|
|
|
|
*
|
|
|
|
* @param object
|
|
|
|
* The newly created wrapped stack object
|
|
|
|
*/
|
|
|
|
public CustomWrappedStack(Object object) {
|
|
|
|
|
|
|
|
/*
|
2013-09-06 19:50:17 +02:00
|
|
|
* If we are given an Item or a Block, convert it to an ItemStack for
|
|
|
|
* further inspection
|
2013-08-23 16:59:50 +02:00
|
|
|
*/
|
|
|
|
if (object instanceof Item) {
|
|
|
|
object = new ItemStack((Item) object);
|
|
|
|
}
|
|
|
|
else if (object instanceof Block) {
|
|
|
|
object = new ItemStack((Block) object);
|
|
|
|
}
|
2013-09-06 19:50:17 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
/*
|
|
|
|
* We are given an ItemStack to wrap
|
|
|
|
*/
|
|
|
|
if (object instanceof ItemStack) {
|
|
|
|
|
|
|
|
ItemStack itemStack = (ItemStack) object;
|
|
|
|
|
2013-10-21 00:30:15 +02:00
|
|
|
this.itemStack = itemStack.copy();
|
|
|
|
oreStack = null;
|
|
|
|
energyStack = null;
|
|
|
|
stackSize = this.itemStack.stackSize;
|
|
|
|
this.itemStack.stackSize = 1;
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Or we are given an OreStack to wrap
|
|
|
|
*/
|
|
|
|
else if (object instanceof OreStack) {
|
|
|
|
|
|
|
|
itemStack = null;
|
|
|
|
oreStack = (OreStack) object;
|
|
|
|
energyStack = null;
|
|
|
|
stackSize = oreStack.stackSize;
|
|
|
|
oreStack.stackSize = 1;
|
|
|
|
}
|
|
|
|
else if (object instanceof ArrayList) {
|
|
|
|
|
|
|
|
itemStack = null;
|
|
|
|
|
|
|
|
ArrayList<?> objectList = (ArrayList<?>) object;
|
|
|
|
|
2013-09-06 19:50:17 +02:00
|
|
|
OreStack tempOreStack = getOreStackFromList(objectList);
|
|
|
|
|
|
|
|
if (tempOreStack != null) {
|
|
|
|
oreStack = new OreStack(tempOreStack.oreName, 1);
|
|
|
|
stackSize = tempOreStack.stackSize;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
oreStack = null;
|
|
|
|
stackSize = -1;
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
energyStack = null;
|
|
|
|
}
|
2013-11-08 02:37:44 +01:00
|
|
|
else if (object instanceof String) {
|
|
|
|
|
|
|
|
String objectString = (String) object;
|
|
|
|
|
|
|
|
if (objectString.startsWith(Strings.NBT_ENCODED_ATTR_ORE_NAME + ":")) {
|
|
|
|
|
|
|
|
String possibleOreName = objectString.substring(Strings.NBT_ENCODED_ATTR_ORE_NAME.length() + 1);
|
|
|
|
OreStack possibleOreStack = new OreStack(possibleOreName);
|
|
|
|
|
|
|
|
if (possibleOreStack.oreId != -1) {
|
|
|
|
|
|
|
|
itemStack = null;
|
|
|
|
oreStack = possibleOreStack;
|
|
|
|
energyStack = null;
|
|
|
|
stackSize = possibleOreStack.stackSize;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
itemStack = null;
|
|
|
|
oreStack = null;
|
|
|
|
energyStack = null;
|
|
|
|
stackSize = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (objectString.startsWith(Strings.NBT_ENCODED_ATTR_ENERGY_NAME + ":")) {
|
|
|
|
|
|
|
|
String possibleEnergyName = objectString.substring(Strings.NBT_ENCODED_ATTR_ENERGY_NAME.length() + 1);
|
|
|
|
|
|
|
|
if (possibleEnergyName.length() > 0) {
|
|
|
|
|
|
|
|
itemStack = null;
|
|
|
|
oreStack = null;
|
|
|
|
energyStack = new EnergyStack(possibleEnergyName);
|
|
|
|
stackSize = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
itemStack = null;
|
|
|
|
oreStack = null;
|
|
|
|
energyStack = null;
|
|
|
|
stackSize = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
itemStack = null;
|
|
|
|
oreStack = null;
|
|
|
|
energyStack = null;
|
|
|
|
stackSize = -1;
|
|
|
|
}
|
|
|
|
}
|
2013-08-23 16:59:50 +02:00
|
|
|
/*
|
|
|
|
* Or we are given an EnergyStack to wrap
|
|
|
|
*/
|
|
|
|
else if (object instanceof EnergyStack) {
|
|
|
|
itemStack = null;
|
|
|
|
oreStack = null;
|
|
|
|
energyStack = (EnergyStack) object;
|
|
|
|
stackSize = energyStack.stackSize;
|
|
|
|
energyStack.stackSize = 1;
|
|
|
|
}
|
|
|
|
else if (object instanceof CustomWrappedStack) {
|
|
|
|
CustomWrappedStack wrappedStack = (CustomWrappedStack) object;
|
2013-09-06 19:50:17 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
itemStack = wrappedStack.itemStack;
|
|
|
|
oreStack = wrappedStack.oreStack;
|
|
|
|
energyStack = wrappedStack.energyStack;
|
|
|
|
stackSize = wrappedStack.stackSize;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Else, we are given something we cannot wrap
|
|
|
|
*/
|
|
|
|
else {
|
2013-09-06 19:50:17 +02:00
|
|
|
itemStack = null;
|
|
|
|
oreStack = null;
|
|
|
|
energyStack = null;
|
2013-08-23 16:59:50 +02:00
|
|
|
stackSize = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the stack size of the wrapped stack, or -1 if we wrapped an
|
|
|
|
* invalid input
|
|
|
|
*
|
|
|
|
* @return The size of the wrapped stack
|
|
|
|
*/
|
|
|
|
public int getStackSize() {
|
|
|
|
|
|
|
|
return stackSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the size of the wrapped stack
|
|
|
|
*
|
|
|
|
* @param stackSize
|
|
|
|
* The new size of the wrapped stack
|
|
|
|
*/
|
|
|
|
public void setStackSize(int stackSize) {
|
|
|
|
|
|
|
|
this.stackSize = stackSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the wrapped stack
|
|
|
|
*
|
|
|
|
* @return The wrapped ItemStack, OreStack, or EnergyStack, or null if
|
|
|
|
* something other than an ItemStack, OreStack, or EnergyStack was
|
|
|
|
* used to create this object
|
|
|
|
*/
|
|
|
|
public Object getWrappedStack() {
|
|
|
|
|
2013-09-06 19:50:17 +02:00
|
|
|
if (itemStack != null)
|
2013-08-23 16:59:50 +02:00
|
|
|
return itemStack;
|
2013-09-06 19:50:17 +02:00
|
|
|
else if (oreStack != null)
|
2013-08-23 16:59:50 +02:00
|
|
|
return oreStack;
|
2013-09-06 19:50:17 +02:00
|
|
|
else if (energyStack != null)
|
2013-08-23 16:59:50 +02:00
|
|
|
return energyStack;
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object object) {
|
|
|
|
|
2013-09-06 19:50:17 +02:00
|
|
|
if (!(object instanceof CustomWrappedStack)) {
|
2013-08-23 16:59:50 +02:00
|
|
|
return false;
|
2013-09-06 19:50:17 +02:00
|
|
|
}
|
2013-08-23 16:59:50 +02:00
|
|
|
|
|
|
|
CustomWrappedStack customWrappedStack = (CustomWrappedStack) object;
|
|
|
|
|
2013-09-06 19:50:17 +02:00
|
|
|
if ((this.getWrappedStack() instanceof ItemStack) && (customWrappedStack.getWrappedStack() instanceof ItemStack)) {
|
2013-10-18 02:53:08 +02:00
|
|
|
return (ItemHelper.compare(itemStack, customWrappedStack.itemStack) && (stackSize == customWrappedStack.itemStack.stackSize));
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
2013-09-06 19:50:17 +02:00
|
|
|
else if ((this.getWrappedStack() instanceof OreStack) && (customWrappedStack.getWrappedStack() instanceof OreStack)) {
|
|
|
|
return (oreStack.equals(customWrappedStack.getWrappedStack()) && (stackSize == customWrappedStack.stackSize));
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
2013-09-06 19:50:17 +02:00
|
|
|
else if ((this.getWrappedStack() instanceof EnergyStack) && (customWrappedStack.getWrappedStack() instanceof EnergyStack)) {
|
|
|
|
return (energyStack.equals(customWrappedStack.energyStack) && (stackSize == customWrappedStack.stackSize));
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
|
|
|
|
if (itemStack != null) {
|
2013-09-10 22:09:11 +02:00
|
|
|
try {
|
|
|
|
stringBuilder.append(String.format("%sxitemStack[%s:%s:%s:%s]", stackSize, itemStack.itemID, itemStack.getItemDamage(), itemStack.getUnlocalizedName(), itemStack.getItem().getClass().getCanonicalName()));
|
|
|
|
} catch (ArrayIndexOutOfBoundsException e) {
|
|
|
|
|
|
|
|
}
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
|
|
|
else if (oreStack != null) {
|
|
|
|
stringBuilder.append(String.format("%dxoreDictionary.%s", stackSize, oreStack.oreName));
|
|
|
|
}
|
|
|
|
else if (energyStack != null) {
|
|
|
|
stringBuilder.append(String.format("%dxenergyStack.%s", stackSize, energyStack.energyName));
|
|
|
|
}
|
|
|
|
else {
|
2013-09-06 19:50:17 +02:00
|
|
|
stringBuilder.append("null");
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return stringBuilder.toString();
|
|
|
|
}
|
2013-09-06 19:50:17 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
public String encodeAsPropertyKey() {
|
2013-09-06 19:50:17 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
|
|
|
|
if (itemStack != null) {
|
2013-09-06 19:50:17 +02:00
|
|
|
stringBuilder.append(String.format("%sxitemStack[%s:%s:%s:%s]", stackSize, itemStack.itemID, itemStack.getItemDamage(), itemStack.getUnlocalizedName(), itemStack.getItem().getClass().getCanonicalName()));
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
|
|
|
else if (oreStack != null) {
|
|
|
|
stringBuilder.append(String.format("%dxoreDictionary.%s", stackSize, oreStack.oreName));
|
|
|
|
}
|
|
|
|
else if (energyStack != null) {
|
|
|
|
stringBuilder.append(String.format("%dxenergyStack.%s", stackSize, energyStack.energyName));
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringBuilder.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
|
|
|
|
int hashCode = 1;
|
|
|
|
|
|
|
|
hashCode = 37 * hashCode + stackSize;
|
|
|
|
|
|
|
|
if (itemStack != null) {
|
|
|
|
hashCode = 37 * hashCode + itemStack.itemID;
|
2013-10-15 22:01:16 +02:00
|
|
|
hashCode = 37 * hashCode + itemStack.getItemDamage();
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
|
|
|
else if (oreStack != null) {
|
2013-09-06 19:50:17 +02:00
|
|
|
if (oreStack.oreName != null) {
|
|
|
|
hashCode = 37 * hashCode + oreStack.oreName.hashCode();
|
|
|
|
}
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
|
|
|
else if (energyStack != null) {
|
2013-09-06 19:50:17 +02:00
|
|
|
if (energyStack.energyName != null) {
|
|
|
|
hashCode = 37 * hashCode + energyStack.energyName.hashCode();
|
|
|
|
}
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return hashCode;
|
|
|
|
}
|
2013-09-06 19:50:17 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
public static boolean canBeWrapped(Object object) {
|
2013-11-08 02:37:44 +01:00
|
|
|
|
|
|
|
// Simple case
|
|
|
|
if (object instanceof CustomWrappedStack || object instanceof ItemStack || object instanceof OreStack || object instanceof EnergyStack || object instanceof Item || object instanceof Block) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// If it's a List, check to see if it could possibly be an OreStack
|
|
|
|
else if (object instanceof List) {
|
|
|
|
if (getOreStackFromList((List<?>) object) != null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If it's a String, check to see if it could be the encoded name for a custom stack (OreStack, EnergyStack, etc)
|
|
|
|
else if (object instanceof String) {
|
|
|
|
|
|
|
|
String objectString = (String) object;
|
|
|
|
|
|
|
|
if (objectString.startsWith(Strings.NBT_ENCODED_ATTR_ORE_NAME + ":")) {
|
|
|
|
|
|
|
|
String possibleOreName = objectString.substring(Strings.NBT_ENCODED_ATTR_ORE_NAME.length() + 1);
|
|
|
|
List<String> oreNames = Arrays.asList(OreDictionary.getOreNames());
|
|
|
|
|
|
|
|
for (String oreName : oreNames) {
|
|
|
|
if (oreName.equalsIgnoreCase(possibleOreName)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (objectString.startsWith(Strings.NBT_ENCODED_ATTR_ENERGY_NAME + ":")) {
|
|
|
|
|
|
|
|
String possibleEnergyName = objectString.substring(Strings.NBT_ENCODED_ATTR_ENERGY_NAME.length() + 1);
|
|
|
|
|
|
|
|
if (possibleEnergyName.length() > 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-09-06 19:50:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
/*
|
|
|
|
* Sort order (class-wise) goes null, EnergyStack, OreStack, ItemStack
|
|
|
|
* @see java.lang.Comparable#compareTo(java.lang.Object)
|
|
|
|
*/
|
|
|
|
public int compareTo(CustomWrappedStack customWrappedStack) {
|
|
|
|
|
|
|
|
if (this.getWrappedStack() instanceof EnergyStack) {
|
|
|
|
|
|
|
|
if (customWrappedStack.getWrappedStack() instanceof EnergyStack) {
|
|
|
|
if (energyStack.equals(customWrappedStack.energyStack))
|
|
|
|
return stackSize - customWrappedStack.stackSize;
|
|
|
|
else
|
|
|
|
return energyStack.compareTo(customWrappedStack.energyStack);
|
|
|
|
}
|
|
|
|
else if (customWrappedStack.getWrappedStack() instanceof OreStack)
|
|
|
|
return -1;
|
|
|
|
else if (customWrappedStack.getWrappedStack() instanceof ItemStack)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (this.getWrappedStack() instanceof OreStack) {
|
|
|
|
|
|
|
|
if (customWrappedStack.getWrappedStack() instanceof EnergyStack)
|
|
|
|
return 1;
|
|
|
|
else if (customWrappedStack.getWrappedStack() instanceof OreStack) {
|
|
|
|
if (oreStack.equals(customWrappedStack.oreStack))
|
|
|
|
return stackSize - customWrappedStack.stackSize;
|
|
|
|
else
|
|
|
|
return oreStack.compareTo(customWrappedStack.oreStack);
|
|
|
|
}
|
|
|
|
else if (customWrappedStack.getWrappedStack() instanceof ItemStack)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (this.getWrappedStack() instanceof ItemStack) {
|
|
|
|
|
|
|
|
if (customWrappedStack.getWrappedStack() instanceof EnergyStack)
|
|
|
|
return 1;
|
|
|
|
else if (customWrappedStack.getWrappedStack() instanceof OreStack)
|
|
|
|
return 1;
|
|
|
|
else if (customWrappedStack.getWrappedStack() instanceof ItemStack) {
|
2013-10-18 02:53:08 +02:00
|
|
|
if (ItemHelper.compare(itemStack, customWrappedStack.itemStack))
|
2013-09-06 19:50:17 +02:00
|
|
|
return stackSize - customWrappedStack.stackSize;
|
|
|
|
else
|
2013-10-18 02:53:08 +02:00
|
|
|
return ItemHelper.ItemStackComparator.compare(itemStack, customWrappedStack.itemStack);
|
2013-09-06 19:50:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (customWrappedStack.getWrappedStack() != null)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
2013-09-06 04:21:55 +02:00
|
|
|
|
2013-11-08 02:37:44 +01:00
|
|
|
private static OreStack getOreStackFromList(List<?> objectList) {
|
2013-09-06 19:50:17 +02:00
|
|
|
|
|
|
|
for (Object listElement : objectList) {
|
|
|
|
if (listElement instanceof ItemStack) {
|
|
|
|
ItemStack stack = (ItemStack) listElement;
|
|
|
|
|
|
|
|
if (OreDictionary.getOreID(stack) != Reference.ORE_DICTIONARY_NOT_FOUND) {
|
|
|
|
return new OreStack(stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|