Handling version issues with Bukkit (especially Bukkit.getServer().getOnlinePlayers())

A lot of plugins have several different versions for several different Bukkit versions, but there are ways around this!

A popular issue is the Bukkit.getServer().getOnlinePlayers() method which was changed with the version 1.6 or 1.7 to return a Collection instead of an Array. For such cases, you just have to decide between those two cases, and work with what you got:
for this example as a solution we grab the method, because Java wants to know what kind of object it will get by calling Object playerList
= Server.getMethod("getOnlinePlayers").invoke(Bukkit.getServer()) and then have our getOnlinePlayers() object:
now we can test it whether it's a collection or not:
if(playerList instanceof Collection<?>){
// it's a collection
} else {
// it's probably an array
and then work with the result (cast it).

Another example is the Material.LOG_2 which was added in 1.7:
a try, e.g. by calling Material.LOG_2.name() and catching the error (NoSuchMethodError) will tell you whether it exists. If it doesn't, you just have to avoid tests with it (by && or || or sth similar).

Thx for reading and a Happy New Year! :)

#Bukkit #Minecraft #Versions #Updates #Plugins

201905307s ago, by Antonio