From 25f54a3d0c27a312ad6c8e4dc7b4cf19cfdf306d Mon Sep 17 00:00:00 2001 From: sk89q Date: Sat, 29 Jan 2011 21:31:07 -0800 Subject: [PATCH] Added /searchitem or //l command to lookup items and block by name and ID. --- plugin.yml | 4 + src/com/sk89q/worldedit/blocks/ItemType.java | 9 +++ .../worldedit/commands/GeneralCommands.java | 78 +++++++++++++++++++ 3 files changed, 91 insertions(+) diff --git a/plugin.yml b/plugin.yml index ebba11b69..825218dd2 100644 --- a/plugin.yml +++ b/plugin.yml @@ -38,6 +38,10 @@ commands: toggleplace: description: usage: / + searchitem: + description: Search for an item + usage: / + aliases: ['/l'] /limit: description: Modify block change limit usage: / diff --git a/src/com/sk89q/worldedit/blocks/ItemType.java b/src/com/sk89q/worldedit/blocks/ItemType.java index 683838858..04e6c43e2 100644 --- a/src/com/sk89q/worldedit/blocks/ItemType.java +++ b/src/com/sk89q/worldedit/blocks/ItemType.java @@ -300,6 +300,15 @@ public enum ItemType { return name; } + /** + * Get a list of aliases. + * + * @return + */ + public String[] getAliases() { + return lookupKeys; + } + /** * Returns true if an item should not be stacked. * diff --git a/src/com/sk89q/worldedit/commands/GeneralCommands.java b/src/com/sk89q/worldedit/commands/GeneralCommands.java index f80361920..695e6bdbf 100644 --- a/src/com/sk89q/worldedit/commands/GeneralCommands.java +++ b/src/com/sk89q/worldedit/commands/GeneralCommands.java @@ -22,6 +22,7 @@ package com.sk89q.worldedit.commands; import com.sk89q.util.commands.Command; import com.sk89q.util.commands.CommandContext; import com.sk89q.worldedit.*; +import com.sk89q.worldedit.blocks.ItemType; /** * General WorldEdit commands. @@ -74,4 +75,81 @@ public class GeneralCommands { player.print("Now placing at the block you stand in."); } } + + @Command( + aliases = {"searchitem", "/l"}, + usage = "", + flags = "bi", + desc = "Search for an item", + min = 1, + max = 1 + ) + public static void searchItem(CommandContext args, WorldEdit we, + LocalSession session, LocalPlayer player, EditSession editSession) + throws WorldEditException { + + String query = args.getString(0).trim().toLowerCase(); + boolean blocksOnly = args.hasFlag('b'); + boolean itemsOnly = args.hasFlag('i'); + + try { + int id = Integer.parseInt(query); + + ItemType type = ItemType.fromID(id); + + if (type != null) { + player.print("#" + type.getID() + " (" + type.getName() + ")"); + } else { + player.printError("No item found by ID " + id); + } + + return; + } catch (NumberFormatException e) { + } + + if (query.length() <= 2) { + player.printError("Enter a longer search string (len > 2)."); + return; + } + + if (!blocksOnly && !itemsOnly) { + player.print("Searching for: " + query); + } else if (blocksOnly && itemsOnly) { + player.printError("You cannot use both the 'b' and 'i' flags simultaneously."); + return; + } else if (blocksOnly) { + player.print("Searching for blocks: " + query); + } else { + player.print("Searching for items: " + query); + } + + int found = 0; + + for (ItemType type : ItemType.values()) { + if (found >= 15) { + player.print("Too many results!"); + break; + } + + if (blocksOnly && type.getID() > 255) { + continue; + } + + if (itemsOnly && type.getID() <= 255) { + continue; + } + + for (String alias : type.getAliases()) { + if (alias.contains(query)) { + player.print("#" + type.getID() + " (" + type.getName() + ")"); + found++; + break; + } + } + } + + if (found == 0) { + player.printError("No items found."); + } + } }