mirror of
https://github.com/GTNewHorizons/EnderIO.git
synced 2026-11-25 22:44:34 +00:00
first part of network support for item lists
This commit is contained in:
1 parent
b372d93351
commit
80254723cd
10 files changed
+535
-122
No files matched your search
@@ -84,7 +84,7 @@ public class NetworkedInventory {
|
||||
}
|
||||
|
||||
boolean canInsert() {
|
||||
if(!inventoryPanel) {
|
||||
if(inventoryPanel) {
|
||||
return false;
|
||||
}
|
||||
ConnectionMode mode = con.getConnectionMode(conDir);
|
||||
|
||||
@@ -17,6 +17,7 @@ public class BlockInventoryPanel extends AbstractMachineBlock<TileInventoryPanel
|
||||
|
||||
public static BlockInventoryPanel create() {
|
||||
PacketHandler.INSTANCE.registerMessage(PacketItemInfo.class, PacketItemInfo.class, PacketHandler.nextID(), Side.CLIENT);
|
||||
PacketHandler.INSTANCE.registerMessage(PacketItemList.class, PacketItemList.class, PacketHandler.nextID(), Side.CLIENT);
|
||||
|
||||
BlockInventoryPanel panel = new BlockInventoryPanel();
|
||||
panel.init();
|
||||
|
||||
@@ -4,11 +4,14 @@ import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import crazypants.enderio.gui.IconEIO;
|
||||
import crazypants.enderio.machine.gui.GuiMachineBase;
|
||||
import crazypants.gui.GhostSlot;
|
||||
import crazypants.render.RenderUtil;
|
||||
import java.awt.Rectangle;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
@@ -18,10 +21,13 @@ public class GuiInventoryPanel extends GuiMachineBase<TileInventoryPanel> {
|
||||
private static final Rectangle btnScrollDown = new Rectangle(216, 109, 16, 8);
|
||||
private static final Rectangle thumbArea = new Rectangle(216, 35, 16, 74);
|
||||
|
||||
private static final int GHOST_COLUMNS = 6;
|
||||
private static final int GHOST_ROWS = 5;
|
||||
|
||||
private boolean scrollUpPressed;
|
||||
private boolean scrollDownPressed;
|
||||
private int scrollPos;
|
||||
private int scrollMax = 100;
|
||||
private int scrollMax;
|
||||
private long scrollLastTime;
|
||||
|
||||
public GuiInventoryPanel(TileInventoryPanel te, Container container) {
|
||||
@@ -29,6 +35,15 @@ public class GuiInventoryPanel extends GuiMachineBase<TileInventoryPanel> {
|
||||
redstoneButton.visible = false;
|
||||
configB.visible = false;
|
||||
scrollLastTime = Minecraft.getSystemTime();
|
||||
|
||||
for(int y = 0; y < GHOST_ROWS; y++) {
|
||||
for(int x = 0; x < GHOST_COLUMNS; x++) {
|
||||
GhostSlot slot = new GhostSlot();
|
||||
slot.x = 109 + x*18;
|
||||
slot.y = 28 + y*18;
|
||||
ghostSlots.add(slot);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -57,21 +72,66 @@ public class GuiInventoryPanel extends GuiMachineBase<TileInventoryPanel> {
|
||||
icon = scrollDownPressed ? IconEIO.DOWN_ARROW_ON : IconEIO.DOWN_ARROW_OFF;
|
||||
icon.renderIcon(sx+btnScrollDown.x, sy+btnScrollDown.y);
|
||||
|
||||
icon = IconEIO.VSCROLL_THUMB;
|
||||
icon.renderIcon(sx+thumbArea.x, sy+thumbArea.y + (thumbArea.height - icon.height) * scrollPos / scrollMax);
|
||||
if(scrollMax > 0) {
|
||||
icon = IconEIO.VSCROLL_THUMB;
|
||||
icon.renderIcon(sx+thumbArea.x, sy+thumbArea.y + (thumbArea.height - icon.height) * scrollPos / scrollMax);
|
||||
}
|
||||
|
||||
tes.draw();
|
||||
|
||||
if(getDatabase().sortClientItems()) {
|
||||
updateGhostSlots();
|
||||
}
|
||||
|
||||
super.drawGuiContainerBackgroundLayer(par1, par2, par3);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void drawFakeItemStack(int x, int y, ItemStack stack) {
|
||||
FontRenderer font = stack.getItem().getFontRenderer(stack);
|
||||
if(font == null) {
|
||||
font = fontRendererObj;
|
||||
}
|
||||
String str = null;
|
||||
if(stack.stackSize > 999) {
|
||||
str = (stack.stackSize / 1000) + "k";
|
||||
}
|
||||
itemRender.renderItemAndEffectIntoGUI(font, mc.renderEngine, stack, x, y);
|
||||
itemRender.renderItemOverlayIntoGUI(font, mc.renderEngine, stack, x, y, str);
|
||||
}
|
||||
|
||||
private InventoryDatabase getDatabase() {
|
||||
return getTileEntity().getDatabase();
|
||||
}
|
||||
|
||||
private void doScroll() {
|
||||
scrollMax = Math.max(0, (getDatabase().getClientItemCount()+GHOST_COLUMNS-1) / GHOST_COLUMNS - GHOST_ROWS);
|
||||
if(scrollUpPressed && scrollPos > 0) {
|
||||
scrollPos--;
|
||||
}
|
||||
if(scrollDownPressed && scrollPos < scrollMax) {
|
||||
if(scrollDownPressed) {
|
||||
scrollPos++;
|
||||
}
|
||||
updateGhostSlots();
|
||||
}
|
||||
|
||||
private void updateGhostSlots() {
|
||||
if(scrollPos > scrollMax) {
|
||||
scrollPos = scrollMax;
|
||||
}
|
||||
|
||||
InventoryDatabase database = getDatabase();
|
||||
int index = scrollPos * GHOST_COLUMNS;
|
||||
int count = database.getClientItemCount();
|
||||
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();
|
||||
} else {
|
||||
slot.stack = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,21 +1,16 @@
|
||||
package crazypants.enderio.machine.invpanel;
|
||||
|
||||
import crazypants.enderio.conduit.item.NetworkedInventory;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutput;
|
||||
import java.io.DataOutputStream;
|
||||
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 java.util.zip.DeflaterOutputStream;
|
||||
import java.util.zip.InflaterInputStream;
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
@@ -31,20 +26,49 @@ public class InventoryDatabase {
|
||||
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);
|
||||
private static final int HAS_NBT_FLAG = 1 << 31;
|
||||
|
||||
|
||||
private final HashMap<Integer, ItemKey> simpleRegsitry;
|
||||
private final HashMap<ItemKey, ItemKey> complexRegistry;
|
||||
private final ArrayList<ItemKey> complexItems;
|
||||
private final ArrayList<ItemKey> clientItems;
|
||||
|
||||
private int generation;
|
||||
private InventoryKey[] inventories;
|
||||
private int currentInventory;
|
||||
private ChangeLog[] changeLogs;
|
||||
|
||||
private boolean clientListNeedsSorting;
|
||||
|
||||
public InventoryDatabase() {
|
||||
simpleRegsitry = new HashMap<Integer, ItemKey>();
|
||||
complexRegistry = new HashMap<ItemKey, ItemKey>();
|
||||
complexItems = new ArrayList<ItemKey>();
|
||||
clientItems = new ArrayList<ItemKey>();
|
||||
changeLogs = new ChangeLog[0];
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -126,97 +150,182 @@ public class InventoryDatabase {
|
||||
}
|
||||
|
||||
public void readCompressedItems(byte[] compressed) throws IOException {
|
||||
InflaterInputStream inf = new InflaterInputStream(new ByteArrayInputStream(compressed));
|
||||
try {
|
||||
BufferedInputStream bis = new BufferedInputStream(inf);
|
||||
DataInputStream dis = new DataInputStream(inf);
|
||||
int srcGeneration = dis.readInt();
|
||||
if(srcGeneration != generation) {
|
||||
return;
|
||||
}
|
||||
int count = dis.readUnsignedShort();
|
||||
for(int i=0 ; i<count ; i++) {
|
||||
int dbID = dis.readInt();
|
||||
NBTTagCompound nbt = null;
|
||||
if((dbID & HAS_NBT_FLAG) == HAS_NBT_FLAG) {
|
||||
dbID &= ~HAS_NBT_FLAG;
|
||||
nbt = CompressedStreamTools.read(dis);
|
||||
}
|
||||
|
||||
if(dbID >= COMPLEX_DBINDEX_START) {
|
||||
int dbIndex = dbID - COMPLEX_DBINDEX_START;
|
||||
int itemID = dis.readUnsignedShort();
|
||||
int meta = dis.readUnsignedShort();
|
||||
|
||||
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 {
|
||||
inf.close();
|
||||
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{
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
DeflaterOutputStream def = new DeflaterOutputStream(baos);
|
||||
CompressedDataOutput cdo = new CompressedDataOutput();
|
||||
try {
|
||||
BufferedOutputStream bos = new BufferedOutputStream(def);
|
||||
DataOutputStream dos = new DataOutputStream(bos);
|
||||
int count = items.size();
|
||||
dos.writeInt(generation);
|
||||
dos.writeShort(count);
|
||||
cdo.writeVariable(generation);
|
||||
cdo.writeVariable(count);
|
||||
for(int i=0 ; i<count ; i++) {
|
||||
ItemKey item = items.get(i);
|
||||
item.write(dos);
|
||||
item.write(cdo);
|
||||
}
|
||||
dos.flush();
|
||||
return cdo.getCompressed();
|
||||
} finally {
|
||||
def.close();
|
||||
cdo.close();
|
||||
}
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
public byte[] compressItemList() throws IOException {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
DeflaterOutputStream def = new DeflaterOutputStream(baos);
|
||||
CompressedDataOutput cdo = new CompressedDataOutput();
|
||||
try {
|
||||
BufferedOutputStream bos = new BufferedOutputStream(def);
|
||||
DataOutputStream dos = new DataOutputStream(bos);
|
||||
cdo.writeVariable(generation);
|
||||
cdo.writeByte(0);
|
||||
for(Map.Entry<Integer, ItemKey> entry : simpleRegsitry.entrySet()) {
|
||||
int count = entry.getValue().countItems(this);
|
||||
if(count > 0) {
|
||||
dos.writeInt(count);
|
||||
dos.writeShort(entry.getKey());
|
||||
cdo.writeVariable(count);
|
||||
cdo.writeShort(entry.getKey());
|
||||
}
|
||||
}
|
||||
dos.writeInt(0);
|
||||
cdo.writeByte(0);
|
||||
int prevID = COMPLEX_DBINDEX_START;
|
||||
for(ItemKey key : complexItems) {
|
||||
if(key != null) {
|
||||
int count = key.countItems(this);
|
||||
if(count > 0) {
|
||||
dos.writeInt(count);
|
||||
dos.writeInt(key.dbID);
|
||||
cdo.writeVariable(count);
|
||||
cdo.writeVariable(key.dbID - prevID);
|
||||
prevID = key.dbID;
|
||||
}
|
||||
}
|
||||
}
|
||||
dos.writeInt(0);
|
||||
dos.flush();
|
||||
cdo.writeByte(0);
|
||||
return cdo.getCompressed();
|
||||
} finally {
|
||||
def.close();
|
||||
cdo.close();
|
||||
}
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -234,6 +343,10 @@ public class InventoryDatabase {
|
||||
inventories[i] = new InventoryKey(sources.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
for(ChangeLog log : changeLogs) {
|
||||
log.databaseReset();
|
||||
}
|
||||
}
|
||||
|
||||
public void scanNextInventory() {
|
||||
@@ -242,13 +355,23 @@ public class InventoryDatabase {
|
||||
}
|
||||
|
||||
InventoryKey inv = inventories[currentInventory];
|
||||
if(inv.scanInventory(this, currentInventory)) {
|
||||
System.out.println("Changed!");
|
||||
}
|
||||
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;
|
||||
public final int hash;
|
||||
@@ -257,6 +380,8 @@ public class InventoryDatabase {
|
||||
public final NBTTagCompound nbt;
|
||||
private HashSet<Integer> slots;
|
||||
|
||||
int clientCount;
|
||||
|
||||
ItemKey(int dbID, int hash, int itemID, int meta, NBTTagCompound nbt) {
|
||||
this.dbID = dbID;
|
||||
this.hash = hash;
|
||||
@@ -289,16 +414,26 @@ public class InventoryDatabase {
|
||||
return "ItemKey{" + "dbID=" + dbID + ", hash=" + hash + ", itemID=" + itemID + ", meta=" + meta + ", nbt=" + nbt + '}';
|
||||
}
|
||||
|
||||
void write(DataOutput dos) throws IOException {
|
||||
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) {
|
||||
dos.writeInt(dbID | HAS_NBT_FLAG);
|
||||
CompressedStreamTools.write(nbt, dos);
|
||||
cdo.writeInt((dbID << 1) | 1);
|
||||
CompressedStreamTools.write(nbt, cdo);
|
||||
if(dbID >= COMPLEX_DBINDEX_START) {
|
||||
dos.writeShort(itemID);
|
||||
dos.writeShort(meta);
|
||||
cdo.writeVariable(itemID);
|
||||
cdo.writeVariable(meta);
|
||||
}
|
||||
} else {
|
||||
dos.writeInt(dbID);
|
||||
cdo.writeVariable(dbID << 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -342,56 +477,55 @@ public class InventoryDatabase {
|
||||
this.ni = ni;
|
||||
}
|
||||
|
||||
boolean scanInventory(InventoryDatabase db, int niIndex) {
|
||||
void scanInventory(InventoryDatabase db, int niIndex) {
|
||||
ItemStack[] items = ni.getExtractableItemStacks();
|
||||
if(items == null) {
|
||||
if(slotItems.length != 0) {
|
||||
resize(0, niIndex);
|
||||
resize(db, 0, niIndex);
|
||||
}
|
||||
return true;
|
||||
return;
|
||||
}
|
||||
|
||||
boolean changed = false;
|
||||
int count = items.length;
|
||||
if(count != slotItems.length) {
|
||||
resize(count, niIndex);
|
||||
changed = true;
|
||||
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(slotItems[slot] != null) {
|
||||
slotItems[slot].removeSlot(niIndex, slot);
|
||||
if(current != null) {
|
||||
slotItems[slot] = null;
|
||||
itemCounts[slot] = 0;
|
||||
changed = true;
|
||||
current.removeSlot(niIndex, slot);
|
||||
db.itemChanged(current);
|
||||
}
|
||||
} else {
|
||||
ItemKey current = slotItems[slot];
|
||||
ItemKey key = db.lookupItem(stack, current);
|
||||
if(key != current) {
|
||||
if(current != null) {
|
||||
current.removeSlot(niIndex, slot);
|
||||
}
|
||||
slotItems[slot] = key;
|
||||
itemCounts[slot] = stack.stackSize;
|
||||
key.addSlot(niIndex, slot);
|
||||
changed = true;
|
||||
db.itemChanged(key);
|
||||
if(current != null) {
|
||||
current.removeSlot(niIndex, slot);
|
||||
db.itemChanged(current);
|
||||
}
|
||||
} else if(itemCounts[slot] != stack.stackSize) {
|
||||
itemCounts[slot] = stack.stackSize;
|
||||
changed = true;
|
||||
db.itemChanged(current);
|
||||
}
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
private void resize(int count, int niIndex) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -399,4 +533,10 @@ public class InventoryDatabase {
|
||||
itemCounts = new int[count];
|
||||
}
|
||||
}
|
||||
|
||||
public interface ChangeLog {
|
||||
void itemChanged(ItemKey key);
|
||||
void databaseReset();
|
||||
void sendChangeLog();
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,18 @@ package crazypants.enderio.machine.invpanel;
|
||||
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import crazypants.enderio.machine.gui.AbstractMachineContainer;
|
||||
import crazypants.enderio.network.PacketHandler;
|
||||
import crazypants.util.ItemUtil;
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.entity.player.InventoryPlayer;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.inventory.ICrafting;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.inventory.InventoryCrafting;
|
||||
import net.minecraft.inventory.Slot;
|
||||
@@ -16,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 {
|
||||
public class InventoryPanelContainer extends AbstractMachineContainer implements InventoryDatabase.ChangeLog {
|
||||
|
||||
private static final int CRAFTING_GRID_X = 7;
|
||||
private static final int CRAFTING_GRID_Y = 16;
|
||||
@@ -24,10 +31,18 @@ public class InventoryPanelContainer extends AbstractMachineContainer {
|
||||
private static final int RETURN_INV_X = 7;
|
||||
private static final int RETURN_INV_Y = 82;
|
||||
|
||||
private final HashSet<InventoryDatabase.ItemKey> changedItems;
|
||||
|
||||
@SuppressWarnings("LeakingThisInConstructor")
|
||||
public InventoryPanelContainer(InventoryPlayer playerInv, TileInventoryPanel te) {
|
||||
super(playerInv, te);
|
||||
te.eventHandler = this;
|
||||
|
||||
if(te.getWorldObj().isRemote) {
|
||||
changedItems = null;
|
||||
} else {
|
||||
changedItems = new HashSet<InventoryDatabase.ItemKey>();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -89,6 +104,40 @@ public class InventoryPanelContainer extends AbstractMachineContainer {
|
||||
if(!tileEntity.getWorldObj().isRemote) {
|
||||
((TileInventoryPanel)tileEntity).eventHandler = null;
|
||||
}
|
||||
removeChangeLog();
|
||||
}
|
||||
|
||||
private void removeChangeLog() {
|
||||
if(changedItems != null) {
|
||||
((TileInventoryPanel)tileEntity).getDatabase().removeChangeLog(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeCraftingFromCrafters(ICrafting crafting) {
|
||||
super.removeCraftingFromCrafters(crafting);
|
||||
removeChangeLog();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCraftingToCrafters(ICrafting crafting) {
|
||||
if(changedItems != null) {
|
||||
sendChangeLog();
|
||||
}
|
||||
super.addCraftingToCrafters(crafting);
|
||||
if(changedItems != null) {
|
||||
((TileInventoryPanel)tileEntity).getDatabase().addChangeLog(this);
|
||||
if(crafting instanceof EntityPlayerMP) {
|
||||
try {
|
||||
byte[] compressed = ((TileInventoryPanel)tileEntity).getDatabase().compressItemList();
|
||||
PacketItemList pil = new PacketItemList((TileInventoryPanel)tileEntity, compressed);
|
||||
PacketHandler.sendTo(pil, (EntityPlayerMP)crafting);
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(InventoryPanelContainer.class.getName()).log(Level.SEVERE,
|
||||
"Exception while compressing item list", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -107,4 +156,33 @@ public class InventoryPanelContainer extends AbstractMachineContainer {
|
||||
tileEntity.setInventorySlotContents(9, CraftingManager.getInstance().findMatchingRecipe(tmp, tileEntity.getWorldObj()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void itemChanged(InventoryDatabase.ItemKey key) {
|
||||
changedItems.add(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void databaseReset() {
|
||||
changedItems.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendChangeLog() {
|
||||
if(!changedItems.isEmpty() && !crafters.isEmpty()) {
|
||||
try {
|
||||
byte[] compressed = ((TileInventoryPanel)tileEntity).getDatabase().compressChangedItems(changedItems);
|
||||
PacketItemList pil = new PacketItemList((TileInventoryPanel)tileEntity, compressed);
|
||||
for(Object crafting : crafters) {
|
||||
if(crafting instanceof EntityPlayerMP) {
|
||||
PacketHandler.sendTo(pil, (EntityPlayerMP)crafting);
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(InventoryPanelContainer.class.getName()).log(Level.SEVERE,
|
||||
"Exception while compressing changed items", ex);
|
||||
}
|
||||
}
|
||||
changedItems.clear();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package crazypants.enderio.machine.invpanel;
|
||||
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessage;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
|
||||
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
|
||||
import crazypants.enderio.EnderIO;
|
||||
import crazypants.enderio.network.MessageTileEntity;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
|
||||
public class PacketItemList extends MessageTileEntity<TileInventoryPanel> implements IMessageHandler<PacketItemList, IMessage> {
|
||||
|
||||
private byte[] compressed;
|
||||
|
||||
public PacketItemList() {
|
||||
}
|
||||
|
||||
public PacketItemList(TileInventoryPanel tile, byte[] compressed) {
|
||||
super(tile);
|
||||
this.compressed = compressed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf) {
|
||||
super.fromBytes(buf);
|
||||
int size = buf.readMedium();
|
||||
compressed = new byte[size];
|
||||
buf.readBytes(compressed);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf) {
|
||||
super.toBytes(buf);
|
||||
buf.writeMedium(compressed.length);
|
||||
buf.writeBytes(compressed);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IMessage onMessage(PacketItemList message, MessageContext ctx) {
|
||||
EntityPlayer player = EnderIO.proxy.getClientPlayer();
|
||||
TileEntity te = player.worldObj.getTileEntity(message.x, message.y, message.z);
|
||||
if(te instanceof TileInventoryPanel) {
|
||||
TileInventoryPanel teInvPanel = (TileInventoryPanel) te;
|
||||
InventoryDatabase db = teInvPanel.getDatabase();
|
||||
try {
|
||||
db.readCompressedItemList(message.compressed);
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(PacketItemInfo.class.getName()).log(Level.SEVERE,
|
||||
"Exception while reading item list", ex);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -69,29 +69,35 @@ public class TileInventoryPanel extends AbstractMachineEntity {
|
||||
public void doUpdate() {
|
||||
if(worldObj.isRemote) return;
|
||||
|
||||
ForgeDirection facingDir = getFacingDir();
|
||||
ForgeDirection backside = facingDir.getOpposite();
|
||||
if(shouldDoWorkThisTick(2)) {
|
||||
ForgeDirection facingDir = getFacingDir();
|
||||
ForgeDirection backside = facingDir.getOpposite();
|
||||
|
||||
ItemConduit conduit = null;
|
||||
ItemConduitNetwork icn = null;
|
||||
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();
|
||||
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();
|
||||
}
|
||||
|
||||
if(icn == null) {
|
||||
network = null;
|
||||
database.setNetworkSources(null);
|
||||
} else if(icn != network || icn.getChangeCount() != networkChangeCount) {
|
||||
updateNetwork(icn, conduit, facingDir);
|
||||
if(shouldDoWorkThisTick(20)) {
|
||||
database.sendChangeLogs();
|
||||
}
|
||||
|
||||
database.scanNextInventory();
|
||||
}
|
||||
|
||||
private void updateNetwork(ItemConduitNetwork icn, ItemConduit conduit, ForgeDirection facingDir) {
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package crazypants.enderio.network;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.zip.InflaterInputStream;
|
||||
|
||||
public class CompressedDataInput extends DataInputStream {
|
||||
|
||||
public CompressedDataInput(byte[] compressed) {
|
||||
this(new ByteArrayInputStream(compressed));
|
||||
}
|
||||
|
||||
public CompressedDataInput(InputStream in) {
|
||||
super(new BufferedInputStream(new InflaterInputStream(in)));
|
||||
}
|
||||
|
||||
public int readVariable() throws IOException {
|
||||
int b, res = 0;
|
||||
int shift = 0;
|
||||
do {
|
||||
b = readUnsignedByte();
|
||||
res |= (b & 0x7F) << shift;
|
||||
shift += 7;
|
||||
} while(b >= 0x80);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package crazypants.enderio.network;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.zip.DeflaterOutputStream;
|
||||
|
||||
public class CompressedDataOutput extends DataOutputStream {
|
||||
|
||||
final ByteArrayOutputStream baos;
|
||||
|
||||
public CompressedDataOutput() {
|
||||
this(new ByteArrayOutputStream());
|
||||
}
|
||||
|
||||
private CompressedDataOutput(ByteArrayOutputStream baos) {
|
||||
super(new BufferedOutputStream(new DeflaterOutputStream(baos)));
|
||||
this.baos = baos;
|
||||
}
|
||||
|
||||
public void writeVariable(int value) throws IOException {
|
||||
while((value & ~0x7F) != 0) {
|
||||
writeByte(value | 0x80);
|
||||
value >>= 7;
|
||||
}
|
||||
writeByte(value);
|
||||
}
|
||||
|
||||
public byte[] getCompressed() throws IOException {
|
||||
close();
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import cpw.mods.fml.common.network.NetworkRegistry;
|
||||
import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessage;
|
||||
import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
|
||||
public class PacketHandler
|
||||
{
|
||||
@@ -25,4 +26,8 @@ public class PacketHandler
|
||||
public static void sendToAllAround(IMessage message, TileEntity te) {
|
||||
sendToAllAround(message, te, 64);
|
||||
}
|
||||
|
||||
public static void sendTo(IMessage message, EntityPlayerMP player) {
|
||||
INSTANCE.sendTo(message, player);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user