diff --git a/plugin/src/main/java/com/lishid/openinv/OpenInv.java b/plugin/src/main/java/com/lishid/openinv/OpenInv.java index 6490af8..57b034c 100644 --- a/plugin/src/main/java/com/lishid/openinv/OpenInv.java +++ b/plugin/src/main/java/com/lishid/openinv/OpenInv.java @@ -27,6 +27,7 @@ import java.util.concurrent.Future; import com.lishid.openinv.commands.AnyChestPluginCommand; import com.lishid.openinv.commands.OpenEnderPluginCommand; import com.lishid.openinv.commands.OpenInvPluginCommand; +import com.lishid.openinv.commands.SearchEnchantPluginCommand; import com.lishid.openinv.commands.SearchInvPluginCommand; import com.lishid.openinv.commands.SilentChestPluginCommand; import com.lishid.openinv.internal.IAnySilentContainer; @@ -123,12 +124,13 @@ public class OpenInv extends JavaPlugin { pm.registerEvents(new OpenInvInventoryListener(this), this); getCommand("openinv").setExecutor(new OpenInvPluginCommand(this)); + getCommand("openender").setExecutor(new OpenEnderPluginCommand(this)); SearchInvPluginCommand searchInv = new SearchInvPluginCommand(); getCommand("searchinv").setExecutor(searchInv); getCommand("searchender").setExecutor(searchInv); + getCommand("searchenchant").setExecutor(new SearchEnchantPluginCommand()); getCommand("silentchest").setExecutor(new SilentChestPluginCommand(this)); getCommand("anychest").setExecutor(new AnyChestPluginCommand(this)); - getCommand("openender").setExecutor(new OpenEnderPluginCommand(this)); } @@ -286,7 +288,7 @@ public class OpenInv extends JavaPlugin { * @param status the status */ public void setPlayerAnyChestStatus(OfflinePlayer player, boolean status) { - getConfig().set("toggles.silent-chest." + playerLoader.getPlayerDataID(player), status); + getConfig().set("toggles.any-chest." + playerLoader.getPlayerDataID(player), status); saveConfig(); } @@ -326,6 +328,10 @@ public class OpenInv extends JavaPlugin { // Attempt exact offline match first - adds UUID support for later versions OfflinePlayer player = this.playerLoader.getPlayerByID(name); + if (player != null) { + return player; + } + // Ensure name is valid if server is in online mode to avoid unnecessary searching if (getServer().getOnlineMode() && !name.matches("[a-zA-Z0-9_]{3,16}")) { return null; diff --git a/plugin/src/main/java/com/lishid/openinv/commands/SearchEnchantPluginCommand.java b/plugin/src/main/java/com/lishid/openinv/commands/SearchEnchantPluginCommand.java new file mode 100644 index 0000000..4e8a16d --- /dev/null +++ b/plugin/src/main/java/com/lishid/openinv/commands/SearchEnchantPluginCommand.java @@ -0,0 +1,111 @@ +package com.lishid.openinv.commands; + +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +/** + * Command adding the ability to search online players' inventories for enchantments of a specific + * type at or above the level specified. + * + * @author Jikoo + */ +public class SearchEnchantPluginCommand implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (args.length == 0) { + return false; + } + + Enchantment enchant = null; + int level = 0; + + for (String argument : args) { + Enchantment localEnchant = Enchantment.getByName(argument.toUpperCase()); + if (localEnchant != null) { + enchant = localEnchant; + continue; + } + try { + level = Integer.parseInt(argument); + } catch (NumberFormatException ignored) {} + } + + // Arguments not set correctly + if (level == 0 && enchant == null) { + return false; + } + + StringBuilder players = new StringBuilder(); + for (Player player : Bukkit.getServer().getOnlinePlayers()) { + boolean flagInventory = containsEnchantment(player.getInventory(), enchant, level); + boolean flagEnder = containsEnchantment(player.getEnderChest(), enchant, level); + + // No matches, continue + if (!flagInventory && !flagEnder) { + continue; + } + + // Matches, append details + players.append(player.getName()).append(" ("); + if (flagInventory) { + players.append("inv"); + } + if (flagEnder) { + if (flagInventory) { + players.append(','); + } + players.append("ender"); + } + players.append("), "); + } + + if (players.length() > 0) { + // Matches found, delete trailing comma and space + players.delete(players.length() - 2, players.length()); + } else { + sender.sendMessage("No players found with " + (enchant == null ? "any enchant" : enchant.getName()) + + " of level " + level + " or higher."); + return true; + } + + sender.sendMessage("Players: " + players.toString()); + return true; + } + + private boolean containsEnchantment(Inventory inventory, Enchantment enchant, int minLevel) { + for (ItemStack item : inventory.getContents()) { + if (item == null || item.getType() == Material.AIR) { + continue; + } + if (enchant != null) { + if (item.containsEnchantment(enchant) && item.getEnchantmentLevel(enchant) >= minLevel) { + return true; + } + } else { + if (!item.hasItemMeta()) { + continue; + } + ItemMeta meta = item.getItemMeta(); + if (!meta.hasEnchants()) { + continue; + } + for (int enchLevel : meta.getEnchants().values()) { + if (enchLevel >= minLevel) { + return true; + } + } + } + } + return false; + } + +} diff --git a/plugin/src/main/java/com/lishid/openinv/commands/SearchInvPluginCommand.java b/plugin/src/main/java/com/lishid/openinv/commands/SearchInvPluginCommand.java index 04ee588..9dc2966 100644 --- a/plugin/src/main/java/com/lishid/openinv/commands/SearchInvPluginCommand.java +++ b/plugin/src/main/java/com/lishid/openinv/commands/SearchInvPluginCommand.java @@ -68,7 +68,7 @@ public class SearchInvPluginCommand implements CommandExecutor { sender.sendMessage("No players found with " + material.toString()); } - sender.sendMessage("Players with the item " + material.toString() + ": " + players.toString()); + sender.sendMessage("Players with the item " + material.toString() + ": " + players.toString()); return true; } diff --git a/plugin/src/main/resources/plugin.yml b/plugin/src/main/resources/plugin.yml index f1fed56..cd626ba 100644 --- a/plugin/src/main/resources/plugin.yml +++ b/plugin/src/main/resources/plugin.yml @@ -29,7 +29,7 @@ commands: permission: OpenInv.*;OpenInv.search description: Searches and lists players having a specific item in their ender chest usage: |- - / [minAmount] - Item is the ID or the Bukkit Material, MinAmount is the minimum amount required + / [MinAmount] - Item is the ID or the Bukkit Material, MinAmount is the minimum amount required silentchest: aliases: [sc, silent] description: Toggle silent chest function, which stops sounds and animations when using containers. @@ -42,4 +42,9 @@ commands: permission: OpenInv.*;OpenInv.anychest usage: |- / [Check] - Checks or toggle anychest - + searchenchant: + aliases: [searchenchants] + description: Search and list players with a specific enchantment. + permission: OpenInv.*;OpenInv.searchenchant + usage: |- + / <[enchantment] [MinLevel]> - Enchantment is the enchantment type, MinLevel is the minimum level. One is optional diff --git a/pom.xml b/pom.xml index 8ca9425..86e93ae 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ UTF-8 - 3.0.1-SNAPSHOT + 3.0.2