Use a list instead of map. Unfortunately not O(1) anymore, but I have no choice due to flags.
This commit is contained in:
@@ -3,49 +3,47 @@ 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 ItemGroupMeta meta;
|
||||
private Item type;
|
||||
private int quantity;
|
||||
private int damage;
|
||||
private NBTTagCompound tag;
|
||||
// Used clientside
|
||||
private int id;
|
||||
|
||||
public ItemGroup(ByteBuf buf) {
|
||||
this.id = buf.readInt();
|
||||
this.meta = new ItemGroupMeta(
|
||||
Item.getItemById(buf.readInt()),
|
||||
buf.readInt(),
|
||||
buf.readBoolean() ? ByteBufUtils.readTag(buf) : null
|
||||
);
|
||||
this.type = Item.getItemById(buf.readInt());
|
||||
this.quantity = buf.readInt();
|
||||
this.damage = buf.readInt();
|
||||
this.tag = buf.readBoolean() ? ByteBufUtils.readTag(buf) : null;
|
||||
}
|
||||
|
||||
public ItemGroup(ItemGroupMeta meta, int quantity) {
|
||||
this.meta = meta;
|
||||
public ItemGroup(Item type, int quantity, int damage, NBTTagCompound tag) {
|
||||
this.type = type;
|
||||
this.quantity = quantity;
|
||||
this.damage = damage;
|
||||
this.tag = tag;
|
||||
}
|
||||
|
||||
public ItemGroup(ItemStack stack) {
|
||||
this(new ItemGroupMeta(stack), stack.stackSize);
|
||||
this(stack.getItem(), stack.stackSize, stack.getItemDamage(), stack.getTagCompound());
|
||||
}
|
||||
|
||||
public void toBytes(ByteBuf buf, int id) {
|
||||
buf.writeInt(id);
|
||||
buf.writeInt(Item.getIdFromItem(meta.getType()));
|
||||
buf.writeInt(meta.getDamage());
|
||||
buf.writeBoolean(meta.hasTag());
|
||||
|
||||
if (meta.hasTag()) {
|
||||
ByteBufUtils.writeTag(buf, meta.getTag());
|
||||
}
|
||||
|
||||
buf.writeInt(Item.getIdFromItem(type));
|
||||
buf.writeInt(quantity);
|
||||
}
|
||||
buf.writeInt(damage);
|
||||
buf.writeBoolean(hasTag());
|
||||
|
||||
public ItemGroupMeta getMeta() {
|
||||
return meta;
|
||||
if (hasTag()) {
|
||||
ByteBufUtils.writeTag(buf, tag);
|
||||
}
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
@@ -56,6 +54,98 @@ public class ItemGroup {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
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(ItemGroup other, int flags) {
|
||||
if (type != other.getType()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((flags & RefinedStorageUtils.COMPARE_QUANTITY) == RefinedStorageUtils.COMPARE_QUANTITY && quantity != other.getQuantity()) {
|
||||
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_QUANTITY) == RefinedStorageUtils.COMPARE_QUANTITY && quantity != stack.stackSize) {
|
||||
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;
|
||||
}
|
||||
|
||||
public boolean compareNoQuantity(ItemGroup other) {
|
||||
return compare(other, RefinedStorageUtils.COMPARE_NBT | RefinedStorageUtils.COMPARE_DAMAGE);
|
||||
}
|
||||
|
||||
public boolean compareNoQuantity(ItemStack stack) {
|
||||
return compare(stack, RefinedStorageUtils.COMPARE_NBT | RefinedStorageUtils.COMPARE_DAMAGE);
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
@@ -65,38 +155,14 @@ public class ItemGroup {
|
||||
}
|
||||
|
||||
public ItemGroup copy(int newQuantity) {
|
||||
return new ItemGroup(meta, newQuantity);
|
||||
return new ItemGroup(type, newQuantity, damage, tag);
|
||||
}
|
||||
|
||||
public ItemStack toStack() {
|
||||
ItemStack stack = new ItemStack(meta.getType(), quantity, meta.getDamage());
|
||||
ItemStack stack = new ItemStack(type, quantity, damage);
|
||||
|
||||
stack.setTagCompound(meta.getTag());
|
||||
stack.setTagCompound(tag);
|
||||
|
||||
return stack;
|
||||
}
|
||||
|
||||
public boolean compare(ItemGroup other, int flags) {
|
||||
if ((flags & RefinedStorageUtils.COMPARE_QUANTITY) == RefinedStorageUtils.COMPARE_QUANTITY && other.getQuantity() != quantity) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return meta.compare(other.getMeta(), flags);
|
||||
}
|
||||
|
||||
public boolean compare(ItemStack stack, int flags) {
|
||||
if ((flags & RefinedStorageUtils.COMPARE_QUANTITY) == RefinedStorageUtils.COMPARE_QUANTITY && stack.stackSize != quantity) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return meta.compare(stack, flags);
|
||||
}
|
||||
|
||||
public boolean compareNoQuantity(ItemGroup other) {
|
||||
return compare(other, RefinedStorageUtils.COMPARE_NBT | RefinedStorageUtils.COMPARE_DAMAGE);
|
||||
}
|
||||
|
||||
public boolean compareNoQuantity(ItemStack stack) {
|
||||
return compare(stack, RefinedStorageUtils.COMPARE_NBT | RefinedStorageUtils.COMPARE_DAMAGE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,133 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
public boolean compareNoQuantity(ItemGroupMeta other) {
|
||||
return compare(other, RefinedStorageUtils.COMPARE_NBT | RefinedStorageUtils.COMPARE_DAMAGE);
|
||||
}
|
||||
|
||||
public boolean compareNoQuantity(ItemStack stack) {
|
||||
return compare(stack, RefinedStorageUtils.COMPARE_NBT | RefinedStorageUtils.COMPARE_DAMAGE);
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
@@ -5,9 +5,8 @@ import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class NBTStorage implements IStorage {
|
||||
public static final String NBT_ITEMS = "Items";
|
||||
@@ -23,8 +22,7 @@ public class NBTStorage implements IStorage {
|
||||
private int priority;
|
||||
private boolean dirty;
|
||||
|
||||
// We use a map here because is much faster than looping over a list
|
||||
private Map<ItemGroupMeta, Integer> groups = new HashMap<ItemGroupMeta, Integer>();
|
||||
private List<ItemGroup> groups = new ArrayList<ItemGroup>();
|
||||
|
||||
public NBTStorage(NBTTagCompound tag, int capacity, int priority) {
|
||||
this.tag = tag;
|
||||
@@ -40,28 +38,29 @@ public class NBTStorage implements IStorage {
|
||||
for (int i = 0; i < list.tagCount(); ++i) {
|
||||
NBTTagCompound tag = list.getCompoundTagAt(i);
|
||||
|
||||
ItemGroupMeta meta = new ItemGroupMeta(
|
||||
ItemGroup group = 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
|
||||
);
|
||||
|
||||
groups.put(meta, tag.getInteger(NBT_ITEM_QUANTITY));
|
||||
groups.add(group);
|
||||
}
|
||||
}
|
||||
|
||||
public void writeToNBT(NBTTagCompound tag) {
|
||||
NBTTagList list = new NBTTagList();
|
||||
|
||||
for (Map.Entry<ItemGroupMeta, Integer> entry : groups.entrySet()) {
|
||||
for (ItemGroup group : groups) {
|
||||
NBTTagCompound itemTag = new NBTTagCompound();
|
||||
|
||||
itemTag.setInteger(NBT_ITEM_TYPE, Item.getIdFromItem(entry.getKey().getType()));
|
||||
itemTag.setInteger(NBT_ITEM_QUANTITY, entry.getValue());
|
||||
itemTag.setInteger(NBT_ITEM_DAMAGE, entry.getKey().getDamage());
|
||||
itemTag.setInteger(NBT_ITEM_TYPE, Item.getIdFromItem(group.getType()));
|
||||
itemTag.setInteger(NBT_ITEM_QUANTITY, group.getQuantity());
|
||||
itemTag.setInteger(NBT_ITEM_DAMAGE, group.getDamage());
|
||||
|
||||
if (entry.getKey().hasTag()) {
|
||||
itemTag.setTag(NBT_ITEM_NBT, entry.getKey().getTag());
|
||||
if (group.hasTag()) {
|
||||
itemTag.setTag(NBT_ITEM_NBT, group.getTag());
|
||||
}
|
||||
|
||||
list.appendTag(itemTag);
|
||||
@@ -72,9 +71,7 @@ public class NBTStorage implements IStorage {
|
||||
|
||||
@Override
|
||||
public void addItems(List<ItemGroup> items) {
|
||||
for (Map.Entry<ItemGroupMeta, Integer> entry : groups.entrySet()) {
|
||||
items.add(new ItemGroup(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
items.addAll(groups);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -83,42 +80,38 @@ public class NBTStorage implements IStorage {
|
||||
|
||||
tag.setInteger(NBT_STORED, getStored(tag) + stack.stackSize);
|
||||
|
||||
for (Map.Entry<ItemGroupMeta, Integer> entry : groups.entrySet()) {
|
||||
if (entry.getKey().compareNoQuantity(stack)) {
|
||||
groups.put(entry.getKey(), entry.getValue() + stack.stackSize);
|
||||
for (ItemGroup group : groups) {
|
||||
if (group.compareNoQuantity(stack)) {
|
||||
group.setQuantity(group.getQuantity() + stack.stackSize);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
groups.put(new ItemGroupMeta(stack), stack.stackSize);
|
||||
groups.add(new ItemGroup(stack));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack take(ItemStack stack, int flags) {
|
||||
int quantity = stack.stackSize;
|
||||
|
||||
for (Map.Entry<ItemGroupMeta, Integer> entry : groups.entrySet()) {
|
||||
if (entry.getKey().compare(stack, flags)) {
|
||||
if (quantity > entry.getValue()) {
|
||||
quantity = entry.getValue();
|
||||
for (ItemGroup group : groups) {
|
||||
if (group.compare(stack, flags)) {
|
||||
if (quantity > group.getQuantity()) {
|
||||
quantity = group.getQuantity();
|
||||
}
|
||||
|
||||
if (entry.getValue() - quantity == 0) {
|
||||
groups.remove(entry.getKey());
|
||||
if (group.getQuantity() - quantity == 0) {
|
||||
groups.remove(group);
|
||||
} else {
|
||||
groups.put(entry.getKey(), entry.getValue() - quantity);
|
||||
group.setQuantity(group.getQuantity() - quantity);
|
||||
}
|
||||
|
||||
tag.setInteger(NBT_STORED, getStored(tag) - quantity);
|
||||
|
||||
ItemStack result = new ItemStack(
|
||||
entry.getKey().getType(),
|
||||
quantity,
|
||||
entry.getKey().getDamage()
|
||||
);
|
||||
ItemStack result = group.toStack();
|
||||
|
||||
result.setTagCompound(entry.getKey().getTag());
|
||||
result.setTagCompound(group.getTag());
|
||||
|
||||
markDirty();
|
||||
|
||||
@@ -139,18 +132,10 @@ public class NBTStorage implements IStorage {
|
||||
return priority;
|
||||
}
|
||||
|
||||
public void setPriority(int priority) {
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
public int getCapacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
public void setCapacity(int capacity) {
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
public NBTTagCompound getTag() {
|
||||
return tag;
|
||||
}
|
||||
|
||||
@@ -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.getMeta().getType() == null) {
|
||||
if (group.getType() == null) {
|
||||
combinedGroups.add(i);
|
||||
} else {
|
||||
for (int j = i + 1; j < itemGroups.size(); ++j) {
|
||||
@@ -679,8 +679,8 @@ public class TileController extends TileBase implements IEnergyReceiver, ISynchr
|
||||
// NO OP, the quantity already set (64) is needed for shift
|
||||
}
|
||||
|
||||
if (quantity > group.getMeta().getType().getItemStackLimit(group.toStack())) {
|
||||
quantity = group.getMeta().getType().getItemStackLimit(group.toStack());
|
||||
if (quantity > group.getType().getItemStackLimit(group.toStack())) {
|
||||
quantity = group.getType().getItemStackLimit(group.toStack());
|
||||
}
|
||||
|
||||
ItemStack took = take(group.copy(quantity).toStack());
|
||||
|
||||
Reference in New Issue
Block a user