104 lines
3.6 KiB
Java
104 lines
3.6 KiB
Java
package com.pahimar.ee3.util;
|
|
|
|
import com.pahimar.ee3.reference.Compare;
|
|
import net.minecraft.item.Item;
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import java.util.Comparator;
|
|
|
|
public class ItemHelper
|
|
{
|
|
public static Comparator<ItemStack> comparator = new Comparator<ItemStack>()
|
|
{
|
|
public int compare(ItemStack itemStack1, ItemStack itemStack2)
|
|
{
|
|
if (itemStack1 != null && itemStack2 != null)
|
|
{
|
|
// Sort on itemID
|
|
if (Item.getIdFromItem(itemStack1.getItem()) - Item.getIdFromItem(itemStack2.getItem()) == Compare.EQUALS)
|
|
{
|
|
// Then sort on meta
|
|
if (itemStack1.getItemDamage() == itemStack2.getItemDamage())
|
|
{
|
|
// Then sort on NBT
|
|
if (itemStack1.hasTagCompound() && itemStack2.hasTagCompound())
|
|
{
|
|
// Then sort on stack size
|
|
if (itemStack1.getTagCompound().equals(itemStack2.getTagCompound()))
|
|
{
|
|
return (itemStack1.stackSize - itemStack2.stackSize);
|
|
}
|
|
else
|
|
{
|
|
return (itemStack1.getTagCompound().hashCode() - itemStack2.getTagCompound().hashCode());
|
|
}
|
|
}
|
|
else if (!(itemStack1.hasTagCompound()) && itemStack2.hasTagCompound())
|
|
{
|
|
return -1;
|
|
}
|
|
else if (itemStack1.hasTagCompound() && !(itemStack2.hasTagCompound()))
|
|
{
|
|
return 1;
|
|
}
|
|
else
|
|
{
|
|
return (itemStack1.stackSize - itemStack2.stackSize);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return (itemStack1.getItemDamage() - itemStack2.getItemDamage());
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return Item.getIdFromItem(itemStack1.getItem()) - Item.getIdFromItem(itemStack2.getItem());
|
|
}
|
|
}
|
|
else if (itemStack1 != null && itemStack2 == null)
|
|
{
|
|
return -1;
|
|
}
|
|
else if (itemStack1 == null && itemStack2 != null)
|
|
{
|
|
return 1;
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Compares two ItemStacks for equality, testing itemID, metaData, stackSize, and their NBTTagCompounds (if they are
|
|
* present)
|
|
*
|
|
* @param first
|
|
* The first ItemStack being tested for equality
|
|
* @param second
|
|
* The second ItemStack being tested for equality
|
|
*
|
|
* @return true if the two ItemStacks are equivalent, false otherwise
|
|
*/
|
|
public static boolean equals(ItemStack first, ItemStack second)
|
|
{
|
|
return (comparator.compare(first, second) == 0);
|
|
}
|
|
|
|
public static int compare(ItemStack itemStack1, ItemStack itemStack2)
|
|
{
|
|
return comparator.compare(itemStack1, itemStack2);
|
|
}
|
|
|
|
public static String toString(ItemStack itemStack)
|
|
{
|
|
if (itemStack != null)
|
|
{
|
|
return String.format("%sxitemStack[%s@%s]", itemStack.stackSize, itemStack.getUnlocalizedName(), itemStack.getItemDamage());
|
|
}
|
|
|
|
return "null";
|
|
}
|
|
}
|