From d04a666bc5fcd8bb3f135058fc9d15fb04564e07 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Sat, 15 Sep 2012 23:07:14 +0200 Subject: [PATCH] Readded a lighter version of network object injector. This version only supports CraftBukkit 1.3.0 and later. --- .../injector/NetworkObjectInjector.java | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 ProtocolLib/src/com/comphenix/protocol/injector/NetworkObjectInjector.java diff --git a/ProtocolLib/src/com/comphenix/protocol/injector/NetworkObjectInjector.java b/ProtocolLib/src/com/comphenix/protocol/injector/NetworkObjectInjector.java new file mode 100644 index 00000000..2981a3db --- /dev/null +++ b/ProtocolLib/src/com/comphenix/protocol/injector/NetworkObjectInjector.java @@ -0,0 +1,97 @@ +package com.comphenix.protocol.injector; + +import java.lang.reflect.InvocationTargetException; + +import net.minecraft.server.Packet; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Proxy; +import java.lang.reflect.Method; +import java.util.Set; + +import org.bukkit.entity.Player; + +/** + * Injection method that overrides the NetworkHandler itself, and it's sendPacket-method. + * + * @author Kristian + */ +public class NetworkObjectInjector extends PlayerInjector { + public NetworkObjectInjector(Player player, PacketFilterManager manager, Set sendingFilters) throws IllegalAccessException { + super(player, manager, sendingFilters); + } + + @Override + public void sendServerPacket(Packet packet, boolean filtered) throws InvocationTargetException { + Object networkDelegate = filtered ? networkManagerRef.getValue() : networkManagerRef.getOldValue(); + + if (networkDelegate != null) { + try { + // Note that invocation target exception is a wrapper for a checked exception + queueMethod.invoke(networkDelegate, packet); + + } catch (IllegalArgumentException e) { + throw e; + } catch (InvocationTargetException e) { + throw e; + } catch (IllegalAccessException e) { + throw new IllegalStateException("Unable to access queue method.", e); + } + } else { + throw new IllegalStateException("Unable to load network mananager. Cannot send packet."); + } + } + + @Override + public void injectManager() { + + if (networkManager != null) { + final Class networkInterface = networkManagerField.getType(); + final Object networkDelegate = networkManagerRef.getOldValue(); + + if (!networkInterface.isInterface()) { + throw new UnsupportedOperationException( + "Must use CraftBukkit 1.3.0 or later to inject into into NetworkMananger."); + } + + // Create our proxy object + Object networkProxy = Proxy.newProxyInstance(networkInterface.getClassLoader(), + new Class[] { networkInterface }, new InvocationHandler() { + + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + // OH OH! The queue method! + if (method.equals(queueMethod)) { + Packet packet = (Packet) args[0]; + + if (packet != null) { + packet = handlePacketRecieved(packet); + + // A NULL packet indicate cancelling + if (packet != null) + args[0] = packet; + else + return null; + } + } + + // Delegate to our underlying class + try { + return method.invoke(networkDelegate, args); + } catch (InvocationTargetException e) { + throw e.getCause(); + } + } + }); + + // Inject it, if we can. + networkManagerRef.setValue(networkProxy); + } + } + + @Override + public void cleanupAll() { + // Clean up + networkManagerRef.revertValue(); + } +}