Speed up the proxy object by using a NoOp filter.

This commit is contained in:
Kristian S. Stangeland 2012-10-02 04:36:47 +02:00
parent 48cedd20d4
commit 1e1875cbd8

View File

@ -5,10 +5,13 @@ import java.lang.reflect.Method;
import java.util.Set; import java.util.Set;
import net.minecraft.server.Packet; import net.minecraft.server.Packet;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.CallbackFilter;
import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.Factory; import net.sf.cglib.proxy.Factory;
import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy; import net.sf.cglib.proxy.MethodProxy;
import net.sf.cglib.proxy.NoOp;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -80,29 +83,38 @@ public class NetworkServerInjector extends PlayerInjector {
Class<?> serverClass = serverHandler.getClass(); Class<?> serverClass = serverHandler.getClass();
Enhancer ex = new Enhancer(); Enhancer ex = new Enhancer();
ex.setClassLoader(manager.getClassLoader()); Callback sendPacketCallback = new MethodInterceptor() {
ex.setSuperclass(serverClass);
ex.setCallback(new MethodInterceptor() {
@Override @Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
// The send packet method! Packet packet = (Packet) args[0];
if (method.equals(sendPacketMethod)) {
Packet packet = (Packet) args[0];
if (packet != null) { if (packet != null) {
packet = handlePacketRecieved(packet); packet = handlePacketRecieved(packet);
// A NULL packet indicate cancelling // A NULL packet indicate cancelling
if (packet != null) if (packet != null)
args[0] = packet; args[0] = packet;
else else
return null; return null;
}
} }
// Call the method directly // Call the method directly
return proxy.invokeSuper(obj, args); return proxy.invokeSuper(obj, args);
};
};
Callback noOpCallback = NoOp.INSTANCE;
ex.setClassLoader(manager.getClassLoader());
ex.setSuperclass(serverClass);
ex.setCallbacks(new Callback[] { sendPacketCallback, noOpCallback });
ex.setCallbackFilter(new CallbackFilter() {
@Override
public int accept(Method method) {
if (method.equals(sendPacketMethod))
return 0;
else
return 1;
} }
}); });