Mekanism-tilera-Edition/src/main/java/mekanism/api/util/StackUtils.java

239 lines
4.7 KiB
Java
Raw Normal View History

package mekanism.api.util;
2013-12-01 05:19:24 +01:00
import java.util.ArrayList;
import java.util.List;
2013-12-05 21:50:38 +01:00
import net.minecraft.item.Item;
2013-12-01 05:19:24 +01:00
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
2013-12-01 05:19:24 +01:00
public final class StackUtils
{
public static List<ItemStack> split(ItemStack stack)
{
if(stack == null || stack.stackSize == 0)
{
return null;
}
2013-12-01 05:19:24 +01:00
List<ItemStack> ret = new ArrayList<ItemStack>();
2013-12-01 05:19:24 +01:00
if(stack.stackSize == 1)
{
ret.add(stack);
return ret;
}
2013-12-01 05:19:24 +01:00
int remain = stack.stackSize % 2;
int split = (int)((float)(stack.stackSize)/2F);
ret.add(size(stack, split+remain));
ret.add(size(stack, split));
2013-12-01 05:19:24 +01:00
return ret;
}
2013-12-05 21:50:38 +01:00
public static Item getItem(ItemStack stack)
{
if(stack == null)
{
return null;
}
2013-12-05 21:50:38 +01:00
return stack.getItem();
}
2013-12-01 06:15:52 +01:00
public static boolean diffIgnoreNull(ItemStack stack1, ItemStack stack2)
{
if(stack1 == null || stack2 == null)
{
return false;
}
2014-05-29 20:01:43 +02:00
return stack1.getItem() != stack2.getItem() || stack1.getItemDamage() != stack2.getItemDamage();
2013-12-01 06:15:52 +01:00
}
public static boolean equalsWildcard(ItemStack wild, ItemStack check)
{
2014-05-29 20:01:43 +02:00
return wild.getItem() == check.getItem() && (wild.getItemDamage() == OreDictionary.WILDCARD_VALUE || wild.getItemDamage() == check.getItemDamage());
}
public static boolean equalsWildcardWithNBT(ItemStack wild, ItemStack check)
{
return equalsWildcard(wild, check) && (wild.stackTagCompound == null ? check.stackTagCompound == null : (wild.stackTagCompound == check.stackTagCompound || wild.stackTagCompound.equals(check.stackTagCompound)));
}
2013-12-01 06:03:40 +01:00
public static List<ItemStack> even(ItemStack stack1, ItemStack stack2)
{
ArrayList<ItemStack> ret = new ArrayList<ItemStack>();
2013-12-01 06:03:40 +01:00
if(getSize(stack1) == getSize(stack2) || Math.abs(getSize(stack1)-getSize(stack2)) == 1)
{
ret.add(stack1);
ret.add(stack2);
2013-12-01 06:03:40 +01:00
return ret;
}
2013-12-01 06:03:40 +01:00
if(getSize(stack1) > getSize(stack2))
{
int diff = getSize(stack1)-getSize(stack2);
2013-12-01 06:03:40 +01:00
List<ItemStack> split = split(size(stack1, diff));
2013-12-01 06:03:40 +01:00
ret.add(subtract(stack1, split.get(0)));
ret.add(add(stack2, split.get(0)));
2013-12-01 06:03:40 +01:00
}
else if(getSize(stack2) > getSize(stack1))
{
int diff = getSize(stack2)-getSize(stack1);
2013-12-01 06:03:40 +01:00
List<ItemStack> split = split(size(stack2, diff));
2013-12-01 06:03:40 +01:00
ret.add(subtract(stack2, split.get(0)));
ret.add(add(stack1, split.get(0)));
2013-12-01 06:03:40 +01:00
}
2013-12-01 06:03:40 +01:00
return ret;
}
2013-12-01 06:03:40 +01:00
public static ItemStack add(ItemStack stack1, ItemStack stack2)
{
if(stack1 == null)
{
return stack2;
}
else if(stack2 == null)
{
return stack1;
}
2013-12-01 06:03:40 +01:00
return size(stack1, getSize(stack1)+getSize(stack2));
}
2013-12-01 06:03:40 +01:00
public static ItemStack subtract(ItemStack stack1, ItemStack stack2)
{
if(stack1 == null)
{
return null;
}
else if(stack2 == null)
{
return stack1;
}
2013-12-01 06:03:40 +01:00
return size(stack1, getSize(stack1)-getSize(stack2));
}
2013-12-01 06:03:40 +01:00
public static ItemStack size(ItemStack stack, int size)
{
if(size <= 0 || stack == null)
{
return null;
}
2013-12-01 06:03:40 +01:00
ItemStack ret = stack.copy();
ret.stackSize = size;
return ret;
}
public static ItemStack copy(ItemStack stack)
{
if(stack == null)
{
return null;
}
return stack.copy();
}
2013-12-01 06:03:40 +01:00
public static int getSize(ItemStack stack)
{
return stack != null ? stack.stackSize : 0;
}
public static List<ItemStack> getMergeRejects(ItemStack[] orig, ItemStack[] toAdd)
{
List<ItemStack> ret = new ArrayList<ItemStack>();
for(int i = 0; i < toAdd.length; i++)
{
if(toAdd[i] != null)
{
ItemStack reject = getMergeReject(orig[i], toAdd[i]);
if(reject != null)
{
ret.add(reject);
}
}
}
return ret;
}
public static void merge(ItemStack[] orig, ItemStack[] toAdd)
{
for(int i = 0; i < toAdd.length; i++)
{
if(toAdd[i] != null)
{
orig[i] = merge(orig[i], toAdd[i]);
}
}
}
public static ItemStack merge(ItemStack orig, ItemStack toAdd)
{
if(orig == null)
{
return toAdd;
}
if(toAdd == null)
{
return orig;
}
if(!orig.isItemEqual(toAdd) || !ItemStack.areItemStackTagsEqual(orig, toAdd))
{
return orig;
}
return StackUtils.size(orig, Math.min(orig.getMaxStackSize(), orig.stackSize+toAdd.stackSize));
}
public static ItemStack getMergeReject(ItemStack orig, ItemStack toAdd)
{
if(orig == null)
{
return null;
}
if(toAdd == null)
{
return orig;
}
if(!orig.isItemEqual(toAdd) || !ItemStack.areItemStackTagsEqual(orig, toAdd))
{
return orig;
}
int newSize = orig.stackSize+toAdd.stackSize;
if(newSize > orig.getMaxStackSize())
{
return StackUtils.size(orig, newSize-orig.getMaxStackSize());
}
else {
return StackUtils.size(orig, newSize);
}
}
public static int hashItemStack(ItemStack stack)
{
return Item.getIdFromItem(stack.getItem()) << 8 | stack.getItemDamage();
}
2013-12-01 05:19:24 +01:00
}