diff --git a/src/main/java/com/viaversion/bungee/util/BungeePipelineUtil.java b/src/main/java/com/viaversion/bungee/util/BungeePipelineUtil.java index e572d4e..7828dda 100644 --- a/src/main/java/com/viaversion/bungee/util/BungeePipelineUtil.java +++ b/src/main/java/com/viaversion/bungee/util/BungeePipelineUtil.java @@ -19,8 +19,8 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToMessageDecoder; -import io.netty.handler.codec.MessageToMessageEncoder; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; @@ -34,14 +34,14 @@ public class BungeePipelineUtil { try { DECODE_METHOD = MessageToMessageDecoder.class.getDeclaredMethod("decode", ChannelHandlerContext.class, Object.class, List.class); DECODE_METHOD.setAccessible(true); - ENCODE_METHOD = MessageToMessageEncoder.class.getDeclaredMethod("encode", ChannelHandlerContext.class, Object.class, List.class); + ENCODE_METHOD = MessageToByteEncoder.class.getDeclaredMethod("encode", ChannelHandlerContext.class, Object.class, ByteBuf.class); ENCODE_METHOD.setAccessible(true); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } } - public static List callDecode(MessageToMessageDecoder decoder, ChannelHandlerContext ctx, ByteBuf input) throws InvocationTargetException { + private static List callDecode(MessageToMessageDecoder decoder, ChannelHandlerContext ctx, ByteBuf input) throws InvocationTargetException { List output = new ArrayList<>(); try { BungeePipelineUtil.DECODE_METHOD.invoke(decoder, ctx, input, output); @@ -51,8 +51,8 @@ public static List callDecode(MessageToMessageDecoder decoder, ChannelHa return output; } - public static List callEncode(MessageToMessageEncoder encoder, ChannelHandlerContext ctx, ByteBuf input) throws InvocationTargetException { - List output = new ArrayList<>(); + private static ByteBuf callEncode(MessageToByteEncoder encoder, ChannelHandlerContext ctx, ByteBuf input) throws InvocationTargetException { + ByteBuf output = ctx.alloc().buffer(); try { BungeePipelineUtil.ENCODE_METHOD.invoke(encoder, ctx, input, output); } catch (IllegalAccessException e) { @@ -71,11 +71,14 @@ public static ByteBuf decompress(ChannelHandlerContext ctx, ByteBuf bytebuf) { } public static ByteBuf compress(ChannelHandlerContext ctx, ByteBuf bytebuf) { + if (WaterfallPipelineUtil.IS_WATERFALL) { + return WaterfallPipelineUtil.compress(ctx, bytebuf); + } try { - return (ByteBuf) callEncode((MessageToMessageEncoder) ctx.pipeline().get("compress"), ctx.pipeline().context("compress"), bytebuf).get(0); + return callEncode((MessageToByteEncoder) ctx.pipeline().get("compress"), ctx.pipeline().context("compress"), bytebuf); } catch (InvocationTargetException e) { e.printStackTrace(); return ctx.alloc().buffer(); } } -} +} \ No newline at end of file diff --git a/src/main/java/com/viaversion/bungee/util/WaterfallPipelineUtil.java b/src/main/java/com/viaversion/bungee/util/WaterfallPipelineUtil.java new file mode 100644 index 0000000..4a574fc --- /dev/null +++ b/src/main/java/com/viaversion/bungee/util/WaterfallPipelineUtil.java @@ -0,0 +1,49 @@ +package com.viaversion.bungee.util; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToMessageEncoder; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +public class WaterfallPipelineUtil { + public static final Method ENCODE_METHOD; + public static boolean IS_WATERFALL; + + static { + try { + Class.forName("io.github.waterfallmc.waterfall.conf.WaterfallConfiguration"); + IS_WATERFALL = true; + } catch (ClassNotFoundException e) { + IS_WATERFALL = false; + } + try { + ENCODE_METHOD = MessageToMessageEncoder.class.getDeclaredMethod("encode", ChannelHandlerContext.class, Object.class, List.class); + ENCODE_METHOD.setAccessible(true); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } + } + + public static List callEncode(MessageToMessageEncoder encoder, ChannelHandlerContext ctx, ByteBuf input) throws InvocationTargetException { + List output = new ArrayList<>(); + try { + ENCODE_METHOD.invoke(encoder, ctx, input, output); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + return output; + } + + public static ByteBuf compress(ChannelHandlerContext ctx, ByteBuf bytebuf) { + try { + return (ByteBuf) callEncode((MessageToMessageEncoder) ctx.pipeline().get("compress"), ctx.pipeline().context("compress"), bytebuf).get(0); + } catch (InvocationTargetException e) { + e.printStackTrace(); + return ctx.alloc().buffer(); + } + } +} +