3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-12-20 05:20:07 +01:00
Paper/src/main/java/net/minecraft/server/Packet51MapChunk.java

195 Zeilen
6.2 KiB
Java

package net.minecraft.server;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.zip.DataFormatException;
2012-07-29 09:33:13 +02:00
import java.util.zip.Deflater;
import java.util.zip.Inflater;
public class Packet51MapChunk extends Packet {
public int a;
public int b;
public int c;
public int d;
2012-11-06 13:05:28 +01:00
private byte[] buffer;
private byte[] inflatedBuffer;
public boolean e;
2012-11-06 13:05:28 +01:00
private int size;
2012-07-29 09:33:13 +02:00
private static byte[] buildBuffer = new byte[196864];
public Packet51MapChunk() {
this.lowPriority = true;
}
2012-03-01 11:49:23 +01:00
public Packet51MapChunk(Chunk chunk, boolean flag, int i) {
this.lowPriority = true;
2012-03-01 11:49:23 +01:00
this.a = chunk.x;
this.b = chunk.z;
2012-07-29 09:33:13 +02:00
this.e = flag;
ChunkMap chunkmap = a(chunk, flag, i);
Deflater deflater = new Deflater(-1);
2012-03-01 11:49:23 +01:00
2012-07-29 09:33:13 +02:00
this.d = chunkmap.c;
this.c = chunkmap.b;
try {
2012-07-29 09:33:13 +02:00
this.inflatedBuffer = chunkmap.a;
deflater.setInput(chunkmap.a, 0, chunkmap.a.length);
deflater.finish();
2012-07-29 09:33:13 +02:00
this.buffer = new byte[chunkmap.a.length];
this.size = deflater.deflate(this.buffer);
} finally {
deflater.end();
2012-07-29 09:33:13 +02:00
}
2012-11-06 13:05:28 +01:00
}
public void a(DataInputStream datainputstream) throws IOException { // CraftBukkit - throws IOException
this.a = datainputstream.readInt();
2012-03-01 11:49:23 +01:00
this.b = datainputstream.readInt();
2012-07-29 09:33:13 +02:00
this.e = datainputstream.readBoolean();
2012-03-01 11:49:23 +01:00
this.c = datainputstream.readShort();
this.d = datainputstream.readShort();
2012-01-12 16:27:39 +01:00
this.size = datainputstream.readInt();
2012-07-29 09:33:13 +02:00
if (buildBuffer.length < this.size) {
buildBuffer = new byte[this.size];
2012-03-01 11:49:23 +01:00
}
2012-07-29 09:33:13 +02:00
datainputstream.readFully(buildBuffer, 0, this.size);
2012-03-01 11:49:23 +01:00
int i = 0;
int j;
for (j = 0; j < 16; ++j) {
i += this.c >> j & 1;
}
j = 12288 * i;
2012-07-29 09:33:13 +02:00
if (this.e) {
2012-03-01 11:49:23 +01:00
j += 256;
}
2012-07-29 09:33:13 +02:00
this.inflatedBuffer = new byte[j];
Inflater inflater = new Inflater();
2012-07-29 09:33:13 +02:00
inflater.setInput(buildBuffer, 0, this.size);
try {
2012-07-29 09:33:13 +02:00
inflater.inflate(this.inflatedBuffer);
} catch (DataFormatException dataformatexception) {
throw new IOException("Bad compressed data format");
} finally {
inflater.end();
}
}
public void a(DataOutputStream dataoutputstream) throws IOException { // CraftBukkit - throws IOException
dataoutputstream.writeInt(this.a);
2012-03-01 11:49:23 +01:00
dataoutputstream.writeInt(this.b);
2012-07-29 09:33:13 +02:00
dataoutputstream.writeBoolean(this.e);
2012-03-01 11:49:23 +01:00
dataoutputstream.writeShort((short) (this.c & '\uffff'));
dataoutputstream.writeShort((short) (this.d & '\uffff'));
2012-01-12 16:27:39 +01:00
dataoutputstream.writeInt(this.size);
dataoutputstream.write(this.buffer, 0, this.size);
}
public void handle(NetHandler nethandler) {
nethandler.a(this);
}
public int a() {
2012-01-12 16:27:39 +01:00
return 17 + this.size;
}
2012-07-29 09:33:13 +02:00
public static ChunkMap a(Chunk chunk, boolean flag, int i) {
int j = 0;
ChunkSection[] achunksection = chunk.i();
int k = 0;
ChunkMap chunkmap = new ChunkMap();
byte[] abyte = buildBuffer;
if (flag) {
chunk.seenByPlayer = true;
}
int l;
for (l = 0; l < achunksection.length; ++l) {
if (achunksection[l] != null && (!flag || !achunksection[l].a()) && (i & 1 << l) != 0) {
chunkmap.b |= 1 << l;
if (achunksection[l].i() != null) {
chunkmap.c |= 1 << l;
++k;
}
}
}
for (l = 0; l < achunksection.length; ++l) {
if (achunksection[l] != null && (!flag || !achunksection[l].a()) && (i & 1 << l) != 0) {
byte[] abyte1 = achunksection[l].g();
System.arraycopy(abyte1, 0, abyte, j, abyte1.length);
j += abyte1.length;
}
}
NibbleArray nibblearray;
for (l = 0; l < achunksection.length; ++l) {
if (achunksection[l] != null && (!flag || !achunksection[l].a()) && (i & 1 << l) != 0) {
nibblearray = achunksection[l].j();
System.arraycopy(nibblearray.a, 0, abyte, j, nibblearray.a.length);
j += nibblearray.a.length;
}
}
for (l = 0; l < achunksection.length; ++l) {
if (achunksection[l] != null && (!flag || !achunksection[l].a()) && (i & 1 << l) != 0) {
nibblearray = achunksection[l].k();
System.arraycopy(nibblearray.a, 0, abyte, j, nibblearray.a.length);
j += nibblearray.a.length;
}
}
for (l = 0; l < achunksection.length; ++l) {
if (achunksection[l] != null && (!flag || !achunksection[l].a()) && (i & 1 << l) != 0) {
nibblearray = achunksection[l].l();
System.arraycopy(nibblearray.a, 0, abyte, j, nibblearray.a.length);
j += nibblearray.a.length;
}
}
if (k > 0) {
for (l = 0; l < achunksection.length; ++l) {
if (achunksection[l] != null && (!flag || !achunksection[l].a()) && achunksection[l].i() != null && (i & 1 << l) != 0) {
nibblearray = achunksection[l].i();
System.arraycopy(nibblearray.a, 0, abyte, j, nibblearray.a.length);
j += nibblearray.a.length;
}
}
}
// CraftBukkit start - Hackiest hack to have ever hacked.
// First of all, check to see if we flagged it to send, and all data is "0"
// This means that it's an "EmptyChunk," HOWEVER... It's not a physical EmptyChunk on the server, there is simply no data present
if (flag && i == 0xffff && j == 0 && chunkmap.b == 0 && chunkmap.c == 0) {
chunkmap.b = 1;
j = 10240;
java.util.Arrays.fill(abyte, 0, j, (byte) 0);
}
// CraftBukkit end
if (flag) {
byte[] abyte2 = chunk.m();
System.arraycopy(abyte2, 0, abyte, j, abyte2.length);
j += abyte2.length;
}
2012-07-29 09:33:13 +02:00
chunkmap.a = new byte[j];
System.arraycopy(abyte, 0, chunkmap.a, 0, j);
return chunkmap;
}
}