2019-08-30 21:47:33 +00:00
|
|
|
package org.geysermc.connector.utils;
|
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.chunk.Chunk;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.chunk.Column;
|
2019-12-21 05:05:20 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.world.block.BlockChangeRecord;
|
2019-08-30 21:47:33 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
|
2019-11-28 00:14:38 +00:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.Tag;
|
|
|
|
import org.geysermc.connector.console.GeyserLogger;
|
2019-12-21 05:05:20 +00:00
|
|
|
import com.nukkitx.math.vector.Vector3i;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket;
|
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2019-08-30 21:47:33 +00:00
|
|
|
import org.geysermc.connector.network.translators.TranslatorsInit;
|
2019-10-10 00:11:50 +00:00
|
|
|
import org.geysermc.connector.network.translators.block.BlockEntry;
|
2019-11-28 00:14:38 +00:00
|
|
|
import org.geysermc.connector.network.translators.block.entity.BlockEntityTranslator;
|
|
|
|
import org.geysermc.connector.network.translators.block.entity.SignBlockEntityTranslator;
|
2019-08-30 21:47:33 +00:00
|
|
|
import org.geysermc.connector.world.chunk.ChunkSection;
|
|
|
|
|
2019-11-28 00:14:38 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
|
2019-08-30 21:47:33 +00:00
|
|
|
public class ChunkUtils {
|
|
|
|
|
|
|
|
public static ChunkData translateToBedrock(Column column) {
|
|
|
|
ChunkData chunkData = new ChunkData();
|
|
|
|
|
2019-09-13 09:39:38 +00:00
|
|
|
Chunk[] chunks = column.getChunks();
|
|
|
|
int chunkSectionCount = chunks.length;
|
|
|
|
chunkData.sections = new ChunkSection[chunkSectionCount];
|
2019-08-30 21:47:33 +00:00
|
|
|
|
2019-11-28 00:14:38 +00:00
|
|
|
List<CompoundTag> blockEntities = new ArrayList<>(Arrays.asList(column.getTileEntities()));
|
2019-09-13 09:39:38 +00:00
|
|
|
for (int chunkY = 0; chunkY < chunkSectionCount; chunkY++) {
|
2019-10-16 03:21:44 +00:00
|
|
|
chunkData.sections[chunkY] = new ChunkSection();
|
2019-09-13 09:39:38 +00:00
|
|
|
Chunk chunk = chunks[chunkY];
|
2019-08-30 21:47:33 +00:00
|
|
|
|
2019-09-08 20:46:10 +00:00
|
|
|
if (chunk == null || chunk.isEmpty())
|
|
|
|
continue;
|
2019-08-30 21:47:33 +00:00
|
|
|
|
2019-09-08 20:46:10 +00:00
|
|
|
ChunkSection section = chunkData.sections[chunkY];
|
2019-08-30 21:47:33 +00:00
|
|
|
|
2019-09-08 20:46:10 +00:00
|
|
|
for (int x = 0; x < 16; x++) {
|
|
|
|
for (int y = 0; y < 16; y++) {
|
|
|
|
for (int z = 0; z < 16; z++) {
|
2019-10-13 20:28:03 +00:00
|
|
|
BlockState blockState = chunk.get(x, y, z);
|
2019-12-21 05:05:20 +00:00
|
|
|
BlockEntry block = TranslatorsInit.getBlockTranslator().getBlockEntry(blockState);
|
2019-12-06 18:32:10 +00:00
|
|
|
|
2019-09-13 09:08:48 +00:00
|
|
|
section.getBlockStorageArray()[0].setFullBlock(ChunkSection.blockPosition(x, y, z),
|
2019-12-21 05:05:20 +00:00
|
|
|
block.getBedrockRuntimeId());
|
2019-10-10 00:11:50 +00:00
|
|
|
|
2019-12-22 09:32:49 +00:00
|
|
|
if (block.isWaterlogged()) {
|
2019-12-21 05:05:20 +00:00
|
|
|
BlockEntry water = TranslatorsInit.getBlockTranslator().getBlockEntry("minecraft:water[level=0]");
|
|
|
|
section.getBlockStorageArray()[1].setFullBlock(ChunkSection.blockPosition(x, y, z), water.getBedrockRuntimeId());
|
2019-10-10 00:11:50 +00:00
|
|
|
}
|
2019-09-08 20:46:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-30 21:47:33 +00:00
|
|
|
}
|
2019-11-28 00:14:38 +00:00
|
|
|
|
|
|
|
List<com.nukkitx.nbt.tag.CompoundTag> bedrockBlockEntities = new ArrayList<>();
|
|
|
|
for (CompoundTag tag : blockEntities) {
|
|
|
|
Tag idTag = tag.get("id");
|
2019-12-06 18:32:10 +00:00
|
|
|
if (idTag == null && !tag.contains("Sign")) {
|
2019-11-28 00:14:38 +00:00
|
|
|
GeyserLogger.DEFAULT.debug("Got tag with no id: " + tag.getValue());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-12-06 18:32:10 +00:00
|
|
|
String id = idTag == null ? "Sign" : BlockEntityUtils.getBedrockBlockEntityId((String) idTag.getValue());
|
2019-11-28 00:14:38 +00:00
|
|
|
BlockEntityTranslator blockEntityTranslator = BlockEntityUtils.getBlockEntityTranslator(id);
|
2019-12-06 18:32:10 +00:00
|
|
|
bedrockBlockEntities.add(blockEntityTranslator.getBlockEntityTag(tag, id));
|
2019-11-28 00:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chunkData.blockEntities = bedrockBlockEntities;
|
2019-09-08 20:46:10 +00:00
|
|
|
return chunkData;
|
2019-08-30 21:47:33 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 05:05:20 +00:00
|
|
|
public static void updateBlock(GeyserSession session, BlockState blockState, Position position) {
|
|
|
|
BlockEntry blockEntry = TranslatorsInit.getBlockTranslator().getBlockEntry(blockState);
|
|
|
|
Vector3i pos = Vector3i.from(position.getX(), position.getY(), position.getZ());
|
|
|
|
|
|
|
|
UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();
|
|
|
|
updateBlockPacket.setDataLayer(0);
|
|
|
|
updateBlockPacket.setBlockPosition(pos);
|
|
|
|
updateBlockPacket.setRuntimeId(blockEntry.getBedrockRuntimeId());
|
|
|
|
updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NEIGHBORS);
|
|
|
|
session.getUpstream().sendPacket(updateBlockPacket);
|
|
|
|
|
|
|
|
UpdateBlockPacket waterPacket = new UpdateBlockPacket();
|
|
|
|
waterPacket.setDataLayer(1);
|
|
|
|
waterPacket.setBlockPosition(pos);
|
2019-12-22 09:32:49 +00:00
|
|
|
if (blockEntry.isWaterlogged()) {
|
2019-12-21 05:05:20 +00:00
|
|
|
BlockEntry water = TranslatorsInit.getBlockTranslator().getBlockEntry("minecraft:water[level=0]");
|
|
|
|
waterPacket.setRuntimeId(water.getBedrockRuntimeId());
|
|
|
|
} else {
|
|
|
|
waterPacket.setRuntimeId(0);
|
|
|
|
}
|
|
|
|
session.getUpstream().sendPacket(waterPacket);
|
|
|
|
}
|
|
|
|
|
2019-09-08 20:46:10 +00:00
|
|
|
public static final class ChunkData {
|
|
|
|
public ChunkSection[] sections;
|
|
|
|
|
|
|
|
public byte[] biomes = new byte[256];
|
2019-11-28 00:14:38 +00:00
|
|
|
public List<com.nukkitx.nbt.tag.CompoundTag> blockEntities = new ArrayList<>();
|
2019-08-30 21:47:33 +00:00
|
|
|
}
|
|
|
|
}
|