From a3784b3cf1583bb2aa78ead6f22ae9828943930c Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Thu, 12 Feb 2015 17:02:38 -0500 Subject: [PATCH] Improve Util.getOnlinePlayers() --- .../injector/PacketFilterManager.java | 8 ++-- .../comphenix/protocol/metrics/Metrics.java | 4 +- .../protocol/utility/BukkitUtil.java | 37 -------------- .../com/comphenix/protocol/utility/Util.java | 48 +++++++++++++++++++ .../protocol/wrappers/WrappedServerPing.java | 4 +- 5 files changed, 56 insertions(+), 45 deletions(-) delete mode 100644 ProtocolLib/src/main/java/com/comphenix/protocol/utility/BukkitUtil.java create mode 100644 ProtocolLib/src/main/java/com/comphenix/protocol/utility/Util.java 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 a92e64e0..86ab87e6 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/PacketFilterManager.java @@ -81,7 +81,7 @@ import com.comphenix.protocol.injector.player.PlayerInjectorBuilder; import com.comphenix.protocol.injector.spigot.SpigotPacketInjector; import com.comphenix.protocol.reflect.FieldAccessException; import com.comphenix.protocol.reflect.FuzzyReflection; -import com.comphenix.protocol.utility.BukkitUtil; +import com.comphenix.protocol.utility.Util; import com.comphenix.protocol.utility.EnhancerFactory; import com.comphenix.protocol.utility.MinecraftReflection; import com.comphenix.protocol.utility.MinecraftVersion; @@ -509,7 +509,7 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok unhookTask.cancel(); else // Inject our hook into already existing players - initializePlayers(BukkitUtil.getOnlinePlayers()); + initializePlayers(Util.getOnlinePlayers()); } } } @@ -530,7 +530,7 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok @Override public void run() { // Inject our hook into already existing players - uninitializePlayers(BukkitUtil.getOnlinePlayers()); + uninitializePlayers(Util.getOnlinePlayers()); } }); } @@ -710,7 +710,7 @@ public final class PacketFilterManager implements ProtocolManager, ListenerInvok @Override public void broadcastServerPacket(PacketContainer packet) { Preconditions.checkNotNull(packet, "packet cannot be NULL."); - broadcastServerPacket(packet, BukkitUtil.getOnlinePlayers()); + broadcastServerPacket(packet, Util.getOnlinePlayers()); } @Override diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/metrics/Metrics.java b/ProtocolLib/src/main/java/com/comphenix/protocol/metrics/Metrics.java index 4c94b4fb..d9f8930c 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/metrics/Metrics.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/metrics/Metrics.java @@ -69,7 +69,7 @@ import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; -import com.comphenix.protocol.utility.BukkitUtil; +import com.comphenix.protocol.utility.Util; import com.comphenix.protocol.utility.WrappedScheduler; /** @@ -375,7 +375,7 @@ public class Metrics { boolean onlineMode = Bukkit.getServer().getOnlineMode(); // TRUE if online mode is enabled String pluginVersion = description.getVersion(); String serverVersion = Bukkit.getVersion(); - int playersOnline = BukkitUtil.getOnlinePlayers().size(); + int playersOnline = Util.getOnlinePlayers().size(); // END server software specific section -- all code below does not use any code outside of this class / Java diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/BukkitUtil.java b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/BukkitUtil.java deleted file mode 100644 index 73ab58b8..00000000 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/BukkitUtil.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.comphenix.protocol.utility; - -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; - -/** - * Utility methods relating to Bukkit. - * @author dmulloy2 - */ - -public class BukkitUtil { - - private static Method getOnlinePlayers; - - /** - * Gets a list of online {@link Player}s. This also provides backwards - * compatibility as Bukkit changed getOnlinePlayers. - * - * @return A list of online Players - */ - @SuppressWarnings("unchecked") - public static List getOnlinePlayers() { - try { - if (getOnlinePlayers == null) - getOnlinePlayers = Bukkit.class.getMethod("getOnlinePlayers"); - if (getOnlinePlayers.getReturnType() != Collection.class) - return Arrays.asList((Player[]) getOnlinePlayers.invoke(null)); - } catch (Throwable ex) { - } - return (List) Bukkit.getOnlinePlayers(); - } -} \ No newline at end of file diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/Util.java b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/Util.java new file mode 100644 index 00000000..36087375 --- /dev/null +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/Util.java @@ -0,0 +1,48 @@ +package com.comphenix.protocol.utility; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import com.comphenix.protocol.reflect.accessors.Accessors; +import com.comphenix.protocol.reflect.accessors.MethodAccessor; + +/** + * General utility class + * + * @author dmulloy2 + */ + +public class Util { + private static MethodAccessor getOnlinePlayers; + private static boolean reflectionRequired; + + static { + try { + Method method = Bukkit.class.getMethod("getOnlinePlayers"); + getOnlinePlayers = Accessors.getMethodAccessor(method); + reflectionRequired = !method.getReturnType().isAssignableFrom(Collection.class); + } catch (Throwable ex) { + throw new RuntimeException("Failed to obtain getOnlinePlayers method.", ex); + } + } + + /** + * Gets a list of online {@link Player}s. This also provides backwards + * compatibility as Bukkit changed getOnlinePlayers. + * + * @return A list of currently online Players + */ + @SuppressWarnings("unchecked") + public static List getOnlinePlayers() { + if (reflectionRequired) { + return Arrays.asList((Player[]) getOnlinePlayers.invoke(null)); + } + + return (List) Bukkit.getOnlinePlayers(); + } +} \ No newline at end of file diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedServerPing.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedServerPing.java index c2d5ab67..3b03a136 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedServerPing.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedServerPing.java @@ -28,7 +28,7 @@ import com.comphenix.protocol.reflect.accessors.Accessors; import com.comphenix.protocol.reflect.accessors.ConstructorAccessor; import com.comphenix.protocol.reflect.accessors.FieldAccessor; import com.comphenix.protocol.reflect.accessors.MethodAccessor; -import com.comphenix.protocol.utility.BukkitUtil; +import com.comphenix.protocol.utility.Util; import com.comphenix.protocol.utility.MinecraftReflection; import com.comphenix.protocol.utility.MinecraftVersion; import com.google.common.base.Charsets; @@ -260,7 +260,7 @@ public class WrappedServerPing extends AbstractWrapper { // Recreate the count and maximum Server server = Bukkit.getServer(); setPlayersMaximum(server.getMaxPlayers()); - setPlayersOnline(BukkitUtil.getOnlinePlayers().size()); + setPlayersOnline(Util.getOnlinePlayers().size()); } else { PLAYERS.set(handle, players = null); }