From fc42dff984c7ea5fd9041fb050a2031e457a5447 Mon Sep 17 00:00:00 2001 From: Raoul Van den Berge Date: Thu, 19 May 2016 00:49:03 +0200 Subject: [PATCH] Separate metadata of a itemgroup from the itemgroup --- src/main/java/refinedstorage/gui/GuiGrid.java | 10 +- .../refinedstorage/storage/ItemGroup.java | 138 ++++-------------- .../refinedstorage/storage/ItemGroupMeta.java | 125 ++++++++++++++++ .../refinedstorage/storage/NBTStorage.java | 10 +- .../refinedstorage/tile/TileController.java | 10 +- 5 files changed, 172 insertions(+), 121 deletions(-) create mode 100755 src/main/java/refinedstorage/storage/ItemGroupMeta.java diff --git a/src/main/java/refinedstorage/gui/GuiGrid.java b/src/main/java/refinedstorage/gui/GuiGrid.java index 260d25a4e..dc9c56cb5 100755 --- a/src/main/java/refinedstorage/gui/GuiGrid.java +++ b/src/main/java/refinedstorage/gui/GuiGrid.java @@ -92,7 +92,7 @@ public class GuiGrid extends GuiBase { while (t.hasNext()) { ItemGroup group = t.next(); - if (!group.toItemStack().getDisplayName().toLowerCase().contains(searchField.getText().toLowerCase())) { + if (!group.toStack().getDisplayName().toLowerCase().contains(searchField.getText().toLowerCase())) { t.remove(); } } @@ -102,9 +102,9 @@ public class GuiGrid extends GuiBase { @Override public int compare(ItemGroup left, ItemGroup right) { if (grid.getSortingDirection() == TileGrid.SORTING_DIRECTION_ASCENDING) { - return right.toItemStack().getDisplayName().compareTo(left.toItemStack().getDisplayName()); + return right.toStack().getDisplayName().compareTo(left.toStack().getDisplayName()); } else if (grid.getSortingDirection() == TileGrid.SORTING_DIRECTION_DESCENDING) { - return left.toItemStack().getDisplayName().compareTo(right.toItemStack().getDisplayName()); + return left.toStack().getDisplayName().compareTo(right.toStack().getDisplayName()); } return 0; @@ -251,7 +251,7 @@ public class GuiGrid extends GuiBase { text = String.valueOf(qty); } - drawItem(x, y, items.get(slot).toItemStack(), true, text); + drawItem(x, y, items.get(slot).toStack(), true, text); } if (inBounds(x, y, 16, 16, mouseX, mouseY) || !grid.isConnected()) { @@ -279,7 +279,7 @@ public class GuiGrid extends GuiBase { } if (isHoveringOverItemInSlot()) { - drawTooltip(mouseX, mouseY, items.get(hoveringSlot).toItemStack()); + drawTooltip(mouseX, mouseY, items.get(hoveringSlot).toStack()); } if (isHoveringOverClear(mouseX, mouseY)) { diff --git a/src/main/java/refinedstorage/storage/ItemGroup.java b/src/main/java/refinedstorage/storage/ItemGroup.java index ba3b4b887..e7ce47fda 100755 --- a/src/main/java/refinedstorage/storage/ItemGroup.java +++ b/src/main/java/refinedstorage/storage/ItemGroup.java @@ -3,50 +3,49 @@ package refinedstorage.storage; import io.netty.buffer.ByteBuf; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.fml.common.network.ByteBufUtils; import refinedstorage.RefinedStorageUtils; public class ItemGroup { - private Item type; + private ItemGroupMeta meta; private int quantity; - private int damage; - private NBTTagCompound tag; + // Used clientside private int id; public ItemGroup(ByteBuf buf) { this.id = buf.readInt(); - this.type = Item.getItemById(buf.readInt()); + this.meta = new ItemGroupMeta( + Item.getItemById(buf.readInt()), + buf.readInt(), + buf.readBoolean() ? ByteBufUtils.readTag(buf) : null + ); this.quantity = buf.readInt(); - this.damage = buf.readInt(); - this.tag = buf.readBoolean() ? ByteBufUtils.readTag(buf) : null; } - public ItemGroup(Item type, int quantity, int damage, NBTTagCompound tag) { - this.type = type; + public ItemGroup(ItemGroupMeta meta, int quantity) { + this.meta = meta; this.quantity = quantity; - this.damage = damage; - this.tag = tag; } public ItemGroup(ItemStack stack) { - this(stack.getItem(), stack.stackSize, stack.getItemDamage(), stack.getTagCompound()); + this(new ItemGroupMeta(stack), stack.stackSize); } public void toBytes(ByteBuf buf, int id) { buf.writeInt(id); - buf.writeInt(Item.getIdFromItem(type)); - buf.writeInt(quantity); - buf.writeInt(damage); - buf.writeBoolean(tag != null); + buf.writeInt(Item.getIdFromItem(meta.getType())); + buf.writeInt(meta.getDamage()); + buf.writeBoolean(meta.hasTag()); - if (tag != null) { - ByteBufUtils.writeTag(buf, tag); + if (meta.hasTag()) { + ByteBufUtils.writeTag(buf, meta.getTag()); } + + buf.writeInt(quantity); } - public Item getType() { - return type; + public ItemGroupMeta getMeta() { + return meta; } public int getQuantity() { @@ -57,22 +56,6 @@ public class ItemGroup { this.quantity = quantity; } - public int getDamage() { - return damage; - } - - public void setDamage(int damage) { - this.damage = damage; - } - - public NBTTagCompound getTag() { - return tag; - } - - public void setTag(NBTTagCompound tag) { - this.tag = tag; - } - public int getId() { return id; } @@ -82,71 +65,31 @@ public class ItemGroup { } public ItemGroup copy(int newQuantity) { - return new ItemGroup(type, newQuantity, damage, tag); + return new ItemGroup(meta, newQuantity); } - public ItemStack toItemStack() { - ItemStack stack = new ItemStack(type, quantity, damage); + public ItemStack toStack() { + ItemStack stack = new ItemStack(meta.getType(), quantity, meta.getDamage()); - stack.setTagCompound(tag); + stack.setTagCompound(meta.getTag()); return stack; } public boolean compare(ItemGroup other, int flags) { - if ((flags & RefinedStorageUtils.COMPARE_DAMAGE) == RefinedStorageUtils.COMPARE_DAMAGE) { - if (damage != other.getDamage()) { - return false; - } + if ((flags & RefinedStorageUtils.COMPARE_QUANTITY) == RefinedStorageUtils.COMPARE_QUANTITY && other.getQuantity() != quantity) { + return false; } - if ((flags & RefinedStorageUtils.COMPARE_NBT) == RefinedStorageUtils.COMPARE_NBT) { - if ((tag != null && other.getTag() == null) || (tag == null && other.getTag() != null)) { - return false; - } - - if (tag != null && other.getTag() != null) { - if (!tag.equals(other.getTag())) { - return false; - } - } - } - - if ((flags & RefinedStorageUtils.COMPARE_QUANTITY) == RefinedStorageUtils.COMPARE_QUANTITY) { - if (quantity != other.getQuantity()) { - return false; - } - } - - return type == other.getType(); + return meta.compare(other.getMeta(), flags); } public boolean compare(ItemStack stack, int flags) { - if ((flags & RefinedStorageUtils.COMPARE_DAMAGE) == RefinedStorageUtils.COMPARE_DAMAGE) { - if (damage != stack.getItemDamage()) { - return false; - } + if ((flags & RefinedStorageUtils.COMPARE_QUANTITY) == RefinedStorageUtils.COMPARE_QUANTITY && stack.stackSize != quantity) { + return false; } - if ((flags & RefinedStorageUtils.COMPARE_NBT) == RefinedStorageUtils.COMPARE_NBT) { - if ((tag != null && stack.getTagCompound() == null) || (tag == null && stack.getTagCompound() != null)) { - return false; - } - - if (tag != null && stack.getTagCompound() != null) { - if (!tag.equals(stack.getTagCompound())) { - return false; - } - } - } - - if ((flags & RefinedStorageUtils.COMPARE_QUANTITY) == RefinedStorageUtils.COMPARE_QUANTITY) { - if (quantity != stack.stackSize) { - return false; - } - } - - return type == stack.getItem(); + return meta.compare(stack, flags); } public boolean compareNoQuantity(ItemGroup other) { @@ -156,27 +99,4 @@ public class ItemGroup { public boolean compareNoQuantity(ItemStack stack) { return compare(stack, RefinedStorageUtils.COMPARE_NBT | RefinedStorageUtils.COMPARE_DAMAGE); } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - ItemGroup itemGroup = (ItemGroup) o; - - if (quantity != itemGroup.quantity) return false; - if (damage != itemGroup.damage) return false; - if (!type.equals(itemGroup.type)) return false; - return tag != null ? tag.equals(itemGroup.tag) : itemGroup.tag == null; - - } - - @Override - public int hashCode() { - int result = type.hashCode(); - result = 31 * result + quantity; - result = 31 * result + damage; - result = 31 * result + (tag != null ? tag.hashCode() : 0); - return result; - } } diff --git a/src/main/java/refinedstorage/storage/ItemGroupMeta.java b/src/main/java/refinedstorage/storage/ItemGroupMeta.java new file mode 100755 index 000000000..a0a6c83d6 --- /dev/null +++ b/src/main/java/refinedstorage/storage/ItemGroupMeta.java @@ -0,0 +1,125 @@ +package refinedstorage.storage; + +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import refinedstorage.RefinedStorageUtils; + +public class ItemGroupMeta { + private Item type; + private int damage; + private NBTTagCompound tag; + + public ItemGroupMeta(Item type, int damage, NBTTagCompound tag) { + this.type = type; + this.damage = damage; + this.tag = tag; + } + + public ItemGroupMeta(ItemStack stack) { + this(stack.getItem(), stack.getItemDamage(), stack.getTagCompound()); + } + + public Item getType() { + return type; + } + + public void setType(Item type) { + this.type = type; + } + + public int getDamage() { + return damage; + } + + public void setDamage(int damage) { + this.damage = damage; + } + + public boolean hasTag() { + return tag != null; + } + + public NBTTagCompound getTag() { + return tag; + } + + public void setTag(NBTTagCompound tag) { + this.tag = tag; + } + + public boolean compare(ItemGroupMeta other, int flags) { + if (type != other.getType()) { + return false; + } + + if ((flags & RefinedStorageUtils.COMPARE_DAMAGE) == RefinedStorageUtils.COMPARE_DAMAGE && damage != other.getDamage()) { + return false; + } + + if ((flags & RefinedStorageUtils.COMPARE_NBT) == RefinedStorageUtils.COMPARE_NBT) { + if ((tag != null && other.getTag() == null) || (tag == null && other.getTag() != null)) { + return false; + } + + if (tag != null && other.getTag() != null) { + if (!tag.equals(other.getTag())) { + return false; + } + } + } + + return true; + } + + public boolean compare(ItemStack stack, int flags) { + if (type != stack.getItem()) { + return false; + } + + if ((flags & RefinedStorageUtils.COMPARE_DAMAGE) == RefinedStorageUtils.COMPARE_DAMAGE && damage != stack.getItemDamage()) { + return false; + } + + if ((flags & RefinedStorageUtils.COMPARE_NBT) == RefinedStorageUtils.COMPARE_NBT) { + if ((tag != null && stack.getTagCompound() == null) || (tag == null && stack.getTagCompound() != null)) { + return false; + } + + if (tag != null && stack.getTagCompound() != null) { + if (!tag.equals(stack.getTagCompound())) { + return false; + } + } + } + + return true; + } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + + if (other == null || getClass() != other.getClass()) { + return false; + } + + ItemGroupMeta meta = (ItemGroupMeta) other; + + if (damage != meta.damage || !type.equals(meta.type)) { + return false; + } + + return tag != null ? tag.equals(meta.tag) : meta.tag == null; + } + + @Override + public int hashCode() { + int result = type.hashCode(); + result = 31 * result + damage; + result = 31 * result + (tag != null ? tag.hashCode() : 0); + return result; + } +} diff --git a/src/main/java/refinedstorage/storage/NBTStorage.java b/src/main/java/refinedstorage/storage/NBTStorage.java index f921eb4aa..86cf8a8cb 100755 --- a/src/main/java/refinedstorage/storage/NBTStorage.java +++ b/src/main/java/refinedstorage/storage/NBTStorage.java @@ -90,7 +90,7 @@ public class NBTStorage implements IStorage { nbt.setInteger(NBT_STORED, getStored(nbt) - quantity); - ItemStack newItem = group.toItemStack(); + ItemStack newItem = group.toStack(); newItem.stackSize = quantity; @@ -116,7 +116,13 @@ public class NBTStorage implements IStorage { } private ItemGroup createItemGroupFromNBT(NBTTagCompound tag) { - return new ItemGroup(Item.getItemById(tag.getInteger(NBT_ITEM_TYPE)), tag.getInteger(NBT_ITEM_QUANTITY), tag.getInteger(NBT_ITEM_DAMAGE), tag.hasKey(NBT_ITEM_NBT) ? ((NBTTagCompound) tag.getTag(NBT_ITEM_NBT)) : null); + return new ItemGroup( + new ItemGroupMeta( + Item.getItemById(tag.getInteger(NBT_ITEM_TYPE)), + tag.getInteger(NBT_ITEM_DAMAGE), + tag.hasKey(NBT_ITEM_NBT) ? ((NBTTagCompound) tag.getTag(NBT_ITEM_NBT)) : null + ), tag.getInteger(NBT_ITEM_QUANTITY) + ); } public static int getStored(NBTTagCompound tag) { diff --git a/src/main/java/refinedstorage/tile/TileController.java b/src/main/java/refinedstorage/tile/TileController.java index a32cc767f..e52d09c22 100755 --- a/src/main/java/refinedstorage/tile/TileController.java +++ b/src/main/java/refinedstorage/tile/TileController.java @@ -338,7 +338,7 @@ public class TileController extends TileBase implements IEnergyReceiver, ISynchr } // If the item doesn't exist anymore, remove it from storage to avoid crashes - if (group.getType() == null) { + if (group.getMeta().getType() == null) { combinedGroups.add(i); } else { for (int j = i + 1; j < itemGroups.size(); ++j) { @@ -679,11 +679,11 @@ public class TileController extends TileBase implements IEnergyReceiver, ISynchr // NO OP, the quantity already set (64) is needed for shift } - if (quantity > group.getType().getItemStackLimit(group.toItemStack())) { - quantity = group.getType().getItemStackLimit(group.toItemStack()); + if (quantity > group.getMeta().getType().getItemStackLimit(group.toStack())) { + quantity = group.getMeta().getType().getItemStackLimit(group.toStack()); } - ItemStack took = take(group.copy(quantity).toItemStack()); + ItemStack took = take(group.copy(quantity).toStack()); if (took != null) { if (GridPullFlags.isPullingWithShift(flags)) { @@ -747,7 +747,7 @@ public class TileController extends TileBase implements IEnergyReceiver, ISynchr public void onCraftingRequested(int id, int quantity) { if (id >= 0 && id < itemGroups.size() && quantity > 0 && quantity <= MAX_CRAFTING_QUANTITY_PER_REQUEST) { - ItemStack requested = itemGroups.get(id).toItemStack(); + ItemStack requested = itemGroups.get(id).toStack(); int quantityPerRequest = 0; CraftingPattern pattern = getPattern(requested);