Skip to content

Commit

Permalink
Fix Waterfall compatibility (#31)
Browse files Browse the repository at this point in the history
Co-authored-by: FlorianMichael <[email protected]>
  • Loading branch information
linsaftw and FlorianMichael authored Dec 13, 2024
1 parent 743a792 commit 7262ae2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class BungeePipelineUtil {
}
}

public static List<Object> callDecode(MessageToMessageDecoder decoder, ChannelHandlerContext ctx, ByteBuf input) throws InvocationTargetException {
private static List<Object> callDecode(MessageToMessageDecoder decoder, ChannelHandlerContext ctx, ByteBuf input) throws InvocationTargetException {
List<Object> output = new ArrayList<>();
try {
BungeePipelineUtil.DECODE_METHOD.invoke(decoder, ctx, input, output);
Expand All @@ -51,7 +51,7 @@ public static List<Object> callDecode(MessageToMessageDecoder decoder, ChannelHa
return output;
}

public static ByteBuf callEncode(MessageToByteEncoder encoder, ChannelHandlerContext ctx, ByteBuf input) throws InvocationTargetException {
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);
Expand All @@ -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 callEncode((MessageToByteEncoder) ctx.pipeline().get("compress"), ctx.pipeline().context("compress"), bytebuf);
} catch (InvocationTargetException e) {
e.printStackTrace();
return ctx.alloc().buffer();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Object> callEncode(MessageToMessageEncoder encoder, ChannelHandlerContext ctx, ByteBuf input) throws InvocationTargetException {
List<Object> 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();
}
}
}

0 comments on commit 7262ae2

Please sign in to comment.