From 71ba21731febec222131a63f228201b58e6ac99d Mon Sep 17 00:00:00 2001 From: Moulberry Date: Wed, 8 May 2024 18:58:18 +0800 Subject: [PATCH] Separate ViaVersion methods to avoid ClassNotFoundExceptions --- .../ManipulateEntityPacketListener.java | 3 +- .../packet/SetBlockBufferPacketListener.java | 5 +-- .../packet/SpawnEntityPacketListener.java | 3 +- .../viaversion/UnknownVersionHelper.java | 34 ++++++++++++++++ .../axiom/viaversion/ViaVersionHelper.java | 40 +++++-------------- 5 files changed, 50 insertions(+), 35 deletions(-) create mode 100644 src/main/java/com/moulberry/axiom/viaversion/UnknownVersionHelper.java diff --git a/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java b/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java index f53aa59..8c5e9e1 100644 --- a/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java @@ -4,6 +4,7 @@ import com.moulberry.axiom.AxiomPaper; import com.moulberry.axiom.NbtSanitization; import com.moulberry.axiom.integration.Integration; import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration; +import com.moulberry.axiom.viaversion.UnknownVersionHelper; import com.moulberry.axiom.viaversion.ViaVersionHelper; import io.netty.buffer.Unpooled; import net.kyori.adventure.text.Component; @@ -63,7 +64,7 @@ public class ManipulateEntityPacketListener implements PluginMessageListener { pitch = friendlyByteBuf.readFloat(); } - CompoundTag nbt = ViaVersionHelper.readTagUnknown(friendlyByteBuf, player); + CompoundTag nbt = UnknownVersionHelper.readTagUnknown(friendlyByteBuf, player); PassengerManipulation passengerManipulation = friendlyByteBuf.readEnum(PassengerManipulation.class); List passengers = List.of(); diff --git a/src/main/java/com/moulberry/axiom/packet/SetBlockBufferPacketListener.java b/src/main/java/com/moulberry/axiom/packet/SetBlockBufferPacketListener.java index aaf85f7..74cad82 100644 --- a/src/main/java/com/moulberry/axiom/packet/SetBlockBufferPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SetBlockBufferPacketListener.java @@ -8,8 +8,7 @@ import com.moulberry.axiom.buffer.BlockBuffer; import com.moulberry.axiom.buffer.CompressedBlockEntity; import com.moulberry.axiom.integration.Integration; import com.moulberry.axiom.integration.SectionPermissionChecker; -import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration; -import com.moulberry.axiom.viaversion.ViaVersionHelper; +import com.moulberry.axiom.viaversion.UnknownVersionHelper; import it.unimi.dsi.fastutil.longs.Long2ObjectMap; import it.unimi.dsi.fastutil.shorts.Short2ObjectMap; import net.minecraft.ChatFormatting; @@ -78,7 +77,7 @@ public class SetBlockBufferPacketListener { boolean continuation = friendlyByteBuf.readBoolean(); if (!continuation) { - ViaVersionHelper.skipTagUnknown(friendlyByteBuf, player.getBukkitEntity()); + UnknownVersionHelper.skipTagUnknown(friendlyByteBuf, player.getBukkitEntity()); } RateLimiter rateLimiter = this.plugin.getBlockBufferRateLimiter(player.getUUID()); diff --git a/src/main/java/com/moulberry/axiom/packet/SpawnEntityPacketListener.java b/src/main/java/com/moulberry/axiom/packet/SpawnEntityPacketListener.java index 639d42a..ba1f031 100644 --- a/src/main/java/com/moulberry/axiom/packet/SpawnEntityPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SpawnEntityPacketListener.java @@ -4,6 +4,7 @@ import com.moulberry.axiom.AxiomPaper; import com.moulberry.axiom.NbtSanitization; import com.moulberry.axiom.integration.Integration; import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration; +import com.moulberry.axiom.viaversion.UnknownVersionHelper; import com.moulberry.axiom.viaversion.ViaVersionHelper; import io.netty.buffer.Unpooled; import net.kyori.adventure.text.Component; @@ -70,7 +71,7 @@ public class SpawnEntityPacketListener implements PluginMessageListener { List entries = friendlyByteBuf.readCollection(FriendlyByteBuf.limitValue(ArrayList::new, 1000), buf -> new SpawnEntry(buf.readUUID(), buf.readDouble(), buf.readDouble(), buf.readDouble(), buf.readFloat(), buf.readFloat(), - buf.readNullable(FriendlyByteBuf::readUUID), ViaVersionHelper.readTagUnknown(buf, player))); + buf.readNullable(FriendlyByteBuf::readUUID), UnknownVersionHelper.readTagUnknown(buf, player))); ServerLevel serverLevel = ((CraftWorld)player.getWorld()).getHandle(); diff --git a/src/main/java/com/moulberry/axiom/viaversion/UnknownVersionHelper.java b/src/main/java/com/moulberry/axiom/viaversion/UnknownVersionHelper.java new file mode 100644 index 0000000..aaa7dca --- /dev/null +++ b/src/main/java/com/moulberry/axiom/viaversion/UnknownVersionHelper.java @@ -0,0 +1,34 @@ +package com.moulberry.axiom.viaversion; + +import com.moulberry.axiom.AxiomPaper; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.FriendlyByteBuf; +import org.bukkit.entity.Player; + +public class UnknownVersionHelper { + + public static void skipTagUnknown(FriendlyByteBuf friendlyByteBuf, Player player) { + if (AxiomPaper.PLUGIN.isMismatchedDataVersion(player.getUniqueId())) { + try { + ViaVersionHelper.skipTagViaVersion(friendlyByteBuf, player); + } catch (Exception e) { + throw new RuntimeException(e); + } + } else { + friendlyByteBuf.readNbt(); // Discard + } + } + + public static CompoundTag readTagUnknown(FriendlyByteBuf friendlyByteBuf, Player player) { + if (AxiomPaper.PLUGIN.isMismatchedDataVersion(player.getUniqueId())) { + try { + return ViaVersionHelper.readTagViaVersion(friendlyByteBuf, player); + } catch (Exception e) { + throw new RuntimeException(e); + } + } else { + return friendlyByteBuf.readNbt(); + } + } + +} diff --git a/src/main/java/com/moulberry/axiom/viaversion/ViaVersionHelper.java b/src/main/java/com/moulberry/axiom/viaversion/ViaVersionHelper.java index d3cafab..cfd0685 100644 --- a/src/main/java/com/moulberry/axiom/viaversion/ViaVersionHelper.java +++ b/src/main/java/com/moulberry/axiom/viaversion/ViaVersionHelper.java @@ -6,9 +6,7 @@ import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.data.MappingData; import com.viaversion.viaversion.api.data.Mappings; import com.viaversion.viaversion.api.protocol.ProtocolPathEntry; -import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.api.type.Type; -import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import net.minecraft.SharedConstants; @@ -65,7 +63,7 @@ public class ViaVersionHelper { return mapper; } - public static IdMapper applyMappings(IdMapper registry, Mappings mappings) { + private static IdMapper applyMappings(IdMapper registry, Mappings mappings) { IdMapper newBlockRegistry = new IdMapper<>(); // Add empty mappings for non-existent blocks @@ -95,38 +93,20 @@ public class ViaVersionHelper { return newBlockRegistry; } - private static final int UNNAMED_COMPOUND_TAG_CHANGE_VERSION = ProtocolVersion.v1_20_2.getVersion(); + private static final int UNNAMED_COMPOUND_TAG_CHANGE_VERSION = 764; // 1.20.2 - public static void skipTagUnknown(FriendlyByteBuf friendlyByteBuf, Player player) { - if (AxiomPaper.PLUGIN.isMismatchedDataVersion(player.getUniqueId())) { - int playerVersion = Via.getAPI().getPlayerVersion(player.getUniqueId()); - try { - ViaVersionHelper.skipTagViaVersion(friendlyByteBuf, playerVersion); - } catch (Exception e) { - throw new RuntimeException(e); - } - } else { - friendlyByteBuf.readNbt(); // Discard - } - } - - public static CompoundTag readTagUnknown(FriendlyByteBuf friendlyByteBuf, Player player) { - if (AxiomPaper.PLUGIN.isMismatchedDataVersion(player.getUniqueId())) { - int playerVersion = Via.getAPI().getPlayerVersion(player.getUniqueId()); - try { - return ViaVersionHelper.readTagViaVersion(friendlyByteBuf, playerVersion); - } catch (Exception e) { - throw new RuntimeException(e); - } - } else { - return friendlyByteBuf.readNbt(); - } + public static void skipTagViaVersion(FriendlyByteBuf friendlyByteBuf, Player player) throws Exception { + skipTagViaVersion(friendlyByteBuf, Via.getAPI().getPlayerVersion(player.getUniqueId())); } public static void skipTagViaVersion(FriendlyByteBuf friendlyByteBuf, int playerVersion) throws Exception { getTagType(playerVersion).read(friendlyByteBuf); } + public static CompoundTag readTagViaVersion(FriendlyByteBuf friendlyByteBuf, Player player) throws Exception { + return readTagViaVersion(friendlyByteBuf, Via.getAPI().getPlayerVersion(player.getUniqueId())); + } + public static CompoundTag readTagViaVersion(FriendlyByteBuf friendlyByteBuf, int playerVersion) throws Exception { Type from = getTagType(playerVersion); Type to = getTagType(SharedConstants.getProtocolVersion()); @@ -150,9 +130,9 @@ public class ViaVersionHelper { } com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag tag = from.read(friendlyByteBuf); - ByteBuf buffer = Unpooled.buffer(); + FriendlyByteBuf buffer = new FriendlyByteBuf(Unpooled.buffer()); to.write(buffer, tag); - return new FriendlyByteBuf(buffer).readNbt(); + return buffer.readNbt(); } }