Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search all slots with /searchinv #218

Merged
merged 2 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -164,7 +159,7 @@ public List<String> 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]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down