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

@@ -11,7 +11,6 @@ import storagecraft.SC;
import storagecraft.inventory.ContainerGrid; import storagecraft.inventory.ContainerGrid;
import storagecraft.network.MessagePullFromStorage; import storagecraft.network.MessagePullFromStorage;
import storagecraft.network.MessagePushToStorage; import storagecraft.network.MessagePushToStorage;
import storagecraft.storage.StorageItem;
import storagecraft.tile.TileController; import storagecraft.tile.TileController;
import storagecraft.tile.TileGrid; import storagecraft.tile.TileGrid;
@@ -54,9 +53,7 @@ public class GuiGrid extends GuiContainer {
ItemStack stack = null; ItemStack stack = null;
if (grid.isConnected() && i < grid.getController().getItems().size()) { if (grid.isConnected() && i < grid.getController().getItems().size()) {
StorageItem item = grid.getController().getItems().get(i); stack = grid.getController().getItems().get(i).toItemStack();
stack = new ItemStack(item.getType(), item.getQuantity(), item.getMeta());
itemRender.renderItemAndEffectIntoGUI(fontRendererObj, mc.getTextureManager(), stack, xx, yy); itemRender.renderItemAndEffectIntoGUI(fontRendererObj, mc.getTextureManager(), stack, xx, yy);
itemRender.renderItemOverlayIntoGUI(fontRendererObj, mc.getTextureManager(), stack, xx, yy); itemRender.renderItemOverlayIntoGUI(fontRendererObj, mc.getTextureManager(), stack, xx, yy);

View File

@@ -24,7 +24,7 @@ public class SlotDrive extends SlotItemFilter {
super.onSlotChanged(); super.onSlotChanged();
if (drive.isConnected()) { if (drive.isConnected()) {
drive.getController().storageSync(); drive.getController().syncStorage();
} }
} }
} }

View File

@@ -69,7 +69,7 @@ public class MessagePullFromStorage implements IMessage, IMessageHandler<Message
quantity = item.getQuantity() / 2; quantity = item.getQuantity() / 2;
} }
ItemStack stack = controller.take(item.getType(), quantity, item.getMeta()); ItemStack stack = controller.take(item.copy(quantity).toItemStack());
if (message.shift) { if (message.shift) {
// @TODO: This doesn't work // @TODO: This doesn't work

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

View File

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

View File

@@ -1,16 +1,20 @@
package storagecraft.storage; package storagecraft.storage;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
public class StorageItem { public class StorageItem {
private Item type; private Item type;
private int quantity; private int quantity;
private int meta; 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.type = type;
this.meta = meta; this.meta = meta;
this.quantity = quantity; this.quantity = quantity;
this.tag = tag;
} }
public StorageItem(Item type) { public StorageItem(Item type) {
@@ -42,4 +46,28 @@ public class StorageItem {
public void setMeta(int meta) { public void setMeta(int meta) {
this.meta = 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;
}
} }

View File

@@ -2,6 +2,7 @@ package storagecraft.tile;
import cofh.api.energy.EnergyStorage; import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyReceiver; import cofh.api.energy.IEnergyReceiver;
import cpw.mods.fml.common.network.ByteBufUtils;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -66,7 +67,7 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
connectedMachines = machines; connectedMachines = machines;
storageSync(); syncStorage();
} }
energyUsage = BASE_ENERGY_USAGE; energyUsage = BASE_ENERGY_USAGE;
@@ -104,7 +105,7 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
return items; return items;
} }
public void storageSync() { public void syncStorage() {
storages.clear(); storages.clear();
for (TileMachine machine : connectedMachines) { for (TileMachine machine : connectedMachines) {
@@ -113,10 +114,10 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
} }
} }
storageItemsSync(); syncStorageItems();
} }
private void storageItemsSync() { private void syncStorageItems() {
items.clear(); items.clear();
for (IStorage storage : storages) { for (IStorage storage : storages) {
@@ -125,41 +126,46 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
} }
public boolean push(ItemStack stack) { public boolean push(ItemStack stack) {
IStorage storageThatCanPush = null; IStorage foundStorage = null;
for (IStorage storage : storages) { for (IStorage storage : storages) {
if (storage.canPush(stack)) { if (storage.canPush(stack)) {
storageThatCanPush = storage; foundStorage = storage;
break; break;
} }
} }
if (storageThatCanPush == null) { if (foundStorage == null) {
return false; return false;
} }
storageThatCanPush.push(stack); foundStorage.push(stack);
storageItemsSync(); syncStorageItems();
return true; return true;
} }
public ItemStack take(Item type, int quantity, int meta) { public ItemStack take(ItemStack stack) {
int needed = stack.stackSize;
int took = 0; int took = 0;
for (IStorage storage : storages) { for (IStorage storage : storages) {
took += storage.take(type, quantity, meta); took += storage.take(stack);
if (took == quantity) { if (took == needed) {
break; break;
} }
} }
storageItemsSync(); syncStorageItems();
return new ItemStack(type, took, meta); ItemStack newStack = stack.copy();
newStack.stackSize = took;
return newStack;
} }
@Override @Override
@@ -217,8 +223,13 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
Item type = Item.getItemById(buf.readInt()); Item type = Item.getItemById(buf.readInt());
int quantity = buf.readInt(); int quantity = buf.readInt();
int meta = buf.readInt(); int meta = buf.readInt();
NBTTagCompound tag = null;
items.add(new StorageItem(type, quantity, meta)); if (buf.readBoolean()) {
tag = ByteBufUtils.readTag(buf);
}
items.add(new StorageItem(type, quantity, meta, tag));
} }
} }
@@ -233,6 +244,11 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
buf.writeInt(Item.getIdFromItem(item.getType())); buf.writeInt(Item.getIdFromItem(item.getType()));
buf.writeInt(item.getQuantity()); buf.writeInt(item.getQuantity());
buf.writeInt(item.getMeta()); buf.writeInt(item.getMeta());
buf.writeBoolean(item.getTag() != null);
if (item.getTag() != null) {
ByteBufUtils.writeTag(buf, item.getTag());
}
} }
} }
} }