There is a method in the Bukkit API: getOnlinePlayers():
public static Collection<? extends Player> getOnlinePlayers()
{
return server.getOnlinePlayers();
}
In older versions of Bukkit it returns an array of Player[]; while in newer versions it returns a Collection<Player>.
I wanted to make it possible to have compatibility in both new and old versions, so I use reflection to invoke it:
public static Collection<Player> getOnlinePlayers(){
Method m;
Object obj = null;
try
{
m = Bukkit.class.getMethod("getOnlinePlayers");
obj = m.invoke(null, (Object[])null);
}catch (...){ // omitted
throw new RuntimeException(...); // omitted
}
if (obj instanceof Player[]){
System.out.println("array"); // Used for testing
return Arrays.asList((Player[])obj);
}else if (obj instanceof Collection){
System.out.println("collection"); // Used for testing
return (Collection<Player>)obj;
}else{
throw new RuntimeException(...); // omitted
}
}
Then here's the problem: When I tried this code in both new and old versions of Bukkit, it always prints "array". I am wondering why this would happen?
Aucun commentaire:
Enregistrer un commentaire