    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
    029    /**
    030     * Whether or not the chunk data contains a light nibble array. This is true in the main world, false in the end +
    031     * nether.
    032     */
    033    private boolean skyLightSent;
    034    private static byte[] chunkDataNotCompressed = new byte[0];
    035    private int maxLen = 0;
    036
    037    private Semaphore deflateGate;
    038
    039    public Packet56MapChunks() {}
    040
    041    public Packet56MapChunks(List par1List)
    042    {
    043        int i = par1List.size();
    044        this.chunkPostX = new int[i];
    045        this.chunkPosZ = new int[i];
    046        this.field_73590_a = new int[i];
    047        this.field_73588_b = new int[i];
    048        this.field_73584_f = new byte[i][];
    049        this.skyLightSent = !par1List.isEmpty() && !((Chunk)par1List.get(0)).worldObj.provider.hasNoSky;
    050        int j = 0;
    051
    052        for (int k = 0; k < i; ++k)
    053        {
    054            Chunk chunk = (Chunk)par1List.get(k);
    055            Packet51MapChunkData packet51mapchunkdata = Packet51MapChunk.getMapChunkData(chunk, true, 65535);
    056            j += packet51mapchunkdata.compressedData.length;
    057            this.chunkPostX[k] = chunk.xPosition;
    058            this.chunkPosZ[k] = chunk.zPosition;
    059            this.field_73590_a[k] = packet51mapchunkdata.chunkExistFlag;
    060            this.field_73588_b[k] = packet51mapchunkdata.chunkHasAddSectionFlag;
    061            this.field_73584_f[k] = packet51mapchunkdata.compressedData;
    062        }
    063        deflateGate = new Semaphore(1);
    064        maxLen = j;
    065    }
    066
    067    private void deflate()
    068    {
    069        byte[] data = new byte[maxLen];
    070        int offset = 0;
    071        for (int x = 0; x < field_73584_f.length; x++)
    072        {
    073            System.arraycopy(field_73584_f[x], 0, data, offset, field_73584_f[x].length);
    074            offset += field_73584_f[x].length;
    075        }
    076
    077        Deflater deflater = new Deflater(-1);
    078
    079        try
    080        {
    081            deflater.setInput(data, 0, maxLen);
    082            deflater.finish();
    083            byte[] deflated = new byte[maxLen];
    084            this.dataLength = deflater.deflate(deflated);
    085            this.chunkDataBuffer = deflated;
    086        }
    087        finally
    088        {
    089            deflater.end();
    090        }
    091    }
    092
    093    /**
    094     * Abstract. Reads the raw packet data from the data stream.
    095     */
    096    public void readPacketData(DataInputStream par1DataInputStream) throws IOException
    097    {
    098        short short1 = par1DataInputStream.readShort();
    099        this.dataLength = par1DataInputStream.readInt();
    100        this.skyLightSent = par1DataInputStream.readBoolean();
    101        this.chunkPostX = new int[short1];
    102        this.chunkPosZ = new int[short1];
    103        this.field_73590_a = new int[short1];
    104        this.field_73588_b = new int[short1];
    105        this.field_73584_f = new byte[short1][];
    106
    107        if (chunkDataNotCompressed.length < this.dataLength)
    108        {
    109            chunkDataNotCompressed = new byte[this.dataLength];
    110        }
    111
    112        par1DataInputStream.readFully(chunkDataNotCompressed, 0, this.dataLength);
    113        byte[] abyte = new byte[196864 * short1];
    114        Inflater inflater = new Inflater();
    115        inflater.setInput(chunkDataNotCompressed, 0, this.dataLength);
    116
    117        try
    118        {
    119            inflater.inflate(abyte);
    120        }
    121        catch (DataFormatException dataformatexception)
    122        {
    123            throw new IOException("Bad compressed data format");
    124        }
    125        finally
    126        {
    127            inflater.end();
    128        }
    129
    130        int i = 0;
    131
    132        for (int j = 0; j < short1; ++j)
    133        {
    134            this.chunkPostX[j] = par1DataInputStream.readInt();
    135            this.chunkPosZ[j] = par1DataInputStream.readInt();
    136            this.field_73590_a[j] = par1DataInputStream.readShort();
    137            this.field_73588_b[j] = par1DataInputStream.readShort();
    138            int k = 0;
    139            int l = 0;
    140            int i1;
    141
    142            for (i1 = 0; i1 < 16; ++i1)
    143            {
    144                k += this.field_73590_a[j] >> i1 & 1;
    145                l += this.field_73588_b[j] >> i1 & 1;
    146            }
    147
    148            i1 = 2048 * 4 * k + 256;
    149            i1 += 2048 * l;
    150
    151            if (this.skyLightSent)
    152            {
    153                i1 += 2048 * k;
    154            }
    155
    156            this.field_73584_f[j] = new byte[i1];
    157            System.arraycopy(abyte, i, this.field_73584_f[j], 0, i1);
    158            i += i1;
    159        }
    160    }
    161
    162    /**
    163     * Abstract. Writes the raw packet data to the data stream.
    164     */
    165    public void writePacketData(DataOutputStream par1DataOutputStream) throws IOException
    166    {
    167        if (this.chunkDataBuffer == null)
    168        {
    169            deflateGate.acquireUninterruptibly();
    170            if (this.chunkDataBuffer == null)
    171            {
    172                deflate();
    173            }
    174            deflateGate.release();
    175        }
    176
    177        par1DataOutputStream.writeShort(this.chunkPostX.length);
    178        par1DataOutputStream.writeInt(this.dataLength);
    179        par1DataOutputStream.writeBoolean(this.skyLightSent);
    180        par1DataOutputStream.write(this.chunkDataBuffer, 0, this.dataLength);
    181
    182        for (int i = 0; i < this.chunkPostX.length; ++i)
    183        {
    184            par1DataOutputStream.writeInt(this.chunkPostX[i]);
    185            par1DataOutputStream.writeInt(this.chunkPosZ[i]);
    186            par1DataOutputStream.writeShort((short)(this.field_73590_a[i] & 65535));
    187            par1DataOutputStream.writeShort((short)(this.field_73588_b[i] & 65535));
    188        }
    189    }
    190
    191    /**
    192     * Passes this Packet on to the NetHandler for processing.
    193     */
    194    public void processPacket(NetHandler par1NetHandler)
    195    {
    196        par1NetHandler.handleMapChunks(this);
    197    }
    198
    199    /**
    200     * Abstract. Return the size of the packet (not counting the header).
    201     */
    202    public int getPacketSize()
    203    {
    204        return 6 + this.dataLength + 12 * this.getNumberOfChunkInPacket();
    205    }
    206
    207    @SideOnly(Side.CLIENT)
    208    public int getChunkPosX(int par1)
    209    {
    210        return this.chunkPostX[par1];
    211    }
    212
    213    @SideOnly(Side.CLIENT)
    214    public int getChunkPosZ(int par1)
    215    {
    216        return this.chunkPosZ[par1];
    217    }
    218
    219    public int getNumberOfChunkInPacket()
    220    {
    221        return this.chunkPostX.length;
    222    }
    223
    224    @SideOnly(Side.CLIENT)
    225    public byte[] getChunkCompressedData(int par1)
    226    {
    227        return this.field_73584_f[par1];
    228    }
    229}