mirror of
https://github.com/dmulloy2/ProtocolLib.git
synced 2024-12-17 22:57:51 +01:00
Improve Util.getOnlinePlayers()
This commit is contained in:
parent
ac54cef2fd
commit
a3784b3cf1
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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 <code>getOnlinePlayers</code>.
|
||||
*
|
||||
* @return A list of online Players
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<Player> 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<Player>) Bukkit.getOnlinePlayers();
|
||||
}
|
||||
}
|
@ -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 <code>getOnlinePlayers</code>.
|
||||
*
|
||||
* @return A list of currently online Players
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<Player> getOnlinePlayers() {
|
||||
if (reflectionRequired) {
|
||||
return Arrays.asList((Player[]) getOnlinePlayers.invoke(null));
|
||||
}
|
||||
|
||||
return (List<Player>) Bukkit.getOnlinePlayers();
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user