From c5738a2203cf29e3eff6ece1e881249a99782b10 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sun, 16 Oct 2022 12:27:26 +0200 Subject: [PATCH] Add Via channel handlers instead of wrapping Vanilla handlers on Bukkit (#3132) --- .../viaversion/util/PipelineUtil.java | 18 +-- .../viaversion/ViaVersionPlugin.java | 7 - .../bukkit/classgenerator/ClassGenerator.java | 13 +- .../handlers/BukkitChannelInitializer.java | 37 ++++-- .../bukkit/handlers/BukkitDecodeHandler.java | 69 +++++----- .../bukkit/handlers/BukkitEncodeHandler.java | 125 ++++++++++-------- .../bukkit/listeners/JoinListener.java | 2 +- .../listeners/ProtocolLibEnableListener.java | 62 --------- .../bukkit/platform/BukkitViaInjector.java | 10 -- .../generated/HandlerConstructor.java | 28 ---- ...rConstructor.java => HandlerSupplier.java} | 28 ++-- .../platform/LegacyViaInjector.java | 10 -- gradle.properties | 2 +- .../sponge/platform/SpongeViaInjector.java | 10 ++ 14 files changed, 173 insertions(+), 248 deletions(-) delete mode 100644 bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/ProtocolLibEnableListener.java delete mode 100644 bukkit/src/main/java/com/viaversion/viaversion/classgenerator/generated/HandlerConstructor.java rename bukkit/src/main/java/com/viaversion/viaversion/classgenerator/generated/{BasicHandlerConstructor.java => HandlerSupplier.java} (57%) diff --git a/api/src/main/java/com/viaversion/viaversion/util/PipelineUtil.java b/api/src/main/java/com/viaversion/viaversion/util/PipelineUtil.java index d44af895f..c1344d9ac 100644 --- a/api/src/main/java/com/viaversion/viaversion/util/PipelineUtil.java +++ b/api/src/main/java/com/viaversion/viaversion/util/PipelineUtil.java @@ -34,29 +34,21 @@ import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; -public class PipelineUtil { - private static Method DECODE_METHOD; - private static Method ENCODE_METHOD; - private static Method MTM_DECODE; +public final class PipelineUtil { + private static final Method DECODE_METHOD; + private static final Method ENCODE_METHOD; + private static final Method MTM_DECODE; static { try { DECODE_METHOD = ByteToMessageDecoder.class.getDeclaredMethod("decode", ChannelHandlerContext.class, ByteBuf.class, List.class); DECODE_METHOD.setAccessible(true); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } - try { ENCODE_METHOD = MessageToByteEncoder.class.getDeclaredMethod("encode", ChannelHandlerContext.class, Object.class, ByteBuf.class); ENCODE_METHOD.setAccessible(true); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } - try { MTM_DECODE = MessageToMessageDecoder.class.getDeclaredMethod("decode", ChannelHandlerContext.class, Object.class, List.class); MTM_DECODE.setAccessible(true); } catch (NoSuchMethodException e) { - e.printStackTrace(); + throw new RuntimeException(e); } } diff --git a/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java b/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java index 4a94559e5..ae02a1054 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java @@ -29,7 +29,6 @@ import com.viaversion.viaversion.api.platform.ViaPlatform; import com.viaversion.viaversion.bukkit.classgenerator.ClassGenerator; import com.viaversion.viaversion.bukkit.commands.BukkitCommandHandler; import com.viaversion.viaversion.bukkit.commands.BukkitCommandSender; -import com.viaversion.viaversion.bukkit.listeners.ProtocolLibEnableListener; import com.viaversion.viaversion.bukkit.platform.BukkitViaAPI; import com.viaversion.viaversion.bukkit.platform.BukkitViaConfig; import com.viaversion.viaversion.bukkit.platform.BukkitViaInjector; @@ -89,10 +88,6 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform @Override public void onLoad() { - // Via should load before PL, so we can't check for it in the constructor - Plugin protocolLib = Bukkit.getPluginManager().getPlugin("ProtocolLib"); - ProtocolLibEnableListener.checkCompat(protocolLib); - // Spigot detector try { Class.forName("org.spigotmc.SpigotConfig"); @@ -134,8 +129,6 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform getCommand("viaversion").setExecutor(commandHandler); getCommand("viaversion").setTabCompleter(commandHandler); - getServer().getPluginManager().registerEvents(new ProtocolLibEnableListener(), this); - // Warn them if they have anti-xray on and they aren't using spigot if (conf.isAntiXRay() && !spigot) { getLogger().info("You have anti-xray on in your config, since you're not using spigot it won't fix xray!"); diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/classgenerator/ClassGenerator.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/classgenerator/ClassGenerator.java index c51dd353c..9555c4d8d 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/classgenerator/ClassGenerator.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/classgenerator/ClassGenerator.java @@ -21,8 +21,7 @@ import com.viaversion.viaversion.ViaVersionPlugin; import com.viaversion.viaversion.bukkit.handlers.BukkitDecodeHandler; import com.viaversion.viaversion.bukkit.handlers.BukkitEncodeHandler; import com.viaversion.viaversion.bukkit.util.NMSUtil; -import com.viaversion.viaversion.classgenerator.generated.BasicHandlerConstructor; -import com.viaversion.viaversion.classgenerator.generated.HandlerConstructor; +import com.viaversion.viaversion.classgenerator.generated.HandlerSupplier; import javassist.CannotCompileException; import javassist.ClassPool; import javassist.CtClass; @@ -48,11 +47,11 @@ import java.lang.reflect.Method; //TODO maybe clean this up a bit 👀 public final class ClassGenerator { private static final boolean useModules = hasModuleMethod(); - private static HandlerConstructor constructor = new BasicHandlerConstructor(); + private static HandlerSupplier constructor = new HandlerSupplier.DefaultHandlerSupplier(); private static String psPackage; private static Class psConnectListener; - public static HandlerConstructor getConstructor() { + public static HandlerSupplier handlerSupplier() { return constructor; } @@ -91,7 +90,7 @@ public final class ClassGenerator { // Implement Constructor CtClass generated = pool.makeClass("com.viaversion.viaversion.classgenerator.generated.GeneratedConstructor"); - CtClass handlerInterface = pool.get(HandlerConstructor.class.getName()); + CtClass handlerInterface = pool.get(HandlerSupplier.class.getName()); generated.setInterfaces(new CtClass[]{handlerInterface}); // Import required classes @@ -107,7 +106,7 @@ public final class ClassGenerator { " return new BukkitDecodeHandler(info, minecraftDecoder);\n" + " }", generated)); - constructor = (HandlerConstructor) toClass(generated).getConstructor().newInstance(); + constructor = (HandlerSupplier) toClass(generated).getConstructor().newInstance(); } catch (ReflectiveOperationException | CannotCompileException | NotFoundException e) { e.printStackTrace(); } @@ -332,7 +331,7 @@ public final class ClassGenerator { @SuppressWarnings("deprecation") private static Class toClass(CtClass ctClass) throws CannotCompileException { - return useModules ? ctClass.toClass(HandlerConstructor.class) : ctClass.toClass(HandlerConstructor.class.getClassLoader()); + return useModules ? ctClass.toClass(HandlerSupplier.class) : ctClass.toClass(HandlerSupplier.class.getClassLoader()); } private static boolean hasModuleMethod() { diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitChannelInitializer.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitChannelInitializer.java index d1d6a93a2..aa72c5664 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitChannelInitializer.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitChannelInitializer.java @@ -20,31 +20,47 @@ package com.viaversion.viaversion.bukkit.handlers; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.bukkit.classgenerator.ClassGenerator; import com.viaversion.viaversion.bukkit.platform.PaperViaInjector; -import com.viaversion.viaversion.classgenerator.generated.HandlerConstructor; import com.viaversion.viaversion.connection.UserConnectionImpl; import com.viaversion.viaversion.platform.WrappedChannelInitializer; import com.viaversion.viaversion.protocol.ProtocolPipelineImpl; import io.netty.channel.Channel; import io.netty.channel.ChannelInitializer; -import io.netty.handler.codec.ByteToMessageDecoder; -import io.netty.handler.codec.MessageToByteEncoder; +import io.netty.channel.ChannelPipeline; +import org.checkerframework.checker.nullness.qual.Nullable; import java.lang.reflect.Method; -public class BukkitChannelInitializer extends ChannelInitializer implements WrappedChannelInitializer { +public final class BukkitChannelInitializer extends ChannelInitializer implements WrappedChannelInitializer { + public static final String VIA_ENCODER = "via-encoder"; + public static final String VIA_DECODER = "via-decoder"; + public static final String MINECRAFT_ENCODER = "encoder"; + public static final String MINECRAFT_DECODER = "decoder"; + public static final String MINECRAFT_COMPRESSOR = "compress"; + public static final String MINECRAFT_DECOMPRESSOR = "decompress"; + public static final Object COMPRESSION_ENABLED_EVENT = paperCompressionEnabledEvent(); private static final Method INIT_CHANNEL_METHOD; private final ChannelInitializer original; + static { try { INIT_CHANNEL_METHOD = ChannelInitializer.class.getDeclaredMethod("initChannel", Channel.class); INIT_CHANNEL_METHOD.setAccessible(true); - } catch (NoSuchMethodException e) { + } catch (final ReflectiveOperationException e) { throw new RuntimeException(e); } } + private static @Nullable Object paperCompressionEnabledEvent() { + try { + final Class eventClass = Class.forName("io.papermc.paper.network.ConnectionEvent"); + return eventClass.getDeclaredField("COMPRESSION_THRESHOLD_SET").get(null); + } catch (final ReflectiveOperationException e) { + return null; + } + } + public BukkitChannelInitializer(ChannelInitializer oldInit) { this.original = oldInit; } @@ -62,7 +78,7 @@ public class BukkitChannelInitializer extends ChannelInitializer implem } public static void afterChannelInitialize(Channel channel) { - UserConnection connection = new UserConnectionImpl(channel); + final UserConnection connection = new UserConnectionImpl(channel); new ProtocolPipelineImpl(connection); if (PaperViaInjector.PAPER_PACKET_LIMITER) { @@ -70,12 +86,9 @@ public class BukkitChannelInitializer extends ChannelInitializer implem } // Add our transformers - HandlerConstructor constructor = ClassGenerator.getConstructor(); - MessageToByteEncoder encoder = constructor.newEncodeHandler(connection, (MessageToByteEncoder) channel.pipeline().get("encoder")); - ByteToMessageDecoder decoder = constructor.newDecodeHandler(connection, (ByteToMessageDecoder) channel.pipeline().get("decoder")); - - channel.pipeline().replace("encoder", "encoder", encoder); - channel.pipeline().replace("decoder", "decoder", decoder); + final ChannelPipeline pipeline = channel.pipeline(); + pipeline.addBefore(MINECRAFT_ENCODER, VIA_ENCODER, ClassGenerator.handlerSupplier().newEncodeHandler(connection)); + pipeline.addBefore(MINECRAFT_DECODER, VIA_DECODER, ClassGenerator.handlerSupplier().newDecodeHandler(connection)); } @Override diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitDecodeHandler.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitDecodeHandler.java index 23628aa6d..42f6c5546 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitDecodeHandler.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitDecodeHandler.java @@ -26,59 +26,64 @@ import com.viaversion.viaversion.exception.CancelDecoderException; import com.viaversion.viaversion.exception.InformativeException; import com.viaversion.viaversion.util.PipelineUtil; import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; +import io.netty.channel.ChannelPipeline; +import io.netty.handler.codec.MessageToMessageDecoder; -import java.lang.reflect.InvocationTargetException; import java.util.List; -public class BukkitDecodeHandler extends ByteToMessageDecoder { - private final ByteToMessageDecoder minecraftDecoder; - private final UserConnection info; +@ChannelHandler.Sharable +public final class BukkitDecodeHandler extends MessageToMessageDecoder { + private final UserConnection connection; - public BukkitDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder) { - this.info = info; - this.minecraftDecoder = minecraftDecoder; + public BukkitDecodeHandler(final UserConnection connection) { + this.connection = connection; } @Override - protected void decode(ChannelHandlerContext ctx, ByteBuf bytebuf, List list) throws Exception { - if (!info.checkServerboundPacket()) { - bytebuf.clear(); // Don't accumulate + protected void decode(final ChannelHandlerContext ctx, final ByteBuf bytebuf, final List out) throws Exception { + if (!connection.checkServerboundPacket()) { throw CancelDecoderException.generate(null); } + if (!connection.shouldTransformPacket()) { + out.add(bytebuf.retain()); + return; + } - ByteBuf transformedBuf = null; + final ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf); try { - if (info.shouldTransformPacket()) { - transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf); - info.transformServerbound(transformedBuf, CancelDecoderException::generate); - } - - try { - list.addAll(PipelineUtil.callDecode(this.minecraftDecoder, ctx, transformedBuf == null ? bytebuf : transformedBuf)); - } catch (InvocationTargetException e) { - if (e.getCause() instanceof Exception) { - throw (Exception) e.getCause(); - } else if (e.getCause() instanceof Error) { - throw (Error) e.getCause(); - } - } + connection.transformIncoming(transformedBuf, CancelDecoderException::generate); + out.add(transformedBuf.retain()); } finally { - if (transformedBuf != null) { - transformedBuf.release(); - } + transformedBuf.release(); } } @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - if (PipelineUtil.containsCause(cause, CancelCodecException.class)) return; // ProtocolLib compat + public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) throws Exception { + if (PipelineUtil.containsCause(cause, CancelCodecException.class)) { + return; + } super.exceptionCaught(ctx, cause); if (!NMSUtil.isDebugPropertySet() && PipelineUtil.containsCause(cause, InformativeException.class) - && (info.getProtocolInfo().getState() != State.HANDSHAKE || Via.getManager().isDebug())) { + && (connection.getProtocolInfo().getState() != State.HANDSHAKE || Via.getManager().isDebug())) { cause.printStackTrace(); // Print if CB doesn't already do it } } + + @Override + public void userEventTriggered(final ChannelHandlerContext ctx, final Object event) throws Exception { + if (BukkitChannelInitializer.COMPRESSION_ENABLED_EVENT == null || event != BukkitChannelInitializer.COMPRESSION_ENABLED_EVENT) { + super.userEventTriggered(ctx, event); + return; + } + + // When compression handlers are added, the order becomes Minecraft Encoder -> Compressor -> Via Encoder; fix the order again + final ChannelPipeline pipeline = ctx.pipeline(); + pipeline.addAfter(BukkitChannelInitializer.MINECRAFT_COMPRESSOR, BukkitChannelInitializer.VIA_ENCODER, pipeline.remove(BukkitChannelInitializer.VIA_ENCODER)); + pipeline.addAfter(BukkitChannelInitializer.MINECRAFT_DECOMPRESSOR, BukkitChannelInitializer.VIA_DECODER, pipeline.remove(BukkitChannelInitializer.VIA_DECODER)); + super.userEventTriggered(ctx, event); + } } diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitEncodeHandler.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitEncodeHandler.java index 77dc7bf20..710d75ac3 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitEncodeHandler.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitEncodeHandler.java @@ -24,84 +24,99 @@ import com.viaversion.viaversion.bukkit.util.NMSUtil; import com.viaversion.viaversion.exception.CancelCodecException; import com.viaversion.viaversion.exception.CancelEncoderException; import com.viaversion.viaversion.exception.InformativeException; -import com.viaversion.viaversion.handlers.ChannelHandlerContextWrapper; -import com.viaversion.viaversion.handlers.ViaCodecHandler; import com.viaversion.viaversion.util.PipelineUtil; import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPipeline; +import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.MessageToByteEncoder; +import io.netty.handler.codec.MessageToMessageEncoder; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; +import java.util.List; -public class BukkitEncodeHandler extends MessageToByteEncoder implements ViaCodecHandler { - private static Field versionField; +@ChannelHandler.Sharable +public final class BukkitEncodeHandler extends MessageToMessageEncoder { + private final UserConnection connection; + private boolean handledCompression = BukkitChannelInitializer.COMPRESSION_ENABLED_EVENT != null; - static { - try { - // Attempt to get any version info from the handler - versionField = NMSUtil.nms( - "PacketEncoder", - "net.minecraft.network.PacketEncoder" - ).getDeclaredField("version"); - - versionField.setAccessible(true); - } catch (Exception e) { - // Not compat version - } - } - - private final UserConnection info; - private final MessageToByteEncoder minecraftEncoder; - - public BukkitEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) { - this.info = info; - this.minecraftEncoder = minecraftEncoder; + public BukkitEncodeHandler(final UserConnection connection) { + this.connection = connection; } @Override - protected void encode(final ChannelHandlerContext ctx, Object o, final ByteBuf bytebuf) throws Exception { - if (versionField != null) { - versionField.set(minecraftEncoder, versionField.get(this)); + protected void encode(final ChannelHandlerContext ctx, final ByteBuf bytebuf, final List out) throws Exception { + if (!connection.checkClientboundPacket()) { + throw CancelEncoderException.generate(null); } - // handle the packet type - if (!(o instanceof ByteBuf)) { - // call minecraft encoder - try { - PipelineUtil.callEncode(this.minecraftEncoder, new ChannelHandlerContextWrapper(ctx, this), o, bytebuf); - } catch (InvocationTargetException e) { - if (e.getCause() instanceof Exception) { - throw (Exception) e.getCause(); - } else if (e.getCause() instanceof Error) { - throw (Error) e.getCause(); - } + if (!connection.shouldTransformPacket()) { + out.add(bytebuf.retain()); + return; + } + + final ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf); + try { + final boolean needsCompression = !handledCompression && handleCompressionOrder(ctx, transformedBuf); + connection.transformClientbound(transformedBuf, CancelEncoderException::generate); + if (needsCompression) { + recompress(ctx, transformedBuf); } - } else { - bytebuf.writeBytes((ByteBuf) o); + out.add(transformedBuf.retain()); + } finally { + transformedBuf.release(); + } + } + + private boolean handleCompressionOrder(final ChannelHandlerContext ctx, final ByteBuf buf) throws Exception { + final ChannelPipeline pipeline = ctx.pipeline(); + final List names = pipeline.names(); + final int compressorIndex = names.indexOf(BukkitChannelInitializer.MINECRAFT_COMPRESSOR); + if (compressorIndex == -1) { + return false; + } + + handledCompression = true; + if (compressorIndex > names.indexOf(BukkitChannelInitializer.VIA_ENCODER)) { + // Need to decompress this packet due to bad order + final ByteBuf decompressed = (ByteBuf) PipelineUtil.callDecode((ByteToMessageDecoder) pipeline.get(BukkitChannelInitializer.MINECRAFT_DECOMPRESSOR), ctx, buf).get(0); + try { + buf.clear().writeBytes(decompressed); + } finally { + decompressed.release(); + } + + pipeline.addAfter(BukkitChannelInitializer.MINECRAFT_COMPRESSOR, BukkitChannelInitializer.VIA_ENCODER, pipeline.remove(BukkitChannelInitializer.VIA_ENCODER)); + pipeline.addAfter(BukkitChannelInitializer.MINECRAFT_DECOMPRESSOR, BukkitChannelInitializer.VIA_DECODER, pipeline.remove(BukkitChannelInitializer.VIA_DECODER)); + return true; + } + return true; + } + + private void recompress(final ChannelHandlerContext ctx, final ByteBuf buf) throws Exception { + final ByteBuf compressed = ctx.alloc().buffer(); + try { + PipelineUtil.callEncode((MessageToByteEncoder) ctx.pipeline().get(BukkitChannelInitializer.MINECRAFT_COMPRESSOR), ctx, buf, compressed); + buf.clear().writeBytes(compressed); + } finally { + compressed.release(); } - transform(bytebuf); } @Override - public void transform(ByteBuf bytebuf) throws Exception { - if (!info.checkClientboundPacket()) throw CancelEncoderException.generate(null); - if (!info.shouldTransformPacket()) return; - info.transformClientbound(bytebuf, CancelEncoderException::generate); - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - if (PipelineUtil.containsCause(cause, CancelCodecException.class)) return; // ProtocolLib compat + public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) throws Exception { + if (PipelineUtil.containsCause(cause, CancelCodecException.class)) { + return; + } super.exceptionCaught(ctx, cause); if (!NMSUtil.isDebugPropertySet() && PipelineUtil.containsCause(cause, InformativeException.class) - && (info.getProtocolInfo().getState() != State.HANDSHAKE || Via.getManager().isDebug())) { + && (connection.getProtocolInfo().getState() != State.HANDSHAKE || Via.getManager().isDebug())) { cause.printStackTrace(); // Print if CB doesn't already do it } } - public UserConnection getInfo() { - return info; + public UserConnection connection() { + return connection; } } diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/JoinListener.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/JoinListener.java index 90d154d46..fc2cfe1c7 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/JoinListener.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/JoinListener.java @@ -103,7 +103,7 @@ public class JoinListener implements Listener { private @Nullable UserConnection getUserConnection(Channel channel) { BukkitEncodeHandler encoder = channel.pipeline().get(BukkitEncodeHandler.class); - return encoder != null ? encoder.getInfo() : null; + return encoder != null ? encoder.connection() : null; } private Channel getChannel(Player player) throws Exception { diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/ProtocolLibEnableListener.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/ProtocolLibEnableListener.java deleted file mode 100644 index 10ff11076..000000000 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/ProtocolLibEnableListener.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion - * Copyright (C) 2016-2022 ViaVersion 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 com.viaversion.viaversion.bukkit.listeners; - -import com.viaversion.viaversion.api.Via; -import com.viaversion.viaversion.bukkit.platform.BukkitViaInjector; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.server.PluginDisableEvent; -import org.bukkit.event.server.PluginEnableEvent; -import org.bukkit.plugin.Plugin; -import org.jetbrains.annotations.Nullable; - -public class ProtocolLibEnableListener implements Listener { - - @EventHandler - public void onPluginEnable(PluginEnableEvent e) { - // Will likely never happen, but try to account for hacky plugin loading systems anyways - if (e.getPlugin().getName().equals("ProtocolLib")) { - checkCompat(e.getPlugin()); - } - } - - @EventHandler - public void onPluginDisable(PluginDisableEvent e) { - if (e.getPlugin().getName().equals("ProtocolLib")) { - ((BukkitViaInjector) Via.getManager().getInjector()).setProtocolLib(false); - } - } - - public static void checkCompat(@Nullable Plugin protocolLib) { - if (protocolLib != null) { - String version = protocolLib.getDescription().getVersion(); - String majorVersion = version.split("\\.", 2)[0]; - try { - // Only need the compat check for version < 5 - if (Integer.parseInt(majorVersion) < 5) { - ((BukkitViaInjector) Via.getManager().getInjector()).setProtocolLib(true); - return; - } - } catch (NumberFormatException ignored) { - Via.getPlatform().getLogger().warning("ProtocolLib version check failed for version " + version); - } - } - ((BukkitViaInjector) Via.getManager().getInjector()).setProtocolLib(false); - } -} diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaInjector.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaInjector.java index 775cfc5e3..da1c2a940 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaInjector.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaInjector.java @@ -35,7 +35,6 @@ import java.lang.reflect.Method; import java.util.List; public class BukkitViaInjector extends LegacyViaInjector { - private boolean protocolLib; @Override public void inject() throws ReflectiveOperationException { @@ -112,11 +111,6 @@ public class BukkitViaInjector extends LegacyViaInjector { throw new RuntimeException("Failed to get server"); } - @Override - public String getDecoderName() { - return protocolLib ? "protocol_lib_decoder" : "decoder"; - } - @Override protected @Nullable Object getServerConnection() throws ReflectiveOperationException { Class serverClass = NMSUtil.nms( @@ -189,8 +183,4 @@ public class BukkitViaInjector extends LegacyViaInjector { } return false; } - - public void setProtocolLib(boolean protocolLib) { - this.protocolLib = protocolLib; - } } \ No newline at end of file diff --git a/bukkit/src/main/java/com/viaversion/viaversion/classgenerator/generated/HandlerConstructor.java b/bukkit/src/main/java/com/viaversion/viaversion/classgenerator/generated/HandlerConstructor.java deleted file mode 100644 index 8204f1953..000000000 --- a/bukkit/src/main/java/com/viaversion/viaversion/classgenerator/generated/HandlerConstructor.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion - * Copyright (C) 2016-2022 ViaVersion 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 com.viaversion.viaversion.classgenerator.generated; - -import com.viaversion.viaversion.api.connection.UserConnection; -import io.netty.handler.codec.ByteToMessageDecoder; -import io.netty.handler.codec.MessageToByteEncoder; - -public interface HandlerConstructor { - public MessageToByteEncoder newEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder); - - public ByteToMessageDecoder newDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder); -} diff --git a/bukkit/src/main/java/com/viaversion/viaversion/classgenerator/generated/BasicHandlerConstructor.java b/bukkit/src/main/java/com/viaversion/viaversion/classgenerator/generated/HandlerSupplier.java similarity index 57% rename from bukkit/src/main/java/com/viaversion/viaversion/classgenerator/generated/BasicHandlerConstructor.java rename to bukkit/src/main/java/com/viaversion/viaversion/classgenerator/generated/HandlerSupplier.java index e0c9480f0..a5dc795a0 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/classgenerator/generated/BasicHandlerConstructor.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/classgenerator/generated/HandlerSupplier.java @@ -20,17 +20,25 @@ package com.viaversion.viaversion.classgenerator.generated; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.bukkit.handlers.BukkitDecodeHandler; import com.viaversion.viaversion.bukkit.handlers.BukkitEncodeHandler; -import io.netty.handler.codec.ByteToMessageDecoder; -import io.netty.handler.codec.MessageToByteEncoder; +import io.netty.buffer.ByteBuf; +import io.netty.handler.codec.MessageToMessageDecoder; +import io.netty.handler.codec.MessageToMessageEncoder; -public class BasicHandlerConstructor implements HandlerConstructor { - @Override - public BukkitEncodeHandler newEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) { - return new BukkitEncodeHandler(info, minecraftEncoder); - } +public interface HandlerSupplier { - @Override - public BukkitDecodeHandler newDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder) { - return new BukkitDecodeHandler(info, minecraftDecoder); + MessageToMessageEncoder newEncodeHandler(UserConnection connection); + + MessageToMessageDecoder newDecodeHandler(UserConnection connection); + + final class DefaultHandlerSupplier implements HandlerSupplier { + @Override + public MessageToMessageEncoder newEncodeHandler(final UserConnection connection) { + return new BukkitEncodeHandler(connection); + } + + @Override + public MessageToMessageDecoder newDecodeHandler(final UserConnection connection) { + return new BukkitDecodeHandler(connection); + } } } diff --git a/common/src/main/java/com/viaversion/viaversion/platform/LegacyViaInjector.java b/common/src/main/java/com/viaversion/viaversion/platform/LegacyViaInjector.java index 3f423fe29..f92d78624 100644 --- a/common/src/main/java/com/viaversion/viaversion/platform/LegacyViaInjector.java +++ b/common/src/main/java/com/viaversion/viaversion/platform/LegacyViaInjector.java @@ -235,16 +235,6 @@ public abstract class LegacyViaInjector implements ViaInjector { return data; } - @Override - public String getEncoderName() { - return "encoder"; - } - - @Override - public String getDecoderName() { - return "decoder"; - } - /** * Returns the Vanilla server connection object the channels to be injected should be searched in. * diff --git a/gradle.properties b/gradle.properties index 6169b6055..30172976d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ # Project properties - we put these here so they can be modified without causing a recompile of the build scripts -projectVersion=4.4.3-SNAPSHOT +projectVersion=4.5.0-SNAPSHOT # Gradle properties org.gradle.daemon=true diff --git a/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeViaInjector.java b/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeViaInjector.java index ae38c962f..6d2937f06 100644 --- a/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeViaInjector.java +++ b/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeViaInjector.java @@ -50,4 +50,14 @@ public class SpongeViaInjector extends LegacyViaInjector { protected void blame(ChannelHandler bootstrapAcceptor) { throw new RuntimeException("Unable to find core component 'childHandler', please check your plugins. Issue: " + bootstrapAcceptor.getClass().getName()); } + + @Override + public String getEncoderName() { + return "encoder"; + } + + @Override + public String getDecoderName() { + return "decoder"; + } } \ No newline at end of file