also check NBT of an item

This commit is contained in:
Raoul Van den Berge
2015-12-13 23:24:12 +01:00
parent 04cbbb105f
commit 62cca754a3
7 changed files with 82 additions and 27 deletions

View File

@@ -14,6 +14,7 @@ public class CellStorage implements IStorage {
public static final String NBT_ITEM_TYPE = "Type";
public static final String NBT_ITEM_QUANTITY = "Quantity";
public static final String NBT_ITEM_META = "Meta";
public static final String NBT_ITEM_NBT = "NBT";
private ItemStack cell;
@@ -22,7 +23,7 @@ public class CellStorage implements IStorage {
}
private StorageItem createItemFromNBT(NBTTagCompound tag) {
return new StorageItem(Item.getItemById(tag.getInteger(NBT_ITEM_TYPE)), tag.getInteger(NBT_ITEM_QUANTITY), tag.getInteger(NBT_ITEM_META));
return new StorageItem(Item.getItemById(tag.getInteger(NBT_ITEM_TYPE)), tag.getInteger(NBT_ITEM_QUANTITY), tag.getInteger(NBT_ITEM_META), tag.hasKey(NBT_ITEM_NBT) ? ((NBTTagCompound) tag.getTag(NBT_ITEM_NBT)) : null);
}
@Override
@@ -50,6 +51,10 @@ public class CellStorage implements IStorage {
StorageItem item = createItemFromNBT(tag);
if (item.getType() == stack.getItem() && item.getMeta() == stack.getItemDamage()) {
if (item.getTag() != null && !item.getTag().equals(stack.stackTagCompound)) {
continue;
}
tag.setInteger(NBT_ITEM_QUANTITY, item.getQuantity() + stack.stackSize);
return;
@@ -62,11 +67,17 @@ public class CellStorage implements IStorage {
tag.setInteger(NBT_ITEM_QUANTITY, stack.stackSize);
tag.setInteger(NBT_ITEM_META, stack.getItemDamage());
if (stack.stackTagCompound != null) {
tag.setTag(NBT_ITEM_NBT, stack.stackTagCompound);
}
list.appendTag(tag);
}
@Override
public int take(Item type, int quantity, int meta) {
public int take(ItemStack stack) {
int quantity = stack.stackSize;
NBTTagList list = (NBTTagList) cell.stackTagCompound.getTag(NBT_ITEMS);
for (int i = 0; i < list.tagCount(); ++i) {
@@ -74,7 +85,11 @@ public class CellStorage implements IStorage {
StorageItem item = createItemFromNBT(tag);
if (item.getType() == type && item.getMeta() == meta) {
if (item.getType() == stack.getItem() && item.getMeta() == stack.getItemDamage()) {
if (item.getTag() != null && !item.getTag().equals(stack.stackTagCompound)) {
continue;
}
if (quantity > item.getQuantity()) {
quantity = item.getQuantity();
}

View File

@@ -1,7 +1,6 @@
package storagecraft.storage;
import java.util.List;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
public interface IStorage {
@@ -9,7 +8,7 @@ public interface IStorage {
public void push(ItemStack stack);
public int take(Item type, int quantity, int meta);
public int take(ItemStack stack);
public boolean canPush(ItemStack stack);
}

View File

@@ -1,16 +1,20 @@
package storagecraft.storage;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
public class StorageItem {
private Item type;
private int quantity;
private int meta;
private NBTTagCompound tag;
public StorageItem(Item type, int quantity, int meta) {
public StorageItem(Item type, int quantity, int meta, NBTTagCompound tag) {
this.type = type;
this.meta = meta;
this.quantity = quantity;
this.tag = tag;
}
public StorageItem(Item type) {
@@ -42,4 +46,28 @@ public class StorageItem {
public void setMeta(int meta) {
this.meta = meta;
}
public NBTTagCompound getTag() {
return tag;
}
public void setTag(NBTTagCompound tag) {
this.tag = tag;
}
public StorageItem copy() {
return copy(quantity);
}
public StorageItem copy(int newQuantity) {
return new StorageItem(type, newQuantity, meta, tag);
}
public ItemStack toItemStack() {
ItemStack stack = new ItemStack(type, quantity, meta);
stack.stackTagCompound = tag;
return stack;
}
}