Untested work
This commit is contained in:
@@ -16,10 +16,6 @@ public class DiskStorage extends NBTStorage {
|
||||
|
||||
@Override
|
||||
public boolean canPush(ItemStack stack) {
|
||||
if (ModeConfigUtils.doesNotViolateMode(diskDrive.getInventory(), diskDrive.getModeConfig(), diskDrive.getCompare(), stack)) {
|
||||
return super.canPush(stack);
|
||||
}
|
||||
|
||||
return false;
|
||||
return ModeConfigUtils.doesNotViolateMode(diskDrive.getInventory(), diskDrive.getModeConfig(), diskDrive.getCompare(), stack) && super.canPush(stack);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,6 +96,14 @@ public class ItemGroupMeta {
|
||||
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) {
|
||||
|
||||
@@ -5,7 +5,9 @@ import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class NBTStorage implements IStorage {
|
||||
public static final String NBT_ITEMS = "Items";
|
||||
@@ -16,98 +18,64 @@ public class NBTStorage implements IStorage {
|
||||
public static final String NBT_ITEM_DAMAGE = "Damage";
|
||||
public static final String NBT_ITEM_NBT = "NBT";
|
||||
|
||||
private NBTTagCompound nbt;
|
||||
private NBTTagCompound tag;
|
||||
private int capacity;
|
||||
private int priority;
|
||||
|
||||
public NBTStorage(NBTTagCompound nbt, int capacity, int priority) {
|
||||
this.nbt = nbt;
|
||||
private Map<ItemGroupMeta, Integer> groups = new HashMap<ItemGroupMeta, Integer>();
|
||||
|
||||
public NBTStorage(NBTTagCompound tag, int capacity, int priority) {
|
||||
this.tag = tag;
|
||||
this.capacity = capacity;
|
||||
this.priority = priority;
|
||||
|
||||
readFromNBT();
|
||||
}
|
||||
|
||||
public void readFromNBT() {
|
||||
NBTTagList list = (NBTTagList) tag.getTag(NBT_ITEMS);
|
||||
|
||||
for (int i = 0; i < list.tagCount(); ++i) {
|
||||
NBTTagCompound tag = list.getCompoundTagAt(i);
|
||||
|
||||
ItemGroupMeta meta = 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
|
||||
);
|
||||
|
||||
groups.put(meta, tag.getInteger(NBT_ITEM_QUANTITY));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addItems(List<ItemGroup> items) {
|
||||
NBTTagList list = (NBTTagList) nbt.getTag(NBT_ITEMS);
|
||||
|
||||
for (int i = 0; i < list.tagCount(); ++i) {
|
||||
items.add(createItemGroupFromNBT(list.getCompoundTagAt(i)));
|
||||
for (Map.Entry<ItemGroupMeta, Integer> entry : groups.entrySet()) {
|
||||
items.add(new ItemGroup(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void push(ItemStack stack) {
|
||||
NBTTagList list = (NBTTagList) nbt.getTag(NBT_ITEMS);
|
||||
|
||||
nbt.setInteger(NBT_STORED, getStored(nbt) + stack.stackSize);
|
||||
|
||||
for (int i = 0; i < list.tagCount(); ++i) {
|
||||
NBTTagCompound tag = list.getCompoundTagAt(i);
|
||||
|
||||
ItemGroup group = createItemGroupFromNBT(tag);
|
||||
|
||||
if (group.compareNoQuantity(stack)) {
|
||||
tag.setInteger(NBT_ITEM_QUANTITY, group.getQuantity() + stack.stackSize);
|
||||
for (Map.Entry<ItemGroupMeta, Integer> entry : groups.entrySet()) {
|
||||
if (entry.getKey().compareNoQuantity(stack)) {
|
||||
groups.put(entry.getKey(), entry.getValue() + stack.stackSize);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
NBTTagCompound tag = new NBTTagCompound();
|
||||
|
||||
tag.setInteger(NBT_ITEM_TYPE, Item.getIdFromItem(stack.getItem()));
|
||||
tag.setInteger(NBT_ITEM_QUANTITY, stack.stackSize);
|
||||
tag.setInteger(NBT_ITEM_DAMAGE, stack.getItemDamage());
|
||||
|
||||
if (stack.hasTagCompound()) {
|
||||
tag.setTag(NBT_ITEM_NBT, stack.getTagCompound());
|
||||
}
|
||||
|
||||
list.appendTag(tag);
|
||||
groups.put(new ItemGroupMeta(stack), stack.stackSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack take(ItemStack stack, int flags) {
|
||||
int quantity = stack.stackSize;
|
||||
|
||||
NBTTagList list = (NBTTagList) nbt.getTag(NBT_ITEMS);
|
||||
|
||||
for (int i = 0; i < list.tagCount(); ++i) {
|
||||
NBTTagCompound tag = list.getCompoundTagAt(i);
|
||||
|
||||
ItemGroup group = createItemGroupFromNBT(tag);
|
||||
|
||||
if (group.compare(stack, flags)) {
|
||||
if (quantity > group.getQuantity()) {
|
||||
quantity = group.getQuantity();
|
||||
}
|
||||
|
||||
tag.setInteger(NBT_ITEM_QUANTITY, group.getQuantity() - quantity);
|
||||
|
||||
if (group.getQuantity() - quantity == 0) {
|
||||
list.removeTag(i);
|
||||
}
|
||||
|
||||
nbt.setInteger(NBT_STORED, getStored(nbt) - quantity);
|
||||
|
||||
ItemStack newItem = group.toStack();
|
||||
|
||||
newItem.stackSize = quantity;
|
||||
|
||||
return newItem;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPush(ItemStack stack) {
|
||||
if (capacity == -1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return (getStored(nbt) + stack.stackSize) <= capacity;
|
||||
return capacity == -1 || (getStored(tag) + stack.stackSize) <= capacity;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -115,14 +83,12 @@ public class NBTStorage implements IStorage {
|
||||
return priority;
|
||||
}
|
||||
|
||||
private ItemGroup createItemGroupFromNBT(NBTTagCompound tag) {
|
||||
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 void setPriority(int priority) {
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
public NBTTagCompound getTag() {
|
||||
return tag;
|
||||
}
|
||||
|
||||
public static int getStored(NBTTagCompound tag) {
|
||||
|
||||
@@ -29,6 +29,8 @@ public class TileDiskDrive extends TileMachine implements IStorageProvider, ISto
|
||||
private InventorySimple inventory = new InventorySimple("disk_drive", 8, this);
|
||||
private InventorySimple filterInventory = new InventorySimple("filters", 9, this);
|
||||
|
||||
private NBTStorage storages[] = new NBTStorage[getSizeInventory()];
|
||||
|
||||
private int priority = 0;
|
||||
private int compare = 0;
|
||||
private int mode = 0;
|
||||
@@ -52,9 +54,9 @@ public class TileDiskDrive extends TileMachine implements IStorageProvider, ISto
|
||||
|
||||
@Override
|
||||
public void provide(List<IStorage> storages) {
|
||||
for (int i = 0; i < getSizeInventory(); ++i) {
|
||||
if (getStackInSlot(i) != null) {
|
||||
storages.add(new DiskStorage(getStackInSlot(i), this));
|
||||
for (NBTStorage storage : this.storages) {
|
||||
if (storage != null) {
|
||||
storages.add(storage);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -249,6 +251,12 @@ public class TileDiskDrive extends TileMachine implements IStorageProvider, ISto
|
||||
|
||||
@Override
|
||||
public void setInventorySlotContents(int slot, ItemStack stack) {
|
||||
if (stack != null) {
|
||||
storages[slot] = new DiskStorage(stack, this);
|
||||
} else {
|
||||
storages[slot] = null;
|
||||
}
|
||||
|
||||
inventory.setInventorySlotContents(slot, stack);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,9 @@ public class TileStorage extends TileMachine implements IStorageProvider, IStora
|
||||
|
||||
private InventorySimple inventory = new InventorySimple("storage", 9, this);
|
||||
|
||||
private NBTTagCompound tag = NBTStorage.createNBT();
|
||||
private NBTTagCompound storageTag = NBTStorage.createNBT();
|
||||
|
||||
private NBTStorage storage;
|
||||
|
||||
private int priority = 0;
|
||||
private int compare = 0;
|
||||
@@ -45,6 +47,14 @@ public class TileStorage extends TileMachine implements IStorageProvider, IStora
|
||||
public void updateMachine() {
|
||||
}
|
||||
|
||||
public NBTStorage getStorage() {
|
||||
if (storage == null) {
|
||||
storage = new NBTStorage(storageTag, priority, getCapacity());
|
||||
}
|
||||
|
||||
return storage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void provide(List<IStorage> storages) {
|
||||
storages.add(this);
|
||||
@@ -56,14 +66,14 @@ public class TileStorage extends TileMachine implements IStorageProvider, IStora
|
||||
|
||||
RefinedStorageUtils.restoreInventory(inventory, 0, nbt);
|
||||
|
||||
if (nbt.hasKey(NBT_STORAGE)) {
|
||||
tag = nbt.getCompoundTag(NBT_STORAGE);
|
||||
}
|
||||
|
||||
if (nbt.hasKey(NBT_PRIORITY)) {
|
||||
priority = nbt.getInteger(NBT_PRIORITY);
|
||||
}
|
||||
|
||||
if (nbt.hasKey(NBT_STORAGE)) {
|
||||
storageTag = nbt.getCompoundTag(NBT_STORAGE);
|
||||
}
|
||||
|
||||
if (nbt.hasKey(NBT_COMPARE)) {
|
||||
compare = nbt.getInteger(NBT_COMPARE);
|
||||
}
|
||||
@@ -79,8 +89,8 @@ public class TileStorage extends TileMachine implements IStorageProvider, IStora
|
||||
|
||||
RefinedStorageUtils.saveInventory(inventory, 0, nbt);
|
||||
|
||||
nbt.setTag(NBT_STORAGE, tag);
|
||||
nbt.setInteger(NBT_PRIORITY, priority);
|
||||
nbt.setTag(NBT_STORAGE, getStorage().getTag());
|
||||
nbt.setInteger(NBT_COMPARE, compare);
|
||||
nbt.setInteger(NBT_MODE, mode);
|
||||
}
|
||||
@@ -97,7 +107,7 @@ public class TileStorage extends TileMachine implements IStorageProvider, IStora
|
||||
public void sendContainerData(ByteBuf buf) {
|
||||
super.sendContainerData(buf);
|
||||
|
||||
buf.writeInt(NBTStorage.getStored(tag));
|
||||
buf.writeInt(NBTStorage.getStored(storageTag));
|
||||
buf.writeInt(priority);
|
||||
buf.writeInt(compare);
|
||||
buf.writeInt(mode);
|
||||
@@ -212,18 +222,14 @@ public class TileStorage extends TileMachine implements IStorageProvider, IStora
|
||||
RefinedStorage.NETWORK.sendToServer(new MessagePriorityUpdate(pos, priority));
|
||||
}
|
||||
|
||||
public NBTStorage getStorage() {
|
||||
return new NBTStorage(tag, getCapacity(), priority);
|
||||
}
|
||||
|
||||
public NBTTagCompound getStorageTag() {
|
||||
return tag;
|
||||
return storageTag;
|
||||
}
|
||||
|
||||
public void setStorageTag(NBTTagCompound tag) {
|
||||
markDirty();
|
||||
|
||||
this.tag = tag;
|
||||
this.storageTag = tag;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -234,6 +240,7 @@ public class TileStorage extends TileMachine implements IStorageProvider, IStora
|
||||
public void setPriority(int priority) {
|
||||
markDirty();
|
||||
|
||||
this.getStorage().setPriority(priority);
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user