Fix getOnlinePlayers call to support all versions
Moved IPlayerDataManager from api module to common module - it is not part of the API as there is no supported way to obtain an instance of it.
This commit is contained in:
@@ -16,6 +16,10 @@
|
||||
|
||||
package com.lishid.openinv;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@@ -143,10 +147,10 @@ public class OpenInv extends JavaPlugin implements IOpenInv {
|
||||
// Register commands to their executors
|
||||
getCommand("openinv").setExecutor(new OpenInvPluginCommand(this));
|
||||
getCommand("openender").setExecutor(new OpenEnderPluginCommand(this));
|
||||
SearchInvPluginCommand searchInv = new SearchInvPluginCommand();
|
||||
SearchInvPluginCommand searchInv = new SearchInvPluginCommand(this);
|
||||
getCommand("searchinv").setExecutor(searchInv);
|
||||
getCommand("searchender").setExecutor(searchInv);
|
||||
getCommand("searchenchant").setExecutor(new SearchEnchantPluginCommand());
|
||||
getCommand("searchenchant").setExecutor(new SearchEnchantPluginCommand(this));
|
||||
getCommand("silentchest").setExecutor(new SilentChestPluginCommand(this));
|
||||
getCommand("anychest").setExecutor(new AnyChestPluginCommand(this));
|
||||
|
||||
@@ -526,6 +530,36 @@ public class OpenInv extends JavaPlugin implements IOpenInv {
|
||||
this.pluginUsage.removeAll(plugin.getClass());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Collection<? extends Player> getOnlinePlayers() {
|
||||
|
||||
if (this.playerLoader != null) {
|
||||
return this.playerLoader.getOnlinePlayers();
|
||||
}
|
||||
|
||||
Method getOnlinePlayers;
|
||||
try {
|
||||
getOnlinePlayers = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
Object onlinePlayers;
|
||||
try {
|
||||
onlinePlayers = getOnlinePlayers.invoke(null);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
if (onlinePlayers instanceof List) {
|
||||
return (Collection<Player>) onlinePlayers;
|
||||
}
|
||||
|
||||
return Arrays.asList((Player[]) onlinePlayers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method for handling a Player coming online.
|
||||
*
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package com.lishid.openinv.commands;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import com.lishid.openinv.OpenInv;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
@@ -19,6 +20,12 @@ import org.bukkit.inventory.meta.ItemMeta;
|
||||
*/
|
||||
public class SearchEnchantPluginCommand implements CommandExecutor {
|
||||
|
||||
private final OpenInv plugin;
|
||||
|
||||
public SearchEnchantPluginCommand(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (args.length == 0) {
|
||||
@@ -45,7 +52,7 @@ public class SearchEnchantPluginCommand implements CommandExecutor {
|
||||
}
|
||||
|
||||
StringBuilder players = new StringBuilder();
|
||||
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
|
||||
for (Player player : plugin.getOnlinePlayers()) {
|
||||
boolean flagInventory = containsEnchantment(player.getInventory(), enchant, level);
|
||||
boolean flagEnder = containsEnchantment(player.getEnderChest(), enchant, level);
|
||||
|
||||
|
@@ -16,7 +16,8 @@
|
||||
|
||||
package com.lishid.openinv.commands;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import com.lishid.openinv.OpenInv;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.Command;
|
||||
@@ -27,6 +28,12 @@ import org.bukkit.inventory.Inventory;
|
||||
|
||||
public class SearchInvPluginCommand implements CommandExecutor {
|
||||
|
||||
private final OpenInv plugin;
|
||||
|
||||
public SearchInvPluginCommand(OpenInv plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
|
||||
@@ -54,7 +61,7 @@ public class SearchInvPluginCommand implements CommandExecutor {
|
||||
}
|
||||
|
||||
StringBuilder players = new StringBuilder();
|
||||
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
|
||||
for (Player player : plugin.getOnlinePlayers()) {
|
||||
Inventory inventory = command.getName().equals("searchinv") ? player.getInventory() : player.getEnderChest();
|
||||
if (inventory.contains(material, count)) {
|
||||
players.append(player.getName()).append(", ");
|
||||
|
Reference in New Issue
Block a user