diff --git a/src/main/java/crazypants/enderio/conduit/item/NetworkedInventory.java b/src/main/java/crazypants/enderio/conduit/item/NetworkedInventory.java index b4792d30..4a02e368 100644 --- a/src/main/java/crazypants/enderio/conduit/item/NetworkedInventory.java +++ b/src/main/java/crazypants/enderio/conduit/item/NetworkedInventory.java @@ -84,7 +84,7 @@ public class NetworkedInventory { } boolean canInsert() { - if(!inventoryPanel) { + if(inventoryPanel) { return false; } ConnectionMode mode = con.getConnectionMode(conDir); diff --git a/src/main/java/crazypants/enderio/machine/invpanel/BlockInventoryPanel.java b/src/main/java/crazypants/enderio/machine/invpanel/BlockInventoryPanel.java index 18e45bf1..59ca23bf 100644 --- a/src/main/java/crazypants/enderio/machine/invpanel/BlockInventoryPanel.java +++ b/src/main/java/crazypants/enderio/machine/invpanel/BlockInventoryPanel.java @@ -17,6 +17,7 @@ public class BlockInventoryPanel extends AbstractMachineBlock { 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 { 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 { 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 diff --git a/src/main/java/crazypants/enderio/machine/invpanel/InventoryDatabase.java b/src/main/java/crazypants/enderio/machine/invpanel/InventoryDatabase.java index fb9d9a6a..25892fa8 100644 --- a/src/main/java/crazypants/enderio/machine/invpanel/InventoryDatabase.java +++ b/src/main/java/crazypants/enderio/machine/invpanel/InventoryDatabase.java @@ -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 simpleRegsitry; private final HashMap complexRegistry; private final ArrayList complexItems; + private final ArrayList clientItems; private int generation; private InventoryKey[] inventories; private int currentInventory; + private ChangeLog[] changeLogs; + + private boolean clientListNeedsSorting; public InventoryDatabase() { simpleRegsitry = new HashMap(); complexRegistry = new HashMap(); complexItems = new ArrayList(); + clientItems = new ArrayList(); + 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= 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> 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 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 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 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 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 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 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(); + } } @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(); + } + } diff --git a/src/main/java/crazypants/enderio/machine/invpanel/PacketItemList.java b/src/main/java/crazypants/enderio/machine/invpanel/PacketItemList.java new file mode 100644 index 00000000..766483fb --- /dev/null +++ b/src/main/java/crazypants/enderio/machine/invpanel/PacketItemList.java @@ -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 implements IMessageHandler { + + 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; + } + +} diff --git a/src/main/java/crazypants/enderio/machine/invpanel/TileInventoryPanel.java b/src/main/java/crazypants/enderio/machine/invpanel/TileInventoryPanel.java index d520411f..7e2d3ca2 100644 --- a/src/main/java/crazypants/enderio/machine/invpanel/TileInventoryPanel.java +++ b/src/main/java/crazypants/enderio/machine/invpanel/TileInventoryPanel.java @@ -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) { diff --git a/src/main/java/crazypants/enderio/network/CompressedDataInput.java b/src/main/java/crazypants/enderio/network/CompressedDataInput.java new file mode 100644 index 00000000..2162605b --- /dev/null +++ b/src/main/java/crazypants/enderio/network/CompressedDataInput.java @@ -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; + } +} diff --git a/src/main/java/crazypants/enderio/network/CompressedDataOutput.java b/src/main/java/crazypants/enderio/network/CompressedDataOutput.java new file mode 100644 index 00000000..c77c266f --- /dev/null +++ b/src/main/java/crazypants/enderio/network/CompressedDataOutput.java @@ -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(); + } +} diff --git a/src/main/java/crazypants/enderio/network/PacketHandler.java b/src/main/java/crazypants/enderio/network/PacketHandler.java index 4b0dd8f3..5343928b 100644 --- a/src/main/java/crazypants/enderio/network/PacketHandler.java +++ b/src/main/java/crazypants/enderio/network/PacketHandler.java @@ -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); + } }