2013-12-27 23:59:59 +01:00
|
|
|
package appeng.core.sync.packets;
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.io.IOException;
|
2014-02-16 08:33:17 +01:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2014-02-06 17:57:01 +01:00
|
|
|
import java.nio.BufferOverflowException;
|
2013-12-27 23:59:59 +01:00
|
|
|
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;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
import net.minecraft.client.Minecraft;
|
|
|
|
import net.minecraft.client.gui.GuiScreen;
|
2014-01-05 09:39:48 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2013-12-27 23:59:59 +01:00
|
|
|
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;
|
2013-12-27 23:59:59 +01:00
|
|
|
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;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.core.sync.AppEngPacket;
|
2014-02-09 02:34:52 +01:00
|
|
|
import appeng.core.sync.network.INetworkInfo;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.util.item.AEItemStack;
|
2014-02-09 02:34:52 +01:00
|
|
|
import cpw.mods.fml.common.network.internal.FMLProxyPacket;
|
2013-12-27 23:59:59 +01:00
|
|
|
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;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
boolean empty = true;
|
|
|
|
|
|
|
|
// input.
|
|
|
|
final List<IAEItemStack> list;
|
|
|
|
|
|
|
|
// automatic.
|
2014-02-16 08:33:17 +01:00
|
|
|
public PacketMEInventoryUpdate(final ByteBuf stream) throws IOException {
|
2013-12-27 23:59:59 +01:00
|
|
|
data = null;
|
2014-02-16 08:33:17 +01:00
|
|
|
compressFrame = null;
|
2013-12-27 23:59:59 +01:00
|
|
|
list = new LinkedList();
|
2014-06-27 04:47:26 +02:00
|
|
|
ref = stream.readByte();
|
2014-02-16 08:33:17 +01:00
|
|
|
|
2014-02-16 22:54:43 +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();
|
|
|
|
|
2014-02-16 22:54:43 +01:00
|
|
|
// 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 ) );
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
empty = list.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
2014-02-09 02:34:52 +01:00
|
|
|
public void clientPacketData(INetworkInfo network, AppEngPacket packet, EntityPlayer player)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2013-12-27 23:59:59 +01: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 );
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public FMLProxyPacket getProxy()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-02-16 08:33:17 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
compressFrame.close();
|
|
|
|
|
|
|
|
configureWrite( data );
|
|
|
|
return super.getProxy();
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
AELog.error( e );
|
|
|
|
}
|
|
|
|
return null;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 );
|
2013-12-27 23:59:59 +01:00
|
|
|
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;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-02-06 17:57:01 +01:00
|
|
|
public void appendItem(IAEItemStack is) throws IOException, BufferOverflowException
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
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!
|
2014-02-06 17:57:01 +01:00
|
|
|
throw new BufferOverflowException();
|
|
|
|
else
|
2014-02-16 08:33:17 +01:00
|
|
|
{
|
|
|
|
writtenBytes += tmp.readableBytes();
|
|
|
|
compressFrame.write( tmp.array(), 0, tmp.readableBytes() );
|
|
|
|
empty = false;
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-02-05 08:25:44 +01:00
|
|
|
public int getLength()
|
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
return data.readableBytes();
|
2014-02-05 08:25:44 +01:00
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
public boolean isEmpty()
|
|
|
|
{
|
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|