Applied-Energistics-2-tiler.../core/sync/packets/PacketMEInventoryUpdate.java
AlgorithmX2 c3549e483c Fixed Bug with Terminal Scroll Bar and Searching.
Switched to Reactive Packet size protection.
2014-02-06 10:57:01 -06:00

98 lines
2.4 KiB
Java

package appeng.core.sync.packets;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.BufferOverflowException;
import java.util.LinkedList;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet250CustomPayload;
import appeng.api.storage.data.IAEItemStack;
import appeng.client.gui.implementations.GuiMEMonitorable;
import appeng.client.gui.implementations.GuiNetworkStatus;
import appeng.core.sync.AppEngPacket;
import appeng.util.item.AEItemStack;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class PacketMEInventoryUpdate extends AppEngPacket
{
// output...
final private ByteArrayOutputStream bytes;
final private DataOutputStream data;
int lastSize = 0;
boolean empty = true;
// input.
final List<IAEItemStack> list;
// automatic.
public PacketMEInventoryUpdate(DataInputStream stream) throws IOException {
bytes = null;
data = null;
list = new LinkedList();
while (stream.available() > 0)
list.add( AEItemStack.loadItemStackFromPacket( stream ) );
empty = list.isEmpty();
}
@Override
@SideOnly(Side.CLIENT)
public void clientPacketData(INetworkManager network, AppEngPacket packet, EntityPlayer player)
{
GuiScreen gs = Minecraft.getMinecraft().currentScreen;
if ( gs instanceof GuiMEMonitorable )
((GuiMEMonitorable) gs).postUpdate( list );
if ( gs instanceof GuiNetworkStatus )
((GuiNetworkStatus) gs).postUpdate( list );
}
@Override
public Packet250CustomPayload getPacket()
{
isChunkDataPacket = false;
byte[] dataOut = new byte[lastSize];
System.arraycopy( bytes.toByteArray(), 0, dataOut, 0, lastSize );
configureWrite( dataOut );
return super.getPacket();
}
// api
public PacketMEInventoryUpdate() throws IOException {
bytes = new ByteArrayOutputStream();
data = new DataOutputStream( bytes );
list = null;
data.writeInt( getPacketID() );
}
public void appendItem(IAEItemStack is) throws IOException, BufferOverflowException
{
is.writeToPacket( data );
empty = false;
if ( bytes.size() > 20000 )
throw new BufferOverflowException();
else
lastSize = bytes.size();
}
public int getLength()
{
return data.size();
}
public boolean isEmpty()
{
return empty;
}
}