Rework the MUtil.getOnlinePlayers slightly again.

This commit is contained in:
Olof Larsson 2014-12-31 11:18:55 +01:00
parent da3549da0e
commit 48291b7c4d

View File

@ -126,40 +126,43 @@ public class MUtil
public static Collection<? extends Player> getOnlinePlayers() public static Collection<? extends Player> getOnlinePlayers()
{ {
// Fetch some kind of playersObject.
Object playersObject = null;
try try
{ {
return Bukkit.getOnlinePlayers(); playersObject = Bukkit.getOnlinePlayers();
} }
catch (Throwable e) catch (Throwable t)
{ {
// That didn't work!
// We probably just caught a NoSuchMethodError. // We probably just caught a NoSuchMethodError.
// So let's try with reflection instead.
try
{
playersObject = methodGetOnlinePlayers.invoke(null);
}
catch (Exception e)
{
e.printStackTrace();
}
} }
try // Now return the playersObject.
if (playersObject instanceof Collection<?>)
{ {
Object playersObject = methodGetOnlinePlayers.invoke(null); @SuppressWarnings("unchecked")
if (playersObject instanceof Collection<?>) Collection<? extends Player> playersCollection = (Collection<? extends Player>)playersObject;
{ return playersCollection;
@SuppressWarnings("unchecked")
Collection<? extends Player> playersCollection = (Collection<? extends Player>)playersObject;
return playersCollection;
}
else if (playersObject instanceof Player[])
{
Player[] playersArray = (Player[])playersObject;
return Arrays.asList(playersArray);
}
else
{
throw new RuntimeException("Unknown return type for getOnlinePlayers using reflection.");
}
} }
catch (Exception e) else if (playersObject instanceof Player[])
{ {
e.printStackTrace(); Player[] playersArray = (Player[])playersObject;
return Arrays.asList(playersArray);
}
else
{
throw new RuntimeException("Failed retrieving online players.");
} }
return null;
} }
// -------------------------------------------- // // -------------------------------------------- //