From a798147e71958bc2c638c2188bcad8d6959d5de3 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Sun, 10 Mar 2013 18:52:41 +0100 Subject: [PATCH] Correctly override SpoutPlugin's sendPacket method. --- ProtocolLib/pom.xml | 24 +++++++------- .../player/NetworkServerInjector.java | 32 +++++++++++++++---- ProtocolLib/src/main/resources/plugin.yml | 2 +- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/ProtocolLib/pom.xml b/ProtocolLib/pom.xml index ac7a8b99..586f9634 100644 --- a/ProtocolLib/pom.xml +++ b/ProtocolLib/pom.xml @@ -2,7 +2,7 @@ 4.0.0 com.comphenix.protocol ProtocolLib - 2.3.0 + 2.3.1-SNAPSHOT jar Provides read/write access to the Minecraft protocol. @@ -219,16 +219,16 @@ test - org.powermock - powermock-module-junit4 - ${powermock.version} - test - - - org.powermock - powermock-api-mockito - ${powermock.version} - test - + org.powermock + powermock-module-junit4 + ${powermock.version} + test + + + org.powermock + powermock-api-mockito + ${powermock.version} + test + \ No newline at end of file diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetworkServerInjector.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetworkServerInjector.java index 16218066..ef6858bc 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetworkServerInjector.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetworkServerInjector.java @@ -20,6 +20,8 @@ package com.comphenix.protocol.injector.player; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.Arrays; + import net.sf.cglib.proxy.*; import org.bukkit.entity.Player; @@ -45,8 +47,8 @@ import com.comphenix.protocol.utility.MinecraftReflection; * @author Kristian */ class NetworkServerInjector extends PlayerInjector { - private volatile static CallbackFilter callbackFilter; + private volatile static boolean foundSendPacket; private volatile static Field disconnectField; private InjectedServerConnection serverInjection; @@ -168,10 +170,12 @@ class NetworkServerInjector extends PlayerInjector { callbackFilter = new CallbackFilter() { @Override public int accept(Method method) { - if (method.equals(sendPacket)) + if (isCallableEqual(sendPacket, method)) { + foundSendPacket = true; return 0; - else + } else { return 1; + } } }; } @@ -204,9 +208,11 @@ class NetworkServerInjector extends PlayerInjector { // Inject it now if (proxyObject != null) { - // This will be done by InjectedServerConnection instead - //copyTo(serverHandler, proxyObject); - + // Did we override a sendPacket method? + if (!foundSendPacket) { + throw new IllegalArgumentException("Unable to find a sendPacket method in " + serverClass); + } + serverInjection.replaceServerHandler(serverHandler, proxyObject); serverHandlerRef.setValue(proxyObject); return true; @@ -215,6 +221,20 @@ class NetworkServerInjector extends PlayerInjector { } } + /** + * Determine if the two methods are equal in terms of call semantics. + *

+ * Two methods are equal if they have the same name, parameter types and return type. + * @param first - first method. + * @param second - second method. + * @return TRUE if they are, FALSE otherwise. + */ + private boolean isCallableEqual(Method first, Method second) { + return first.getName().equals(second.getName()) && + first.getReturnType().equals(second.getReturnType()) && + Arrays.equals(first.getParameterTypes(), second.getParameterTypes()); + } + private Object getProxyServerHandler() { if (proxyServerField != null && !proxyServerField.equals(serverHandlerRef.getField())) { try { diff --git a/ProtocolLib/src/main/resources/plugin.yml b/ProtocolLib/src/main/resources/plugin.yml index 23fb34bf..c95bbdcf 100644 --- a/ProtocolLib/src/main/resources/plugin.yml +++ b/ProtocolLib/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: ProtocolLib -version: 2.3.0 +version: 2.3.1-SNAPSHOT description: Provides read/write access to the Minecraft protocol. author: Comphenix website: http://www.comphenix.net/ProtocolLib