Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix waterfall compatibility #31

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
}