some restructuring

This commit is contained in:
Matthias Mann committed 2015-04-20 02:47:54 +02:00
1 parent 80254723cd
commit 4d238fd80b
9 files changed
+647 -504

No files matched your search

@@ -100,12 +100,12 @@ public class GuiInventoryPanel extends GuiMachineBase<TileInventoryPanel> {
itemRender.renderItemOverlayIntoGUI(font, mc.renderEngine, stack, x, y, str);
}
private InventoryDatabase getDatabase() {
return getTileEntity().getDatabase();
private InventoryDatabaseClient getDatabase() {
return (InventoryDatabaseClient) getTileEntity().getDatabaseClient();
}
private void doScroll() {
scrollMax = Math.max(0, (getDatabase().getClientItemCount()+GHOST_COLUMNS-1) / GHOST_COLUMNS - GHOST_ROWS);
scrollMax = Math.max(0, (getDatabase().getNumEntries()+GHOST_COLUMNS-1) / GHOST_COLUMNS - GHOST_ROWS);
if(scrollUpPressed && scrollPos > 0) {
scrollPos--;
}
@@ -120,14 +120,13 @@ public class GuiInventoryPanel extends GuiMachineBase<TileInventoryPanel> {
scrollPos = scrollMax;
}
InventoryDatabase database = getDatabase();
InventoryDatabaseClient database = getDatabase();
int index = scrollPos * GHOST_COLUMNS;
int count = database.getClientItemCount();
int count = database.getNumEntries();
for(int i = 0; i < GHOST_ROWS*GHOST_COLUMNS; i++,index++) {
GhostSlot slot = ghostSlots.get(i);
if(index < count) {
InventoryDatabase.ItemKey key = database.getClientItem(index);
slot.stack = key.makeItemStack();
slot.stack = database.getItemStack(index);
} else {
slot.stack = null;
}
@@ -1,23 +1,14 @@
package crazypants.enderio.machine.invpanel;
import crazypants.enderio.conduit.item.NetworkedInventory;
import crazypants.enderio.network.CompressedDataInput;
import crazypants.enderio.network.CompressedDataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
public class InventoryDatabase {
public abstract class InventoryDatabase<ItemEntry extends ItemEntryBase> {
private static final Item META_EXTRACTOR = new Item();
private static final int SIMPLE_ITEMID_BITS = 12;
private static final int SIMPLE_META_BITS = 4;
@@ -25,59 +16,27 @@ public class InventoryDatabase {
private static final int SIMPLE_MAX_META = 1<<SIMPLE_META_BITS;
private static final int SIMPLE_META_MASK = SIMPLE_MAX_META - 1;
private static final int COMPLEX_DBINDEX_START = 1<<(SIMPLE_ITEMID_BITS + SIMPLE_META_BITS);
protected static final int COMPLEX_DBINDEX_START = 1<<(SIMPLE_ITEMID_BITS + SIMPLE_META_BITS);
private final HashMap<Integer, ItemKey> simpleRegsitry;
private final HashMap<ItemKey, ItemKey> complexRegistry;
private final ArrayList<ItemKey> complexItems;
private final ArrayList<ItemKey> clientItems;
protected final HashMap<Integer, ItemEntry> simpleRegsitry;
protected final HashMap<ItemEntry, ItemEntry> complexRegistry;
protected final ArrayList<ItemEntry> complexItems;
private int generation;
private InventoryKey[] inventories;
private int currentInventory;
private ChangeLog[] changeLogs;
private boolean clientListNeedsSorting;
protected int generation;
public InventoryDatabase() {
simpleRegsitry = new HashMap<Integer, ItemKey>();
complexRegistry = new HashMap<ItemKey, ItemKey>();
complexItems = new ArrayList<ItemKey>();
clientItems = new ArrayList<ItemKey>();
changeLogs = new ChangeLog[0];
simpleRegsitry = new HashMap<Integer, ItemEntry>();
complexRegistry = new HashMap<ItemEntry, ItemEntry>();
complexItems = new ArrayList<ItemEntry>();
}
public void addChangeLog(ChangeLog log) {
for(ChangeLog alog : changeLogs) {
if(alog == log) {
return;
}
}
int count = changeLogs.length;
changeLogs = Arrays.copyOf(changeLogs, count+1);
changeLogs[count] = log;
}
public void removeChangeLog(ChangeLog log) {
int count = changeLogs.length;
for(int i = 0; i < count ; i++) {
if(changeLogs[i] == log) {
ChangeLog[] tmp = new ChangeLog[--count];
System.arraycopy(changeLogs, 0, tmp, 0, i);
System.arraycopy(changeLogs, i+1, tmp, i, count-i);
changeLogs = tmp;
break;
}
}
}
public ItemKey lookupItem(ItemStack stack, ItemKey hint) {
public ItemEntry lookupItem(ItemStack stack, ItemEntry hint) {
if(stack == null || stack.getItem() == null) {
return null;
}
int itemID = Item.getIdFromItem(stack.getItem());
int meta = Items.bone.getDamage(stack); // hack
int meta = META_EXTRACTOR.getDamage(stack);
NBTTagCompound nbt = stack.stackTagCompound;
if(nbt != null && nbt.hasNoTags()) {
@@ -95,21 +54,28 @@ public class InventoryDatabase {
}
}
private ItemKey getComplexItem(int itemID, int meta, NBTTagCompound nbt) {
protected abstract ItemEntry createItemEntry(int dbId, int hash, int itemID, int meta, NBTTagCompound nbt);
protected ItemEntry createItemEntry(int dbId, int itemID, int meta, NBTTagCompound nbt) {
int hash = computeComplexHash(itemID, meta, nbt);
ItemKey key = new ItemKey(-1, hash, itemID, meta, nbt);
key = complexRegistry.get(key);
if(key == null) {
return createItemEntry(dbId, hash, itemID, meta, nbt);
}
private ItemEntry getComplexItem(int itemID, int meta, NBTTagCompound nbt) {
int hash = computeComplexHash(itemID, meta, nbt);
ItemEntryKey key = new ItemEntryKey(hash, itemID, meta, nbt);
@SuppressWarnings("element-type-mismatch")
ItemEntry entry = complexRegistry.get(key);
if(entry == null) {
if(nbt != null) {
nbt = (NBTTagCompound)nbt.copy();
}
key = new ItemKey(COMPLEX_DBINDEX_START + complexItems.size(), hash, itemID, meta, nbt);
complexItems.add(key);
complexRegistry.put(key, key);
System.out.println("New complex item: " + key);
entry = createItemEntry(COMPLEX_DBINDEX_START + complexItems.size(), hash, itemID, meta, nbt);
complexItems.add(entry);
complexRegistry.put(entry, entry);
System.out.println("New complex item: " + entry);
}
return key;
return entry;
}
private static int computeComplexHash(int itemID, int meta, NBTTagCompound nbt) {
@@ -120,24 +86,24 @@ public class InventoryDatabase {
return hash;
}
private ItemKey getSimpleItem(int itemID, int meta) {
private ItemEntry getSimpleItem(int itemID, int meta) {
Integer dbID = (itemID << SIMPLE_META_BITS) | meta;
ItemKey key = (ItemKey) simpleRegsitry.get(dbID);
if(key == null) {
key = new ItemKey(dbID, dbID, itemID, meta, null);
simpleRegsitry.put(dbID, key);
System.out.println("New simple item: " + key);
ItemEntry entry = simpleRegsitry.get(dbID);
if(entry == null) {
entry = createItemEntry(dbID, dbID, itemID, meta, null);
simpleRegsitry.put(dbID, entry);
System.out.println("New simple item: " + entry);
}
return key;
return entry;
}
private ItemKey getSimpleItem(int dbID) {
protected ItemEntry getSimpleItem(int dbID) {
int itemID = dbID >> SIMPLE_META_BITS;
int meta = dbID & SIMPLE_META_MASK;
return getSimpleItem(itemID, meta);
}
public ItemKey getItem(int dbID) {
public ItemEntry getItem(int dbID) {
if(dbID < COMPLEX_DBINDEX_START) {
return getSimpleItem(dbID);
}
@@ -149,241 +115,13 @@ public class InventoryDatabase {
return null;
}
public void readCompressedItems(byte[] compressed) throws IOException {
CompressedDataInput cdi = new CompressedDataInput(compressed);
try {
int pktGeneration = cdi.readVariable();
if(pktGeneration != generation) {
return;
}
int count = cdi.readVariable();
for(int i=0 ; i<count ; i++) {
int code = cdi.readVariable();
int dbID = code >> 1;
NBTTagCompound nbt = null;
if((code & 1) == 1) {
nbt = CompressedStreamTools.read(cdi);
}
if(dbID >= COMPLEX_DBINDEX_START) {
int dbIndex = dbID - COMPLEX_DBINDEX_START;
int itemID = cdi.readVariable();
int meta = cdi.readVariable();
while(complexItems.size() <= dbIndex) {
complexItems.add(null);
}
ItemKey item = complexItems.get(dbIndex);
if(item == null) {
int hash = computeComplexHash(itemID, meta, nbt);
item = new ItemKey(dbID, hash, itemID, meta, nbt);
complexItems.set(dbIndex, item);
complexRegistry.put(item, item);
}
} else {
getSimpleItem(dbID);
}
}
} finally {
cdi.close();
}
}
public byte[] compressItemInfo(List<ItemKey> items) throws IOException{
CompressedDataOutput cdo = new CompressedDataOutput();
try {
int count = items.size();
cdo.writeVariable(generation);
cdo.writeVariable(count);
for(int i=0 ; i<count ; i++) {
ItemKey item = items.get(i);
item.write(cdo);
}
return cdo.getCompressed();
} finally {
cdo.close();
}
}
public byte[] compressItemList() throws IOException {
CompressedDataOutput cdo = new CompressedDataOutput();
try {
cdo.writeVariable(generation);
cdo.writeByte(0);
for(Map.Entry<Integer, ItemKey> entry : simpleRegsitry.entrySet()) {
int count = entry.getValue().countItems(this);
if(count > 0) {
cdo.writeVariable(count);
cdo.writeShort(entry.getKey());
}
}
cdo.writeByte(0);
int prevID = COMPLEX_DBINDEX_START;
for(ItemKey key : complexItems) {
if(key != null) {
int count = key.countItems(this);
if(count > 0) {
cdo.writeVariable(count);
cdo.writeVariable(key.dbID - prevID);
prevID = key.dbID;
}
}
}
cdo.writeByte(0);
return cdo.getCompressed();
} finally {
cdo.close();
}
}
public byte[] compressChangedItems(Collection<ItemKey> items) throws IOException {
CompressedDataOutput cdo = new CompressedDataOutput();
try {
cdo.writeVariable(generation);
cdo.writeVariable(items.size());
for(ItemKey key : items) {
cdo.writeVariable(key.dbID);
cdo.writeVariable(key.countItems(this));
}
return cdo.getCompressed();
} finally {
cdo.close();
}
}
public void readCompressedItemList(byte[] compressed) throws IOException {
CompressedDataInput cdi = new CompressedDataInput(compressed);
try {
int pktGeneration = cdi.readVariable();
int changed = cdi.readVariable();
if(changed > 0) {
if(pktGeneration != generation) {
return;
}
for(int i = 0; i < changed; i++) {
int dbID = cdi.readVariable();
int count = cdi.readVariable();
ItemKey key = getItem(dbID);
if(key != null) {
if(key.clientCount == 0 && count > 0) {
clientItems.add(key);
} else if(key.clientCount > 0 && count == 0) {
clientItems.remove(key);
}
key.clientCount = count;
}
}
} else {
clientItems.clear();
for(ItemKey key : simpleRegsitry.values()) {
key.clientCount = 0;
}
for(ItemKey key : complexItems) {
key.clientCount = 0;
}
generation = pktGeneration;
int count = cdi.readVariable();
while(count > 0) {
int dbID = cdi.readUnsignedShort();
ItemKey key = getSimpleItem(dbID);
key.clientCount = count;
clientItems.add(key);
count = cdi.readVariable();
}
count = cdi.readVariable();
int dbID = COMPLEX_DBINDEX_START;
while(count > 0) {
dbID += cdi.readVariable();
ItemKey key = getItem(dbID);
if(key != null) {
key.clientCount = count;
clientItems.add(key);
}
count = cdi.readVariable();
}
}
clientListNeedsSorting = true;
} finally {
cdi.close();
}
}
public boolean sortClientItems() {
boolean res = clientListNeedsSorting;
clientListNeedsSorting = false;
return res;
}
public int getClientItemCount() {
return clientItems.size();
}
public ItemKey getClientItem(int index) {
return clientItems.get(index);
}
public void setNetworkSources(List<NetworkedInventory> sources) {
simpleRegsitry.clear();
complexRegistry.clear();
complexItems.clear();
generation++;
currentInventory = 0;
if(sources == null || sources.isEmpty()) {
this.inventories = null;
} else {
this.inventories = new InventoryKey[sources.size()];
for(int i=0; i<sources.size(); i++) {
inventories[i] = new InventoryKey(sources.get(i));
}
}
for(ChangeLog log : changeLogs) {
log.databaseReset();
}
}
public void scanNextInventory() {
if(inventories == null) {
return;
}
InventoryKey inv = inventories[currentInventory];
inv.scanInventory(this, currentInventory);
currentInventory = (currentInventory+1) % inventories.length;
}
public void sendChangeLogs() {
for(ChangeLog log : changeLogs) {
log.sendChangeLog();
}
}
void itemChanged(ItemKey key) {
for(ChangeLog log : changeLogs) {
log.itemChanged(key);
}
}
public static final class ItemKey {
public final int dbID;
static final class ItemEntryKey {
public final int hash;
public final int itemID;
public final int meta;
public final NBTTagCompound nbt;
private HashSet<Integer> slots;
int clientCount;
ItemKey(int dbID, int hash, int itemID, int meta, NBTTagCompound nbt) {
this.dbID = dbID;
ItemEntryKey(int hash, int itemID, int meta, NBTTagCompound nbt) {
this.hash = hash;
this.itemID = itemID;
this.meta = meta;
@@ -397,146 +135,11 @@ public class InventoryDatabase {
@Override
public boolean equals(Object obj) {
if(obj instanceof ItemKey) {
final ItemKey other = (ItemKey) obj;
return equals(other.itemID, other.meta, other.nbt);
if(obj instanceof ItemEntryBase) {
final ItemEntryBase other = (ItemEntryBase) obj;
return other.equals(itemID, meta, nbt);
}
return false;
}
boolean equals(int itemID, int meta, NBTTagCompound nbt) {
return this.itemID == itemID && this.meta == meta &&
(this.nbt == nbt) || (this.nbt != null && this.nbt.equals(nbt));
}
@Override
public String toString() {
return "ItemKey{" + "dbID=" + dbID + ", hash=" + hash + ", itemID=" + itemID + ", meta=" + meta + ", nbt=" + nbt + '}';
}
public int getClientCount() {
return clientCount;
}
public ItemStack makeItemStack() {
ItemStack stack = new ItemStack(Item.getItemById(itemID), clientCount, meta);
stack.stackTagCompound = nbt;
return stack;
}
void write(CompressedDataOutput cdo) throws IOException {
if(nbt != null) {
cdo.writeInt((dbID << 1) | 1);
CompressedStreamTools.write(nbt, cdo);
if(dbID >= COMPLEX_DBINDEX_START) {
cdo.writeVariable(itemID);
cdo.writeVariable(meta);
}
} else {
cdo.writeVariable(dbID << 1);
}
}
static int encodeNISlot(int niIndex, int slot) {
return (niIndex << 12) | slot;
}
void addSlot(int niIndex, int slot) {
if(slots == null) {
slots = new HashSet<Integer>();
}
slots.add(encodeNISlot(niIndex, slot));
}
void removeSlot(int niIndex, int slot) {
if(slots != null) {
slots.remove(encodeNISlot(niIndex, slot));
}
}
int countItems(InventoryDatabase db) {
int count = 0;
if(slots != null) {
for(Integer idx : slots) {
InventoryKey key = db.inventories[idx >> 12];
count += key.itemCounts[idx & 4095];
}
}
return count;
}
}
static class InventoryKey {
private static final ItemKey[] NO_SLOTS = new ItemKey[0];
final NetworkedInventory ni;
ItemKey[] slotItems = NO_SLOTS;
int[] itemCounts;
InventoryKey(NetworkedInventory ni) {
this.ni = ni;
}
void scanInventory(InventoryDatabase db, int niIndex) {
ItemStack[] items = ni.getExtractableItemStacks();
if(items == null) {
if(slotItems.length != 0) {
resize(db, 0, niIndex);
}
return;
}
int count = items.length;
if(count != slotItems.length) {
resize(db, count, niIndex);
}
for(int slot=0; slot<count; slot++) {
ItemStack stack = items[slot];
ItemKey current = slotItems[slot];
if(stack == null || stack.stackSize <= 0) {
if(current != null) {
slotItems[slot] = null;
itemCounts[slot] = 0;
current.removeSlot(niIndex, slot);
db.itemChanged(current);
}
} else {
ItemKey key = db.lookupItem(stack, current);
if(key != current) {
slotItems[slot] = key;
itemCounts[slot] = stack.stackSize;
key.addSlot(niIndex, slot);
db.itemChanged(key);
if(current != null) {
current.removeSlot(niIndex, slot);
db.itemChanged(current);
}
} else if(itemCounts[slot] != stack.stackSize) {
itemCounts[slot] = stack.stackSize;
db.itemChanged(current);
}
}
}
}
private void resize(InventoryDatabase db, int count, int niIndex) {
for(int slot=0; slot<slotItems.length; slot++) {
ItemKey key = slotItems[slot];
if(key != null) {
key.removeSlot(niIndex, slot);
db.itemChanged(key);
}
}
slotItems = new ItemKey[count];
itemCounts = new int[count];
}
}
public interface ChangeLog {
void itemChanged(ItemKey key);
void databaseReset();
void sendChangeLog();
}
}
@@ -0,0 +1,160 @@
package crazypants.enderio.machine.invpanel;
import crazypants.enderio.network.CompressedDataInput;
import java.io.IOException;
import java.util.ArrayList;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
public class InventoryDatabaseClient extends InventoryDatabase<InventoryDatabaseClient.ItemEntry> {
private final ArrayList<ItemEntry> clientItems;
private boolean clientListNeedsSorting;
public InventoryDatabaseClient() {
clientItems = new ArrayList<ItemEntry>();
}
public void readCompressedItems(byte[] compressed) throws IOException {
CompressedDataInput cdi = new CompressedDataInput(compressed);
try {
int pktGeneration = cdi.readVariable();
if(pktGeneration != generation) {
return;
}
int count = cdi.readVariable();
for(int i=0 ; i<count ; i++) {
int code = cdi.readVariable();
int dbID = code >> 1;
NBTTagCompound nbt = null;
if((code & 1) == 1) {
nbt = CompressedStreamTools.read(cdi);
}
if(dbID >= COMPLEX_DBINDEX_START) {
int dbIndex = dbID - COMPLEX_DBINDEX_START;
int itemID = cdi.readVariable();
int meta = cdi.readVariable();
while(complexItems.size() <= dbIndex) {
complexItems.add(null);
}
ItemEntry entry = complexItems.get(dbIndex);
if(entry == null) {
entry = createItemEntry(dbID, itemID, meta, nbt);
complexItems.set(dbIndex, entry);
complexRegistry.put(entry, entry);
}
} else {
getSimpleItem(dbID);
}
}
} finally {
cdi.close();
}
}
public void readCompressedItemList(byte[] compressed) throws IOException {
CompressedDataInput cdi = new CompressedDataInput(compressed);
try {
int pktGeneration = cdi.readVariable();
int changed = cdi.readVariable();
if(changed > 0) {
if(pktGeneration != generation) {
return;
}
for(int i = 0; i < changed; i++) {
int dbID = cdi.readVariable();
int count = cdi.readVariable();
ItemEntry entry = getItem(dbID);
if(entry != null) {
if(entry.clientCount == 0 && count > 0) {
clientItems.add(entry);
} else if(entry.clientCount > 0 && count == 0) {
clientItems.remove(entry);
}
entry.clientCount = count;
}
}
} else {
for(ItemEntry entry : clientItems) {
entry.clientCount = 0;
}
clientItems.clear();
generation = pktGeneration;
int count = cdi.readVariable();
while(count > 0) {
int dbID = cdi.readUnsignedShort();
ItemEntry entry = getSimpleItem(dbID);
entry.clientCount = count;
clientItems.add(entry);
count = cdi.readVariable();
}
count = cdi.readVariable();
int dbID = COMPLEX_DBINDEX_START;
while(count > 0) {
dbID += cdi.readVariable();
ItemEntry entry = getItem(dbID);
if(entry != null) {
entry.clientCount = count;
clientItems.add(entry);
}
count = cdi.readVariable();
}
}
clientListNeedsSorting = true;
} finally {
cdi.close();
}
}
public boolean sortClientItems() {
boolean res = clientListNeedsSorting;
clientListNeedsSorting = false;
return res;
}
public int getNumEntries() {
return clientItems.size();
}
public ItemEntry getItemEntry(int index) {
return clientItems.get(index);
}
public ItemStack getItemStack(int index) {
return getItemEntry(index).makeItemStack();
}
@Override
protected ItemEntry createItemEntry(int dbId, int hash, int itemID, int meta, NBTTagCompound nbt) {
return new ItemEntry(dbId, hash, itemID, meta, nbt);
}
public static class ItemEntry extends ItemEntryBase {
int clientCount;
public ItemEntry(int dbID, int hash, int itemID, int meta, NBTTagCompound nbt) {
super(dbID, hash, itemID, meta, nbt);
}
public int getClientCount() {
return clientCount;
}
public ItemStack makeItemStack() {
ItemStack stack = new ItemStack(Item.getItemById(itemID), clientCount, meta);
stack.stackTagCompound = nbt;
return stack;
}
}
}
@@ -0,0 +1,312 @@
package crazypants.enderio.machine.invpanel;
import crazypants.enderio.conduit.item.NetworkedInventory;
import crazypants.enderio.network.CompressedDataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
public class InventoryDatabaseServer extends InventoryDatabase<InventoryDatabaseServer.ItemEntry> {
private InventoryKey[] inventories;
private int currentInventory;
private ChangeLog changeLog;
public InventoryDatabaseServer() {
}
public void addChangeLog(ChangeLog cl) {
if(changeLog == null) {
changeLog = cl;
} else if(changeLog instanceof ChangeLogList) {
((ChangeLogList)changeLog).add(cl);
} else if(changeLog != cl) {
changeLog = new ChangeLogList(changeLog, cl);
}
}
public void removeChangeLog(ChangeLog cl) {
if(changeLog == cl) {
changeLog = null;
} else if(changeLog instanceof ChangeLogList) {
changeLog = ((ChangeLogList)changeLog).remove(cl);
}
}
public byte[] compressItemInfo(List<ItemEntry> items) throws IOException{
CompressedDataOutput cdo = new CompressedDataOutput();
try {
int count = items.size();
cdo.writeVariable(generation);
cdo.writeVariable(count);
for(ItemEntry entry : items) {
entry.write(cdo);
}
return cdo.getCompressed();
} finally {
cdo.close();
}
}
public byte[] compressItemList() throws IOException {
CompressedDataOutput cdo = new CompressedDataOutput();
try {
cdo.writeVariable(generation);
cdo.writeByte(0);
for(Map.Entry<Integer, ItemEntry> entry : simpleRegsitry.entrySet()) {
int count = entry.getValue().countItems(this);
if(count > 0) {
cdo.writeVariable(count);
cdo.writeShort(entry.getKey());
}
}
cdo.writeByte(0);
int prevID = COMPLEX_DBINDEX_START;
for(ItemEntry entry : complexItems) {
if(entry != null) {
int count = entry.countItems(this);
if(count > 0) {
cdo.writeVariable(count);
cdo.writeVariable(entry.dbID - prevID);
prevID = entry.dbID;
}
}
}
cdo.writeByte(0);
return cdo.getCompressed();
} finally {
cdo.close();
}
}
public byte[] compressChangedItems(Collection<ItemEntry> items) throws IOException {
CompressedDataOutput cdo = new CompressedDataOutput();
try {
cdo.writeVariable(generation);
cdo.writeVariable(items.size());
for(ItemEntry entry : items) {
cdo.writeVariable(entry.dbID);
cdo.writeVariable(entry.countItems(this));
}
return cdo.getCompressed();
} finally {
cdo.close();
}
}
public void setNetworkSources(List<NetworkedInventory> sources) {
simpleRegsitry.clear();
complexRegistry.clear();
complexItems.clear();
generation++;
currentInventory = 0;
if(sources == null || sources.isEmpty()) {
this.inventories = null;
} else {
this.inventories = new InventoryKey[sources.size()];
for(int i=0; i<sources.size(); i++) {
inventories[i] = new InventoryKey(sources.get(i));
}
}
if(changeLog != null) {
changeLog.databaseReset();
}
}
public void scanNextInventory() {
if(inventories == null) {
return;
}
InventoryKey inv = inventories[currentInventory];
inv.scanInventory(this, currentInventory);
currentInventory = (currentInventory+1) % inventories.length;
}
void entryChanged(ItemEntry entry) {
if(changeLog != null) {
changeLog.entryChanged(entry);
}
}
public void sendChangeLogs() {
if(changeLog != null) {
changeLog.sendChangeLog();
}
}
@Override
protected ItemEntry createItemEntry(int dbId, int hash, int itemID, int meta, NBTTagCompound nbt) {
return new ItemEntry(dbId, hash, itemID, meta, nbt);
}
public static class ItemEntry extends ItemEntryBase {
private final HashSet<Integer> slots = new HashSet<Integer>();
public ItemEntry(int dbID, int hash, int itemID, int meta, NBTTagCompound nbt) {
super(dbID, hash, itemID, meta, nbt);
}
void write(CompressedDataOutput cdo) throws IOException {
if(nbt != null) {
cdo.writeInt((dbID << 1) | 1);
CompressedStreamTools.write(nbt, cdo);
if(dbID >= COMPLEX_DBINDEX_START) {
cdo.writeVariable(itemID);
cdo.writeVariable(meta);
}
} else {
cdo.writeVariable(dbID << 1);
}
}
static int encodeNISlot(int niIndex, int slot) {
return (niIndex << 12) | slot;
}
void addSlot(int niIndex, int slot) {
slots.add(encodeNISlot(niIndex, slot));
}
void removeSlot(int niIndex, int slot) {
slots.remove(encodeNISlot(niIndex, slot));
}
int countItems(InventoryDatabaseServer db) {
int count = 0;
for(Integer idx : slots) {
InventoryKey key = db.inventories[idx >> 12];
count += key.itemCounts[idx & 4095];
}
return count;
}
}
static class InventoryKey {
private static final ItemEntry[] NO_SLOTS = new ItemEntry[0];
final NetworkedInventory ni;
ItemEntry[] slotItems = NO_SLOTS;
int[] itemCounts;
InventoryKey(NetworkedInventory ni) {
this.ni = ni;
}
void scanInventory(InventoryDatabaseServer db, int niIndex) {
ItemStack[] items = ni.getExtractableItemStacks();
if(items == null) {
if(slotItems.length != 0) {
reset(db, 0, niIndex);
}
return;
}
int count = items.length;
if(count != slotItems.length) {
reset(db, count, niIndex);
}
for(int slot=0; slot<count; slot++) {
ItemStack stack = items[slot];
ItemEntry current = slotItems[slot];
if(stack == null || stack.stackSize <= 0) {
if(current != null) {
slotItems[slot] = null;
itemCounts[slot] = 0;
current.removeSlot(niIndex, slot);
db.entryChanged(current);
}
} else {
ItemEntry key = db.lookupItem(stack, current);
if(key != current) {
slotItems[slot] = key;
itemCounts[slot] = stack.stackSize;
key.addSlot(niIndex, slot);
db.entryChanged(key);
if(current != null) {
current.removeSlot(niIndex, slot);
db.entryChanged(current);
}
} else if(itemCounts[slot] != stack.stackSize) {
itemCounts[slot] = stack.stackSize;
db.entryChanged(current);
}
}
}
}
private void reset(InventoryDatabaseServer db, int count, int niIndex) {
for(int slot=0; slot<slotItems.length; slot++) {
ItemEntry key = slotItems[slot];
if(key != null) {
key.removeSlot(niIndex, slot);
db.entryChanged(key);
}
}
slotItems = new ItemEntry[count];
itemCounts = new int[count];
}
}
public interface ChangeLog {
void entryChanged(ItemEntry entry);
void databaseReset();
void sendChangeLog();
}
static final class ChangeLogList implements ChangeLog {
final ArrayList<ChangeLog> clList;
public ChangeLogList(ChangeLog cl0, ChangeLog cl1) {
clList = new ArrayList<InventoryDatabaseServer.ChangeLog>(2);
clList.add(cl0);
clList.add(cl1);
}
@Override
public void entryChanged(ItemEntry entry) {
for(ChangeLog cl : clList) {
cl.entryChanged(entry);
}
}
@Override
public void databaseReset() {
for(ChangeLog cl : clList) {
cl.databaseReset();
}
}
@Override
public void sendChangeLog() {
for(ChangeLog cl : clList) {
cl.sendChangeLog();
}
}
ChangeLog remove(ChangeLog cl) {
clList.remove(cl);
if(clList.size() == 1) {
return clList.get(0);
}
return this;
}
void add(ChangeLog cl) {
if(!clList.contains(cl)) {
clList.add(cl);
}
}
}
}
@@ -23,7 +23,7 @@ import net.minecraft.item.crafting.CraftingManager;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent;
public class InventoryPanelContainer extends AbstractMachineContainer implements InventoryDatabase.ChangeLog {
public class InventoryPanelContainer extends AbstractMachineContainer implements InventoryDatabaseServer.ChangeLog {
private static final int CRAFTING_GRID_X = 7;
private static final int CRAFTING_GRID_Y = 16;
@@ -31,7 +31,7 @@ public class InventoryPanelContainer extends AbstractMachineContainer implements
private static final int RETURN_INV_X = 7;
private static final int RETURN_INV_Y = 82;
private final HashSet<InventoryDatabase.ItemKey> changedItems;
private final HashSet<InventoryDatabaseServer.ItemEntry> changedItems;
@SuppressWarnings("LeakingThisInConstructor")
public InventoryPanelContainer(InventoryPlayer playerInv, TileInventoryPanel te) {
@@ -41,7 +41,7 @@ public class InventoryPanelContainer extends AbstractMachineContainer implements
if(te.getWorldObj().isRemote) {
changedItems = null;
} else {
changedItems = new HashSet<InventoryDatabase.ItemKey>();
changedItems = new HashSet<InventoryDatabaseServer.ItemEntry>();
}
}
@@ -107,9 +107,13 @@ public class InventoryPanelContainer extends AbstractMachineContainer implements
removeChangeLog();
}
private TileInventoryPanel getInventoryPanel() {
return (TileInventoryPanel) tileEntity;
}
private void removeChangeLog() {
if(changedItems != null) {
((TileInventoryPanel)tileEntity).getDatabase().removeChangeLog(this);
getInventoryPanel().getDatabaseServer().removeChangeLog(this);
}
}
@@ -126,11 +130,12 @@ public class InventoryPanelContainer extends AbstractMachineContainer implements
}
super.addCraftingToCrafters(crafting);
if(changedItems != null) {
((TileInventoryPanel)tileEntity).getDatabase().addChangeLog(this);
InventoryDatabaseServer db = getInventoryPanel().getDatabaseServer();
db.addChangeLog(this);
if(crafting instanceof EntityPlayerMP) {
try {
byte[] compressed = ((TileInventoryPanel)tileEntity).getDatabase().compressItemList();
PacketItemList pil = new PacketItemList((TileInventoryPanel)tileEntity, compressed);
byte[] compressed = db.compressItemList();
PacketItemList pil = new PacketItemList(getInventoryPanel(), compressed);
PacketHandler.sendTo(pil, (EntityPlayerMP)crafting);
} catch (IOException ex) {
Logger.getLogger(InventoryPanelContainer.class.getName()).log(Level.SEVERE,
@@ -157,8 +162,8 @@ public class InventoryPanelContainer extends AbstractMachineContainer implements
}
@Override
public void itemChanged(InventoryDatabase.ItemKey key) {
changedItems.add(key);
public void entryChanged(InventoryDatabaseServer.ItemEntry entry) {
changedItems.add(entry);
}
@Override
@@ -170,11 +175,12 @@ public class InventoryPanelContainer extends AbstractMachineContainer implements
public void sendChangeLog() {
if(!changedItems.isEmpty() && !crafters.isEmpty()) {
try {
byte[] compressed = ((TileInventoryPanel)tileEntity).getDatabase().compressChangedItems(changedItems);
PacketItemList pil = new PacketItemList((TileInventoryPanel)tileEntity, compressed);
InventoryDatabaseServer db = getInventoryPanel().getDatabaseServer();
byte[] compressed = db.compressChangedItems(changedItems);
PacketItemList pil = new PacketItemList(getInventoryPanel(), compressed);
for(Object crafting : crafters) {
if(crafting instanceof EntityPlayerMP) {
PacketHandler.sendTo(pil, (EntityPlayerMP)crafting);
PacketHandler.sendTo(pil, (EntityPlayerMP) crafting);
}
}
} catch (IOException ex) {
@@ -0,0 +1,44 @@
package crazypants.enderio.machine.invpanel;
import net.minecraft.nbt.NBTTagCompound;
public class ItemEntryBase {
public final int dbID;
public final int hash;
public final int itemID;
public final int meta;
public final NBTTagCompound nbt;
ItemEntryBase(int dbID, int hash, int itemID, int meta, NBTTagCompound nbt) {
this.dbID = dbID;
this.hash = hash;
this.itemID = itemID;
this.meta = meta;
this.nbt = nbt;
}
@Override
public int hashCode() {
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof ItemEntryBase) {
ItemEntryBase other = (ItemEntryBase) obj;
return this.dbID == other.dbID;
}
return false;
}
boolean equals(int itemID, int meta, NBTTagCompound nbt) {
return this.itemID == itemID && this.meta == meta &&
(this.nbt == nbt) || (this.nbt != null && this.nbt.equals(nbt));
}
@Override
public String toString() {
return "ItemEntryBase{" + "dbID=" + dbID + ", hash=" + hash + ", itemID=" + itemID + ", meta=" + meta + ", nbt=" + nbt + '}';
}
}
@@ -20,10 +20,10 @@ public class PacketItemInfo extends MessageTileEntity<TileInventoryPanel> implem
public PacketItemInfo() {
}
public PacketItemInfo(TileInventoryPanel tile, List<InventoryDatabase.ItemKey> items) {
public PacketItemInfo(TileInventoryPanel tile, List<InventoryDatabaseServer.ItemEntry> items) {
super(tile);
try {
compressed = tile.getDatabase().compressItemInfo(items);
compressed = tile.getDatabaseServer().compressItemInfo(items);
} catch (IOException ex) {
Logger.getLogger(PacketItemInfo.class.getName()).log(Level.SEVERE, "Exception while compressing items", ex);
compressed = new byte[0];
@@ -51,7 +51,7 @@ public class PacketItemInfo extends MessageTileEntity<TileInventoryPanel> implem
TileEntity te = player.worldObj.getTileEntity(message.x, message.y, message.z);
if(te instanceof TileInventoryPanel) {
TileInventoryPanel teInvPanel = (TileInventoryPanel) te;
InventoryDatabase db = teInvPanel.getDatabase();
InventoryDatabaseClient db = teInvPanel.getDatabaseClient();
try {
db.readCompressedItems(message.compressed);
} catch (IOException ex) {
@@ -45,7 +45,7 @@ public class PacketItemList extends MessageTileEntity<TileInventoryPanel> implem
TileEntity te = player.worldObj.getTileEntity(message.x, message.y, message.z);
if(te instanceof TileInventoryPanel) {
TileInventoryPanel teInvPanel = (TileInventoryPanel) te;
InventoryDatabase db = teInvPanel.getDatabase();
InventoryDatabaseClient db = teInvPanel.getDatabaseClient();
try {
db.readCompressedItemList(message.compressed);
} catch (IOException ex) {
@@ -17,7 +17,8 @@ import net.minecraftforge.common.util.ForgeDirection;
public class TileInventoryPanel extends AbstractMachineEntity {
private final InventoryDatabase database;
private InventoryDatabaseServer dbServer;
private InventoryDatabaseClient dbClient;
private ItemConduitNetwork network;
private int networkChangeCount;
@@ -26,11 +27,20 @@ public class TileInventoryPanel extends AbstractMachineEntity {
public TileInventoryPanel() {
super(new SlotDefinition(0, 8, 10, 19, 20, 19));
this.database = new InventoryDatabase();
}
public InventoryDatabase getDatabase() {
return database;
public InventoryDatabaseServer getDatabaseServer() {
if(dbServer == null) {
dbServer = new InventoryDatabaseServer();
}
return dbServer;
}
public InventoryDatabaseClient getDatabaseClient() {
if(dbClient == null) {
dbClient = new InventoryDatabaseClient();
}
return dbClient;
}
@Override
@@ -70,49 +80,58 @@ public class TileInventoryPanel extends AbstractMachineEntity {
if(worldObj.isRemote) return;
if(shouldDoWorkThisTick(2)) {
ForgeDirection facingDir = getFacingDir();
ForgeDirection backside = facingDir.getOpposite();
ItemConduit conduit = null;
ItemConduitNetwork icn = null;
TileEntity te = worldObj.getTileEntity(xCoord+backside.offsetX, yCoord+backside.offsetY, zCoord+backside.offsetZ);
if(te instanceof TileConduitBundle) {
TileConduitBundle teCB = (TileConduitBundle) te;
conduit = teCB.getConduit(ItemConduit.class);
if(conduit != null) {
icn = (ItemConduitNetwork) conduit.getNetwork();
}
}
if(icn == null) {
network = null;
database.setNetworkSources(null);
} else if(icn != network || icn.getChangeCount() != networkChangeCount) {
updateNetwork(icn, conduit, facingDir);
}
database.scanNextInventory();
scanNetwork();
}
if(shouldDoWorkThisTick(20)) {
database.sendChangeLogs();
if(dbServer != null && shouldDoWorkThisTick(20)) {
dbServer.sendChangeLogs();
}
}
private void scanNetwork() {
ForgeDirection facingDir = getFacingDir();
ForgeDirection backside = facingDir.getOpposite();
ItemConduit conduit = null;
ItemConduitNetwork icn = null;
TileEntity te = worldObj.getTileEntity(xCoord+backside.offsetX, yCoord+backside.offsetY, zCoord+backside.offsetZ);
if(te instanceof TileConduitBundle) {
TileConduitBundle teCB = (TileConduitBundle) te;
conduit = teCB.getConduit(ItemConduit.class);
if(conduit != null) {
icn = (ItemConduitNetwork) conduit.getNetwork();
}
}
if(icn != network || (icn != null && icn.getChangeCount() != networkChangeCount)) {
updateNetwork(icn, conduit, facingDir);
}
if(dbServer != null) {
dbServer.scanNextInventory();
}
}
private void updateNetwork(ItemConduitNetwork icn, ItemConduit conduit, ForgeDirection facingDir) {
this.network = icn;
this.networkChangeCount = icn.getChangeCount();
List<NetworkedInventory> sources = null;
ConnectionMode mode = conduit.getConnectionMode(facingDir);
System.out.println("updateNetwork: mode="+mode);
if(mode == ConnectionMode.OUTPUT || mode == ConnectionMode.IN_OUT) {
DyeColor color = conduit.getOutputColor(facingDir);
List<NetworkedInventory> sources = icn.getSourcesForColor(color);
System.out.println("Color="+color+" sources="+sources.size());
database.setNetworkSources(sources);
} else {
database.setNetworkSources(null);
if(icn != null) {
networkChangeCount = icn.getChangeCount();
ConnectionMode mode = conduit.getConnectionMode(facingDir);
System.out.println("updateNetwork: mode="+mode);
if(mode == ConnectionMode.OUTPUT || mode == ConnectionMode.IN_OUT) {
DyeColor color = conduit.getOutputColor(facingDir);
sources = icn.getSourcesForColor(color);
System.out.println("Color="+color+" sources="+sources.size());
}
}
this.network = icn;
if(dbServer != null || sources != null) {
getDatabaseServer().setNetworkSources(sources);
}
}