TechReborn/src/main/java/techreborn/util/ItemUtils.java

90 lines
2.2 KiB
Java
Raw Normal View History

package techreborn.util;
2015-04-15 18:27:05 +02:00
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
2015-04-15 18:27:05 +02:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.oredict.OreDictionary;
/**
* Created by mark on 12/04/15.
*/
public class ItemUtils {
2015-04-24 15:20:09 +02:00
public static boolean isItemEqual(final ItemStack a, final ItemStack b,
final boolean matchDamage, final boolean matchNBT)
{
if (a == null || b == null)
return false;
if (a.getItem() != b.getItem())
return false;
if (matchNBT && !ItemStack.areItemStackTagsEqual(a, b))
return false;
if (matchDamage && a.getHasSubtypes())
{
if (isWildcard(a) || isWildcard(b))
return true;
if (a.getItemDamage() != b.getItemDamage())
return false;
}
return true;
}
2015-04-24 15:20:09 +02:00
public static boolean isWildcard(ItemStack stack)
{
return isWildcard(stack.getItemDamage());
}
2015-04-24 15:20:09 +02:00
public static boolean isWildcard(int damage)
{
return damage == -1 || damage == OreDictionary.WILDCARD_VALUE;
}
2015-04-15 18:27:05 +02:00
2015-04-24 15:20:09 +02:00
public static void writeInvToNBT(IInventory inv, String tag,
NBTTagCompound data)
{
NBTTagList list = new NBTTagList();
for (byte slot = 0; slot < inv.getSizeInventory(); slot++)
{
ItemStack stack = inv.getStackInSlot(slot);
if (stack != null)
{
NBTTagCompound itemTag = new NBTTagCompound();
itemTag.setByte("Slot", slot);
writeItemToNBT(stack, itemTag);
list.appendTag(itemTag);
}
}
data.setTag(tag, list);
}
2015-04-15 18:27:05 +02:00
2015-04-24 15:20:09 +02:00
public static void readInvFromNBT(IInventory inv, String tag,
NBTTagCompound data)
{
NBTTagList list = data.getTagList(tag, 10);
for (byte entry = 0; entry < list.tagCount(); entry++)
{
NBTTagCompound itemTag = list.getCompoundTagAt(entry);
int slot = itemTag.getByte("Slot");
if (slot >= 0 && slot < inv.getSizeInventory())
{
ItemStack stack = readItemFromNBT(itemTag);
inv.setInventorySlotContents(slot, stack);
}
}
}
2015-04-15 18:27:05 +02:00
2015-04-24 15:20:09 +02:00
public static void writeItemToNBT(ItemStack stack, NBTTagCompound data)
{
if (stack == null || stack.stackSize <= 0)
return;
if (stack.stackSize > 127)
stack.stackSize = 127;
stack.writeToNBT(data);
}
2015-04-15 18:27:05 +02:00
2015-04-24 15:20:09 +02:00
public static ItemStack readItemFromNBT(NBTTagCompound data)
{
return ItemStack.loadItemStackFromNBT(data);
}
}