Archived
0

Chunk Allocation and Data

This commit is contained in:
2018-04-20 08:49:29 +03:00
parent e31a83670e
commit 9d22d7c91b
5 changed files with 125 additions and 0 deletions

View File

@@ -17,6 +17,10 @@ public class PacketEncoder extends MessageToByteEncoder<SCPacket> {
protected void encode(ChannelHandlerContext ctx, SCPacket pkt, ByteBuf out) throws Exception { protected void encode(ChannelHandlerContext ctx, SCPacket pkt, ByteBuf out) throws Exception {
log.debug("{}: {}", pkt.getClass().getSimpleName(), pkt.toString()); log.debug("{}: {}", pkt.getClass().getSimpleName(), pkt.toString());
Integer id = PacketManager.getServirSidePacket(pkt.getClass()); Integer id = PacketManager.getServirSidePacket(pkt.getClass());
if (id == null) {
log.warn("Not defined ID packet \"{}\"", pkt.getClass().getSimpleName());
return;
}
byte[] bytes = pkt.toByteArray(); byte[] bytes = pkt.toByteArray();
out.writeByte(id); out.writeByte(id);

View File

@@ -93,6 +93,48 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
infoPkt.setPing(4); infoPkt.setPing(4);
channel.write(infoPkt); channel.write(infoPkt);
// send Chunk allocation
ChunkAllocationPacket chInitPkt = new ChunkAllocationPacket();
chInitPkt.setX(0);
chInitPkt.setZ(0);
chInitPkt.setInitChunk(true);
channel.write(chInitPkt);
for (int x = -17; x <= 17; x++) {
for (int z = -17; z <= 17; z++) {
if (x == 0 && z == 0) continue;
chInitPkt = new ChunkAllocationPacket();
chInitPkt.setX(x);
chInitPkt.setZ(z);
chInitPkt.setInitChunk(true);
channel.write(chInitPkt);
}
}
// send Chunk data
ChunkDataPacket chDataPkt = new ChunkDataPacket();
chDataPkt.setX(0);
chDataPkt.setZ(0);
chDataPkt.setNeedInitChunk(false);
chDataPkt.setYMin(0);
chDataPkt.setYMax(0);
channel.write(chDataPkt);
for (int x = -17; x <= 17; x++) {
for (int z = -17; z <= 17; z++) {
if (x == 0 && z == 0) continue;
chDataPkt = new ChunkDataPacket();
chDataPkt.setX(x);
chDataPkt.setZ(z);
chDataPkt.setNeedInitChunk(false);
chDataPkt.setYMin(0);
chDataPkt.setYMax(0);
channel.write(chDataPkt);
}
}
// send Position and look // send Position and look
PositionAndLookPacket posLookPkt = new PositionAndLookPacket(); PositionAndLookPacket posLookPkt = new PositionAndLookPacket();
posLookPkt.setLocation(spawnLoc); posLookPkt.setLocation(spawnLoc);

View File

@@ -0,0 +1,28 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-04-20
*/
package mc.core.network.proto_125.packets;
import lombok.Setter;
import lombok.ToString;
import mc.core.network.SCPacket;
import mc.core.network.proto_125.ByteArrayOutputNetStream;
@Setter
@ToString
public class ChunkAllocationPacket implements SCPacket {
private int x, z;
private boolean initChunk;
@Override
public byte[] toByteArray() {
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
netStream.writeInt(x);
netStream.writeInt(z);
netStream.writeBoolean(initChunk);
return netStream.toByteArray();
}
}

View File

@@ -0,0 +1,49 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-04-20
*/
package mc.core.network.proto_125.packets;
import lombok.Setter;
import lombok.ToString;
import mc.core.network.SCPacket;
import mc.core.network.proto_125.ByteArrayOutputNetStream;
import java.util.zip.Deflater;
@Setter
@ToString
public class ChunkDataPacket implements SCPacket {
private static final byte[] data = new byte[65536 + 32768 + 32768 + 32768/* + 0*/ + 256];
private static byte[] compressData = null;
private int x, z;
private boolean needInitChunk;
private int yMin,yMax;
@Override
public byte[] toByteArray() {
if (compressData == null) {
Deflater zlib = new Deflater();
zlib.setInput(data);
byte[] preCompress = new byte[data.length];
int len = zlib.deflate(preCompress);
compressData = new byte[len];
System.arraycopy(preCompress, 0, compressData, 0, len);
}
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
netStream.writeInt(x);
netStream.writeInt(z);
netStream.writeBoolean(needInitChunk);
netStream.writeShort(yMin);
netStream.writeShort(yMax);
netStream.writeInt(compressData.length);
netStream.writeInt(0);
netStream.writeBytes(compressData);
return netStream.toByteArray();
}
}

View File

@@ -16,6 +16,8 @@ public class PacketManager {
.put(0x02, HandshakePacket.class) .put(0x02, HandshakePacket.class)
.put(0x06, SpawnPositionPacket.class) .put(0x06, SpawnPositionPacket.class)
.put(0x0D, PositionAndLookPacket.class) .put(0x0D, PositionAndLookPacket.class)
.put(0x32, ChunkAllocationPacket.class)
.put(0x33, ChunkDataPacket.class)
.put(0xC9, PlayerInfoPacket.class) .put(0xC9, PlayerInfoPacket.class)
.put(0xCA, PlayerAbilitiesPacket.class) .put(0xCA, PlayerAbilitiesPacket.class)
.put(0xFE, PingPacket.class) .put(0xFE, PingPacket.class)