use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.
the class Chunk1_14Type method read.
@Override
public Chunk read(ByteBuf input) throws Exception {
int chunkX = input.readInt();
int chunkZ = input.readInt();
boolean fullChunk = input.readBoolean();
int primaryBitmask = Type.VAR_INT.readPrimitive(input);
CompoundTag heightMap = Type.NBT.read(input);
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_13.CHUNK_SECTION.read(input);
section.setNonAirBlocksCount(nonAirBlocksCount);
sections[i] = section;
}
int[] biomeData = fullChunk ? new int[256] : null;
if (fullChunk) {
for (int i = 0; i < 256; i++) {
biomeData[i] = input.readInt();
}
}
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, false, primaryBitmask, sections, biomeData, heightMap, nbtData);
}
use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.
the class WorldPackets method register.
public static void register(Protocol protocol) {
BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14);
blockRewriter.registerBlockAction(ClientboundPackets1_16.BLOCK_ACTION);
blockRewriter.registerBlockChange(ClientboundPackets1_16.BLOCK_CHANGE);
blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_16.ACKNOWLEDGE_PLAYER_DIGGING);
protocol.registerClientbound(ClientboundPackets1_16.CHUNK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
Chunk chunk = wrapper.read(new Chunk1_16Type());
wrapper.write(new Chunk1_16_2Type(), chunk);
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));
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_16.MULTI_BLOCK_CHANGE, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
wrapper.cancel();
int chunkX = wrapper.read(Type.INT);
int chunkZ = wrapper.read(Type.INT);
long chunkPosition = 0;
chunkPosition |= (chunkX & 0x3FFFFFL) << 42;
chunkPosition |= (chunkZ & 0x3FFFFFL) << 20;
List<BlockChangeRecord>[] sectionRecords = new List[16];
BlockChangeRecord[] blockChangeRecord = wrapper.read(Type.BLOCK_CHANGE_RECORD_ARRAY);
for (BlockChangeRecord record : blockChangeRecord) {
int chunkY = record.getY() >> 4;
List<BlockChangeRecord> list = sectionRecords[chunkY];
if (list == null) {
sectionRecords[chunkY] = (list = new ArrayList<>());
}
// Absolute y -> relative chunk section y
int blockId = protocol.getMappingData().getNewBlockStateId(record.getBlockId());
list.add(new BlockChangeRecord1_16_2(record.getSectionX(), record.getSectionY(), record.getSectionZ(), blockId));
}
// Now send separate packets for the different chunk sections
for (int chunkY = 0; chunkY < sectionRecords.length; chunkY++) {
List<BlockChangeRecord> sectionRecord = sectionRecords[chunkY];
if (sectionRecord == null)
continue;
PacketWrapper newPacket = wrapper.create(ClientboundPackets1_16_2.MULTI_BLOCK_CHANGE);
newPacket.write(Type.LONG, chunkPosition | (chunkY & 0xFFFFFL));
// Ignore light updates
newPacket.write(Type.BOOLEAN, false);
newPacket.write(Type.VAR_LONG_BLOCK_CHANGE_RECORD_ARRAY, sectionRecord.toArray(EMPTY_RECORDS));
newPacket.send(Protocol1_16_2To1_16_1.class);
}
});
}
});
blockRewriter.registerEffect(ClientboundPackets1_16.EFFECT, 1010, 2001);
}
use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.
the class Chunk1_16_2Type method read.
@Override
public Chunk read(ByteBuf input) throws Exception {
int chunkX = input.readInt();
int chunkZ = input.readInt();
boolean fullChunk = input.readBoolean();
int primaryBitmask = Type.VAR_INT.readPrimitive(input);
CompoundTag heightMap = Type.NBT.read(input);
int[] biomeData = null;
if (fullChunk) {
biomeData = Type.VAR_INT_ARRAY_PRIMITIVE.read(input);
}
// 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, false, primaryBitmask, sections, biomeData, heightMap, nbtData);
}
use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.
the class Chunk1_16_2Type method write.
@Override
public void write(ByteBuf output, Chunk chunk) throws Exception {
output.writeInt(chunk.getX());
output.writeInt(chunk.getZ());
output.writeBoolean(chunk.isFullChunk());
Type.VAR_INT.writePrimitive(output, chunk.getBitmask());
Type.NBT.write(output, chunk.getHeightMap());
// Write biome data
if (chunk.isBiomeData()) {
Type.VAR_INT_ARRAY_PRIMITIVE.write(output, chunk.getBiomeData());
}
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;
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_15To1_14_4 protocol) {
BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14);
blockRewriter.registerBlockAction(ClientboundPackets1_14.BLOCK_ACTION);
blockRewriter.registerBlockChange(ClientboundPackets1_14.BLOCK_CHANGE);
blockRewriter.registerMultiBlockChange(ClientboundPackets1_14.MULTI_BLOCK_CHANGE);
blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_14.ACKNOWLEDGE_PLAYER_DIGGING);
protocol.registerClientbound(ClientboundPackets1_14.CHUNK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Chunk chunk = wrapper.read(new Chunk1_14Type());
wrapper.write(new Chunk1_15Type(), chunk);
if (chunk.isFullChunk()) {
int[] biomeData = chunk.getBiomeData();
int[] newBiomeData = new int[1024];
if (biomeData != null) {
// Now in 4x4x4 areas - take the biome of each "middle"
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
int x = (j << 2) + 2;
int z = (i << 2) + 2;
int oldIndex = (z << 4 | x);
newBiomeData[i << 2 | j] = biomeData[oldIndex];
}
}
// ... and copy it to the new y layers
for (int i = 1; i < 64; ++i) {
System.arraycopy(newBiomeData, 0, newBiomeData, i * 16, 16);
}
}
chunk.setBiomeData(newBiomeData);
}
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);
int newId = protocol.getMappingData().getNewBlockStateId(old);
section.setPaletteEntry(i, newId);
}
}
}
});
}
});
blockRewriter.registerEffect(ClientboundPackets1_14.EFFECT, 1010, 2001);
protocol.registerClientbound(ClientboundPackets1_14.SPAWN_PARTICLE, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Particle ID
map(Type.INT);
// 1 - Long Distance
map(Type.BOOLEAN);
// 2 - X
map(Type.FLOAT, Type.DOUBLE);
// 3 - Y
map(Type.FLOAT, Type.DOUBLE);
// 4 - Z
map(Type.FLOAT, Type.DOUBLE);
// 5 - Offset X
map(Type.FLOAT);
// 6 - Offset Y
map(Type.FLOAT);
// 7 - Offset Z
map(Type.FLOAT);
// 8 - Particle Data
map(Type.FLOAT);
// 9 - Particle Count
map(Type.INT);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int id = wrapper.get(Type.INT, 0);
if (id == 3 || id == 23) {
int data = wrapper.passthrough(Type.VAR_INT);
wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data));
} else if (id == 32) {
protocol.getItemRewriter().handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
}
}
});
}
});
}
Aggregations