    001package net.minecraft.network.packet;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.io.DataInputStream;
    006import java.io.DataOutputStream;
    007import java.io.IOException;
    008import java.util.List;
    009import java.util.concurrent.Semaphore;
    010import java.util.zip.DataFormatException;
    011import java.util.zip.Deflater;
    012import java.util.zip.Inflater;
    013import net.minecraft.world.chunk.Chunk;
    014
    015public class Packet56MapChunks extends Packet
    016{
    017    private int[] chunkPostX;
    018    private int[] chunkPosZ;
    019    public int[] field_73590_a;
    020    public int[] field_73588_b;
    021
    022    /** The compressed chunk data buffer */
    023    private byte[] chunkDataBuffer;
    024    private byte[][] field_73584_f;
    025
    026    /** total size of the compressed data */
    027    private int dataLength;
    028    private boolean field_92076_h;
    029    private static byte[] chunkDataNotCompressed = new byte[0];
    030    private int maxLen = 0;
    031
    032    private Semaphore deflateGate;
    033
    034    public Packet56MapChunks() {}
    035
    036    public Packet56MapChunks(List par1List)
    037    {
    038        int var2 = par1List.size();
    039        this.chunkPostX = new int[var2];
    040        this.chunkPosZ = new int[var2];
    041        this.field_73590_a = new int[var2];
    042        this.field_73588_b = new int[var2];
    043        this.field_73584_f = new byte[var2][];
    044        this.field_92076_h = !par1List.isEmpty() && !((Chunk)par1List.get(0)).worldObj.provider.hasNoSky;
    045        int var3 = 0;
    046
    047        for (int var4 = 0; var4 < var2; ++var4)
    048        {
    049            Chunk var5 = (Chunk)par1List.get(var4);
    050            Packet51MapChunkData var6 = Packet51MapChunk.getMapChunkData(var5, true, 65535);
    051            var3 += var6.compressedData.length;
    052            this.chunkPostX[var4] = var5.xPosition;
    053            this.chunkPosZ[var4] = var5.zPosition;
    054            this.field_73590_a[var4] = var6.chunkExistFlag;
    055            this.field_73588_b[var4] = var6.chunkHasAddSectionFlag;
    056            this.field_73584_f[var4] = var6.compressedData;
    057        }
    058        deflateGate = new Semaphore(1);
    059        maxLen = var3;
    060    }
    061
    062    private void deflate()
    063    {
    064        byte[] data = new byte[maxLen];
    065        int offset = 0;
    066        for (int x = 0; x < field_73584_f.length; x++)
    067        {
    068            System.arraycopy(field_73584_f[x], 0, data, offset, field_73584_f[x].length);
    069            offset += field_73584_f[x].length;
    070        }
    071
    072        Deflater var11 = new Deflater(-1);
    073
    074        try
    075        {
    076            var11.setInput(data, 0, maxLen);
    077            var11.finish();
    078            byte[] deflated = new byte[maxLen];
    079            this.dataLength = var11.deflate(deflated);
    080            this.chunkDataBuffer = deflated;
    081        }
    082        finally
    083        {
    084            var11.end();
    085        }
    086    }
    087
    088    /**
    089     * Abstract. Reads the raw packet data from the data stream.
    090     */
    091    public void readPacketData(DataInputStream par1DataInputStream) throws IOException
    092    {
    093        short var2 = par1DataInputStream.readShort();
    094        this.dataLength = par1DataInputStream.readInt();
    095        this.field_92076_h = par1DataInputStream.readBoolean();
    096        this.chunkPostX = new int[var2];
    097        this.chunkPosZ = new int[var2];
    098        this.field_73590_a = new int[var2];
    099        this.field_73588_b = new int[var2];
    100        this.field_73584_f = new byte[var2][];
    101
    102        if (chunkDataNotCompressed.length < this.dataLength)
    103        {
    104            chunkDataNotCompressed = new byte[this.dataLength];
    105        }
    106
    107        par1DataInputStream.readFully(chunkDataNotCompressed, 0, this.dataLength);
    108        byte[] var3 = new byte[196864 * var2];
    109        Inflater var4 = new Inflater();
    110        var4.setInput(chunkDataNotCompressed, 0, this.dataLength);
    111
    112        try
    113        {
    114            var4.inflate(var3);
    115        }
    116        catch (DataFormatException var12)
    117        {
    118            throw new IOException("Bad compressed data format");
    119        }
    120        finally
    121        {
    122            var4.end();
    123        }
    124
    125        int var5 = 0;
    126
    127        for (int var6 = 0; var6 < var2; ++var6)
    128        {
    129            this.chunkPostX[var6] = par1DataInputStream.readInt();
    130            this.chunkPosZ[var6] = par1DataInputStream.readInt();
    131            this.field_73590_a[var6] = par1DataInputStream.readShort();
    132            this.field_73588_b[var6] = par1DataInputStream.readShort();
    133            int var7 = 0;
    134            int var8 = 0;
    135            int var9;
    136
    137            for (var9 = 0; var9 < 16; ++var9)
    138            {
    139                var7 += this.field_73590_a[var6] >> var9 & 1;
    140                var8 += this.field_73588_b[var6] >> var9 & 1;
    141            }
    142
    143            var9 = 2048 * 4 * var7 + 256;
    144            var9 += 2048 * var8;
    145
    146            if (this.field_92076_h)
    147            {
    148                var9 += 2048 * var7;
    149            }
    150
    151            this.field_73584_f[var6] = new byte[var9];
    152            System.arraycopy(var3, var5, this.field_73584_f[var6], 0, var9);
    153            var5 += var9;
    154        }
    155    }
    156
    157    /**
    158     * Abstract. Writes the raw packet data to the data stream.
    159     */
    160    public void writePacketData(DataOutputStream par1DataOutputStream) throws IOException
    161    {
    162        if (this.chunkDataBuffer == null)
    163        {
    164            deflateGate.acquireUninterruptibly();
    165            if (this.chunkDataBuffer == null)
    166            {
    167                deflate();
    168            }
    169            deflateGate.release();
    170        }
    171
    172        par1DataOutputStream.writeShort(this.chunkPostX.length);
    173        par1DataOutputStream.writeInt(this.dataLength);
    174        par1DataOutputStream.writeBoolean(this.field_92076_h);
    175        par1DataOutputStream.write(this.chunkDataBuffer, 0, this.dataLength);
    176
    177        for (int var2 = 0; var2 < this.chunkPostX.length; ++var2)
    178        {
    179            par1DataOutputStream.writeInt(this.chunkPostX[var2]);
    180            par1DataOutputStream.writeInt(this.chunkPosZ[var2]);
    181            par1DataOutputStream.writeShort((short)(this.field_73590_a[var2] & 65535));
    182            par1DataOutputStream.writeShort((short)(this.field_73588_b[var2] & 65535));
    183        }
    184    }
    185
    186    /**
    187     * Passes this Packet on to the NetHandler for processing.
    188     */
    189    public void processPacket(NetHandler par1NetHandler)
    190    {
    191        par1NetHandler.handleMapChunks(this);
    192    }
    193
    194    /**
    195     * Abstract. Return the size of the packet (not counting the header).
    196     */
    197    public int getPacketSize()
    198    {
    199        return 6 + this.dataLength + 12 * this.getNumberOfChunkInPacket();
    200    }
    201
    202    @SideOnly(Side.CLIENT)
    203    public int getChunkPosX(int par1)
    204    {
    205        return this.chunkPostX[par1];
    206    }
    207
    208    @SideOnly(Side.CLIENT)
    209    public int getChunkPosZ(int par1)
    210    {
    211        return this.chunkPosZ[par1];
    212    }
    213
    214    public int getNumberOfChunkInPacket()
    215    {
    216        return this.chunkPostX.length;
    217    }
    218
    219    @SideOnly(Side.CLIENT)
    220    public byte[] getChunkCompressedData(int par1)
    221    {
    222        return this.field_73584_f[par1];
    223    }
    224}