Added /searchenchant, fixed a couple little mistakes

Mistakes being breaking UUID-based lookups and /anychest's toggle.
MY BAD, SORRY.
This commit is contained in:
Jikoo 2016-11-27 06:20:44 -05:00
parent 6aa25dd2dc
commit 718b4bb5dd
5 changed files with 128 additions and 6 deletions

View file

@ -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;

View file

@ -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;
}
}

View file

@ -29,7 +29,7 @@ commands:
permission: OpenInv.*;OpenInv.search
description: Searches and lists players having a specific item in their ender chest
usage: |-
/<command> <item> [minAmount] - Item is the ID or the Bukkit Material, MinAmount is the minimum amount required
/<command> <item> [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: |-
/<command> [Check] - Checks or toggle anychest
searchenchant:
aliases: [searchenchants]
description: Search and list players with a specific enchantment.
permission: OpenInv.*;OpenInv.searchenchant
usage: |-
/<command> <[enchantment] [MinLevel]> - Enchantment is the enchantment type, MinLevel is the minimum level. One is optional

View file

@ -10,7 +10,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<openinv.version>3.0.1-SNAPSHOT</openinv.version>
<openinv.version>3.0.2</openinv.version>
</properties>
<profiles>