use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.
the class Chunk1_17Type method write.
@Override
public void write(ByteBuf output, Chunk chunk) throws Exception {
output.writeInt(chunk.getX());
output.writeInt(chunk.getZ());
Type.LONG_ARRAY_PRIMITIVE.write(output, chunk.getChunkMask().toLongArray());
Type.NBT.write(output, chunk.getHeightMap());
// Write biome data
Type.VAR_INT_ARRAY_PRIMITIVE.write(output, chunk.getBiomeData());
ByteBuf buf = output.alloc().buffer();
try {
ChunkSection[] sections = chunk.getSections();
for (ChunkSection section : sections) {
// Section not set
if (section == null)
continue;
buf.writeShort(section.getNonAirBlocksCount());
Types1_16.CHUNK_SECTION.write(buf, section);
}
buf.readerIndex(0);
Type.VAR_INT.writePrimitive(output, buf.readableBytes());
output.writeBytes(buf);
} finally {
// release buffer
buf.release();
}
// Write Block Entities
Type.NBT_ARRAY.write(output, chunk.getBlockEntities().toArray(EMPTY_COMPOUNDS));
}
use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.
the class WorldPackets method register.
public static void register(Protocol1_16To1_15_2 protocol) {
BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14);
blockRewriter.registerBlockAction(ClientboundPackets1_15.BLOCK_ACTION);
blockRewriter.registerBlockChange(ClientboundPackets1_15.BLOCK_CHANGE);
blockRewriter.registerMultiBlockChange(ClientboundPackets1_15.MULTI_BLOCK_CHANGE);
blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_15.ACKNOWLEDGE_PLAYER_DIGGING);
protocol.registerClientbound(ClientboundPackets1_15.UPDATE_LIGHT, new PacketRemapper() {
@Override
public void registerMap() {
// x
map(Type.VAR_INT);
// y
map(Type.VAR_INT);
// Take neighbour's light into account as well
handler(wrapper -> wrapper.write(Type.BOOLEAN, true));
}
});
protocol.registerClientbound(ClientboundPackets1_15.CHUNK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
Chunk chunk = wrapper.read(new Chunk1_15Type());
wrapper.write(new Chunk1_16Type(), chunk);
chunk.setIgnoreOldLightData(chunk.isFullChunk());
for (int s = 0; s < chunk.getSections().length; s++) {
ChunkSection section = chunk.getSections()[s];
if (section == null)
continue;
for (int i = 0; i < section.getPaletteSize(); i++) {
int old = section.getPaletteEntry(i);
section.setPaletteEntry(i, protocol.getMappingData().getNewBlockStateId(old));
}
}
CompoundTag heightMaps = chunk.getHeightMap();
for (Tag heightMapTag : heightMaps.values()) {
LongArrayTag heightMap = (LongArrayTag) heightMapTag;
int[] heightMapData = new int[256];
CompactArrayUtil.iterateCompactArray(9, heightMapData.length, heightMap.getValue(), (i, v) -> heightMapData[i] = v);
heightMap.setValue(CompactArrayUtil.createCompactArrayWithPadding(9, heightMapData.length, i -> heightMapData[i]));
}
if (chunk.getBlockEntities() == null)
return;
for (CompoundTag blockEntity : chunk.getBlockEntities()) {
handleBlockEntity(blockEntity);
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_15.BLOCK_ENTITY_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
Position position = wrapper.passthrough(Type.POSITION1_14);
short action = wrapper.passthrough(Type.UNSIGNED_BYTE);
CompoundTag tag = wrapper.passthrough(Type.NBT);
handleBlockEntity(tag);
});
}
});
blockRewriter.registerEffect(ClientboundPackets1_15.EFFECT, 1010, 2001);
}
use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.
the class Chunk1_16Type method read.
@Override
public Chunk read(ByteBuf input) throws Exception {
int chunkX = input.readInt();
int chunkZ = input.readInt();
boolean fullChunk = input.readBoolean();
boolean ignoreOldLightData = input.readBoolean();
int primaryBitmask = Type.VAR_INT.readPrimitive(input);
CompoundTag heightMap = Type.NBT.read(input);
int[] biomeData = fullChunk ? new int[1024] : null;
if (fullChunk) {
for (int i = 0; i < 1024; i++) {
biomeData[i] = input.readInt();
}
}
// data size in bytes
Type.VAR_INT.readPrimitive(input);
// Read sections
ChunkSection[] sections = new ChunkSection[16];
for (int i = 0; i < 16; i++) {
// Section not set
if ((primaryBitmask & (1 << i)) == 0)
continue;
short nonAirBlocksCount = input.readShort();
ChunkSection section = Types1_16.CHUNK_SECTION.read(input);
section.setNonAirBlocksCount(nonAirBlocksCount);
sections[i] = section;
}
List<CompoundTag> nbtData = new ArrayList<>(Arrays.asList(Type.NBT_ARRAY.read(input)));
// Read all the remaining bytes (workaround for #681)
if (input.readableBytes() > 0) {
byte[] array = Type.REMAINING_BYTES.read(input);
if (Via.getManager().isDebug()) {
Via.getPlatform().getLogger().warning("Found " + array.length + " more bytes than expected while reading the chunk: " + chunkX + "/" + chunkZ);
}
}
return new BaseChunk(chunkX, chunkZ, fullChunk, ignoreOldLightData, primaryBitmask, sections, biomeData, heightMap, nbtData);
}
use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.
the class Chunk1_9_3_4Type method write.
@Override
public void write(ByteBuf output, ClientWorld world, Chunk chunk) throws Exception {
output.writeInt(chunk.getX());
output.writeInt(chunk.getZ());
output.writeBoolean(chunk.isFullChunk());
Type.VAR_INT.writePrimitive(output, chunk.getBitmask());
ByteBuf buf = output.alloc().buffer();
try {
for (int i = 0; i < 16; i++) {
ChunkSection section = chunk.getSections()[i];
// Section not set
if (section == null)
continue;
Types1_9.CHUNK_SECTION.write(buf, section);
section.getLight().writeBlockLight(buf);
// No sky light, we're done here.
if (!section.getLight().hasSkyLight())
continue;
section.getLight().writeSkyLight(buf);
}
buf.readerIndex(0);
Type.VAR_INT.writePrimitive(output, buf.readableBytes() + (chunk.isBiomeData() ? 256 : 0));
output.writeBytes(buf);
} finally {
// release buffer
buf.release();
}
// Write biome data
if (chunk.isBiomeData()) {
for (int biome : chunk.getBiomeData()) {
output.writeByte((byte) biome);
}
}
// Write Block Entities
Type.NBT_ARRAY.write(output, chunk.getBlockEntities().toArray(new CompoundTag[0]));
}
use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.
the class Protocol1_9_3To1_9_1_2 method registerPackets.
@Override
protected void registerPackets() {
// Sign update packet
registerClientbound(ClientboundPackets1_9.UPDATE_SIGN, null, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// read data
Position position = wrapper.read(Type.POSITION);
JsonElement[] lines = new JsonElement[4];
for (int i = 0; i < 4; i++) {
lines[i] = wrapper.read(Type.COMPONENT);
}
wrapper.clearInputBuffer();
// write data
// Update block entity
wrapper.setId(0x09);
// Block location
wrapper.write(Type.POSITION, position);
// Action type (9 update sign)
wrapper.write(Type.UNSIGNED_BYTE, (short) 9);
// Create nbt
CompoundTag tag = new CompoundTag();
tag.put("id", new StringTag("Sign"));
tag.put("x", new IntTag(position.x()));
tag.put("y", new IntTag(position.y()));
tag.put("z", new IntTag(position.z()));
for (int i = 0; i < lines.length; i++) {
tag.put("Text" + (i + 1), new StringTag(lines[i].toString()));
}
wrapper.write(Type.NBT, tag);
}
});
}
});
registerClientbound(ClientboundPackets1_9.CHUNK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
Chunk chunk = wrapper.read(new Chunk1_9_1_2Type(clientWorld));
wrapper.write(new Chunk1_9_3_4Type(clientWorld), chunk);
List<CompoundTag> tags = chunk.getBlockEntities();
for (int i = 0; i < chunk.getSections().length; i++) {
ChunkSection section = chunk.getSections()[i];
if (section == null)
continue;
for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) {
int block = section.getBlockWithoutData(x, y, z);
if (FakeTileEntity.isTileEntity(block)) {
tags.add(FakeTileEntity.createTileEntity(x + (chunk.getX() << 4), y + (i << 4), z + (chunk.getZ() << 4), block));
}
}
}
}
}
}
});
}
});
registerClientbound(ClientboundPackets1_9.JOIN_GAME, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.INT);
// 1 - Gamemode
map(Type.UNSIGNED_BYTE);
// 2 - Dimension
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 1);
clientWorld.setEnvironment(dimensionId);
}
});
}
});
registerClientbound(ClientboundPackets1_9.RESPAWN, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Dimension ID
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 0);
clientWorld.setEnvironment(dimensionId);
}
});
}
});
// Sound effect
registerClientbound(ClientboundPackets1_9.SOUND, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Sound name
map(Type.VAR_INT);
// 1 - Sound Category
map(Type.VAR_INT);
// 2 - x
map(Type.INT);
// 3 - y
map(Type.INT);
// 4 - z
map(Type.INT);
// 5 - Volume
map(Type.FLOAT);
// 6 - Pitch
map(ADJUST_PITCH);
}
});
}
Aggregations