Added a //help command.

Dieser Commit ist enthalten in:
TomyLobo 2011-12-13 09:23:21 +01:00
Ursprung c4031c38ba
Commit 05404b005d
3 geänderte Dateien mit 115 neuen und 14 gelöschten Zeilen

Datei anzeigen

@ -87,6 +87,12 @@ public abstract class CommandsManager<T> {
*/ */
protected Injector injector; protected Injector injector;
/**
* Mapping of commands (not including aliases) with a description. This
* is only for top level commands.
*/
protected Map<String, String> helpMessages = new HashMap<String, String>();
/** /**
* Register an class that contains commands (denoted by {@link Command}. * Register an class that contains commands (denoted by {@link Command}.
* If no dependency injector is specified, then the methods of the * If no dependency injector is specified, then the methods of the
@ -189,11 +195,32 @@ public abstract class CommandsManager<T> {
// Build a list of commands and their usage details, at least for // Build a list of commands and their usage details, at least for
// root level commands // root level commands
if (parent == null) { if (parent == null) {
if (cmd.usage().length() == 0) { final String commandName = cmd.aliases()[0];
descs.put(cmd.aliases()[0], cmd.desc()); final String desc = cmd.desc();
final String usage = cmd.usage();
if (usage.length() == 0) {
descs.put(commandName, desc);
} else { } else {
descs.put(cmd.aliases()[0], cmd.usage() + " - " + cmd.desc()); descs.put(commandName, usage + " - " + desc);
} }
String help = cmd.help();
if (help.length() == 0) {
help = desc;
}
final CharSequence arguments = getArguments(cmd);
for (String alias : cmd.aliases()) {
final String helpMessage = "/"+alias+" "+arguments+"\n\n"+help;
final String key = alias.replaceAll("/", "");
String previous = helpMessages.put(key, helpMessage);
if (previous != null && !previous.replaceAll("^/[^ ]+ ", "").equals(helpMessage.replaceAll("^/[^ ]+ ", ""))) {
helpMessages.put(key, previous+"\n\n"+helpMessage);
}
}
} }
// Add the command to the registered command list for return // Add the command to the registered command list for return
@ -233,6 +260,15 @@ public abstract class CommandsManager<T> {
return descs; return descs;
} }
/**
* Get a map from command name to help message. This is only for root commands.
*
* @return
*/
public Map<String, String> getHelpMessages() {
return helpMessages;
}
/** /**
* Get the usage string for a command. * Get the usage string for a command.
* *
@ -250,17 +286,7 @@ public abstract class CommandsManager<T> {
command.append(args[i]); command.append(args[i]);
command.append(' '); command.append(' ');
} }
if (cmd.flags().length() > 0) { command.append(getArguments(cmd));
String flags = cmd.flags().replaceAll(".:", "");
if (flags.length() > 0) {
command.append("[-");
for (int i = 0; i < flags.length(); ++i) {
command.append(flags.charAt(i));
}
command.append("] ");
}
}
command.append(cmd.usage());
final String help = cmd.help(); final String help = cmd.help();
if (!help.isEmpty()) { if (!help.isEmpty()) {
@ -271,6 +297,26 @@ public abstract class CommandsManager<T> {
return command.toString(); return command.toString();
} }
protected CharSequence getArguments(Command cmd) {
final String flags = cmd.flags();
final StringBuilder command2 = new StringBuilder();
if (flags.length() > 0) {
String flagString = flags.replaceAll(".:", "");
if (flagString.length() > 0) {
command2.append("[-");
for (int i = 0; i < flagString.length(); ++i) {
command2.append(flagString.charAt(i));
}
command2.append("] ");
}
}
command2.append(cmd.usage());
return command2;
}
/** /**
* Get the usage string for a nested command. * Get the usage string for a nested command.
* *

Datei anzeigen

@ -983,6 +983,13 @@ public class WorldEdit {
return commands.getCommands(); return commands.getCommands();
} }
/**
* @return the commands
*/
public CommandsManager<LocalPlayer> getCommandsManager() {
return commands;
}
/** /**
* *
* @param player * @param player

Datei anzeigen

@ -20,9 +20,13 @@
package com.sk89q.worldedit.commands; package com.sk89q.worldedit.commands;
import java.util.Set; import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandContext; import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandPermissions; import com.sk89q.minecraft.util.commands.CommandPermissions;
import com.sk89q.minecraft.util.commands.CommandsManager;
import com.sk89q.minecraft.util.commands.Logging; import com.sk89q.minecraft.util.commands.Logging;
import static com.sk89q.minecraft.util.commands.Logging.LogMode.*; import static com.sk89q.minecraft.util.commands.Logging.LogMode.*;
import com.sk89q.worldedit.*; import com.sk89q.worldedit.*;
@ -426,4 +430,48 @@ public class UtilityCommands {
int removed = player.getWorld().removeEntities(type, origin, radius); int removed = player.getWorld().removeEntities(type, origin, radius);
player.print("Marked " + removed + " entit(ies) for removal."); player.print("Marked " + removed + " entit(ies) for removal.");
} }
@Command(
aliases = { "/help" },
usage = "[<command>]",
desc = "Displays help for the given command or lists all commands.",
min = 0,
max = -1
)
public static void help(CommandContext args, WorldEdit we,
LocalSession session, LocalPlayer player, EditSession editSession)
throws WorldEditException {
final CommandsManager<LocalPlayer> commandsManager = we.getCommandsManager();
if (args.argsLength() == 0) {
StringBuilder sb = new StringBuilder();
boolean first = true;
SortedSet<String> commands = new TreeSet<String>(commandsManager.getCommands().keySet());
for (String command : commands) {
if (!first) {
sb.append(", ");
}
sb.append('/');
sb.append(command);
first = false;
}
player.print(sb.toString());
return;
}
String command = args.getJoinedStrings(0).replaceAll("/", "");
String helpMessage = commandsManager.getHelpMessages().get(command);
if (helpMessage == null) {
player.printError("Unknown command '" + command + "'.");
return;
}
player.print(helpMessage);
}
} }