Skip to content

Commit

Permalink
Updated chunk reading and writing to work with Via again
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Oct 20, 2023
1 parent 5ed1d3a commit 2650e7a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

import com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.Protocol1_7_6_10To1_8;
import com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.storage.WorldBorderEmulator;
import com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.types.chunk.Chunk1_7_6_10Type;
import com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.types.chunk.ChunkBulk1_7_6_10Type;
import com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.types.chunk.ChunkType1_7_6;
import com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.types.chunk.BulkChunkType1_7_6;
import com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.model.ParticleIndex1_7_6_10;
import com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.types.Types1_7_6_10;
import com.viaversion.viarewind.utils.ChatUtil;
Expand All @@ -33,7 +33,7 @@
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.CustomByteType;
import com.viaversion.viaversion.api.type.types.FixedByteArrayType;
import com.viaversion.viaversion.api.type.types.chunk.BulkChunkType1_8;
import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_8;
import com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8;
Expand All @@ -56,10 +56,10 @@ public static void rewriteBlockIds(final Protocol1_7_6_10To1_8 protocol, final C
public static void register(Protocol1_7_6_10To1_8 protocol) {
protocol.registerClientbound(ClientboundPackets1_8.CHUNK_DATA, wrapper -> {
final ClientWorld world = wrapper.user().get(ClientWorld.class);
final Chunk chunk = wrapper.read(new ChunkType1_8(world));
final Chunk chunk = wrapper.read(ChunkType1_8.forEnvironment(world.getEnvironment()));
rewriteBlockIds(protocol, chunk);

wrapper.write(new Chunk1_7_6_10Type(world), chunk);
wrapper.write(ChunkType1_7_6.TYPE, chunk);
});

protocol.registerClientbound(ClientboundPackets1_8.MULTI_BLOCK_CHANGE, new PacketHandlers() {
Expand Down Expand Up @@ -116,14 +116,12 @@ public void register() {
});

protocol.registerClientbound(ClientboundPackets1_8.MAP_BULK_CHUNK, wrapper -> {
final ClientWorld world = wrapper.user().get(ClientWorld.class);

final Chunk[] chunks = wrapper.read(new BulkChunkType1_8(world));
final Chunk[] chunks = wrapper.read(BulkChunkType1_8.TYPE);
for (Chunk chunk : chunks) {
rewriteBlockIds(protocol, chunk);
}

wrapper.write(new ChunkBulk1_7_6_10Type(world), chunks);
wrapper.write(BulkChunkType1_7_6.TYPE, chunks);
});

protocol.registerClientbound(ClientboundPackets1_8.EFFECT, new PacketHandlers() {
Expand Down Expand Up @@ -237,7 +235,7 @@ public void register() {
final PacketWrapper mapData = PacketWrapper.create(ClientboundPackets1_8.MAP_DATA, wrapper.user());
mapData.write(Type.VAR_INT, id); // map id
mapData.write(Type.SHORT, (short) columnData.length); // data length
mapData.write(new CustomByteType(columnData.length), columnData); // data
mapData.write(new FixedByteArrayType(columnData.length), columnData); // data

mapData.send(Protocol1_7_6_10To1_8.class, true);
}
Expand All @@ -255,7 +253,7 @@ public void register() {
final PacketWrapper mapData = PacketWrapper.create(ClientboundPackets1_8.MAP_DATA, wrapper.user());
mapData.write(Type.VAR_INT, id); // map id
mapData.write(Type.SHORT, (short) iconData.length); // data length
mapData.write(new CustomByteType(iconData.length), iconData); // data
mapData.write(new FixedByteArrayType(iconData.length), iconData); // data

mapData.send(Protocol1_7_6_10To1_8.class, true);
}
Expand All @@ -264,7 +262,7 @@ public void register() {
final PacketWrapper mapData = PacketWrapper.create(ClientboundPackets1_8.MAP_DATA, wrapper.user());
mapData.write(Type.VAR_INT, id); // map id
mapData.write(Type.SHORT, (short) 2); // data length
mapData.write(new CustomByteType(2), new byte[]{ 2, scale }); // data
mapData.write(new FixedByteArrayType(2), new byte[]{ 2, scale }); // data

mapData.send(Protocol1_7_6_10To1_8.class, true);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
package com.viaversion.viarewind.protocol.protocol1_7_6_10to1_8.types.chunk;

import com.viaversion.viaversion.api.minecraft.chunks.Chunk;
import com.viaversion.viaversion.api.type.PartialType;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.chunk.BaseChunkBulkType;
import com.viaversion.viaversion.api.minecraft.ClientWorld;
import com.viaversion.viaversion.util.Pair;
import io.netty.buffer.ByteBuf;

import java.io.ByteArrayOutputStream;
import java.util.zip.Deflater;

public class ChunkBulk1_7_6_10Type extends PartialType<Chunk[], ClientWorld> {
public class BulkChunkType1_7_6 extends Type<Chunk[]> {

public ChunkBulk1_7_6_10Type(final ClientWorld clientWorld) {
super(clientWorld, Chunk[].class);
}
public final static BulkChunkType1_7_6 TYPE = new BulkChunkType1_7_6();

@Override
public Class<? extends Type> getBaseClass() {
return BaseChunkBulkType.class;
public BulkChunkType1_7_6() {
super(Chunk[].class);
}

@Override
public Chunk[] read(ByteBuf byteBuf, ClientWorld clientWorld) throws Exception {
public Chunk[] read(ByteBuf byteBuf) throws Exception {
throw new UnsupportedOperationException(); // Not needed, see https://github.com/ViaVersion/ViaLegacy/blob/main/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/ChunkBulk1_7_6Type.java
}

@Override
public void write(ByteBuf byteBuf, ClientWorld clientWorld, Chunk[] chunks) throws Exception {
public void write(ByteBuf byteBuf, Chunk[] chunks) throws Exception {
final int chunkCount = chunks.length;
final ByteArrayOutputStream output = new ByteArrayOutputStream();
final int[] chunkX = new int[chunkCount];
Expand All @@ -38,7 +32,7 @@ public void write(ByteBuf byteBuf, ClientWorld clientWorld, Chunk[] chunks) thro

for (int i = 0; i < chunkCount; i++) {
final Chunk chunk = chunks[i];
final Pair<byte[], Short> chunkData = Chunk1_7_6_10Type.serialize(chunk);
final Pair<byte[], Short> chunkData = ChunkType1_7_6.serialize(chunk);
output.write(chunkData.key());
chunkX[i] = chunk.getX();
chunkZ[i] = chunk.getZ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,29 @@

import com.viaversion.viarewind.api.minecraft.ExtendedBlockStorage;
import com.viaversion.viaversion.api.minecraft.chunks.*;
import com.viaversion.viaversion.api.type.PartialType;
import com.viaversion.viaversion.api.minecraft.ClientWorld;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.util.Pair;
import io.netty.buffer.ByteBuf;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;

public class Chunk1_7_6_10Type extends PartialType<Chunk, ClientWorld> {
public class ChunkType1_7_6 extends Type<Chunk> {

public Chunk1_7_6_10Type(ClientWorld param) {
super(param, Chunk.class);
public final static ChunkType1_7_6 TYPE = new ChunkType1_7_6();

public ChunkType1_7_6() {
super(Chunk.class);
}

@Override
public Chunk read(ByteBuf byteBuf, ClientWorld clientWorld) throws Exception {
public Chunk read(ByteBuf byteBuf) throws Exception {
throw new UnsupportedOperationException(); // Not needed, see https://github.com/ViaVersion/ViaLegacy/blob/main/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/Chunk1_7_6Type.java
}

@Override
public void write(ByteBuf output, ClientWorld clientWorld, Chunk chunk) throws Exception {
public void write(ByteBuf output, Chunk chunk) throws Exception {
final Pair<byte[], Short> chunkData = serialize(chunk);
final byte[] data = chunkData.key();
final short additionalBitMask = chunkData.value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,12 @@ public void register() {
@Override
public void register() {
handler(packetWrapper -> {
int chunkX = packetWrapper.read(Type.INT);
int chunkZ = packetWrapper.read(Type.INT);
ClientWorld world = packetWrapper.user().get(ClientWorld.class);
packetWrapper.write(new ChunkType1_8(world), new BaseChunk(chunkX, chunkZ, true, false, 0, new ChunkSection[16], null, new ArrayList<>()));
final Environment environment = packetWrapper.user().get(ClientWorld.class).getEnvironment();

final int chunkX = packetWrapper.read(Type.INT);
final int chunkZ = packetWrapper.read(Type.INT);

packetWrapper.write(ChunkType1_8.forEnvironment(environment), new BaseChunk(chunkX, chunkZ, true, false, 0, new ChunkSection[16], null, new ArrayList<>()));
});
}
});
Expand All @@ -182,9 +184,9 @@ public void register() {
@Override
public void register() {
handler(packetWrapper -> {
ClientWorld world = packetWrapper.user().get(ClientWorld.class);
final Environment environment = packetWrapper.user().get(ClientWorld.class).getEnvironment();

Chunk chunk = packetWrapper.read(new ChunkType1_9_1(world));
Chunk chunk = packetWrapper.read(ChunkType1_9_1.forEnvironment(environment));

for (ChunkSection section : chunk.getSections()) {
if (section == null) continue;
Expand All @@ -197,7 +199,7 @@ public void register() {
}

if (chunk.isFullChunk() && chunk.getBitmask() == 0) { //This would be an unload packet for 1.8 clients. Just set one air section
boolean skylight = world.getEnvironment() == Environment.NORMAL;
boolean skylight = environment == Environment.NORMAL;
ChunkSection[] sections = new ChunkSection[16];
ChunkSection section = new ChunkSectionImpl(true);
sections[0] = section;
Expand All @@ -206,7 +208,7 @@ public void register() {
chunk = new BaseChunk(chunk.getX(), chunk.getZ(), true, false, 1, sections, chunk.getBiomeData(), chunk.getBlockEntities());
}

packetWrapper.write(new ChunkType1_8(world), chunk);
packetWrapper.write(ChunkType1_8.forEnvironment(environment), chunk);

final UserConnection user = packetWrapper.user();
chunk.getBlockEntities().forEach(nbt -> {
Expand Down

0 comments on commit 2650e7a

Please sign in to comment.