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

131 lines
3.1 KiB
Java
Raw Normal View History

package techreborn.util;
2015-04-15 18:27:05 +02:00
import net.minecraft.inventory.IInventory;
2015-05-10 22:03:44 +02:00
import net.minecraft.item.Item;
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;
2015-05-10 22:03:44 +02:00
import java.util.ArrayList;
2015-05-08 22:07:59 +02:00
import java.util.List;
/**
* Created by mark on 12/04/15.
*/
public class ItemUtils {
2015-05-08 22:07:59 +02:00
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-05-08 22:07:59 +02:00
public static boolean isItemEqual(ItemStack a, ItemStack b,
boolean matchDamage, boolean matchNBT, boolean useOreDic)
{
if(isItemEqual(a, b, matchDamage, matchNBT)){
return true;
}
if (a == null || b == null)
return false;
if(useOreDic){
for (int inta : OreDictionary.getOreIDs(a)){
for (int intb : OreDictionary.getOreIDs(b)){
if(inta == intb){
return true;
}
}
}
}
return false;
2015-05-08 22:07:59 +02:00
}
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);
}
2015-05-10 22:03:44 +02:00
public static List<ItemStack> getStackWithAllOre(ItemStack stack){
ArrayList<ItemStack> list = new ArrayList<ItemStack>();
for (int oreID : OreDictionary.getOreIDs(stack)){
for(ItemStack ore : OreDictionary.getOres(OreDictionary.getOreName(oreID))){
ItemStack newOre = ore;
2015-05-16 17:55:54 +02:00
newOre.stackSize = stack.stackSize;
list.add(newOre);
2015-05-10 22:03:44 +02:00
}
}
if(list.isEmpty()){
list.add(stack);
}
return list;
}
}