From 2650e7ae446e727d55ee33defa9e5b2e1b50aa4d Mon Sep 17 00:00:00 2001 From: FlorianMichael <60033407+FlorianMichael@users.noreply.github.com> Date: Fri, 20 Oct 2023 12:05:42 +0200 Subject: [PATCH] Updated chunk reading and writing to work with Via again --- .../packets/WorldPackets.java | 22 +++++++++---------- ..._6_10Type.java => BulkChunkType1_7_6.java} | 20 ++++++----------- ...k1_7_6_10Type.java => ChunkType1_7_6.java} | 15 +++++++------ .../packets/WorldPackets.java | 18 ++++++++------- 4 files changed, 35 insertions(+), 40 deletions(-) rename common/src/main/java/com/viaversion/viarewind/protocol/protocol1_7_6_10to1_8/types/chunk/{ChunkBulk1_7_6_10Type.java => BulkChunkType1_7_6.java} (72%) rename common/src/main/java/com/viaversion/viarewind/protocol/protocol1_7_6_10to1_8/types/chunk/{Chunk1_7_6_10Type.java => ChunkType1_7_6.java} (90%) diff --git a/common/src/main/java/com/viaversion/viarewind/protocol/protocol1_7_6_10to1_8/packets/WorldPackets.java b/common/src/main/java/com/viaversion/viarewind/protocol/protocol1_7_6_10to1_8/packets/WorldPackets.java index 2c68168e4..0ee2bb694 100644 --- a/common/src/main/java/com/viaversion/viarewind/protocol/protocol1_7_6_10to1_8/packets/WorldPackets.java +++ b/common/src/main/java/com/viaversion/viarewind/protocol/protocol1_7_6_10to1_8/packets/WorldPackets.java @@ -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; @@ -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; @@ -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() { @@ -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() { @@ -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); } @@ -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); } @@ -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); }); diff --git a/common/src/main/java/com/viaversion/viarewind/protocol/protocol1_7_6_10to1_8/types/chunk/ChunkBulk1_7_6_10Type.java b/common/src/main/java/com/viaversion/viarewind/protocol/protocol1_7_6_10to1_8/types/chunk/BulkChunkType1_7_6.java similarity index 72% rename from common/src/main/java/com/viaversion/viarewind/protocol/protocol1_7_6_10to1_8/types/chunk/ChunkBulk1_7_6_10Type.java rename to common/src/main/java/com/viaversion/viarewind/protocol/protocol1_7_6_10to1_8/types/chunk/BulkChunkType1_7_6.java index 60e732d85..a8b4dcf6d 100644 --- a/common/src/main/java/com/viaversion/viarewind/protocol/protocol1_7_6_10to1_8/types/chunk/ChunkBulk1_7_6_10Type.java +++ b/common/src/main/java/com/viaversion/viarewind/protocol/protocol1_7_6_10to1_8/types/chunk/BulkChunkType1_7_6.java @@ -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 { +public class BulkChunkType1_7_6 extends Type { - 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 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]; @@ -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 chunkData = Chunk1_7_6_10Type.serialize(chunk); + final Pair chunkData = ChunkType1_7_6.serialize(chunk); output.write(chunkData.key()); chunkX[i] = chunk.getX(); chunkZ[i] = chunk.getZ(); diff --git a/common/src/main/java/com/viaversion/viarewind/protocol/protocol1_7_6_10to1_8/types/chunk/Chunk1_7_6_10Type.java b/common/src/main/java/com/viaversion/viarewind/protocol/protocol1_7_6_10to1_8/types/chunk/ChunkType1_7_6.java similarity index 90% rename from common/src/main/java/com/viaversion/viarewind/protocol/protocol1_7_6_10to1_8/types/chunk/Chunk1_7_6_10Type.java rename to common/src/main/java/com/viaversion/viarewind/protocol/protocol1_7_6_10to1_8/types/chunk/ChunkType1_7_6.java index fa68e5cb6..55263a298 100644 --- a/common/src/main/java/com/viaversion/viarewind/protocol/protocol1_7_6_10to1_8/types/chunk/Chunk1_7_6_10Type.java +++ b/common/src/main/java/com/viaversion/viarewind/protocol/protocol1_7_6_10to1_8/types/chunk/ChunkType1_7_6.java @@ -20,8 +20,7 @@ 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; @@ -29,19 +28,21 @@ import java.io.IOException; import java.util.zip.Deflater; -public class Chunk1_7_6_10Type extends PartialType { +public class ChunkType1_7_6 extends Type { - 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 chunkData = serialize(chunk); final byte[] data = chunkData.key(); final short additionalBitMask = chunkData.value(); diff --git a/common/src/main/java/com/viaversion/viarewind/protocol/protocol1_8to1_9/packets/WorldPackets.java b/common/src/main/java/com/viaversion/viarewind/protocol/protocol1_8to1_9/packets/WorldPackets.java index a60225195..03a4a7b2d 100644 --- a/common/src/main/java/com/viaversion/viarewind/protocol/protocol1_8to1_9/packets/WorldPackets.java +++ b/common/src/main/java/com/viaversion/viarewind/protocol/protocol1_8to1_9/packets/WorldPackets.java @@ -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<>())); }); } }); @@ -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; @@ -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; @@ -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 -> {