    001    package net.minecraft.network.packet;
    002
    003    import java.io.ByteArrayOutputStream;
    004    import java.io.DataInputStream;
    005    import java.io.DataOutputStream;
    006    import java.io.IOException;
    007    import net.minecraft.world.World;
    008    import net.minecraft.world.chunk.Chunk;
    009
    010    public class Packet52MultiBlockChange extends Packet
    011    {
    012        /** Chunk X position. */
    013        public int xPosition;
    014
    015        /** Chunk Z position. */
    016        public int zPosition;
    017
    018        /** The metadata for each block changed. */
    019        public byte[] metadataArray;
    020
    021        /** The size of the arrays. */
    022        public int size;
    023        private static byte[] field_73449_e = new byte[0];
    024
    025        public Packet52MultiBlockChange()
    026        {
    027            this.isChunkDataPacket = true;
    028        }
    029
    030        public Packet52MultiBlockChange(int par1, int par2, short[] par3ArrayOfShort, int par4, World par5World)
    031        {
    032            this.isChunkDataPacket = true;
    033            this.xPosition = par1;
    034            this.zPosition = par2;
    035            this.size = par4;
    036            int var6 = 4 * par4;
    037            Chunk var7 = par5World.getChunkFromChunkCoords(par1, par2);
    038
    039            try
    040            {
    041                if (par4 >= 64)
    042                {
    043                    System.out.println("ChunkTilesUpdatePacket compress " + par4);
    044
    045                    if (field_73449_e.length < var6)
    046                    {
    047                        field_73449_e = new byte[var6];
    048                    }
    049                }
    050                else
    051                {
    052                    ByteArrayOutputStream var8 = new ByteArrayOutputStream(var6);
    053                    DataOutputStream var9 = new DataOutputStream(var8);
    054
    055                    for (int var10 = 0; var10 < par4; ++var10)
    056                    {
    057                        int var11 = par3ArrayOfShort[var10] >> 12 & 15;
    058                        int var12 = par3ArrayOfShort[var10] >> 8 & 15;
    059                        int var13 = par3ArrayOfShort[var10] & 255;
    060                        var9.writeShort(par3ArrayOfShort[var10]);
    061                        var9.writeShort((short)((var7.getBlockID(var11, var13, var12) & 4095) << 4 | var7.getBlockMetadata(var11, var13, var12) & 15));
    062                    }
    063
    064                    this.metadataArray = var8.toByteArray();
    065
    066                    if (this.metadataArray.length != var6)
    067                    {
    068                        throw new RuntimeException("Expected length " + var6 + " doesn\'t match received length " + this.metadataArray.length);
    069                    }
    070                }
    071            }
    072            catch (IOException var14)
    073            {
    074                System.err.println(var14.getMessage());
    075                this.metadataArray = null;
    076            }
    077        }
    078
    079        /**
    080         * Abstract. Reads the raw packet data from the data stream.
    081         */
    082        public void readPacketData(DataInputStream par1DataInputStream) throws IOException
    083        {
    084            this.xPosition = par1DataInputStream.readInt();
    085            this.zPosition = par1DataInputStream.readInt();
    086            this.size = par1DataInputStream.readShort() & 65535;
    087            int var2 = par1DataInputStream.readInt();
    088
    089            if (var2 > 0)
    090            {
    091                this.metadataArray = new byte[var2];
    092                par1DataInputStream.readFully(this.metadataArray);
    093            }
    094        }
    095
    096        /**
    097         * Abstract. Writes the raw packet data to the data stream.
    098         */
    099        public void writePacketData(DataOutputStream par1DataOutputStream) throws IOException
    100        {
    101            par1DataOutputStream.writeInt(this.xPosition);
    102            par1DataOutputStream.writeInt(this.zPosition);
    103            par1DataOutputStream.writeShort((short)this.size);
    104
    105            if (this.metadataArray != null)
    106            {
    107                par1DataOutputStream.writeInt(this.metadataArray.length);
    108                par1DataOutputStream.write(this.metadataArray);
    109            }
    110            else
    111            {
    112                par1DataOutputStream.writeInt(0);
    113            }
    114        }
    115
    116        /**
    117         * Passes this Packet on to the NetHandler for processing.
    118         */
    119        public void processPacket(NetHandler par1NetHandler)
    120        {
    121            par1NetHandler.handleMultiBlockChange(this);
    122        }
    123
    124        /**
    125         * Abstract. Return the size of the packet (not counting the header).
    126         */
    127        public int getPacketSize()
    128        {
    129            return 10 + this.size * 4;
    130        }
    131    }