Skip to content

Instantly share code, notes, and snippets.

@MrPowerGamerBR
Last active January 20, 2017 11:43
Show Gist options
  • Save MrPowerGamerBR/e751de1a5cf17fa93212768e91648f64 to your computer and use it in GitHub Desktop.
Save MrPowerGamerBR/e751de1a5cf17fa93212768e91648f64 to your computer and use it in GitHub Desktop.
package com.mrpowergamerbr.dreamproxy.client.packets;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import org.spacehq.mc.protocol.data.game.Chunk;
import org.spacehq.mc.protocol.packet.ingame.server.world.ServerChunkDataPacket;
import org.spacehq.mc.protocol.packet.ingame.server.world.ServerMultiChunkDataPacket;
import org.spacehq.packetlib.event.session.PacketReceivedEvent;
import org.spacehq.packetlib.event.session.SessionAdapter;
import com.mrpowergamerbr.dreamproxy.client.MinecraftClient;
import com.mrpowergamerbr.dreamproxy.server.packets.PEChunkRadiusUpdatePacket;
import com.mrpowergamerbr.dreamproxy.server.packets.PEFullChunkDataPacket;
import cn.nukkit.network.protocol.FullChunkDataPacket;
public class PCServerMultiChunkDataPacket extends SessionAdapter {
MinecraftClient client;
public PCServerMultiChunkDataPacket(MinecraftClient client) {
this.client = client;
}
@Override
public void packetReceived(PacketReceivedEvent event) {
if (event.getPacket() instanceof ServerChunkDataPacket) {
System.out.println("********* ServerChunkDataPacket!");
/* ServerChunkDataPacket scdp = event.<ServerChunkDataPacket>getPacket();
System.out.println("ChunkPacket Info: " + scdp.getChunks().length + " chunks");
/*
* Every chunk must have 16 sections
*
* MCPE only supports 8 sections
*/
/* for (Chunk chunk : scdp.getChunks()) {
int x = 0;
int y = 0;
int z = 0;
try {
while (255 > y) {
while (x >= 15) {
while (z >= 15) {
System.out.println("Block at " + x + ", " + y + ", " + z + " is " + (chunk.getBlocks().getBlock(x, y, z) >> 4));
z++;
}
x++;
}
y++;
}
} catch (Exception e) {
System.out.println("Chunk's Limit: " + x + ", " + y + ", " + z + "");
}
} */
}
if (event.getPacket() instanceof ServerMultiChunkDataPacket) {
System.out.println("********* ServerMultiChunkDataPacket!");
ServerMultiChunkDataPacket smcdp = event.<ServerMultiChunkDataPacket>getPacket();
ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
DataOutputStream dos1 = new DataOutputStream(bos1);
ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
DataOutputStream dos2 = new DataOutputStream(bos2);
ByteArrayOutputStream bosTiles = new ByteArrayOutputStream();
DataOutputStream dosTiles = new DataOutputStream(bosTiles);
try {
for (int col = 0; col < smcdp.getColumns(); col++) {
bos1.reset();
bos2.reset();
bosTiles.reset();
PEFullChunkDataPacket chunkToSend = new PEFullChunkDataPacket();
chunkToSend.chunkX = smcdp.getX(col);
chunkToSend.chunkZ = smcdp.getZ(col);
chunkToSend.order = chunkToSend.order = FullChunkDataPacket.ORDER_COLUMNS;
Chunk[] pcChunks = smcdp.getChunks(col);
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
for (int y = 0; y < 128; y++) {
if (pcChunks[y >> 4] == null || pcChunks[y >> 4].isEmpty()) {
dos1.writeByte((byte) 0);
} else {
int pcBlock = pcChunks[y >> 4].getBlocks().getBlock(x, y % 16, z);
int peBlock = pcBlock;
// peBlock = Remapper.toMCPEBlockID(pcBlock, 0).getID();
dos1.writeByte((byte) (peBlock & 0xFF));
// dos1.writeByte((byte) 1);
}
}
}
}
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
for (int y = 0; y < 128; y += 2) {
byte data1 = 0;
byte data2 = 0;
try {
data1 = (byte) pcChunks[y >> 4].getBlocks().getData(x, y % 16, z);
} catch (Exception e) {
}
try {
data2 = (byte) pcChunks[y >> 4].getBlocks().getData(x, (y + 1) % 16, z);
} catch (Exception e) {
}
data1 |= ((data2 & 0xF) << 4);
dos1.writeByte(data1);
}
}
}
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
for (int y = 0; y < 128; y += 2) {
//if (noSkyLight) {
// temp.writeByte(0);
//} else {
byte data = 0;
try {
data = (byte) (pcChunks[y >> 4].getSkyLight().get(x, y & 0xF, z) & 0xF);
data |= (pcChunks[y >> 4].getSkyLight().get(x, (y + 1) & 0xF, z) & 0xF);
} catch (Exception e) {
}
dos1.writeByte(data);
//}
}
}
}
dos1.write(bos2.toByteArray()); //Not bos1 contains previously generated data! Don't reset!
bos2.reset();//Now it's empty
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
for (int y = 0; y < 128; y += 2) {
byte data = pcChunks[y >> 4] == null || pcChunks[y >> 4].isEmpty() ? (byte) 0 : (byte) ((pcChunks[y >> 4].getBlockLight().get(x, y % 16, z) & 0xF) << 4);
data |= pcChunks[(y + 1) >> 4] == null || pcChunks[(y + 1) >> 4].isEmpty() ? (byte) 0 : (byte) (pcChunks[(y + 1) >> 4].getBlockLight().get(x, (y + 1) % 16, z) & 0xF);
dos1.writeByte(data);
}
}
}
//Height Map
for (int i = 0; i < 256; i++) {
dos1.writeByte((byte) 0xFF);
}
//Biome Colors
for (int i = 0; i < 256; i++) {
dos1.writeByte((byte) 0x01);
dos1.writeByte((byte) 0x85);
dos1.writeByte((byte) 0xB2);
dos1.writeByte((byte) 0x4A);
}
bos2.reset();
dos1.writeInt(0);//Extra data, should be little-endian but it's 0 here for now so it's okay.
dos1.write(bosTiles.toByteArray());
chunkToSend.data = bos1.toByteArray();
client.player.dataPacket(chunkToSend);
PEChunkRadiusUpdatePacket pePacket = new PEChunkRadiusUpdatePacket();
pePacket.radius = 10;
client.player.dataPacket(pePacket);
client.player.dataPacket(chunkToSend);
// client.chunkStorage.add(new ChunkStorage(chunkToSend.chunkX, chunkToSend.chunkZ, pcChunks));
}
} catch (Exception e) {
System.out.println("oh no...");
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment