mirror of
https://github.com/GTNewHorizons/EnderIO.git
synced 2026-11-28 07:55:41 +00:00
Merge pull request #2573 from MatthiasMann/invpanel_db_cleanup
cleanup DB slot tracking
This commit is contained in:
commit
2d9d8dc3e2
6 files changed
+89
-83
No files matched your search
@@ -3,68 +3,64 @@ package crazypants.enderio.machine.invpanel.server;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
abstract class AbstractInventory {
|
||||
static final ItemEntry[] NO_SLOTS = new ItemEntry[0];
|
||||
ItemEntry[] slotItems = NO_SLOTS;
|
||||
int[] itemCounts;
|
||||
static final SlotKey[] NO_SLOTS = new SlotKey[0];
|
||||
SlotKey[] slotKeys = NO_SLOTS;
|
||||
|
||||
protected void setEmpty(InventoryDatabaseServer db, int aiIndex) {
|
||||
if (slotItems.length != 0) {
|
||||
reset(db, 0, aiIndex);
|
||||
protected void setEmpty(InventoryDatabaseServer db) {
|
||||
if (slotKeys.length != 0) {
|
||||
reset(db, 0);
|
||||
}
|
||||
}
|
||||
|
||||
protected void reset(InventoryDatabaseServer db, int count, int aiIndex) {
|
||||
for (int slot = 0; slot < slotItems.length; slot++) {
|
||||
ItemEntry key = slotItems[slot];
|
||||
if (key != null) {
|
||||
key.removeSlot(aiIndex, slot);
|
||||
db.entryChanged(key);
|
||||
protected void reset(InventoryDatabaseServer db, int count) {
|
||||
for (SlotKey slotKey : slotKeys) {
|
||||
if(slotKey != null) {
|
||||
slotKey.remove(db);
|
||||
}
|
||||
}
|
||||
slotItems = new ItemEntry[count];
|
||||
itemCounts = new int[count];
|
||||
slotKeys = new SlotKey[count];
|
||||
}
|
||||
|
||||
protected void updateSlot(InventoryDatabaseServer db, int slot, int aiIndex, ItemStack stack) {
|
||||
ItemEntry current = slotItems[slot];
|
||||
if (stack == null || stack.stackSize <= 0) {
|
||||
if (current != null) {
|
||||
slotItems[slot] = null;
|
||||
itemCounts[slot] = 0;
|
||||
current.removeSlot(aiIndex, slot);
|
||||
db.entryChanged(current);
|
||||
}
|
||||
} else {
|
||||
ItemEntry key = db.lookupItem(stack, current, true);
|
||||
if (key != current) {
|
||||
slotItems[slot] = key;
|
||||
itemCounts[slot] = stack.stackSize;
|
||||
key.addSlot(aiIndex, slot);
|
||||
db.entryChanged(key);
|
||||
if (current != null) {
|
||||
current.removeSlot(aiIndex, slot);
|
||||
db.entryChanged(current);
|
||||
}
|
||||
} else if (itemCounts[slot] != stack.stackSize) {
|
||||
itemCounts[slot] = stack.stackSize;
|
||||
db.entryChanged(current);
|
||||
}
|
||||
protected void updateSlot(InventoryDatabaseServer db, int slot, ItemStack stack) {
|
||||
SlotKey slotKey = slotKeys[slot];
|
||||
ItemEntry current = slotKey != null ? slotKey.item : null;
|
||||
ItemEntry key = db.lookupItem(stack, current, true);
|
||||
if (key != current) {
|
||||
updateSlotKey(db, slot, slotKey, key, stack);
|
||||
} else if (slotKey != null && slotKey.count != stack.stackSize) {
|
||||
slotKey.count = stack.stackSize;
|
||||
db.entryChanged(current);
|
||||
}
|
||||
}
|
||||
|
||||
protected void updateCount(InventoryDatabaseServer db, int slot, int aiIndex, ItemEntry entry, int count) {
|
||||
if (itemCounts[slot] != count) {
|
||||
itemCounts[slot] = count;
|
||||
private void updateSlotKey(InventoryDatabaseServer db, int slot, SlotKey slotKey, ItemEntry key, ItemStack stack) {
|
||||
if (slotKey != null) {
|
||||
slotKey.remove(db);
|
||||
slotKey = null;
|
||||
}
|
||||
if (key != null) {
|
||||
slotKey = new SlotKey(this, slot, key, stack.stackSize);
|
||||
key.addSlot(slotKey);
|
||||
db.entryChanged(key);
|
||||
}
|
||||
slotKeys[slot] = slotKey;
|
||||
}
|
||||
|
||||
protected void updateCount(InventoryDatabaseServer db, int slot, ItemEntry entry, int count) {
|
||||
SlotKey slotKey = slotKeys[slot];
|
||||
if (slotKey != null && slotKey.count != count && slotKey.item == entry) {
|
||||
if (count == 0) {
|
||||
slotItems[slot] = null;
|
||||
entry.removeSlot(aiIndex, slot);
|
||||
slotKey.remove(db);
|
||||
slotKeys[slot] = null;
|
||||
} else {
|
||||
slotKey.count = count;
|
||||
db.entryChanged(slotKey.item);
|
||||
}
|
||||
db.entryChanged(entry);
|
||||
}
|
||||
}
|
||||
|
||||
abstract int scanInventory(InventoryDatabaseServer db, int aiIndex);
|
||||
abstract int scanInventory(InventoryDatabaseServer db);
|
||||
|
||||
abstract int extractItem(InventoryDatabaseServer db, ItemEntry entry, int slot, int aiIndex, int count);
|
||||
abstract int extractItem(InventoryDatabaseServer db, ItemEntry entry, int slot, int count);
|
||||
|
||||
}
|
||||
@@ -8,23 +8,18 @@ class DSUInventory extends AbstractInventory {
|
||||
|
||||
DSUInventory(IDeepStorageUnit dsu) {
|
||||
this.dsu = dsu;
|
||||
this.slotKeys = new SlotKey[1];
|
||||
}
|
||||
|
||||
@Override
|
||||
public int scanInventory(InventoryDatabaseServer db, int aiIndex) {
|
||||
if (slotItems.length != 1) {
|
||||
reset(db, 1, aiIndex);
|
||||
}
|
||||
public int scanInventory(InventoryDatabaseServer db) {
|
||||
ItemStack stack = dsu.getStoredItemType();
|
||||
updateSlot(db, 0, aiIndex, stack);
|
||||
updateSlot(db, 0, stack);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int extractItem(InventoryDatabaseServer db, ItemEntry entry, int slot, int aiIndex, int count) {
|
||||
if (slotItems.length != 1) {
|
||||
return 0;
|
||||
}
|
||||
public int extractItem(InventoryDatabaseServer db, ItemEntry entry, int slot, int count) {
|
||||
ItemStack stack = dsu.getStoredItemType();
|
||||
if (db.lookupItem(stack, entry, false) != entry) {
|
||||
return 0;
|
||||
@@ -35,7 +30,7 @@ class DSUInventory extends AbstractInventory {
|
||||
}
|
||||
remaining -= count;
|
||||
dsu.setStoredItemCount(remaining);
|
||||
updateCount(db, slot, aiIndex, entry, remaining);
|
||||
updateCount(db, 0, entry, remaining);
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -234,7 +234,7 @@ public class InventoryDatabaseServer extends InventoryDatabase<ItemEntry> {
|
||||
}
|
||||
|
||||
AbstractInventory inv = inventories[currentInventory];
|
||||
int slots = inv.scanInventory(this, currentInventory);
|
||||
int slots = inv.scanInventory(this);
|
||||
|
||||
currentInventory = (currentInventory+1) % inventories.length;
|
||||
tickPause += 1 + (slots + 8) / 9;
|
||||
|
||||
@@ -1,47 +1,42 @@
|
||||
package crazypants.enderio.machine.invpanel.server;
|
||||
|
||||
import crazypants.enderio.machine.invpanel.ItemEntryBase;
|
||||
import java.util.HashSet;
|
||||
import java.util.IdentityHashMap;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
public class ItemEntry extends ItemEntryBase {
|
||||
private final HashSet<Integer> slots = new HashSet<Integer>();
|
||||
private final IdentityHashMap<SlotKey, SlotKey> slots = new IdentityHashMap<SlotKey, SlotKey>();
|
||||
|
||||
public ItemEntry(int dbID, int hash, int itemID, int meta, NBTTagCompound nbt) {
|
||||
super(dbID, hash, itemID, meta, nbt);
|
||||
}
|
||||
|
||||
static int encodeAISlot(int aiIndex, int slot) {
|
||||
return (aiIndex << 12) | slot;
|
||||
void addSlot(SlotKey slotKey) {
|
||||
slots.put(slotKey, slotKey);
|
||||
}
|
||||
|
||||
void addSlot(int aiIndex, int slot) {
|
||||
slots.add(encodeAISlot(aiIndex, slot));
|
||||
}
|
||||
|
||||
void removeSlot(int aiIndex, int slot) {
|
||||
slots.remove(encodeAISlot(aiIndex, slot));
|
||||
void removeSlot(SlotKey slotKey) {
|
||||
slots.remove(slotKey);
|
||||
}
|
||||
|
||||
int countItems(InventoryDatabaseServer db) {
|
||||
int count = 0;
|
||||
for (Integer idx : slots) {
|
||||
AbstractInventory key = db.getInventory(idx >> 12);
|
||||
count += key.itemCounts[idx & 4095];
|
||||
for (SlotKey slotKey : slots.values()) {
|
||||
count += slotKey.count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int extractItems(InventoryDatabaseServer db, int count) {
|
||||
int extracted = 0;
|
||||
Integer[] copy = slots.toArray(new Integer[slots.size()]);
|
||||
for (Integer idx : copy) {
|
||||
int aiIndex = idx >> 12;
|
||||
int slotIndex = idx & 4095;
|
||||
AbstractInventory key = db.getInventory(aiIndex);
|
||||
int amount = key.extractItem(db, this, slotIndex, aiIndex, count);
|
||||
SlotKey[] copy = slots.values().toArray(new SlotKey[slots.size()]);
|
||||
for (SlotKey slotKey : copy) {
|
||||
int amount = slotKey.inv.extractItem(db, this, slotKey.slot, count);
|
||||
count -= amount;
|
||||
extracted += amount;
|
||||
if (count <= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return extracted;
|
||||
}
|
||||
|
||||
@@ -12,17 +12,17 @@ class NormalInventory extends AbstractInventory {
|
||||
}
|
||||
|
||||
@Override
|
||||
int scanInventory(InventoryDatabaseServer db, int aiIndex) {
|
||||
int scanInventory(InventoryDatabaseServer db) {
|
||||
ISidedInventory inv = ni.getInventoryRecheck();
|
||||
int side = ni.getInventorySide();
|
||||
int[] slotIndices = inv.getAccessibleSlotsFromSide(side);
|
||||
if (slotIndices == null || slotIndices.length == 0) {
|
||||
setEmpty(db, aiIndex);
|
||||
setEmpty(db);
|
||||
return 0;
|
||||
}
|
||||
int count = slotIndices.length;
|
||||
if (count != slotItems.length) {
|
||||
reset(db, count, aiIndex);
|
||||
if (count != slotKeys.length) {
|
||||
reset(db, count);
|
||||
}
|
||||
for (int slot = 0; slot < count; slot++) {
|
||||
int invSlot = slotIndices[slot];
|
||||
@@ -30,13 +30,13 @@ class NormalInventory extends AbstractInventory {
|
||||
if (stack != null && !inv.canExtractItem(invSlot, stack, side)) {
|
||||
stack = null;
|
||||
}
|
||||
updateSlot(db, slot, aiIndex, stack);
|
||||
updateSlot(db, slot, stack);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int extractItem(InventoryDatabaseServer db, ItemEntry entry, int slot, int aiIndex, int count) {
|
||||
public int extractItem(InventoryDatabaseServer db, ItemEntry entry, int slot, int count) {
|
||||
ISidedInventory inv = ni.getInventoryRecheck();
|
||||
int side = ni.getInventorySide();
|
||||
int[] slotIndices = inv.getAccessibleSlotsFromSide(side);
|
||||
@@ -57,7 +57,7 @@ class NormalInventory extends AbstractInventory {
|
||||
}
|
||||
ni.itemExtracted(invSlot, count);
|
||||
remaining -= count;
|
||||
updateCount(db, slot, aiIndex, entry, remaining);
|
||||
updateCount(db, slot, entry, remaining);
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package crazypants.enderio.machine.invpanel.server;
|
||||
|
||||
public final class SlotKey {
|
||||
final AbstractInventory inv;
|
||||
final int slot;
|
||||
final ItemEntry item;
|
||||
int count;
|
||||
|
||||
SlotKey(AbstractInventory inv, int slot, ItemEntry item, int count) {
|
||||
this.inv = inv;
|
||||
this.slot = slot;
|
||||
this.item = item;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
void remove(InventoryDatabaseServer db) {
|
||||
item.removeSlot(this);
|
||||
db.entryChanged(item);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user