also check NBT of an item
This commit is contained in:
@@ -11,7 +11,6 @@ import storagecraft.SC;
|
||||
import storagecraft.inventory.ContainerGrid;
|
||||
import storagecraft.network.MessagePullFromStorage;
|
||||
import storagecraft.network.MessagePushToStorage;
|
||||
import storagecraft.storage.StorageItem;
|
||||
import storagecraft.tile.TileController;
|
||||
import storagecraft.tile.TileGrid;
|
||||
|
||||
@@ -54,9 +53,7 @@ public class GuiGrid extends GuiContainer {
|
||||
ItemStack stack = null;
|
||||
|
||||
if (grid.isConnected() && i < grid.getController().getItems().size()) {
|
||||
StorageItem item = grid.getController().getItems().get(i);
|
||||
|
||||
stack = new ItemStack(item.getType(), item.getQuantity(), item.getMeta());
|
||||
stack = grid.getController().getItems().get(i).toItemStack();
|
||||
|
||||
itemRender.renderItemAndEffectIntoGUI(fontRendererObj, mc.getTextureManager(), stack, xx, yy);
|
||||
itemRender.renderItemOverlayIntoGUI(fontRendererObj, mc.getTextureManager(), stack, xx, yy);
|
||||
|
@@ -24,7 +24,7 @@ public class SlotDrive extends SlotItemFilter {
|
||||
super.onSlotChanged();
|
||||
|
||||
if (drive.isConnected()) {
|
||||
drive.getController().storageSync();
|
||||
drive.getController().syncStorage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -69,7 +69,7 @@ public class MessagePullFromStorage implements IMessage, IMessageHandler<Message
|
||||
quantity = item.getQuantity() / 2;
|
||||
}
|
||||
|
||||
ItemStack stack = controller.take(item.getType(), quantity, item.getMeta());
|
||||
ItemStack stack = controller.take(item.copy(quantity).toItemStack());
|
||||
|
||||
if (message.shift) {
|
||||
// @TODO: This doesn't work
|
||||
|
@@ -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();
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ package storagecraft.tile;
|
||||
|
||||
import cofh.api.energy.EnergyStorage;
|
||||
import cofh.api.energy.IEnergyReceiver;
|
||||
import cpw.mods.fml.common.network.ByteBufUtils;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -66,7 +67,7 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
|
||||
|
||||
connectedMachines = machines;
|
||||
|
||||
storageSync();
|
||||
syncStorage();
|
||||
}
|
||||
|
||||
energyUsage = BASE_ENERGY_USAGE;
|
||||
@@ -104,7 +105,7 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
|
||||
return items;
|
||||
}
|
||||
|
||||
public void storageSync() {
|
||||
public void syncStorage() {
|
||||
storages.clear();
|
||||
|
||||
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();
|
||||
|
||||
for (IStorage storage : storages) {
|
||||
@@ -125,41 +126,46 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
|
||||
}
|
||||
|
||||
public boolean push(ItemStack stack) {
|
||||
IStorage storageThatCanPush = null;
|
||||
IStorage foundStorage = null;
|
||||
|
||||
for (IStorage storage : storages) {
|
||||
if (storage.canPush(stack)) {
|
||||
storageThatCanPush = storage;
|
||||
foundStorage = storage;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (storageThatCanPush == null) {
|
||||
if (foundStorage == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
storageThatCanPush.push(stack);
|
||||
foundStorage.push(stack);
|
||||
|
||||
storageItemsSync();
|
||||
syncStorageItems();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public ItemStack take(Item type, int quantity, int meta) {
|
||||
public ItemStack take(ItemStack stack) {
|
||||
int needed = stack.stackSize;
|
||||
int took = 0;
|
||||
|
||||
for (IStorage storage : storages) {
|
||||
took += storage.take(type, quantity, meta);
|
||||
took += storage.take(stack);
|
||||
|
||||
if (took == quantity) {
|
||||
if (took == needed) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
storageItemsSync();
|
||||
syncStorageItems();
|
||||
|
||||
return new ItemStack(type, took, meta);
|
||||
ItemStack newStack = stack.copy();
|
||||
|
||||
newStack.stackSize = took;
|
||||
|
||||
return newStack;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -217,8 +223,13 @@ public class TileController extends TileSC implements IEnergyReceiver, INetworkT
|
||||
Item type = Item.getItemById(buf.readInt());
|
||||
int quantity = 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.getQuantity());
|
||||
buf.writeInt(item.getMeta());
|
||||
buf.writeBoolean(item.getTag() != null);
|
||||
|
||||
if (item.getTag() != null) {
|
||||
ByteBufUtils.writeTag(buf, item.getTag());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user