Move getOnlinePlayers() from Utils to BukkitService; delete Wrapper

This commit is contained in:
ljacqu
2016-04-23 15:24:41 +02:00
parent a78e0408c6
commit 59d3bc95c0
26 changed files with 262 additions and 428 deletions
+4 -71
View File
@@ -14,31 +14,16 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.Player;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
/**
* Utility class for various operations used in the codebase.
*/
public final class Utils {
private static AuthMe plugin;
private static Wrapper wrapper;
private static boolean getOnlinePlayersIsCollection = false;
private static Method getOnlinePlayers;
static {
wrapper = Wrapper.getInstance();
plugin = wrapper.getAuthMe();
initializeOnlinePlayersIsCollectionField();
}
private static AuthMe plugin = AuthMe.getInstance();
private Utils() {
// Utility class
}
/**
@@ -167,12 +152,12 @@ public final class Utils {
final World world = theWorld;
final Location loc = new Location(world, x, y, z);
Bukkit.getScheduler().scheduleSyncDelayedTask(wrapper.getAuthMe(), new Runnable() {
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@Override
public void run() {
AuthMeTeleportEvent tpEvent = new AuthMeTeleportEvent(pl, loc);
wrapper.getServer().getPluginManager().callEvent(tpEvent);
plugin.getServer().getPluginManager().callEvent(tpEvent);
if (!tpEvent.isCancelled()) {
pl.teleport(tpEvent.getTo());
}
@@ -180,58 +165,6 @@ public final class Utils {
});
}
/**
* Safe way to retrieve the list of online players from the server. Depending on the
* implementation of the server, either an array of {@link Player} instances is being returned,
* or a Collection. Always use this wrapper to retrieve online players instead of {@link
* Bukkit#getOnlinePlayers()} directly.
*
* @return collection of online players
*
* @see <a href="https://www.spigotmc.org/threads/solved-cant-use-new-getonlineplayers.33061/">SpigotMC
* forum</a>
* @see <a href="http://stackoverflow.com/questions/32130851/player-changed-from-array-to-collection">StackOverflow</a>
*/
@SuppressWarnings("unchecked")
public static Collection<? extends Player> getOnlinePlayers() {
if (getOnlinePlayersIsCollection) {
return Bukkit.getOnlinePlayers();
}
try {
// The lookup of a method via Reflections is rather expensive, so we keep a reference to it
if (getOnlinePlayers == null) {
getOnlinePlayers = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
}
Object obj = getOnlinePlayers.invoke(null);
if (obj instanceof Collection<?>) {
return (Collection<? extends Player>) obj;
} else if (obj instanceof Player[]) {
return Arrays.asList((Player[]) obj);
} else {
String type = (obj != null) ? obj.getClass().getName() : "null";
ConsoleLogger.showError("Unknown list of online players of type " + type);
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
ConsoleLogger.logException("Could not retrieve list of online players:", e);
}
return Collections.emptyList();
}
/**
* Method run when the Utils class is loaded to verify whether or not the Bukkit implementation
* returns the online players as a Collection.
*
* @see Utils#getOnlinePlayers()
*/
private static void initializeOnlinePlayersIsCollectionField() {
try {
Method method = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
getOnlinePlayersIsCollection = method.getReturnType() == Collection.class;
} catch (NoSuchMethodException e) {
ConsoleLogger.showError("Error verifying if getOnlinePlayers is a collection! Method doesn't exist");
}
}
public static boolean isNPC(Player player) {
return player.hasMetadata("NPC") || plugin.getPluginHooks().isNpcInCombatTagPlus(player);
}
@@ -240,7 +173,7 @@ public final class Utils {
if (Settings.isTeleportToSpawnEnabled && !Settings.noTeleport) {
Location spawn = plugin.getSpawnLocation(player);
AuthMeTeleportEvent tpEvent = new AuthMeTeleportEvent(player, spawn);
wrapper.getServer().getPluginManager().callEvent(tpEvent);
plugin.getServer().getPluginManager().callEvent(tpEvent);
if (!tpEvent.isCancelled()) {
player.teleport(tpEvent.getTo());
}