Skip to content

Instantly share code, notes, and snippets.

@Scarsz
Created February 2, 2017 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Scarsz/33b3215f972fd67860b88aab3efc425c to your computer and use it in GitHub Desktop.
Save Scarsz/33b3215f972fd67860b88aab3efc425c to your computer and use it in GitHub Desktop.
Backwards-compatible Server::getOnlinePlayers::size
private int getOnlinePlayers() {
try {
Method onlinePlayerMethod = Server.class.getMethod("getOnlinePlayers");
if (onlinePlayerMethod.getReturnType().equals(Collection.class)) {
return ((Collection<?>) onlinePlayerMethod.invoke(Bukkit.getServer())).size();
} else {
return ((Player[]) onlinePlayerMethod.invoke(Bukkit.getServer())).length;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment