From 8828e45afe19a8e4700d038e5fc8dc2308beaa90 Mon Sep 17 00:00:00 2001 From: EnZaXD <60033407+FlorianMichael@users.noreply.github.com> Date: Fri, 20 Oct 2023 12:25:07 +0200 Subject: [PATCH] Updated Via API usage * Update chunk system to work with latest Via again * Move Chunk type instances into protocol versioned type classes * Deleted unused type instances and cleaned up some protocols --- .../types/Typesb1_8_0_1.java | 2 - .../Protocolc0_30toc0_30cpe.java | 6 +-- .../types/ChunkType1_1.java | 10 +---- .../Protocol1_3_1_2to1_2_4_5.java | 10 ++--- .../types/ChunkType1_2_4.java | 18 +++------ .../types/ItemType.java | 9 ++--- .../types/Types1_2_4.java | 5 +-- .../types/Types1_3_1.java | 1 - .../Protocol1_4_6_7to1_4_4_5.java | 11 +++--- ...Type1_4_4.java => BulkChunkType1_4_4.java} | 13 ++----- .../types/Types1_4_4.java | 24 ++++++++++++ .../Protocol1_7_2_5to1_6_4.java | 6 +-- .../Protocol1_8to1_7_6_10.java | 13 ++++--- ...Type1_7_6.java => BulkChunkType1_7_6.java} | 34 ++++++----------- .../types/ChunkType1_7_6.java | 38 ++++++++----------- .../protocol1_8to1_7_6_10/types/ItemType.java | 9 ++--- .../types/Types1_7_6.java | 15 ++++++-- 17 files changed, 104 insertions(+), 120 deletions(-) rename src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_6_7to1_4_4_5/types/{ChunkBulkType1_4_4.java => BulkChunkType1_4_4.java} (70%) create mode 100644 src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_6_7to1_4_4_5/types/Types1_4_4.java rename src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/{ChunkBulkType1_7_6.java => BulkChunkType1_7_6.java} (81%) diff --git a/src/main/java/net/raphimc/vialegacy/protocols/beta/protocol1_0_0_1tob1_8_0_1/types/Typesb1_8_0_1.java b/src/main/java/net/raphimc/vialegacy/protocols/beta/protocol1_0_0_1tob1_8_0_1/types/Typesb1_8_0_1.java index 295cb06..42071a9 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/beta/protocol1_0_0_1tob1_8_0_1/types/Typesb1_8_0_1.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/beta/protocol1_0_0_1tob1_8_0_1/types/Typesb1_8_0_1.java @@ -19,11 +19,9 @@ import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.type.Type; -import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ItemArrayType; public class Typesb1_8_0_1 { public static final Type CREATIVE_ITEM = new NbtLessItemType(); - public static final Type CREATIVE_ITEM_ARRAY = new ItemArrayType<>(CREATIVE_ITEM); } diff --git a/src/main/java/net/raphimc/vialegacy/protocols/classic/protocolc0_28_30toc0_28_30cpe/Protocolc0_30toc0_30cpe.java b/src/main/java/net/raphimc/vialegacy/protocols/classic/protocolc0_28_30toc0_28_30cpe/Protocolc0_30toc0_30cpe.java index 6a6ef01..759060f 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/classic/protocolc0_28_30toc0_28_30cpe/Protocolc0_30toc0_30cpe.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/classic/protocolc0_28_30toc0_28_30cpe/Protocolc0_30toc0_30cpe.java @@ -26,7 +26,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 net.raphimc.vialegacy.ViaLegacy; import net.raphimc.vialegacy.api.data.BlockList1_6; import net.raphimc.vialegacy.api.model.ChunkCoord; @@ -220,8 +220,8 @@ public void register() { final ClassicLevel level = levelStorage.getClassicLevel(); final int count = wrapper.read(Type.UNSIGNED_BYTE) + 1; // count - final byte[] indices = wrapper.read(new CustomByteType(1024)); // indices - final byte[] blocks = wrapper.read(new CustomByteType(256)); // blocks + final byte[] indices = wrapper.read(new FixedByteArrayType(1024)); // indices + final byte[] blocks = wrapper.read(new FixedByteArrayType(256)); // blocks if (wrapper.user().getProtocolInfo().getPipeline().contains(Protocola1_0_16_2toa1_0_15.class)) { final Map> records = new HashMap<>(); diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_2_1_3to1_1/types/ChunkType1_1.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_2_1_3to1_1/types/ChunkType1_1.java index 9c35631..58a9d6d 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_2_1_3to1_1/types/ChunkType1_1.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_2_1_3to1_1/types/ChunkType1_1.java @@ -20,8 +20,7 @@ import com.viaversion.viaversion.api.minecraft.Position; import com.viaversion.viaversion.api.minecraft.chunks.*; import com.viaversion.viaversion.api.type.Type; -import com.viaversion.viaversion.api.type.types.CustomByteType; -import com.viaversion.viaversion.api.type.types.chunk.BaseChunkType; +import com.viaversion.viaversion.api.type.types.FixedByteArrayType; import io.netty.buffer.ByteBuf; import net.raphimc.vialegacy.api.model.IdAndData; import net.raphimc.vialegacy.protocols.release.protocol1_2_1_3to1_1.chunks.NibbleArray1_1; @@ -38,11 +37,6 @@ public ChunkType1_1() { super(Chunk.class); } - @Override - public Class getBaseClass() { - return BaseChunkType.class; - } - @Override public Chunk read(ByteBuf byteBuf) throws Exception { final int xPosition = byteBuf.readInt(); @@ -52,7 +46,7 @@ public Chunk read(ByteBuf byteBuf) throws Exception { final int ySize = byteBuf.readUnsignedByte() + 1; final int zSize = byteBuf.readUnsignedByte() + 1; final int chunkSize = byteBuf.readInt(); - final byte[] compressedData = new CustomByteType(chunkSize).read(byteBuf); + final byte[] compressedData = new FixedByteArrayType(chunkSize).read(byteBuf); final byte[] uncompressedData = new byte[(xSize * ySize * zSize * 5) / 2]; final Inflater inflater = new Inflater(); inflater.setInput(compressedData); diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_3_1_2to1_2_4_5/Protocol1_3_1_2to1_2_4_5.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_3_1_2to1_2_4_5/Protocol1_3_1_2to1_2_4_5.java index 6f334b3..433646b 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_3_1_2to1_2_4_5/Protocol1_3_1_2to1_2_4_5.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_3_1_2to1_2_4_5/Protocol1_3_1_2to1_2_4_5.java @@ -459,7 +459,7 @@ public void register() { if (!load) { final Chunk chunk = new BaseChunk(chunkX, chunkZ, true, false, 0, new ChunkSection[16], null, new ArrayList<>()); - wrapper.write(new ChunkType1_7_6(wrapper.user().get(ClientWorld.class)), chunk); + wrapper.write(Types1_7_6.getChunk(wrapper.user().get(ClientWorld.class).getEnvironment()), chunk); } else { wrapper.cancel(); } @@ -470,7 +470,7 @@ public void register() { @Override public void register() { handler(wrapper -> { - final ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); + final Environment environment = wrapper.user().get(ClientWorld.class).getEnvironment(); Chunk chunk = wrapper.read(Types1_2_4.CHUNK); wrapper.user().get(ChestStateTracker.class).unload(chunk.getX(), chunk.getZ()); @@ -481,7 +481,7 @@ public void register() { for (int i = 0; i < chunk.getSections().length; i++) { final ChunkSection section = chunk.getSections()[i] = new ChunkSectionImpl(true); section.palette(PaletteType.BLOCKS).addId(0); - if (clientWorld.getEnvironment() == Environment.NORMAL) { + if (environment == Environment.NORMAL) { final byte[] skyLight = new byte[2048]; Arrays.fill(skyLight, (byte) 255); section.getLight().setSkyLight(skyLight); @@ -489,14 +489,14 @@ public void register() { } } - if (clientWorld.getEnvironment() != Environment.NORMAL) { + if (environment != Environment.NORMAL) { for (ChunkSection section : chunk.getSections()) { if (section != null) { section.getLight().setSkyLight(null); } } } - wrapper.write(new ChunkType1_7_6(clientWorld), chunk); + wrapper.write(Types1_7_6.getChunk(environment), chunk); }); } }); diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_3_1_2to1_2_4_5/types/ChunkType1_2_4.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_3_1_2to1_2_4_5/types/ChunkType1_2_4.java index 82774eb..0dfea0b 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_3_1_2to1_2_4_5/types/ChunkType1_2_4.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_3_1_2to1_2_4_5/types/ChunkType1_2_4.java @@ -17,8 +17,6 @@ */ package net.raphimc.vialegacy.protocols.release.protocol1_3_1_2to1_2_4_5.types; -import com.viaversion.viaversion.api.minecraft.ClientWorld; -import com.viaversion.viaversion.api.minecraft.Environment; import com.viaversion.viaversion.api.minecraft.chunks.Chunk; import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection; import io.netty.buffer.ByteBuf; @@ -26,35 +24,29 @@ public class ChunkType1_2_4 extends ChunkType1_7_6 { - private static final ClientWorld OVERWORLD = new ClientWorld(null); - - static { - OVERWORLD.setEnvironment(Environment.NORMAL.id()); - } - public ChunkType1_2_4() { - super(OVERWORLD); + super(true); } @Override - protected void readUnusedInt(ByteBuf byteBuf, ClientWorld clientWorld) { + protected void readUnusedInt(ByteBuf byteBuf) { byteBuf.readInt(); } @Override - protected void writeUnusedInt(ByteBuf byteBuf, ClientWorld clientWorld, Chunk chunk) { + protected void writeUnusedInt(ByteBuf byteBuf, Chunk chunk) { byteBuf.writeInt(0); } @Override - public void write(ByteBuf byteBuf, ClientWorld clientWorld, Chunk chunk) throws Exception { + public void write(ByteBuf byteBuf, Chunk chunk) throws Exception { for (ChunkSection section : chunk.getSections()) { if (section != null && !section.getLight().hasSkyLight()) { throw new IllegalStateException("Chunk section does not have skylight"); } } - super.write(byteBuf, clientWorld, chunk); + super.write(byteBuf, chunk); } } diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_3_1_2to1_2_4_5/types/ItemType.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_3_1_2to1_2_4_5/types/ItemType.java index 9b704ca..a4246a5 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_3_1_2to1_2_4_5/types/ItemType.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_3_1_2to1_2_4_5/types/ItemType.java @@ -26,11 +26,8 @@ public class ItemType extends Type { - private final boolean compressed; - - public ItemType(boolean compressed) { + public ItemType() { super(Item.class); - this.compressed = compressed; } public Item read(ByteBuf buffer) throws Exception { @@ -43,7 +40,7 @@ public Item read(ByteBuf buffer) throws Exception { item.setAmount(buffer.readByte()); item.setData(buffer.readShort()); if (NbtItemList.hasNbt(id)) { - item.setTag((this.compressed ? Types1_7_6.COMPRESSED_NBT : Types1_7_6.NBT).read(buffer)); + item.setTag(Types1_7_6.COMPRESSED_NBT.read(buffer)); } return item; } @@ -57,7 +54,7 @@ public void write(ByteBuf buffer, Item item) throws Exception { buffer.writeByte(item.amount()); buffer.writeShort(item.data()); if (NbtItemList.hasNbt(item.identifier())) { - (this.compressed ? Types1_7_6.COMPRESSED_NBT : Types1_7_6.NBT).write(buffer, item.tag()); + Types1_7_6.COMPRESSED_NBT.write(buffer, item.tag()); } } } diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_3_1_2to1_2_4_5/types/Types1_2_4.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_3_1_2to1_2_4_5/types/Types1_2_4.java index 957fca2..1745cef 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_3_1_2to1_2_4_5/types/Types1_2_4.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_3_1_2to1_2_4_5/types/Types1_2_4.java @@ -24,10 +24,7 @@ public class Types1_2_4 { - public static final Type ITEM = new ItemType(false); - public static final Type COMPRESSED_NBT_ITEM = new ItemType(true); - - public static final Type ITEM_ARRAY = new ItemArrayType<>(ITEM); + public static final Type COMPRESSED_NBT_ITEM = new ItemType(); public static final Type COMPRESSED_NBT_ITEM_ARRAY = new ItemArrayType<>(COMPRESSED_NBT_ITEM); public static final Type CHUNK = new ChunkType1_2_4(); diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_2to1_3_1_2/types/Types1_3_1.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_2to1_3_1_2/types/Types1_3_1.java index 78bc4b8..b327ba2 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_2to1_3_1_2/types/Types1_3_1.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_2to1_3_1_2/types/Types1_3_1.java @@ -28,7 +28,6 @@ public class Types1_3_1 { public static final Type NBTLESS_ITEM = new NbtLessItemType(); - public static final Type NBTLESS_ITEM_ARRAY = new ItemArrayType<>(NBTLESS_ITEM); public static final Type METADATA = new MetadataType(); public static final Type> METADATA_LIST = new MetaListType(METADATA); diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_6_7to1_4_4_5/Protocol1_4_6_7to1_4_4_5.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_6_7to1_4_4_5/Protocol1_4_6_7to1_4_4_5.java index 2c7a121..615cdcf 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_6_7to1_4_4_5/Protocol1_4_6_7to1_4_4_5.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_6_7to1_4_4_5/Protocol1_4_6_7to1_4_4_5.java @@ -20,6 +20,7 @@ import com.google.common.collect.Lists; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.ClientWorld; +import com.viaversion.viaversion.api.minecraft.Environment; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_10; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.minecraft.metadata.Metadata; @@ -30,13 +31,14 @@ import net.raphimc.vialegacy.api.remapper.LegacyItemRewriter; import net.raphimc.vialegacy.api.splitter.PreNettySplitter; import net.raphimc.vialegacy.protocols.release.protocol1_4_6_7to1_4_4_5.rewriter.ItemRewriter; -import net.raphimc.vialegacy.protocols.release.protocol1_4_6_7to1_4_4_5.types.ChunkBulkType1_4_4; +import net.raphimc.vialegacy.protocols.release.protocol1_4_6_7to1_4_4_5.types.BulkChunkType1_4_4; +import net.raphimc.vialegacy.protocols.release.protocol1_4_6_7to1_4_4_5.types.Types1_4_4; import net.raphimc.vialegacy.protocols.release.protocol1_5_0_1to1_4_6_7.ClientboundPackets1_4_6; import net.raphimc.vialegacy.protocols.release.protocol1_6_1to1_5_2.ServerboundPackets1_5_2; import net.raphimc.vialegacy.protocols.release.protocol1_7_2_5to1_6_4.types.MetaType1_6_4; import net.raphimc.vialegacy.protocols.release.protocol1_7_2_5to1_6_4.types.Types1_6_4; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.metadata.MetaIndex1_8to1_7_6; -import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkBulkType1_7_6; +import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.BulkChunkType1_7_6; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Types1_7_6; public class Protocol1_4_6_7to1_4_4_5 extends StatelessProtocol { @@ -127,10 +129,7 @@ public void register() { this.registerClientbound(ClientboundPackets1_4_4.MAP_BULK_CHUNK, new PacketHandlers() { @Override public void register() { - handler(wrapper -> { - final ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); - wrapper.write(new ChunkBulkType1_7_6(clientWorld), wrapper.read(new ChunkBulkType1_4_4(clientWorld))); - }); + map(Types1_7_6.CHUNK_BULK, Types1_4_4.CHUNK_BULK); } }); diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_6_7to1_4_4_5/types/ChunkBulkType1_4_4.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_6_7to1_4_4_5/types/BulkChunkType1_4_4.java similarity index 70% rename from src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_6_7to1_4_4_5/types/ChunkBulkType1_4_4.java rename to src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_6_7to1_4_4_5/types/BulkChunkType1_4_4.java index ea3f068..d5f6d16 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_6_7to1_4_4_5/types/ChunkBulkType1_4_4.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_6_7to1_4_4_5/types/BulkChunkType1_4_4.java @@ -17,23 +17,18 @@ */ package net.raphimc.vialegacy.protocols.release.protocol1_4_6_7to1_4_4_5.types; -import com.viaversion.viaversion.api.minecraft.ClientWorld; import io.netty.buffer.ByteBuf; -import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkBulkType1_7_6; +import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.BulkChunkType1_7_6; -public class ChunkBulkType1_4_4 extends ChunkBulkType1_7_6 { - - public ChunkBulkType1_4_4(ClientWorld clientWorld) { - super(clientWorld); - } +public class BulkChunkType1_4_4 extends BulkChunkType1_7_6 { @Override - protected boolean readHasSkyLight(ByteBuf byteBuf, ClientWorld clientWorld) { + protected boolean readHasSkyLight(ByteBuf byteBuf) { return true; } @Override - protected void writeHasSkyLight(ByteBuf byteBuf, ClientWorld clientWorld, boolean hasSkyLight) { + protected void writeHasSkyLight(ByteBuf byteBuf, boolean hasSkyLight) { } } diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_6_7to1_4_4_5/types/Types1_4_4.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_6_7to1_4_4_5/types/Types1_4_4.java new file mode 100644 index 0000000..a34d49e --- /dev/null +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_4_6_7to1_4_4_5/types/Types1_4_4.java @@ -0,0 +1,24 @@ +/* + * This file is part of ViaLegacy - https://github.com/RaphiMC/ViaLegacy + * Copyright (C) 2023 RK_01/RaphiMC and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.raphimc.vialegacy.protocols.release.protocol1_4_6_7to1_4_4_5.types; + +public class Types1_4_4 { + + public static final BulkChunkType1_4_4 CHUNK_BULK = new BulkChunkType1_4_4(); + +} diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_7_2_5to1_6_4/Protocol1_7_2_5to1_6_4.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_7_2_5to1_6_4/Protocol1_7_2_5to1_6_4.java index 785137e..19897df 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_7_2_5to1_6_4/Protocol1_7_2_5to1_6_4.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_7_2_5to1_6_4/Protocol1_7_2_5to1_6_4.java @@ -67,7 +67,7 @@ import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.model.GameProfile; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.providers.GameProfileFetcher; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkType1_7_6; -import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkBulkType1_7_6; +import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.BulkChunkType1_7_6; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.MetaType1_7_6; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Types1_7_6; @@ -421,7 +421,7 @@ public void register() { @Override public void register() { handler(wrapper -> { - final Chunk chunk = wrapper.passthrough(new ChunkType1_7_6(wrapper.user().get(ClientWorld.class))); + final Chunk chunk = wrapper.passthrough(Types1_7_6.getChunk(wrapper.user().get(ClientWorld.class).getEnvironment())); wrapper.user().get(ChunkTracker.class).trackAndRemap(chunk); }); } @@ -486,7 +486,7 @@ public void register() { @Override public void register() { handler(wrapper -> { - final Chunk[] chunks = wrapper.passthrough(new ChunkBulkType1_7_6(wrapper.user().get(ClientWorld.class))); + final Chunk[] chunks = wrapper.passthrough(Types1_7_6.CHUNK_BULK); for (Chunk chunk : chunks) { wrapper.user().get(ChunkTracker.class).trackAndRemap(chunk); } diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/Protocol1_8to1_7_6_10.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/Protocol1_8to1_7_6_10.java index 8e17e6e..31d25c8 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/Protocol1_8to1_7_6_10.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/Protocol1_8to1_7_6_10.java @@ -23,6 +23,7 @@ import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.BlockChangeRecord; import com.viaversion.viaversion.api.minecraft.ClientWorld; +import com.viaversion.viaversion.api.minecraft.Environment; import com.viaversion.viaversion.api.minecraft.Position; import com.viaversion.viaversion.api.minecraft.chunks.Chunk; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_10; @@ -63,7 +64,7 @@ import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.rewriter.ItemRewriter; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.rewriter.TranslationRewriter; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.storage.*; -import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkBulkType1_7_6; +import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.BulkChunkType1_7_6; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.ChunkType1_7_6; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types.Types1_7_6; @@ -633,9 +634,11 @@ public void register() { @Override public void register() { handler(wrapper -> { - final Chunk chunk = wrapper.read(new ChunkType1_7_6(wrapper.user().get(ClientWorld.class))); + final Environment environment = wrapper.user().get(ClientWorld.class).getEnvironment(); + + final Chunk chunk = wrapper.read(Types1_7_6.getChunk(environment)); wrapper.user().get(ChunkTracker.class).trackAndRemap(chunk); - wrapper.write(new ChunkType1_8(wrapper.user().get(ClientWorld.class)), chunk); + wrapper.write(ChunkType1_8.forEnvironment(environment), chunk); }); } }); @@ -696,11 +699,11 @@ public void register() { @Override public void register() { handler(wrapper -> { - final Chunk[] chunks = wrapper.read(new ChunkBulkType1_7_6(wrapper.user().get(ClientWorld.class))); + final Chunk[] chunks = wrapper.read(Types1_7_6.CHUNK_BULK); for (Chunk chunk : chunks) { wrapper.user().get(ChunkTracker.class).trackAndRemap(chunk); } - wrapper.write(new BulkChunkType1_8(wrapper.user().get(ClientWorld.class)), chunks); + wrapper.write(BulkChunkType1_8.TYPE, chunks); }); } }); diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/ChunkBulkType1_7_6.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/BulkChunkType1_7_6.java similarity index 81% rename from src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/ChunkBulkType1_7_6.java rename to src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/BulkChunkType1_7_6.java index d7af4fd..683630c 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/ChunkBulkType1_7_6.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/BulkChunkType1_7_6.java @@ -17,12 +17,9 @@ */ package net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types; -import com.viaversion.viaversion.api.minecraft.ClientWorld; 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.CustomByteType; -import com.viaversion.viaversion.api.type.types.chunk.BaseChunkBulkType; +import com.viaversion.viaversion.api.type.types.FixedByteArrayType; import com.viaversion.viaversion.util.Pair; import io.netty.buffer.ByteBuf; @@ -32,25 +29,19 @@ import java.util.zip.Deflater; import java.util.zip.Inflater; -public class ChunkBulkType1_7_6 extends PartialType { +public class BulkChunkType1_7_6 extends Type { - public ChunkBulkType1_7_6(final ClientWorld clientWorld) { - super(clientWorld, Chunk[].class); - } - - @Override - public Class getBaseClass() { - return BaseChunkBulkType.class; + public BulkChunkType1_7_6() { + super(Chunk[].class); } /** * This method is here to allow overriding the code for 1.4.5 -{@literal >} 1.4.7 * - * @param byteBuf The buffer - * @param clientWorld The ClientWorld + * @param byteBuf The buffer * @return Read skylight array or not */ - protected boolean readHasSkyLight(final ByteBuf byteBuf, final ClientWorld clientWorld) { + protected boolean readHasSkyLight(final ByteBuf byteBuf) { return byteBuf.readBoolean(); } @@ -58,19 +49,18 @@ protected boolean readHasSkyLight(final ByteBuf byteBuf, final ClientWorld clien * This method is here to allow overriding the code for 1.4.5 -{@literal >} 1.4.7 * * @param byteBuf The buffer - * @param clientWorld The ClientWorld * @param hasSkyLight Has skylight */ - protected void writeHasSkyLight(final ByteBuf byteBuf, final ClientWorld clientWorld, final boolean hasSkyLight) { + protected void writeHasSkyLight(final ByteBuf byteBuf, final boolean hasSkyLight) { byteBuf.writeBoolean(hasSkyLight); } @Override - public Chunk[] read(ByteBuf byteBuf, ClientWorld clientWorld) throws Exception { + public Chunk[] read(ByteBuf byteBuf) throws Exception { final short chunkCount = byteBuf.readShort(); final int compressedSize = byteBuf.readInt(); - final boolean hasSkyLight = this.readHasSkyLight(byteBuf, clientWorld); - final byte[] data = new CustomByteType(compressedSize).read(byteBuf); + final boolean hasSkyLight = this.readHasSkyLight(byteBuf); + final byte[] data = new FixedByteArrayType(compressedSize).read(byteBuf); final int[] chunkX = new int[chunkCount]; final int[] chunkZ = new int[chunkCount]; final short[] primaryBitMask = new short[chunkCount]; @@ -106,7 +96,7 @@ public Chunk[] read(ByteBuf byteBuf, ClientWorld clientWorld) throws Exception { } @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]; @@ -139,7 +129,7 @@ public void write(ByteBuf byteBuf, ClientWorld clientWorld, Chunk[] chunks) thro byteBuf.writeShort(chunkCount); byteBuf.writeInt(compressedSize); - this.writeHasSkyLight(byteBuf, clientWorld, true); + this.writeHasSkyLight(byteBuf, true); byteBuf.writeBytes(compressedData, 0, compressedSize); for (int i = 0; i < chunkCount; i++) { diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/ChunkType1_7_6.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/ChunkType1_7_6.java index 3c51410..3521141 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/ChunkType1_7_6.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/ChunkType1_7_6.java @@ -17,13 +17,9 @@ */ package net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types; -import com.viaversion.viaversion.api.minecraft.ClientWorld; -import com.viaversion.viaversion.api.minecraft.Environment; import com.viaversion.viaversion.api.minecraft.chunks.*; -import com.viaversion.viaversion.api.type.PartialType; import com.viaversion.viaversion.api.type.Type; -import com.viaversion.viaversion.api.type.types.CustomByteType; -import com.viaversion.viaversion.api.type.types.chunk.BaseChunkType; +import com.viaversion.viaversion.api.type.types.FixedByteArrayType; import com.viaversion.viaversion.util.Pair; import io.netty.buffer.ByteBuf; import net.raphimc.vialegacy.api.model.IdAndData; @@ -36,48 +32,44 @@ import java.util.zip.Deflater; import java.util.zip.Inflater; -public class ChunkType1_7_6 extends PartialType { +public class ChunkType1_7_6 extends Type { - public ChunkType1_7_6(final ClientWorld clientWorld) { - super(clientWorld, Chunk.class); - } + private final boolean hasSkyLight; - @Override - public Class getBaseClass() { - return BaseChunkType.class; + public ChunkType1_7_6(boolean hasSkyLight) { + super(Chunk.class); + this.hasSkyLight = hasSkyLight; } /** * This method is here to allow overriding the code for 1.2.5 -{@literal >} 1.3.2 because it introduced an unused int * * @param byteBuf The buffer - * @param clientWorld The ClientWorld */ - protected void readUnusedInt(final ByteBuf byteBuf, final ClientWorld clientWorld) { + protected void readUnusedInt(final ByteBuf byteBuf) { } /** * This method is here to allow overriding the code for 1.2.5 -{@literal >} 1.3.2 because it introduced an unused int * * @param byteBuf The buffer - * @param clientWorld The ClientWorld * @param chunk The Chunk */ - protected void writeUnusedInt(final ByteBuf byteBuf, final ClientWorld clientWorld, final Chunk chunk) { + protected void writeUnusedInt(final ByteBuf byteBuf, final Chunk chunk) { } @Override - public Chunk read(ByteBuf byteBuf, ClientWorld clientWorld) throws Exception { + public Chunk read(ByteBuf byteBuf) throws Exception { final int chunkX = byteBuf.readInt(); final int chunkZ = byteBuf.readInt(); final boolean fullChunk = byteBuf.readBoolean(); final short primaryBitMask = byteBuf.readShort(); final short additionalBitMask = byteBuf.readShort(); final int compressedSize = byteBuf.readInt(); - this.readUnusedInt(byteBuf, clientWorld); - final byte[] data = new CustomByteType(compressedSize).read(byteBuf); + this.readUnusedInt(byteBuf); + final byte[] data = new FixedByteArrayType(compressedSize).read(byteBuf); - final byte[] uncompressedData = new byte[getSize(primaryBitMask, additionalBitMask, fullChunk, clientWorld.getEnvironment() == Environment.NORMAL)]; + final byte[] uncompressedData = new byte[getSize(primaryBitMask, additionalBitMask, fullChunk, this.hasSkyLight)]; final Inflater inflater = new Inflater(); try { inflater.setInput(data, 0, compressedSize); @@ -93,11 +85,11 @@ public Chunk read(ByteBuf byteBuf, ClientWorld clientWorld) throws Exception { return new BaseChunk(chunkX, chunkZ, true, false, 0, new ChunkSection[16], null, new ArrayList<>()); } - return deserialize(chunkX, chunkZ, fullChunk, clientWorld.getEnvironment() == Environment.NORMAL, primaryBitMask, additionalBitMask, uncompressedData); + return deserialize(chunkX, chunkZ, fullChunk, this.hasSkyLight, primaryBitMask, additionalBitMask, uncompressedData); } @Override - public void write(ByteBuf byteBuf, ClientWorld clientWorld, Chunk chunk) throws Exception { + public void write(ByteBuf byteBuf, Chunk chunk) throws Exception { final Pair chunkData = serialize(chunk); final byte[] data = chunkData.key(); final short additionalBitMask = chunkData.value(); @@ -120,7 +112,7 @@ public void write(ByteBuf byteBuf, ClientWorld clientWorld, Chunk chunk) throws byteBuf.writeShort(chunk.getBitmask()); byteBuf.writeShort(additionalBitMask); byteBuf.writeInt(compressedSize); - this.writeUnusedInt(byteBuf, clientWorld, chunk); + this.writeUnusedInt(byteBuf, chunk); byteBuf.writeBytes(compressedData, 0, compressedSize); } diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/ItemType.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/ItemType.java index a847300..34a1f89 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/ItemType.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/ItemType.java @@ -24,11 +24,8 @@ public class ItemType extends Type { - private final boolean compressed; - - public ItemType(boolean compressed) { + public ItemType() { super(Item.class); - this.compressed = compressed; } @Override @@ -42,7 +39,7 @@ public Item read(ByteBuf buffer) throws Exception { item.setIdentifier(id); item.setAmount(buffer.readByte()); item.setData(buffer.readShort()); - item.setTag((this.compressed ? Types1_7_6.COMPRESSED_NBT : Types1_7_6.NBT).read(buffer)); + item.setTag(Types1_7_6.COMPRESSED_NBT.read(buffer)); return item; } @@ -54,7 +51,7 @@ public void write(ByteBuf buffer, Item item) throws Exception { buffer.writeShort(item.identifier()); buffer.writeByte(item.amount()); buffer.writeShort(item.data()); - (this.compressed ? Types1_7_6.COMPRESSED_NBT : Types1_7_6.NBT).write(buffer, item.tag()); + Types1_7_6.COMPRESSED_NBT.write(buffer, item.tag()); } } diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/Types1_7_6.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/Types1_7_6.java index a8df5c1..d8c65ec 100644 --- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/Types1_7_6.java +++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/types/Types1_7_6.java @@ -18,6 +18,7 @@ package net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.types; import com.viaversion.viaversion.api.minecraft.BlockChangeRecord; +import com.viaversion.viaversion.api.minecraft.Environment; import com.viaversion.viaversion.api.minecraft.Position; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.minecraft.metadata.Metadata; @@ -34,10 +35,7 @@ public class Types1_7_6 { public static final Type NBT = new NBTType(false); public static final Type COMPRESSED_NBT = new NBTType(true); - public static final Type ITEM = new ItemType(false); - public static final Type COMPRESSED_ITEM = new ItemType(true); - - public static final Type ITEM_ARRAY = new ItemArrayType<>(ITEM); + public static final Type COMPRESSED_ITEM = new ItemType(); public static final Type COMPRESSED_ITEM_ARRAY = new ItemArrayType<>(COMPRESSED_ITEM); public static final Type METADATA = new MetadataType(); @@ -50,4 +48,13 @@ public class Types1_7_6 { public static final Type POSITION_SHORT = new PositionVarYType<>(Type.SHORT, i -> (short) i); public static final Type POSITION_INT = new PositionVarYType<>(Type.INT, i -> i); + private static final ChunkType1_7_6 CHUNK_WITH_SKYLIGHT = new ChunkType1_7_6(true); + private static final ChunkType1_7_6 CHUNK_WITHOUT_SKYLIGHT = new ChunkType1_7_6(false); + + public static final BulkChunkType1_7_6 CHUNK_BULK = new BulkChunkType1_7_6(); + + public static ChunkType1_7_6 getChunk(Environment environment) { + return environment == Environment.NORMAL ? CHUNK_WITH_SKYLIGHT : CHUNK_WITHOUT_SKYLIGHT; + } + }