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 i = par1List.size(); 039 this.chunkPostX = new int[i]; 040 this.chunkPosZ = new int[i]; 041 this.field_73590_a = new int[i]; 042 this.field_73588_b = new int[i]; 043 this.field_73584_f = new byte[i][]; 044 this.field_92076_h = !par1List.isEmpty() && !((Chunk)par1List.get(0)).worldObj.provider.hasNoSky; 045 int j = 0; 046 047 for (int k = 0; k < i; ++k) 048 { 049 Chunk chunk = (Chunk)par1List.get(k); 050 Packet51MapChunkData packet51mapchunkdata = Packet51MapChunk.getMapChunkData(chunk, true, 65535); 051 j += packet51mapchunkdata.compressedData.length; 052 this.chunkPostX[k] = chunk.xPosition; 053 this.chunkPosZ[k] = chunk.zPosition; 054 this.field_73590_a[k] = packet51mapchunkdata.chunkExistFlag; 055 this.field_73588_b[k] = packet51mapchunkdata.chunkHasAddSectionFlag; 056 this.field_73584_f[k] = packet51mapchunkdata.compressedData; 057 } 058 deflateGate = new Semaphore(1); 059 maxLen = j; 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 deflater = new Deflater(-1); 073 074 try 075 { 076 deflater.setInput(data, 0, maxLen); 077 deflater.finish(); 078 byte[] deflated = new byte[maxLen]; 079 this.dataLength = deflater.deflate(deflated); 080 this.chunkDataBuffer = deflated; 081 } 082 finally 083 { 084 deflater.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 short1 = par1DataInputStream.readShort(); 094 this.dataLength = par1DataInputStream.readInt(); 095 this.field_92076_h = par1DataInputStream.readBoolean(); 096 this.chunkPostX = new int[short1]; 097 this.chunkPosZ = new int[short1]; 098 this.field_73590_a = new int[short1]; 099 this.field_73588_b = new int[short1]; 100 this.field_73584_f = new byte[short1][]; 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[] abyte = new byte[196864 * short1]; 109 Inflater inflater = new Inflater(); 110 inflater.setInput(chunkDataNotCompressed, 0, this.dataLength); 111 112 try 113 { 114 inflater.inflate(abyte); 115 } 116 catch (DataFormatException dataformatexception) 117 { 118 throw new IOException("Bad compressed data format"); 119 } 120 finally 121 { 122 inflater.end(); 123 } 124 125 int i = 0; 126 127 for (int j = 0; j < short1; ++j) 128 { 129 this.chunkPostX[j] = par1DataInputStream.readInt(); 130 this.chunkPosZ[j] = par1DataInputStream.readInt(); 131 this.field_73590_a[j] = par1DataInputStream.readShort(); 132 this.field_73588_b[j] = par1DataInputStream.readShort(); 133 int k = 0; 134 int l = 0; 135 int i1; 136 137 for (i1 = 0; i1 < 16; ++i1) 138 { 139 k += this.field_73590_a[j] >> i1 & 1; 140 l += this.field_73588_b[j] >> i1 & 1; 141 } 142 143 i1 = 2048 * 4 * k + 256; 144 i1 += 2048 * l; 145 146 if (this.field_92076_h) 147 { 148 i1 += 2048 * k; 149 } 150 151 this.field_73584_f[j] = new byte[i1]; 152 System.arraycopy(abyte, i, this.field_73584_f[j], 0, i1); 153 i += i1; 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 i = 0; i < this.chunkPostX.length; ++i) 178 { 179 par1DataOutputStream.writeInt(this.chunkPostX[i]); 180 par1DataOutputStream.writeInt(this.chunkPosZ[i]); 181 par1DataOutputStream.writeShort((short)(this.field_73590_a[i] & 65535)); 182 par1DataOutputStream.writeShort((short)(this.field_73588_b[i] & 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}