Added command flag support, removed / prefix from command handling process.

This commit is contained in:
sk89q 2011-01-29 11:36:28 -08:00
parent d1ff0250aa
commit 059f30808d
16 changed files with 140 additions and 86 deletions

View File

@ -28,4 +28,5 @@
String desc();
int min();
int max();
String flags() default "";
}

View File

@ -18,14 +18,35 @@
package com.sk89q.util.commands;
import java.util.HashSet;
import java.util.Set;
public class CommandContext {
protected String[] args;
protected Set<Character> flags = new HashSet<Character>();
public CommandContext(String args) {
this.args = args.split(" ");
this(args.split(" "));
}
public CommandContext(String[] args) {
int i = 1;
for (; i < args.length; i++) {
if (args[i].charAt(0) == '-') {
for (int k = 1; k < args[i].length(); k++) {
flags.add(args[i].charAt(k));
}
} else {
break;
}
}
String[] newArgs = new String[args.length - i + 1];
System.arraycopy(args, i, newArgs, 1, args.length - i);
newArgs[0] = args[0];
this.args = args;
}
@ -70,6 +91,14 @@ public String[] getPaddedSlice(int index, int padding) {
return slice;
}
public boolean hasFlag(char ch) {
return flags.contains(ch);
}
public Set<Character> getFlags() {
return flags;
}
public int length() {
return args.length;
}

View File

@ -678,12 +678,13 @@ public boolean handleBlockLeftClick(LocalPlayer player, WorldVector clicked) {
*/
public boolean handleCommand(LocalPlayer player, String[] split) {
try {
split[0] = split[0].substring(1);
// Quick script shortcut
if (split[0].matches("^/[^/].*\\.js$")) {
if (split[0].matches("^[^/].*\\.js$")) {
String[] newSplit = new String[split.length + 1];
System.arraycopy(split, 0, newSplit, 1, split.length);
newSplit[0] = "/cs";
newSplit[0] = "cs";
newSplit[1] = newSplit[1].substring(1);
split = newSplit;
}
@ -691,7 +692,7 @@ public boolean handleCommand(LocalPlayer player, String[] split) {
String searchCmd = split[0].toLowerCase();
if (commands.hasCommand(searchCmd)
|| (config.noDoubleSlash && commands.hasCommand("/" + searchCmd))
|| ((searchCmd.length() < 3 || searchCmd.charAt(2) != '/')
|| (searchCmd.length() >= 2 && searchCmd.charAt(0) == '/'
&& commands.hasCommand(searchCmd.substring(1)))) {
if (config.noDoubleSlash && commands.hasCommand("/" + searchCmd)) {
split[0] = "/" + split[0];

View File

@ -33,7 +33,7 @@
*/
public class ChunkCommands {
@Command(
aliases = {"/chunkinfo"},
aliases = {"chunkinfo"},
usage = "",
desc = "Get information about the chunk that you are inside",
min = 0,
@ -58,7 +58,7 @@ public static void chunkInfo(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/listchunks"},
aliases = {"listchunks"},
usage = "",
desc = "List chunks that your selection includes",
min = 0,
@ -77,7 +77,7 @@ public static void listChunks(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/delchunks"},
aliases = {"delchunks"},
usage = "",
desc = "Delete chunks that your selection includes",
min = 0,

View File

@ -35,7 +35,7 @@
*/
public class ClipboardCommands {
@Command(
aliases = {"//copy"},
aliases = {"/copy"},
usage = "",
desc = "Copy the selection to the clipboard",
min = 0,
@ -61,7 +61,7 @@ public static void copy(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//cut"},
aliases = {"/cut"},
usage = "[leave-id]",
desc = "Cut the selection to the clipboard",
min = 0,
@ -94,7 +94,7 @@ public static void cut(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//paste"},
aliases = {"/paste"},
usage = "[at-origin?]",
desc = "Paste the clipboard's contents",
min = 0,
@ -124,7 +124,7 @@ public static void paste(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//rotate"},
aliases = {"/rotate"},
usage = "<angle-in-degrees>",
desc = "Rotate the contents of the clipboard",
min = 1,
@ -147,7 +147,7 @@ public static void rotate(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//flip"},
aliases = {"/flip"},
usage = "[dir]",
desc = "Flip the contents of the clipboard",
min = 0,
@ -167,7 +167,7 @@ public static void flip(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//load"},
aliases = {"/load"},
usage = "<filename>",
desc = "Load a schematic into your clipboard",
min = 1,
@ -209,7 +209,7 @@ public static void load(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//save"},
aliases = {"/save"},
usage = "<filename>",
desc = "Save a schematic into your clipboard",
min = 1,
@ -265,7 +265,7 @@ public static void save(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/clearclipboard"},
aliases = {"clearclipboard"},
usage = "",
desc = "Clear your clipboard",
min = 0,

View File

@ -95,6 +95,19 @@ public Map<String, String> getCommands() {
return descs;
}
/**
* Get the usage string for a command.
*
* @param command
* @param cmd
* @return
*/
private String getUsage(String command, Command cmd) {
return command
+ (cmd.flags().length() > 0 ? " [-" + cmd.flags() + "]" : " ")
+ cmd.usage();
}
/**
* Attempt to execute a command.
*
@ -121,15 +134,25 @@ public boolean execute(CommandContext args, WorldEdit we,
Command cmd = method.getAnnotation(Command.class);
if (args.argsLength() < cmd.min()) {
player.printError(args.getCommand() + " " + cmd.usage());
player.printError("Too few arguments.");
player.printError(getUsage(args.getCommand(), cmd));
return true;
}
if (cmd.max() != -1 && args.argsLength() > cmd.max()) {
player.printError(args.getCommand() + " " + cmd.usage());
player.printError("Too many arguments.");
player.printError(getUsage(args.getCommand(), cmd));
return true;
}
for (char flag : args.getFlags()) {
if (cmd.flags().indexOf(String.valueOf(flag)) == -1) {
player.printError("Unknown flag: " + flag);
player.printError(getUsage(args.getCommand(), cmd));
return true;
}
}
try {
method.invoke(null, args, we, session, player, editSession);
} catch (IllegalArgumentException e) {

View File

@ -30,7 +30,7 @@
*/
public class GeneralCommands {
@Command(
aliases = {"//limit"},
aliases = {"/limit"},
usage = "<limit>",
desc = "Modify block change limit",
min = 1,
@ -58,7 +58,7 @@ public static void limit(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/toggleplace"},
aliases = {"toggleplace"},
usage = "",
desc = "",
min = 0,

View File

@ -31,7 +31,7 @@
*/
public class GenerationCommands {
@Command(
aliases = {"//hcyl"},
aliases = {"/hcyl"},
usage = "<block> <radius> [height] ",
desc = "Generate a hollow cylinder",
min = 2,
@ -52,7 +52,7 @@ public static void hcyl(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//cyl"},
aliases = {"/cyl"},
usage = "<block> <radius> [height] ",
desc = "Generate a cylinder",
min = 2,
@ -73,7 +73,7 @@ public static void cyl(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//hsphere"},
aliases = {"/hsphere"},
usage = "<block> <radius> [raised?] ",
desc = "Generate a hollow sphere",
min = 2,
@ -102,7 +102,7 @@ public static void hsphere(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//sphere"},
aliases = {"/sphere"},
usage = "<block> <radius> [raised?] ",
desc = "Generate a filled sphere",
min = 2,
@ -131,7 +131,7 @@ public static void sphere(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/forestgen"},
aliases = {"forestgen"},
usage = "[size] [density] ",
desc = "Generate a forest",
min = 0,
@ -151,7 +151,7 @@ public static void forestGen(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/pinegen"},
aliases = {"pinegen"},
usage = "[size] [density]",
desc = "Generate a pine forest",
min = 0,
@ -171,7 +171,7 @@ public static void pineGen(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/pumpkins"},
aliases = {"pumpkins"},
usage = "[size]",
desc = "Generate pumpkin patches",
min = 0,

View File

@ -30,7 +30,7 @@
*/
public class HistoryCommands {
@Command(
aliases = {"//undo"},
aliases = {"/undo"},
usage = "",
desc = "Undoes the last action",
min = 0,
@ -51,7 +51,7 @@ public static void undo(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//redo"},
aliases = {"/redo"},
usage = "",
desc = "Redoes the last action (from history)",
min = 0,
@ -72,7 +72,7 @@ public static void redo(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/clearhistory"},
aliases = {"clearhistory"},
usage = "",
desc = "Clear your history",
min = 0,

View File

@ -30,7 +30,7 @@
*/
public class NavigationCommands {
@Command(
aliases = {"/unstuck"},
aliases = {"unstuck"},
usage = "",
desc = "Escape from being stuck inside a block",
min = 0,
@ -46,7 +46,7 @@ public static void unstuck(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/ascend"},
aliases = {"ascend"},
usage = "",
desc = "Go up a floor",
min = 0,
@ -65,7 +65,7 @@ public static void ascend(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/descend"},
aliases = {"descend"},
usage = "",
desc = "Go down a floor",
min = 0,
@ -84,7 +84,7 @@ public static void descend(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/ceil"},
aliases = {"ceil"},
usage = "[clearance]",
desc = "Go to the celing",
min = 0,
@ -106,7 +106,7 @@ public static void ceiling(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/thru"},
aliases = {"thru"},
usage = "",
desc = "Passthrough walls",
min = 0,
@ -125,7 +125,7 @@ public static void thru(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/jumpto"},
aliases = {"jumpto"},
usage = "",
desc = "Teleport to a location",
min = 0,
@ -146,7 +146,7 @@ public static void jumpTo(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/up"},
aliases = {"up"},
usage = "<block>",
desc = "Go upwards some distance",
min = 1,

View File

@ -36,7 +36,7 @@
*/
public class RegionCommands {
@Command(
aliases = {"//set"},
aliases = {"/set"},
usage = "<block>",
desc = "Set all the blocks inside the selection to a block",
min = 1,
@ -62,7 +62,7 @@ public static void set(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//replace"},
aliases = {"/replace"},
usage = "[from-block] <to-block>",
desc = "Replace all blocks in the selection with another",
min = 1,
@ -95,7 +95,7 @@ public static void replace(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//overlay"},
aliases = {"/overlay"},
usage = "<block>",
desc = "Set a block on top of blocks in the region",
min = 1,
@ -114,7 +114,7 @@ public static void overlay(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//walls"},
aliases = {"/walls"},
usage = "<block>",
desc = "Build the four sides of the selection",
min = 1,
@ -132,7 +132,7 @@ public static void walls(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//faces", "//outline"},
aliases = {"/faces", "/outline"},
usage = "<block>",
desc = "Build the walls, ceiling, and roof of a selection",
min = 1,
@ -149,7 +149,7 @@ public static void faces(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//smooth"},
aliases = {"/smooth"},
usage = "[iterations]",
desc = "Smooth the elevation in the selection",
min = 0,
@ -173,7 +173,7 @@ public static void smooth(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//move"},
aliases = {"/move"},
usage = "[count] [direction] [leave-id] ",
desc = "Move the contents of the selection",
min = 0,
@ -203,7 +203,7 @@ public static void move(CommandContext args, WorldEdit we,
@Command(
aliases = {"//stack"},
aliases = {"/stack"},
usage = "[count] [direction] ",
desc = "Repeat the contents of the selection",
min = 0,

View File

@ -30,7 +30,7 @@
*/
public class ScriptingCommands {
@Command(
aliases = {"/cs"},
aliases = {"cs"},
usage = "<filename> [args...]",
desc = "Execute a CraftScript",
min = 1,
@ -50,7 +50,7 @@ public static void execute(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/.s"},
aliases = {".s"},
usage = "[args...]",
desc = "Execute last CraftScript",
min = 0,

View File

@ -35,7 +35,7 @@
*/
public class SelectionCommands {
@Command(
aliases = {"//pos1"},
aliases = {"/pos1"},
usage = "",
desc = "Set position 1",
min = 0,
@ -57,7 +57,7 @@ public static void pos1(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//pos2"},
aliases = {"/pos2"},
usage = "",
desc = "Set position 2",
min = 0,
@ -79,7 +79,7 @@ public static void pos2(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//hpos1"},
aliases = {"/hpos1"},
usage = "",
desc = "Set position 1 to targeted block",
min = 0,
@ -106,7 +106,7 @@ public static void hpos1(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//hpos2"},
aliases = {"/hpos2"},
usage = "",
desc = "Set position 2 to targeted block",
min = 0,
@ -134,7 +134,7 @@ public static void hpos2(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//chunk"},
aliases = {"/chunk"},
usage = "",
desc = "Set the selection to your current chunk",
min = 0,
@ -157,7 +157,7 @@ public static void chunk(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//wand"},
aliases = {"/wand"},
usage = "",
desc = "Get the wand object",
min = 0,
@ -173,7 +173,7 @@ public static void wand(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/toggleeditwand"},
aliases = {"toggleeditwand"},
usage = "",
desc = "Toggle functionality of the edit wand",
min = 0,
@ -194,7 +194,7 @@ public static void toggleWand(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//expand"},
aliases = {"/expand"},
usage = "<amount> [reverse-amount] <direction>",
desc = "Expand the selection area",
min = 1,
@ -259,7 +259,7 @@ public static void expand(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//contract"},
aliases = {"/contract"},
usage = "<amount> [reverse-amount] [direction]",
desc = "Contract the selection area",
min = 1,
@ -305,7 +305,7 @@ public static void contract(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//shift"},
aliases = {"/shift"},
usage = "<amount> [direction]",
desc = "Shift the selection area",
min = 1,
@ -334,7 +334,7 @@ public static void shift(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//size"},
aliases = {"/size"},
usage = "",
desc = "Get information about the selection",
min = 0,
@ -375,7 +375,7 @@ public static void count(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//distr"},
aliases = {"/distr"},
usage = "",
desc = "Get the distribution of blocks in the selection",
min = 0,

View File

@ -37,7 +37,7 @@
*/
public class SnapshotCommands {
@Command(
aliases = {"/listsnapshots"},
aliases = {"listsnapshots"},
usage = "[num]",
desc = "List snapshots",
min = 0,
@ -71,7 +71,7 @@ public static void list(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//use"},
aliases = {"/use"},
usage = "<snapshot>",
desc = "Choose a snapshot to use",
min = 1,
@ -112,7 +112,7 @@ public static void use(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//restore"},
aliases = {"/restore"},
usage = "[snapshot]",
desc = "Restore the selection from a snapshot",
min = 0,

View File

@ -32,7 +32,7 @@
*/
public class SuperPickaxeCommands {
@Command(
aliases = {"//", "/,"},
aliases = {"/", ","},
usage = "",
desc = "Toggle the super pickaxe pickaxe function",
min = 0,
@ -51,7 +51,7 @@ public static void togglePickaxe(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/single"},
aliases = {"single"},
usage = "",
desc = "Enable the single block super pickaxe mode",
min = 0,
@ -68,7 +68,7 @@ public static void single(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/area"},
aliases = {"area"},
usage = "<radius>",
desc = "Enable the area super pickaxe pickaxe mode",
min = 1,
@ -93,7 +93,7 @@ public static void area(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/recur"},
aliases = {"recur"},
usage = "<radius>",
desc = "Enable the recursive super pickaxe pickaxe mode",
min = 1,
@ -118,7 +118,7 @@ public static void recursive(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/none"},
aliases = {"none"},
usage = "",
desc = "Turn off all superpickaxe alternate modes",
min = 0,
@ -134,7 +134,7 @@ public static void none(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/info"},
aliases = {"info"},
usage = "",
desc = "Block information tool",
min = 0,
@ -151,7 +151,7 @@ public static void info(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/tree"},
aliases = {"tree"},
usage = "",
desc = "Tree generator tool",
min = 0,
@ -168,7 +168,7 @@ public static void tree(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/bigtree"},
aliases = {"bigtree"},
usage = "",
desc = "Big tree generator tool",
min = 0,
@ -185,7 +185,7 @@ public static void bigTree(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/pinetree"},
aliases = {"pinetree"},
usage = "",
desc = "Pine tree generator tool",
min = 0,
@ -202,7 +202,7 @@ public static void pineTree(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/repl"},
aliases = {"repl"},
usage = "<block>",
desc = "Block replacer tool",
min = 1,
@ -220,7 +220,7 @@ public static void repl(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/brush"},
aliases = {"brush"},
usage = "<block> [radius] [no-replace?]",
desc = "Build spheres from far away",
min = 1,
@ -253,7 +253,7 @@ public static void brush(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/rbrush"},
aliases = {"rbrush"},
usage = "<block> [radius] ",
desc = "Brush tool that will only replace blocks",
min = 1,

View File

@ -35,7 +35,7 @@
*/
public class UtilityCommands {
@Command(
aliases = {"//fill"},
aliases = {"/fill"},
usage = " <block> <radius> [depth] ",
desc = "Fill a hole",
min = 2,
@ -64,7 +64,7 @@ public static void fill(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//fillr"},
aliases = {"/fillr"},
usage = " <block> <radius> [depth] ",
desc = "Fill a hole recursively",
min = 2,
@ -93,7 +93,7 @@ public static void fillr(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"//drain"},
aliases = {"/drain"},
usage = "<radius>",
desc = "Drain a pool",
min = 1,
@ -112,7 +112,7 @@ public static void drain(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/fixlava"},
aliases = {"fixlava"},
usage = "<radius>",
desc = "Fix lava to be stationary",
min = 1,
@ -131,7 +131,7 @@ public static void fixLava(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/fixwater"},
aliases = {"fixwater"},
usage = "<radius>",
desc = "Fix water to be stationary",
min = 1,
@ -150,7 +150,7 @@ public static void fixWater(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/removeabove"},
aliases = {"removeabove"},
usage = "[size] [height] ",
desc = "Remove blocks above your head. ",
min = 0,
@ -171,7 +171,7 @@ public static void removeAbove(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/removebelow"},
aliases = {"removebelow"},
usage = "[size] [height] ",
desc = "Remove blocks below your head. ",
min = 0,
@ -192,7 +192,7 @@ public static void removeBelow(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/removenear"},
aliases = {"removenear"},
usage = "<block> [size] ",
desc = "Remove blocks near you.",
min = 1,
@ -213,7 +213,7 @@ public static void removeNear(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/replacenear"},
aliases = {"replacenear"},
usage = "<size> <from-id> <to-id> ",
desc = "Replace nearby blocks",
min = 3,
@ -244,7 +244,7 @@ public static void replaceNear(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/snow"},
aliases = {"snow"},
usage = "[radius]",
desc = "Simulates snow",
min = 0,
@ -262,7 +262,7 @@ public static void snow(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/thaw"},
aliases = {"thaw"},
usage = "[radius]",
desc = "Thaws the area",
min = 0,
@ -280,7 +280,7 @@ public static void thaw(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/ex", "/ext", "/extinguish"},
aliases = {"ex", "ext", "extinguish"},
usage = "[radius]",
desc = "Extinguish nearby fire",
min = 0,
@ -304,7 +304,7 @@ public static void extinguish(CommandContext args, WorldEdit we,
}
@Command(
aliases = {"/butcher"},
aliases = {"butcher"},
usage = "[radius]",
desc = "Kill all or nearby mobs",
min = 0,