Applied-Energistics-2-tiler.../core/sync/packets/PacketMEInventoryUpdate.java

176 lines
4 KiB
Java
Raw Normal View History

package appeng.core.sync.packets;
2014-02-09 02:34:52 +01:00
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.io.IOException;
2014-02-16 08:33:17 +01:00
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.BufferOverflowException;
import java.util.LinkedList;
import java.util.List;
2014-02-16 08:33:17 +01:00
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
import appeng.api.storage.data.IAEItemStack;
2014-06-28 22:18:36 +02:00
import appeng.client.gui.implementations.GuiCraftConfirm;
2014-06-26 03:49:37 +02:00
import appeng.client.gui.implementations.GuiCraftingCPU;
import appeng.client.gui.implementations.GuiMEMonitorable;
2014-01-23 17:28:12 +01:00
import appeng.client.gui.implementations.GuiNetworkStatus;
2014-02-16 08:33:17 +01:00
import appeng.core.AELog;
import appeng.core.sync.AppEngPacket;
2014-02-09 02:34:52 +01:00
import appeng.core.sync.network.INetworkInfo;
import appeng.util.item.AEItemStack;
2014-02-09 02:34:52 +01:00
import cpw.mods.fml.common.network.internal.FMLProxyPacket;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class PacketMEInventoryUpdate extends AppEngPacket
{
// output...
2014-06-27 04:47:26 +02:00
final private byte ref;
2014-02-09 02:34:52 +01:00
final private ByteBuf data;
2014-02-16 08:33:17 +01:00
final private GZIPOutputStream compressFrame;
int writtenBytes = 0;
boolean empty = true;
// input.
final List<IAEItemStack> list;
// automatic.
2014-02-16 08:33:17 +01:00
public PacketMEInventoryUpdate(final ByteBuf stream) throws IOException {
data = null;
2014-02-16 08:33:17 +01:00
compressFrame = null;
list = new LinkedList();
2014-06-27 04:47:26 +02:00
ref = stream.readByte();
2014-02-16 08:33:17 +01:00
// int originalBytes = stream.readableBytes();
2014-02-16 08:33:17 +01:00
GZIPInputStream gzReader = new GZIPInputStream( new InputStream() {
@Override
public int read() throws IOException
{
if ( stream.readableBytes() <= 0 )
return -1;
return (int) stream.readByte() & 0xff;
}
} );
ByteBuf uncompesssed = Unpooled.buffer( stream.readableBytes() );
byte tmp[] = new byte[1024];
while (gzReader.available() != 0)
{
int bytes = gzReader.read( tmp );
if ( bytes > 0 )
uncompesssed.writeBytes( tmp, 0, bytes );
}
gzReader.close();
// int uncompesssedBytes = uncompesssed.readableBytes();
2014-02-16 08:33:17 +01:00
// AELog.info( "Recv: " + originalBytes + " -> " + uncompesssedBytes );
while (uncompesssed.readableBytes() > 0)
list.add( AEItemStack.loadItemStackFromPacket( uncompesssed ) );
empty = list.isEmpty();
}
@Override
@SideOnly(Side.CLIENT)
2014-02-09 02:34:52 +01:00
public void clientPacketData(INetworkInfo network, AppEngPacket packet, EntityPlayer player)
{
GuiScreen gs = Minecraft.getMinecraft().currentScreen;
2014-01-23 17:28:12 +01:00
2014-06-28 22:18:36 +02:00
if ( gs instanceof GuiCraftConfirm )
((GuiCraftConfirm) gs).postUpdate( list, ref );
2014-06-26 03:49:37 +02:00
if ( gs instanceof GuiCraftingCPU )
2014-06-27 04:47:26 +02:00
((GuiCraftingCPU) gs).postUpdate( list, ref );
2014-06-26 03:49:37 +02:00
if ( gs instanceof GuiMEMonitorable )
((GuiMEMonitorable) gs).postUpdate( list );
2014-01-23 17:28:12 +01:00
if ( gs instanceof GuiNetworkStatus )
((GuiNetworkStatus) gs).postUpdate( list );
}
@Override
2014-02-09 02:34:52 +01:00
public FMLProxyPacket getProxy()
{
2014-02-16 08:33:17 +01:00
try
{
compressFrame.close();
configureWrite( data );
return super.getProxy();
}
catch (IOException e)
{
AELog.error( e );
}
return null;
}
// api
public PacketMEInventoryUpdate() throws IOException {
2014-06-27 04:47:26 +02:00
this( (byte) 0 );
}
// api
public PacketMEInventoryUpdate(byte ref) throws IOException {
2014-02-16 08:33:17 +01:00
2014-02-09 02:34:52 +01:00
data = Unpooled.buffer( 2048 );
data.writeInt( getPacketID() );
2014-06-27 04:47:26 +02:00
data.writeByte( this.ref = ref );
2014-02-16 08:33:17 +01:00
compressFrame = new GZIPOutputStream( new OutputStream() {
@Override
public void write(int value) throws IOException
{
data.writeByte( value );
}
} );
list = null;
}
public void appendItem(IAEItemStack is) throws IOException, BufferOverflowException
{
2014-02-16 08:33:17 +01:00
ByteBuf tmp = Unpooled.buffer( 2048 );
is.writeToPacket( tmp );
compressFrame.flush();
2014-05-11 18:25:15 +02:00
if ( writtenBytes + tmp.readableBytes() > 2 * 1024 * 1024 ) // 2mb!
throw new BufferOverflowException();
else
2014-02-16 08:33:17 +01:00
{
writtenBytes += tmp.readableBytes();
compressFrame.write( tmp.array(), 0, tmp.readableBytes() );
empty = false;
}
}
public int getLength()
{
2014-02-09 02:34:52 +01:00
return data.readableBytes();
}
public boolean isEmpty()
{
return empty;
}
}