From b70c7fa775578da8156bfc3be5dd0b7c88d97f4e Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Mon, 9 Dec 2013 00:02:44 +0100 Subject: [PATCH] Update the player reference (from TemporaryPlayer) as soon as possible. --- .../injector/PacketFilterManager.java | 42 +++++++++++++------ .../injector/netty/NettyProtocolInjector.java | 12 +++--- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java index 02ef0eeb..be3d692e 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java @@ -922,9 +922,13 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok try { manager.registerEvents(new Listener() { - @EventHandler(priority = EventPriority.MONITOR) + @EventHandler(priority = EventPriority.LOWEST) public void onPrePlayerJoin(PlayerJoinEvent event) { PacketFilterManager.this.onPrePlayerJoin(event); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerJoin(PlayerJoinEvent event) { PacketFilterManager.this.onPlayerJoin(event); } @EventHandler(priority = EventPriority.MONITOR) @@ -945,20 +949,13 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok } private void onPrePlayerJoin(PlayerJoinEvent event) { - try { - // Let's clean up the other injection first. - playerInjection.uninjectPlayer(event.getPlayer().getAddress()); - playerInjection.updatePlayer(event.getPlayer()); - } catch (Exception e) { - reporter.reportDetailed(PacketFilterManager.this, - Report.newBuilder(REPORT_CANNOT_UNINJECT_PLAYER).callerParam(event).error(e) - ); - } + playerInjection.updatePlayer(event.getPlayer()); } private void onPlayerJoin(PlayerJoinEvent event) { try { - // This call will be ignored if no listeners are registered + // Let's clean up the other injection first. + playerInjection.uninjectPlayer(event.getPlayer().getAddress()); playerInjection.injectPlayer(event.getPlayer(), ConflictStrategy.OVERRIDE); } catch (ServerHandlerNull e) { // Caused by logged out players, or fake login events in MCPC++. Ignore it. @@ -1064,6 +1061,7 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok Class eventPriority = loader.loadClass("org.bukkit.event.Event$Priority"); // Get the priority + Object priorityLowest = Enum.valueOf(eventPriority, "Lowest"); Object priorityMonitor = Enum.valueOf(eventPriority, "Monitor"); // Get event types @@ -1079,9 +1077,28 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok Method registerEvent = FuzzyReflection.fromObject(manager).getMethodByParameters("registerEvent", eventTypes, Listener.class, eventPriority, Plugin.class); + Enhancer playerLow = new Enhancer(); Enhancer playerEx = new Enhancer(); Enhancer serverEx = new Enhancer(); + playerLow.setSuperclass(playerListener); + playerLow.setClassLoader(classLoader); + playerLow.setCallback(new MethodInterceptor() { + @Override + public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) + throws Throwable { + // Must have a parameter + if (args.length == 1) { + Object event = args[0]; + + if (event instanceof PlayerJoinEvent) { + onPrePlayerJoin((PlayerJoinEvent) event); + } + } + return null; + } + }); + playerEx.setSuperclass(playerListener); playerEx.setClassLoader(classLoader); playerEx.setCallback(new MethodInterceptor() { @@ -1092,7 +1109,6 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok // Check for the correct event if (event instanceof PlayerJoinEvent) { - onPrePlayerJoin((PlayerJoinEvent) event); onPlayerJoin((PlayerJoinEvent) event); } else if (event instanceof PlayerQuitEvent) { onPlayerQuit((PlayerQuitEvent) event); @@ -1120,9 +1136,11 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok }); // Create our listener + Object playerProxyLow = playerLow.create(); Object playerProxy = playerEx.create(); Object serverProxy = serverEx.create(); + registerEvent.invoke(manager, playerJoinType, playerProxyLow, priorityLowest, plugin); registerEvent.invoke(manager, playerJoinType, playerProxy, priorityMonitor, plugin); registerEvent.invoke(manager, playerQuitType, playerProxy, priorityMonitor, plugin); registerEvent.invoke(manager, pluginDisabledType, serverProxy, priorityMonitor, plugin); diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/NettyProtocolInjector.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/NettyProtocolInjector.java index 4d28097a..0089f652 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/NettyProtocolInjector.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/NettyProtocolInjector.java @@ -273,7 +273,12 @@ public class NettyProtocolInjector implements ChannelListener { @Override public void updatePlayer(Player player) { - // Ignore it + ChannelInjector.fromPlayer(player, listener).inject(); + + } + @Override + public void injectPlayer(Player player, ConflictStrategy strategy) { + ChannelInjector.fromPlayer(player, listener).inject(); } @Override @@ -313,11 +318,6 @@ public class NettyProtocolInjector implements ChannelListener { recieveClientPacket(mcPacket, null, true); } - @Override - public void injectPlayer(Player player, ConflictStrategy strategy) { - ChannelInjector.fromPlayer(player, listener).inject(); - } - @Override public PacketEvent handlePacketRecieved(PacketContainer packet, InputStream input, byte[] buffered) { // Ignore this