From cf5d9dfc7d102c5dd992f6feffbab929248396a3 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Fri, 3 Aug 2018 22:18:32 -0400 Subject: [PATCH] Account for new network manager send packet method Fixes #497 --- .../protocol/utility/MinecraftMethods.java | 38 ++++++++++--------- .../injector/netty/ChannelInjector.java | 2 +- .../utility/MinecraftMethodsTest.java | 3 +- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/modules/API/src/main/java/com/comphenix/protocol/utility/MinecraftMethods.java b/modules/API/src/main/java/com/comphenix/protocol/utility/MinecraftMethods.java index 1c8f3b86..9745b4c5 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/utility/MinecraftMethods.java +++ b/modules/API/src/main/java/com/comphenix/protocol/utility/MinecraftMethods.java @@ -1,23 +1,22 @@ package com.comphenix.protocol.utility; -import io.netty.buffer.Unpooled; -import io.netty.channel.ChannelHandlerContext; -import io.netty.util.concurrent.GenericFutureListener; - import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.List; import java.util.Map; -import net.sf.cglib.proxy.Enhancer; -import net.sf.cglib.proxy.MethodInterceptor; -import net.sf.cglib.proxy.MethodProxy; - import com.comphenix.protocol.PacketType; import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.reflect.FuzzyReflection; import com.comphenix.protocol.reflect.fuzzy.FuzzyMethodContract; +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelHandlerContext; +import io.netty.util.concurrent.GenericFutureListener; + +import net.sf.cglib.proxy.Enhancer; +import net.sf.cglib.proxy.MethodInterceptor; + /** * Static methods for accessing Minecraft methods. * @@ -103,10 +102,12 @@ public class MinecraftMethods { */ public static Method getNetworkManagerHandleMethod() { if (networkManagerHandle == null) { - networkManagerHandle = FuzzyReflection.fromClass(MinecraftReflection.getNetworkManagerClass(), true). - getMethodByParameters("handle", MinecraftReflection.getPacketClass(), GenericFutureListener[].class); + networkManagerHandle = FuzzyReflection + .fromClass(MinecraftReflection.getNetworkManagerClass(), true) + .getMethodByParameters("handle", MinecraftReflection.getPacketClass()); networkManagerHandle.setAccessible(true); } + return networkManagerHandle; } @@ -170,15 +171,16 @@ public class MinecraftMethods { // This object will allow us to detect which methods were called Enhancer enhancer = EnhancerFactory.getInstance().createEnhancer(); enhancer.setSuperclass(MinecraftReflection.getPacketDataSerializerClass()); - enhancer.setCallback(new MethodInterceptor() { - @Override - public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { - if (method.getName().contains("read")) - throw new ReadMethodException(); - if (method.getName().contains("write")) - throw new WriteMethodException(); - return proxy.invokeSuper(obj, args); + enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> { + if (method.getName().contains("read")) { + throw new ReadMethodException(); } + + if (method.getName().contains("write")) { + throw new WriteMethodException(); + } + + return proxy.invokeSuper(obj, args); }); // Create our proxy object diff --git a/modules/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ChannelInjector.java b/modules/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ChannelInjector.java index 372a5719..9d7a0923 100644 --- a/modules/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ChannelInjector.java +++ b/modules/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ChannelInjector.java @@ -647,7 +647,7 @@ public class ChannelInjector extends ByteToMessageDecoder implements Injector { // Attempt to send the packet with NetworkMarker.handle(), or the PlayerConnection if its active try { if (player instanceof Factory) { - MinecraftMethods.getNetworkManagerHandleMethod().invoke(networkManager, packet, new GenericFutureListener[0]); + MinecraftMethods.getNetworkManagerHandleMethod().invoke(networkManager, packet); } else { MinecraftMethods.getSendPacketMethod().invoke(getPlayerConnection(), packet); } diff --git a/modules/ProtocolLib/src/test/java/com/comphenix/protocol/utility/MinecraftMethodsTest.java b/modules/ProtocolLib/src/test/java/com/comphenix/protocol/utility/MinecraftMethodsTest.java index 013ea076..fc2fc1c4 100644 --- a/modules/ProtocolLib/src/test/java/com/comphenix/protocol/utility/MinecraftMethodsTest.java +++ b/modules/ProtocolLib/src/test/java/com/comphenix/protocol/utility/MinecraftMethodsTest.java @@ -15,7 +15,8 @@ public class MinecraftMethodsTest { } @Test - public void testSendPacketMethod() { + public void testSendPacketMethods() { assertNotNull(MinecraftMethods.getSendPacketMethod()); + assertNotNull(MinecraftMethods.getNetworkManagerHandleMethod()); } }