From 8c2f6bddd874ed8f88e7ba3ca150f52e61799671 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Mon, 25 Feb 2013 20:28:57 +0100 Subject: [PATCH] Moved classes that inject into the server thread to a separate package --- .../player/InjectedServerConnection.java | 1 + .../injector/player/NetLoginInjector.java | 2 + .../player/NetworkObjectInjector.java | 8 ++-- .../injector/player/PlayerInjector.java | 1 + .../player/ProxyPlayerInjectionHandler.java | 21 ++++----- .../DelegatedServerSocket.java | 2 +- .../{player => server}/DelegatedSocket.java | 2 +- .../DelegatedSocketInjector.java | 2 +- .../injector/server/InjectContainer.java | 19 ++++++++ .../InjectedServerSocket.java | 14 ++++-- .../{player => server}/SocketInjector.java | 4 +- .../TemporaryPlayerFactory.java | 47 +++++++++++-------- 12 files changed, 78 insertions(+), 45 deletions(-) rename ProtocolLib/src/main/java/com/comphenix/protocol/injector/{player => server}/DelegatedServerSocket.java (98%) rename ProtocolLib/src/main/java/com/comphenix/protocol/injector/{player => server}/DelegatedSocket.java (99%) rename ProtocolLib/src/main/java/com/comphenix/protocol/injector/{player => server}/DelegatedSocketInjector.java (96%) create mode 100644 ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/InjectContainer.java rename ProtocolLib/src/main/java/com/comphenix/protocol/injector/{player => server}/InjectedServerSocket.java (98%) rename ProtocolLib/src/main/java/com/comphenix/protocol/injector/{player => server}/SocketInjector.java (95%) rename ProtocolLib/src/main/java/com/comphenix/protocol/injector/{player => server}/TemporaryPlayerFactory.java (85%) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/InjectedServerConnection.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/InjectedServerConnection.java index d502ea9c..0c259f79 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/InjectedServerConnection.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/InjectedServerConnection.java @@ -27,6 +27,7 @@ import net.sf.cglib.proxy.Factory; import org.bukkit.Server; import com.comphenix.protocol.error.ErrorReporter; +import com.comphenix.protocol.injector.server.InjectedServerSocket; import com.comphenix.protocol.reflect.FieldUtils; import com.comphenix.protocol.reflect.FuzzyReflection; import com.comphenix.protocol.reflect.ObjectWriter; diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetLoginInjector.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetLoginInjector.java index 96c2e8e8..9632be59 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetLoginInjector.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetLoginInjector.java @@ -25,6 +25,8 @@ import org.bukkit.entity.Player; import com.comphenix.protocol.error.ErrorReporter; import com.comphenix.protocol.injector.GamePhase; +import com.comphenix.protocol.injector.server.InjectedServerSocket; +import com.comphenix.protocol.injector.server.SocketInjector; import com.comphenix.protocol.reflect.FuzzyReflection; import com.comphenix.protocol.utility.MinecraftReflection; import com.google.common.collect.Maps; diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetworkObjectInjector.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetworkObjectInjector.java index cfff267d..69acf343 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetworkObjectInjector.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/NetworkObjectInjector.java @@ -39,7 +39,7 @@ import com.comphenix.protocol.events.PacketListener; import com.comphenix.protocol.injector.GamePhase; import com.comphenix.protocol.injector.ListenerInvoker; import com.comphenix.protocol.injector.PacketFilterManager.PlayerInjectHooks; -import com.comphenix.protocol.injector.player.TemporaryPlayerFactory.InjectContainer; +import com.comphenix.protocol.injector.server.TemporaryPlayerFactory; /** * Injection method that overrides the NetworkHandler itself, and its queue-method. @@ -91,10 +91,8 @@ public class NetworkObjectInjector extends PlayerInjector { if (tempPlayerFactory == null) tempPlayerFactory = new TemporaryPlayerFactory(); - // Create and associate this fake player with this network injector - Player player = tempPlayerFactory.createTemporaryPlayer(server); - ((InjectContainer) player).setInjector(this); - return player; + // Create and associate the fake player with this network injector + return tempPlayerFactory.createTemporaryPlayer(server, this); } @Override diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjector.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjector.java index 9ac43b29..5764ac9a 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjector.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/PlayerInjector.java @@ -37,6 +37,7 @@ import com.comphenix.protocol.injector.BukkitUnwrapper; import com.comphenix.protocol.injector.GamePhase; import com.comphenix.protocol.injector.ListenerInvoker; import com.comphenix.protocol.injector.PacketFilterManager.PlayerInjectHooks; +import com.comphenix.protocol.injector.server.SocketInjector; import com.comphenix.protocol.reflect.FieldUtils; import com.comphenix.protocol.reflect.FuzzyReflection; import com.comphenix.protocol.reflect.StructureModifier; diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/ProxyPlayerInjectionHandler.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/ProxyPlayerInjectionHandler.java index 4c2cf262..908a1ac9 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/ProxyPlayerInjectionHandler.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/ProxyPlayerInjectionHandler.java @@ -36,7 +36,9 @@ import com.comphenix.protocol.injector.GamePhase; import com.comphenix.protocol.injector.ListenerInvoker; import com.comphenix.protocol.injector.PlayerLoggedOutException; import com.comphenix.protocol.injector.PacketFilterManager.PlayerInjectHooks; -import com.comphenix.protocol.injector.player.TemporaryPlayerFactory.InjectContainer; +import com.comphenix.protocol.injector.server.InjectedServerSocket; +import com.comphenix.protocol.injector.server.SocketInjector; +import com.comphenix.protocol.injector.server.TemporaryPlayerFactory; import com.google.common.base.Predicate; import com.google.common.collect.Maps; @@ -104,7 +106,7 @@ class ProxyPlayerInjectionHandler implements PlayerInjectionHandler { public void postWorldLoaded() { // This will actually create a socket and a seperate thread ... if (serverSocket != null) { - serverSocket.cycleServerPorts(); + serverSocket.postWorldLoaded(); } } @@ -498,16 +500,13 @@ class ProxyPlayerInjectionHandler implements PlayerInjectionHandler { if (injector == null) { // Try getting it from the player itself - if (player instanceof InjectContainer) { - SocketInjector socket = ((InjectContainer) player).getInjector(); - - // It may be a player injector too - if (socket instanceof PlayerInjector) - return (PlayerInjector) socket; + SocketInjector socket = TemporaryPlayerFactory.getInjectorFromPlayer(player); + + // Only accept it if it's a player injector + if (!(socket instanceof PlayerInjector)) { + socket = serverSocket.getSocketInjector(player.getAddress()); } - - SocketInjector socket = serverSocket.getSocketInjector(player.getAddress()); - + // Ensure that it is a player injector if (socket instanceof PlayerInjector) return (PlayerInjector) socket; diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/DelegatedServerSocket.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/DelegatedServerSocket.java similarity index 98% rename from ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/DelegatedServerSocket.java rename to ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/DelegatedServerSocket.java index 7b39f246..88b5607c 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/DelegatedServerSocket.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/DelegatedServerSocket.java @@ -1,4 +1,4 @@ -package com.comphenix.protocol.injector.player; +package com.comphenix.protocol.injector.server; import java.io.IOException; import java.net.InetAddress; diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/DelegatedSocket.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/DelegatedSocket.java similarity index 99% rename from ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/DelegatedSocket.java rename to ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/DelegatedSocket.java index af4bf3ed..b24975c9 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/DelegatedSocket.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/DelegatedSocket.java @@ -1,4 +1,4 @@ -package com.comphenix.protocol.injector.player; +package com.comphenix.protocol.injector.server; import java.io.IOException; import java.io.InputStream; diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/DelegatedSocketInjector.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/DelegatedSocketInjector.java similarity index 96% rename from ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/DelegatedSocketInjector.java rename to ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/DelegatedSocketInjector.java index f8d4afc1..6185a4b9 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/DelegatedSocketInjector.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/DelegatedSocketInjector.java @@ -1,4 +1,4 @@ -package com.comphenix.protocol.injector.player; +package com.comphenix.protocol.injector.server; import java.lang.reflect.InvocationTargetException; import java.net.Socket; diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/InjectContainer.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/InjectContainer.java new file mode 100644 index 00000000..24149a35 --- /dev/null +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/InjectContainer.java @@ -0,0 +1,19 @@ +package com.comphenix.protocol.injector.server; + +/** + * Able to store a socket injector. + *

+ * A necessary hack. + * @author Kristian + */ +class InjectContainer { + private SocketInjector injector; + + public SocketInjector getInjector() { + return injector; + } + + public void setInjector(SocketInjector injector) { + this.injector = injector; + } +} \ No newline at end of file diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/InjectedServerSocket.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/InjectedServerSocket.java similarity index 98% rename from ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/InjectedServerSocket.java rename to ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/InjectedServerSocket.java index 7231bb2a..058a1663 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/InjectedServerSocket.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/InjectedServerSocket.java @@ -1,4 +1,4 @@ -package com.comphenix.protocol.injector.player; +package com.comphenix.protocol.injector.server; import java.io.FilterInputStream; import java.io.IOException; @@ -23,7 +23,6 @@ import org.bukkit.entity.Player; import com.comphenix.protocol.Packets; import com.comphenix.protocol.error.ErrorReporter; -import com.comphenix.protocol.injector.player.TemporaryPlayerFactory.InjectContainer; import com.comphenix.protocol.reflect.FieldAccessException; import com.comphenix.protocol.reflect.FieldUtils; import com.comphenix.protocol.reflect.FuzzyReflection; @@ -234,12 +233,19 @@ public class InjectedServerSocket { } } + /** + * Invoked when the world has loaded. + */ + public void postWorldLoaded() { + cycleServerPorts(); + } + /** * Invoked when we need to cycle the injected server port. *

* This uses a fairly significant hack - we connect to our own server. */ - public void cycleServerPorts() { + private void cycleServerPorts() { final ServerSocket serverSocket = (ServerSocket) injectedServerSocket.getValue(); final SocketAddress address = new InetSocketAddress("127.0.0.1", serverSocket.getLocalPort()); @@ -423,7 +429,7 @@ public class InjectedServerSocket { injectedServerSocket.revertValue(); // This is going to suck - //cycleServerPorts(); + cycleServerPorts(); } } } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/SocketInjector.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/SocketInjector.java similarity index 95% rename from ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/SocketInjector.java rename to ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/SocketInjector.java index 9b7fc814..6407d320 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/SocketInjector.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/SocketInjector.java @@ -1,4 +1,4 @@ -package com.comphenix.protocol.injector.player; +package com.comphenix.protocol.injector.server; import java.lang.reflect.InvocationTargetException; import java.net.Socket; @@ -11,7 +11,7 @@ import org.bukkit.entity.Player; * * @author Kristian */ -interface SocketInjector { +public interface SocketInjector { /** * Retrieve the associated socket of this player. * @return The associated socket. diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/TemporaryPlayerFactory.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/TemporaryPlayerFactory.java similarity index 85% rename from ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/TemporaryPlayerFactory.java rename to ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/TemporaryPlayerFactory.java index c8df9197..75e5b49e 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/player/TemporaryPlayerFactory.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/server/TemporaryPlayerFactory.java @@ -15,7 +15,7 @@ * 02111-1307 USA */ -package com.comphenix.protocol.injector.player; +package com.comphenix.protocol.injector.server; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -36,25 +36,7 @@ import com.comphenix.protocol.reflect.FieldAccessException; /** * Create fake player instances that represents pre-authenticated clients. */ -class TemporaryPlayerFactory { - /** - * Able to store a socket injector. - *

- * A necessary hack. - * @author Kristian - */ - public static class InjectContainer { - private SocketInjector injector; - - public SocketInjector getInjector() { - return injector; - } - - public void setInjector(SocketInjector injector) { - this.injector = injector; - } - } - +public class TemporaryPlayerFactory { // Helpful constructors private final PacketConstructor chatPacket; @@ -65,6 +47,18 @@ class TemporaryPlayerFactory { chatPacket = PacketConstructor.DEFAULT.withPacket(3, new Object[] { "DEMO" }); } + /** + * Retrieve the injector from a given player if it contains one. + * @param player - the player that may contain a reference to a player injector. + * @return The referenced player injector, or NULL if none can be found. + */ + public static SocketInjector getInjectorFromPlayer(Player player) { + if (player instanceof InjectContainer) { + return ((InjectContainer) player).getInjector(); + } + return null; + } + /** * Construct a temporary player that supports a subset of every player command. *

@@ -164,6 +158,19 @@ class TemporaryPlayerFactory { return (Player) ex.create(); } + /** + * Construct a temporary player with the given associated socket injector. + * @param server - the parent server. + * @param injector - the referenced socket injector. + * @return The temporary player. + */ + public Player createTemporaryPlayer(Server server, SocketInjector injector) { + Player temporary = createTemporaryPlayer(server); + + ((InjectContainer) temporary).setInjector(injector); + return temporary; + } + /** * Send a message to the given client. * @param injector - the injector representing the client.