diff --git a/plugin/src/main/java/com/lishid/openinv/commands/SearchContainerCommand.java b/plugin/src/main/java/com/lishid/openinv/commands/SearchContainerCommand.java index 161dfedf..b524b2f9 100644 --- a/plugin/src/main/java/com/lishid/openinv/commands/SearchContainerCommand.java +++ b/plugin/src/main/java/com/lishid/openinv/commands/SearchContainerCommand.java @@ -105,7 +105,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command } // Matches found, delete trailing comma and space - if (locations.length() > 0) { + if (!locations.isEmpty()) { locations.delete(locations.length() - 2, locations.length()); } else { plugin.sendMessage( diff --git a/plugin/src/main/java/com/lishid/openinv/commands/SearchEnchantCommand.java b/plugin/src/main/java/com/lishid/openinv/commands/SearchEnchantCommand.java index 2c52f5d9..8abaec9e 100644 --- a/plugin/src/main/java/com/lishid/openinv/commands/SearchEnchantCommand.java +++ b/plugin/src/main/java/com/lishid/openinv/commands/SearchEnchantCommand.java @@ -19,10 +19,9 @@ import com.lishid.openinv.OpenInv; import com.lishid.openinv.util.TabCompleter; import com.lishid.openinv.util.lang.Replacement; -import java.util.Collections; -import java.util.List; import org.bukkit.Material; import org.bukkit.NamespacedKey; +import org.bukkit.Registry; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; @@ -34,6 +33,9 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Collections; +import java.util.List; + /** * Command adding the ability to search online players' inventories for enchantments of a specific * type at or above the level specified. @@ -64,19 +66,12 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command } catch (NumberFormatException ignored) {} argument = argument.toLowerCase(); - int colon = argument.indexOf(':'); - NamespacedKey key; - try { - if (colon > -1 && colon < argument.length() - 1) { - key = new NamespacedKey(argument.substring(0, colon), argument.substring(colon + 1)); - } else { - key = NamespacedKey.minecraft(argument); - } - } catch (IllegalArgumentException ignored) { + NamespacedKey key = NamespacedKey.fromString(argument); + if (key == null) { continue; } - Enchantment localEnchant = Enchantment.getByKey(key); + Enchantment localEnchant = Registry.ENCHANTMENT.get(key); if (localEnchant != null) { enchant = localEnchant; } @@ -111,7 +106,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command players.append("), "); } - if (players.length() > 0) { + if (!players.isEmpty()) { // Matches found, delete trailing comma and space players.delete(players.length() - 2, players.length()); } else { @@ -164,7 +159,7 @@ public List onTabComplete(@NotNull CommandSender sender, @NotNull Comman } if (args.length == 1) { - return TabCompleter.completeObject(args[0], enchantment -> enchantment.getKey().toString(), Enchantment.values()); + return TabCompleter.completeObject(args[0], enchantment -> enchantment.getKey().toString(), Registry.ENCHANTMENT.stream().toArray(Enchantment[]::new)); } else { return TabCompleter.completeInteger(args[1]); } diff --git a/plugin/src/main/java/com/lishid/openinv/commands/SearchInvCommand.java b/plugin/src/main/java/com/lishid/openinv/commands/SearchInvCommand.java index 60235bd4..f40588c5 100644 --- a/plugin/src/main/java/com/lishid/openinv/commands/SearchInvCommand.java +++ b/plugin/src/main/java/com/lishid/openinv/commands/SearchInvCommand.java @@ -19,16 +19,18 @@ import com.lishid.openinv.OpenInv; import com.lishid.openinv.util.TabCompleter; import com.lishid.openinv.util.lang.Replacement; -import java.util.Collections; -import java.util.List; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; +import java.util.Collections; +import java.util.List; + public class SearchInvCommand implements TabExecutor { private final OpenInv plugin; @@ -72,13 +74,20 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command boolean searchInv = command.getName().equals("searchinv"); for (Player player : plugin.getServer().getOnlinePlayers()) { Inventory inventory = searchInv ? player.getInventory() : player.getEnderChest(); - if (inventory.contains(material, count)) { - players.append(player.getName()).append(", "); + int total = 0; + for (ItemStack itemStack : inventory.getContents()) { + if (itemStack != null && itemStack.getType() == material) { + total += itemStack.getAmount(); + if (total >= count) { + players.append(player.getName()).append(", "); + break; + } + } } } // Matches found, delete trailing comma and space - if (players.length() > 0) { + if (!players.isEmpty()) { players.delete(players.length() - 2, players.length()); } else { plugin.sendMessage(