From 7faa9cbe859813a11938a56dc382ebf1a3f22514 Mon Sep 17 00:00:00 2001 From: filoghost Date: Thu, 3 Mar 2016 12:17:56 +0100 Subject: [PATCH] Fix old missing method --- .../chestcommands/command/CommandHandler.java | 3 +- .../chestcommands/task/RefreshMenusTask.java | 4 +- .../chestcommands/util/VersionUtils.java | 41 +++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 ChestCommands/src/com/gmail/filoghost/chestcommands/util/VersionUtils.java diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/command/CommandHandler.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/command/CommandHandler.java index 9b517f0..6882d26 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/command/CommandHandler.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/command/CommandHandler.java @@ -12,6 +12,7 @@ import com.gmail.filoghost.chestcommands.internal.ExtendedIconMenu; import com.gmail.filoghost.chestcommands.internal.MenuInventoryHolder; import com.gmail.filoghost.chestcommands.task.ErrorLoggerTask; import com.gmail.filoghost.chestcommands.util.ErrorLogger; +import com.gmail.filoghost.chestcommands.util.VersionUtils; public class CommandHandler extends CommandFramework { @@ -46,7 +47,7 @@ public class CommandHandler extends CommandFramework { if (args[0].equalsIgnoreCase("reload")) { CommandValidate.isTrue(sender.hasPermission(Permissions.COMMAND_BASE + "reload"), "You don't have permission."); - for (Player player : Bukkit.getOnlinePlayers()) { + for (Player player : VersionUtils.getOnlinePlayers()) { if (player.getOpenInventory() != null) { if (player.getOpenInventory().getTopInventory().getHolder() instanceof MenuInventoryHolder || player.getOpenInventory().getBottomInventory().getHolder() instanceof MenuInventoryHolder) { player.closeInventory(); diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/task/RefreshMenusTask.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/task/RefreshMenusTask.java index 3d16afc..b541c5a 100644 --- a/ChestCommands/src/com/gmail/filoghost/chestcommands/task/RefreshMenusTask.java +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/task/RefreshMenusTask.java @@ -1,12 +1,12 @@ package com.gmail.filoghost.chestcommands.task; -import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryView; import com.gmail.filoghost.chestcommands.internal.ExtendedIconMenu; import com.gmail.filoghost.chestcommands.internal.MenuInventoryHolder; +import com.gmail.filoghost.chestcommands.util.VersionUtils; public class RefreshMenusTask implements Runnable { @@ -15,7 +15,7 @@ public class RefreshMenusTask implements Runnable { @Override public void run() { - for (Player player : Bukkit.getOnlinePlayers()) { + for (Player player : VersionUtils.getOnlinePlayers()) { InventoryView view = player.getOpenInventory(); if (view == null) { diff --git a/ChestCommands/src/com/gmail/filoghost/chestcommands/util/VersionUtils.java b/ChestCommands/src/com/gmail/filoghost/chestcommands/util/VersionUtils.java new file mode 100644 index 0000000..b5f021b --- /dev/null +++ b/ChestCommands/src/com/gmail/filoghost/chestcommands/util/VersionUtils.java @@ -0,0 +1,41 @@ +package com.gmail.filoghost.chestcommands.util; + +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.Collections; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import com.google.common.collect.ImmutableList; + +public class VersionUtils { + + private static boolean setup; + private static Method oldGetOnlinePlayersMethod; + private static boolean useReflection; + + public static Collection getOnlinePlayers() { + try { + + if (!setup) { + oldGetOnlinePlayersMethod = Bukkit.class.getDeclaredMethod("getOnlinePlayers"); + if (oldGetOnlinePlayersMethod.getReturnType() == Player[].class) { + useReflection = true; + } + + setup = true; + } + + if (!useReflection) { + return Bukkit.getOnlinePlayers(); + } else { + Player[] playersArray = (Player[]) oldGetOnlinePlayersMethod.invoke(null); + return ImmutableList.copyOf(playersArray); + } + + } catch (Exception e) { + e.printStackTrace(); + return Collections.emptyList(); + } + } +}