From de0d28b12d2c8f9ea0ffb4ac08928bb17bdb0966 Mon Sep 17 00:00:00 2001 From: ShadowRanger Date: Thu, 9 Jun 2016 22:43:20 +1000 Subject: [PATCH] Update to 1.10 --- .../openinv/commands/OpenInvCommand.java | 100 ++++++------------ 1 file changed, 32 insertions(+), 68 deletions(-) diff --git a/src/main/java/com/lishid/openinv/commands/OpenInvCommand.java b/src/main/java/com/lishid/openinv/commands/OpenInvCommand.java index 29b9094..addeba0 100644 --- a/src/main/java/com/lishid/openinv/commands/OpenInvCommand.java +++ b/src/main/java/com/lishid/openinv/commands/OpenInvCommand.java @@ -45,9 +45,12 @@ public class OpenInvCommand implements CommandExecutor { } @Override - public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (command.getName().equalsIgnoreCase("openinv")) { - final boolean isConsole = sender instanceof ConsoleCommandSender; + if (!(sender instanceof Player)) { + sender.sendMessage(ChatColor.RED + "You can't use this command from the console."); + return true; + } if (!OpenInv.hasPermission(sender, Permissions.PERM_OPENINV)) { sender.sendMessage(ChatColor.RED + "You do not have permission to access player inventories."); @@ -55,38 +58,34 @@ public class OpenInvCommand implements CommandExecutor { } if (args.length > 0 && args[0].equalsIgnoreCase("?")) { - OpenInv.showHelp(sender); + OpenInv.showHelp((Player) sender); return true; } - Player player = isConsole ? null : (Player) sender; + final Player player = (Player) sender; + + // History management + UUID history = openInvHistory.get(player.getUniqueId()); + if (history == null) { + history = player.getUniqueId(); + openInvHistory.put(player.getUniqueId(), history); + } + final UUID uuid; // Read from history if target is not named if (args.length < 1) { - if (isConsole) { - // TODO: Should this output the command's usage instead? - sender.sendMessage(ChatColor.RED + "Player not found!"); - return true; - } - - // History management - UUID history = openInvHistory.get(player.getUniqueId()); - if (history == null) { - history = player.getUniqueId(); - openInvHistory.put(player.getUniqueId(), history); - } - uuid = history; - } else { + } + else { uuid = UUIDUtils.getPlayerUUID(args[0]); if (uuid == null) { - sender.sendMessage(ChatColor.RED + "Player not found!"); + player.sendMessage(ChatColor.RED + "Player not found!"); return true; } } - final UUID playerUUID = isConsole ? null : player.getUniqueId(); + final UUID playerUUID = player.getUniqueId(); Player target = Bukkit.getPlayer(uuid); if (target == null) { @@ -97,40 +96,27 @@ public class OpenInvCommand implements CommandExecutor { // Try loading the player's data asynchronously final Player target = plugin.getPlayerLoader().loadPlayer(uuid); if (target == null) { - sender.sendMessage(ChatColor.RED + "Player not found!"); + player.sendMessage(ChatColor.RED + "Player not found!"); return; } - // Open/output target's inventory synchronously - if (isConsole) { - Bukkit.getScheduler().runTask(plugin, new Runnable() { - @Override - public void run() { - outputInventory(sender, target); + // Open target's inventory synchronously + Bukkit.getScheduler().runTask(plugin, new Runnable() { + @Override + public void run() { + Player player = Bukkit.getPlayer(playerUUID); + // If sender is no longer online after loading the target, abort! + if (player == null) { + return; } - }); - } else { - Bukkit.getScheduler().runTask(plugin, new Runnable() { - @Override - public void run() { - Player player = Bukkit.getPlayer(playerUUID); - // If sender is no longer online after loading the target, abort! - if (player == null) { - return; - } - openInventory(player, target); - } - }); - } + openInventory(player, target); + } + }); } }); } else { - if (isConsole) { - outputInventory(sender, target); - } else { - openInventory(player, target); - } + openInventory(player, target); } return true; @@ -139,28 +125,6 @@ public class OpenInvCommand implements CommandExecutor { return false; } - @SuppressWarnings("deprecation") - private void outputInventory(CommandSender sender, Player target) { - // Get the inventory and open it - SpecialPlayerInventory specialInv = plugin.getPlayerInventory(target, true); - Inventory inventory = specialInv.getBukkitInventory(); - - for (int slot = 0; slot < inventory.getSize(); slot++) { - ItemStack itemStack = inventory.getItem(slot); - - if (itemStack != null) { - String itemID = (itemStack.getDurability() != -1) - ? (itemStack.getTypeId() + ":" + itemStack.getDurability()) - : String.valueOf(itemStack.getTypeId()); - - sender.sendMessage(ChatColor.GREEN + "Slot " + slot + ": " + ChatColor.WHITE - + itemStack.getType().toString() + "(" + itemID + ") x" + itemStack.getAmount()); - } else { - sender.sendMessage(ChatColor.GREEN + "Slot " + slot + ": " + ChatColor.WHITE + "Empty"); - } - } - } - private void openInventory(Player player, Player target) { // Null target check if (target == null) {