Improved the styling of SimpleDispatcher's list of sub-commands.

This commit is contained in:
sk89q 2014-06-28 18:43:54 -07:00
parent 46c429606e
commit edb6c56aab

View File

@ -21,6 +21,9 @@ package com.sk89q.worldedit.util.command;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.sk89q.minecraft.util.commands.*; import com.sk89q.minecraft.util.commands.*;
import com.sk89q.worldedit.util.formatting.ColorCodeBuilder;
import com.sk89q.worldedit.util.formatting.CommandListBox;
import com.sk89q.worldedit.util.formatting.StyledFragment;
import java.util.*; import java.util.*;
@ -32,6 +35,16 @@ public class SimpleDispatcher implements Dispatcher {
private final Map<String, CommandMapping> commands = new HashMap<String, CommandMapping>(); private final Map<String, CommandMapping> commands = new HashMap<String, CommandMapping>();
private final SimpleDescription description = new SimpleDescription(); private final SimpleDescription description = new SimpleDescription();
/**
* Create a new instance.
*/
public SimpleDispatcher() {
description.getParameters().add(new SimpleParameter("subcommand"));
SimpleParameter extraArgs = new SimpleParameter("...");
extraArgs.setOptional(true);
description.getParameters().add(extraArgs);
}
@Override @Override
public void registerCommand(CommandCallable callable, String... alias) { public void registerCommand(CommandCallable callable, String... alias) {
CommandMapping mapping = new CommandMapping(callable, alias); CommandMapping mapping = new CommandMapping(callable, alias);
@ -119,7 +132,7 @@ public class SimpleDispatcher implements Dispatcher {
} }
throw new InvalidUsageException(getSubcommandList(locals), getDescription()); throw new InvalidUsageException(ColorCodeBuilder.asColorCodes(getSubcommandList(locals, parentCommands)), getDescription());
} }
@Override @Override
@ -172,25 +185,16 @@ public class SimpleDispatcher implements Dispatcher {
return false; return false;
} }
/** private StyledFragment getSubcommandList(CommandLocals locals, String[] parentCommands) {
* Get a list of subcommands for display. CommandListBox box = new CommandListBox("Subcommands");
* String prefix = parentCommands.length > 0 ? "/" + Joiner.on(" ").join(parentCommands) + " " : "";
* @return a string
*/
private String getSubcommandList(CommandLocals locals) {
Set<String> aliases = getPrimaryAliases();
StringBuilder builder = new StringBuilder("Subcommands: ");
for (CommandMapping mapping : getCommands()) { for (CommandMapping mapping : getCommands()) {
if (mapping.getCallable().testPermission(locals)) { if (mapping.getCallable().testPermission(locals)) {
for (String alias : mapping.getAllAliases()) { box.appendCommand(prefix + mapping.getPrimaryAlias(), mapping.getDescription().getShortDescription());
builder.append("\n- ").append(alias);
}
} }
} }
return builder.toString(); return box;
} }
} }