-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: FlorianMichael <[email protected]>
- Loading branch information
1 parent
743a792
commit 7262ae2
Showing
2 changed files
with
55 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/com/viaversion/bungee/util/WaterfallPipelineUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
|