Update new commands with suggestion and help support.

This commit is contained in:
Albert Pham
2015-10-27 13:51:45 -07:00
parent 935de4c93d
commit f4bb5272e1
32 changed files with 1150 additions and 215 deletions

View File

@ -19,6 +19,7 @@
package com.sk89q.worldedit.util;
import com.google.common.collect.Sets;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.Vector;
@ -26,10 +27,12 @@ import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BlockID;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
/**
* Tree generator.
@ -105,6 +108,7 @@ public class TreeGenerator {
* Stores a map of the names for fast access.
*/
private static final Map<String, TreeType> lookup = new HashMap<String, TreeType>();
private static final Set<String> primaryAliases = Sets.newHashSet();
private final String name;
private final String[] lookupKeys;
@ -114,6 +118,9 @@ public class TreeGenerator {
for (String key : type.lookupKeys) {
lookup.put(key, type);
}
if (type.lookupKeys.length > 0) {
primaryAliases.add(type.lookupKeys[0]);
}
}
}
@ -122,6 +129,14 @@ public class TreeGenerator {
this.lookupKeys = lookupKeys;
}
public static Set<String> getAliases() {
return Collections.unmodifiableSet(lookup.keySet());
}
public static Set<String> getPrimaryAliases() {
return Collections.unmodifiableSet(primaryAliases);
}
public boolean generate(EditSession editSession, Vector pos) throws MaxChangedBlocksException {
return editSession.getWorld().generateTree(this, editSession, pos);
}

View File

@ -102,8 +102,9 @@ public class SimpleDescription implements Description {
*
* @param usage usage string, or null
*/
public void overrideUsage(String usage) {
public SimpleDescription overrideUsage(String usage) {
this.overrideUsage = usage;
return this;
}
@Override

View File

@ -0,0 +1,39 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.command.argument;
public class ArgumentException extends Exception {
public ArgumentException() {
}
public ArgumentException(String message) {
super(message);
}
public ArgumentException(String message, Throwable cause) {
super(message, cause);
}
public ArgumentException(Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,39 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.command.argument;
public class ArgumentParseException extends ArgumentException {
public ArgumentParseException() {
}
public ArgumentParseException(String message) {
super(message);
}
public ArgumentParseException(String message, Throwable cause) {
super(message, cause);
}
public ArgumentParseException(Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,45 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.command.argument;
import com.google.common.collect.Lists;
import java.util.Collection;
import java.util.List;
public final class ArgumentUtils {
private ArgumentUtils() {
}
public static List<String> getMatchingSuggestions(Collection<String> items, String s) {
if (s.isEmpty()) {
return Lists.newArrayList(items);
}
List<String> suggestions = Lists.newArrayList();
for (String item : items) {
if (item.toLowerCase().startsWith(s)) {
suggestions.add(item);
}
}
return suggestions;
}
}

View File

@ -46,23 +46,23 @@ public class CommandArgs {
return position < arguments.size();
}
public String next() throws CommandException {
public String next() throws MissingArgumentException {
try {
return arguments.get(position++);
} catch (IndexOutOfBoundsException ignored) {
throw new CommandException("Too few arguments specified.");
throw new MissingArgumentException("Too few arguments specified.");
}
}
public String peek() throws CommandException {
public String peek() throws MissingArgumentException {
try {
return arguments.get(position);
} catch (IndexOutOfBoundsException ignored) {
throw new CommandException("Too few arguments specified.");
throw new MissingArgumentException("Too few arguments specified.");
}
}
public String remaining() throws CommandException {
public String remaining() throws MissingArgumentException {
if (hasNext()) {
StringBuilder builder = new StringBuilder();
boolean first = true;
@ -75,11 +75,11 @@ public class CommandArgs {
}
return builder.toString();
} else {
throw new CommandException("Too few arguments specified.");
throw new MissingArgumentException("Too few arguments specified.");
}
}
public String peekRemaining() throws CommandException {
public String peekRemaining() throws MissingArgumentException {
if (hasNext()) {
StringBuilder builder = new StringBuilder();
boolean first = true;
@ -92,7 +92,7 @@ public class CommandArgs {
}
return builder.toString();
} else {
throw new CommandException("Too few arguments specified.");
throw new MissingArgumentException();
}
}
@ -131,7 +131,7 @@ public class CommandArgs {
return flags;
}
public void requireAllConsumed() throws CommandException {
public void requireAllConsumed() throws UnusedArgumentsException {
StringBuilder builder = new StringBuilder();
boolean hasUnconsumed = false;
@ -149,67 +149,71 @@ public class CommandArgs {
if (hasNext()) {
hasUnconsumed = true;
builder.append(peekRemaining());
try {
builder.append(peekRemaining());
} catch (MissingArgumentException e) {
throw new RuntimeException("This should not have happened", e);
}
}
if (hasUnconsumed) {
throw new CommandException("There were unused arguments: " + builder);
throw new UnusedArgumentsException("There were unused arguments: " + builder);
}
}
public int nextInt() throws CommandException {
public int nextInt() throws ArgumentParseException, MissingArgumentException {
String next = next();
try {
return Integer.parseInt(next);
} catch (NumberFormatException ignored) {
throw new CommandException("Expected a number, got '" + next + "'");
throw new ArgumentParseException("Expected a number, got '" + next + "'");
}
}
public short nextShort() throws CommandException {
public short nextShort() throws ArgumentParseException, MissingArgumentException {
String next = next();
try {
return Short.parseShort(next);
} catch (NumberFormatException ignored) {
throw new CommandException("Expected a number, got '" + next + "'");
throw new ArgumentParseException("Expected a number, got '" + next + "'");
}
}
public byte nextByte() throws CommandException {
public byte nextByte() throws ArgumentParseException, MissingArgumentException {
String next = next();
try {
return Byte.parseByte(next);
} catch (NumberFormatException ignored) {
throw new CommandException("Expected a number, got '" + next + "'");
throw new ArgumentParseException("Expected a number, got '" + next + "'");
}
}
public double nextDouble() throws CommandException {
public double nextDouble() throws ArgumentParseException, MissingArgumentException {
String next = next();
try {
return Double.parseDouble(next);
} catch (NumberFormatException ignored) {
throw new CommandException("Expected a number, got '" + next + "'");
throw new ArgumentParseException("Expected a number, got '" + next + "'");
}
}
public float nextFloat() throws CommandException {
public float nextFloat() throws ArgumentParseException, MissingArgumentException {
String next = next();
try {
return Float.parseFloat(next);
} catch (NumberFormatException ignored) {
throw new CommandException("Expected a number, got '" + next + "'");
throw new ArgumentParseException("Expected a number, got '" + next + "'");
}
}
public boolean nextBoolean() throws CommandException {
public boolean nextBoolean() throws ArgumentParseException, MissingArgumentException {
String next = next();
if (next.equalsIgnoreCase("yes") || next.equalsIgnoreCase("true") || next.equalsIgnoreCase("y") || next.equalsIgnoreCase("1")) {
return true;
} else if (next.equalsIgnoreCase("no") || next.equalsIgnoreCase("false") || next.equalsIgnoreCase("n") || next.equalsIgnoreCase("0")) {
return false;
} else {
throw new CommandException("Expected a boolean (yes/no), got '" + next + "'");
throw new ArgumentParseException("Expected a boolean (yes/no), got '" + next + "'");
}
}
@ -235,64 +239,65 @@ public class CommandArgs {
return fallback;
}
public int getIntFlag(char c, int fallback) throws CommandException {
public int getIntFlag(char c, int fallback) throws ArgumentParseException {
String next = getFlag(c, String.valueOf(fallback));
try {
return Integer.parseInt(next);
} catch (NumberFormatException ignored) {
throw new CommandException("Expected a number for flag '-" + c + "', got '" + next + "'");
throw new ArgumentParseException("Expected a number for flag '-" + c + "', got '" + next + "'");
}
}
public short getShortFlag(char c, short fallback) throws CommandException {
public short getShortFlag(char c, short fallback) throws ArgumentParseException {
String next = getFlag(c, String.valueOf(fallback));
try {
return Short.parseShort(next);
} catch (NumberFormatException ignored) {
throw new CommandException("Expected a number for flag '-" + c + "', got '" + next + "'");
throw new ArgumentParseException("Expected a number for flag '-" + c + "', got '" + next + "'");
}
}
public byte getByteFlag(char c, byte fallback) throws CommandException {
public byte getByteFlag(char c, byte fallback) throws ArgumentParseException {
String next = getFlag(c, String.valueOf(fallback));
try {
return Byte.parseByte(next);
} catch (NumberFormatException ignored) {
throw new CommandException("Expected a number for flag '-" + c + "', got '" + next + "'");
throw new ArgumentParseException("Expected a number for flag '-" + c + "', got '" + next + "'");
}
}
public double getDoubleFlag(char c, double fallback) throws CommandException {
public double getDoubleFlag(char c, double fallback) throws ArgumentParseException {
String next = getFlag(c, String.valueOf(fallback));
try {
return Double.parseDouble(next);
} catch (NumberFormatException ignored) {
throw new CommandException("Expected a number for flag '-" + c + "', got '" + next + "'");
throw new ArgumentParseException("Expected a number for flag '-" + c + "', got '" + next + "'");
}
}
public float getFloatFlag(char c, float fallback) throws CommandException {
public float getFloatFlag(char c, float fallback) throws ArgumentParseException {
String next = getFlag(c, String.valueOf(fallback));
try {
return Float.parseFloat(next);
} catch (NumberFormatException ignored) {
throw new CommandException("Expected a number for flag '-" + c + "', got '" + next + "'");
throw new ArgumentParseException("Expected a number for flag '-" + c + "', got '" + next + "'");
}
}
public boolean getBooleanFlag(char c, boolean fallback) throws CommandException {
public boolean getBooleanFlag(char c, boolean fallback) throws ArgumentParseException {
String next = getFlag(c, String.valueOf(fallback));
if (next.equalsIgnoreCase("yes") || next.equalsIgnoreCase("true") || next.equalsIgnoreCase("y") || next.equalsIgnoreCase("1")) {
return true;
} else if (next.equalsIgnoreCase("no") || next.equalsIgnoreCase("false") || next.equalsIgnoreCase("n") || next.equalsIgnoreCase("0")) {
return false;
} else {
throw new CommandException("Expected a boolean (yes/no), got '" + next + "'");
throw new ArgumentParseException("Expected a boolean (yes/no), got '" + next + "'");
}
}
public static class Parser {
private boolean parseFlags = true;
private boolean usingHangingArguments = false;
private Set<Character> valueFlags = Sets.newHashSet();
public boolean isParseFlags() {
@ -304,6 +309,15 @@ public class CommandArgs {
return this;
}
public boolean isUsingHangingArguments() {
return usingHangingArguments;
}
public Parser setUsingHangingArguments(boolean usingHangingArguments) {
this.usingHangingArguments = usingHangingArguments;
return this;
}
public Set<Character> getValueFlags() {
return valueFlags;
}
@ -319,6 +333,11 @@ public class CommandArgs {
for (int i = 0; i < context.argsLength(); i++) {
args.add(context.getString(i));
}
if (isUsingHangingArguments()) {
if (arguments.isEmpty() || arguments.endsWith(" ")) {
args.add("");
}
}
Map<Character, String> flags = Maps.newHashMap(context.getValueFlags());
for (Character c : context.getFlags()) {
flags.put(c, null);

View File

@ -0,0 +1,39 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.command.argument;
public class MissingArgumentException extends ArgumentException {
public MissingArgumentException() {
}
public MissingArgumentException(String message) {
super(message);
}
public MissingArgumentException(String message, Throwable cause) {
super(message, cause);
}
public MissingArgumentException(Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,39 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.command.argument;
public class UnusedArgumentsException extends ArgumentException {
public UnusedArgumentsException() {
}
public UnusedArgumentsException(String message) {
super(message);
}
public UnusedArgumentsException(String message, Throwable cause) {
super(message, cause);
}
public UnusedArgumentsException(Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,111 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.command.composition;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.CommandLocals;
import com.sk89q.worldedit.util.command.argument.ArgumentUtils;
import com.sk89q.worldedit.util.command.argument.CommandArgs;
import com.sk89q.worldedit.util.command.argument.MissingArgumentException;
import java.util.List;
import java.util.Map;
import java.util.Set;
public abstract class BranchingCommand<T> implements CommandExecutor<T> {
private final String name;
private final Map<String, CommandExecutor<? extends T>> options = Maps.newHashMap();
private final Set<String> primaryAliases = Sets.newHashSet();
public BranchingCommand(String name) {
this.name = name;
}
public void putOption(CommandExecutor<? extends T> executor, String primaryAlias, String... aliases) {
options.put(primaryAlias, executor);
primaryAliases.add(primaryAlias);
for (String alias : aliases) {
options.put(alias, executor);
}
}
@Override
public T call(CommandArgs args, CommandLocals locals) throws CommandException {
try {
String classifier = args.next();
CommandExecutor<? extends T> executor = options.get(classifier.toLowerCase());
if (executor != null) {
return executor.call(args, locals);
} else {
throw new CommandException("'" + classifier + "' isn't a valid option for '" + name + "'. " +
"Try one of: " + Joiner.on(", ").join(primaryAliases));
}
} catch (MissingArgumentException e) {
throw new CommandException("Missing value for <" + name + "> " +
"(try one of " + Joiner.on(" | ").join(primaryAliases) + ").");
}
}
@Override
public List<String> getSuggestions(CommandArgs args, CommandLocals locals) throws MissingArgumentException {
String classifier = args.next();
try {
CommandExecutor<? extends T> executor = options.get(classifier.toLowerCase());
if (executor != null) {
return executor.getSuggestions(args, locals);
}
} catch (MissingArgumentException ignored) {
}
return ArgumentUtils.getMatchingSuggestions((classifier.isEmpty() ? primaryAliases : options.keySet()), classifier);
}
@Override
public String getUsage() {
List<String> optionUsages = Lists.newArrayList();
for (String alias : primaryAliases) {
CommandExecutor<? extends T> executor = options.get(alias);
String usage = executor.getUsage();
if (usage.isEmpty()) {
optionUsages.add(alias);
} else {
optionUsages.add(alias + " " + executor.getUsage());
}
}
return "(" + Joiner.on(" | ").join(optionUsages) + ")";
}
@Override
public boolean testPermission(CommandLocals locals) {
for (CommandExecutor<?> executor : options.values()) {
if (!executor.testPermission(locals)) {
return false;
}
}
return true;
}
}

View File

@ -17,40 +17,25 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.command;
package com.sk89q.worldedit.util.command.composition;
import com.google.common.collect.Lists;
import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.CommandLocals;
import com.sk89q.worldedit.util.command.argument.CommandArgs;
import com.sk89q.worldedit.util.command.argument.MissingArgumentException;
import java.util.List;
public abstract class CommandExecutor<T> implements CommandCallable {
public interface CommandExecutor<T> {
@Override
public final T call(String arguments, CommandLocals locals, String[] parentCommands) throws CommandException {
CommandArgs args = new CommandArgs.Parser().parse(arguments);
T ret = call(args, locals, parentCommands);
args.requireAllConsumed();
return ret;
}
T call(CommandArgs args, CommandLocals locals) throws CommandException;
public abstract T call(CommandArgs args, CommandLocals locals, String[] parentCommands) throws CommandException;
List<String> getSuggestions(CommandArgs args, CommandLocals locals) throws MissingArgumentException;
@Override
public Description getDescription() {
return new SimpleDescription();
}
String getUsage();
@Override
public boolean testPermission(CommandLocals locals) {
return false;
}
String getDescription();
@Override
public List<String> getSuggestions(String arguments, CommandLocals locals) throws CommandException {
return Lists.newArrayList();
}
boolean testPermission(CommandLocals locals);
}

View File

@ -0,0 +1,79 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.command.composition;
import com.google.common.collect.Lists;
import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.CommandLocals;
import com.sk89q.worldedit.util.command.CommandCallable;
import com.sk89q.worldedit.util.command.Description;
import com.sk89q.worldedit.util.command.SimpleDescription;
import com.sk89q.worldedit.util.command.argument.CommandArgs;
import com.sk89q.worldedit.util.command.argument.MissingArgumentException;
import com.sk89q.worldedit.util.command.argument.UnusedArgumentsException;
import java.util.List;
public class LegacyCommandAdapter implements CommandCallable {
private final CommandExecutor<?> executor;
public LegacyCommandAdapter(CommandExecutor<?> executor) {
this.executor = executor;
}
@Override
public final Object call(String arguments, CommandLocals locals, String[] parentCommands) throws CommandException {
CommandArgs args = new CommandArgs.Parser().parse(arguments);
if (args.containsFlag('?')) {
throw new CommandException(executor.getUsage());
} else {
Object ret = executor.call(args, locals);
try {
args.requireAllConsumed();
} catch (UnusedArgumentsException e) {
throw new CommandException(e.getMessage());
}
return ret;
}
}
@Override
public Description getDescription() {
return new SimpleDescription()
.setDescription(executor.getDescription())
.overrideUsage(executor.getUsage());
}
@Override
public boolean testPermission(CommandLocals locals) {
return executor.testPermission(locals);
}
@Override
public List<String> getSuggestions(String arguments, CommandLocals locals) throws CommandException {
CommandArgs args = new CommandArgs.Parser().setUsingHangingArguments(true).parse(arguments);
try {
return executor.getSuggestions(args, locals);
} catch (MissingArgumentException e) {
return Lists.newArrayList();
}
}
}

View File

@ -0,0 +1,63 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.command.composition;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.sk89q.minecraft.util.commands.CommandLocals;
import com.sk89q.worldedit.util.command.composition.CommandExecutor;
import java.util.List;
public abstract class ParameterCommand<T> implements CommandExecutor<T> {
private final List<CommandExecutor<?>> parameters = Lists.newArrayList();
protected List<CommandExecutor<?>> getParameters() {
return parameters;
}
public <T extends CommandExecutor<?>> T addParameter(T executor) {
parameters.add(executor);
return executor;
}
@Override
public final String getUsage() {
List<String> parts = Lists.newArrayList();
for (CommandExecutor<?> executor : parameters) {
parts.add(executor.getUsage());
}
return Joiner.on(" ").join(parts);
}
@Override
public final boolean testPermission(CommandLocals locals) {
for (CommandExecutor<?> executor : parameters) {
if (!executor.testPermission(locals)) {
return false;
}
}
return testPermission0(locals);
}
protected abstract boolean testPermission0(CommandLocals locals);
}

View File

@ -0,0 +1,49 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.command.composition;
import com.google.common.collect.Lists;
import com.sk89q.minecraft.util.commands.CommandLocals;
import com.sk89q.worldedit.util.command.argument.CommandArgs;
import com.sk89q.worldedit.util.command.argument.MissingArgumentException;
import java.util.List;
public abstract class SimpleCommand<T> extends ParameterCommand<T> {
@Override
public final List<String> getSuggestions(CommandArgs args, CommandLocals locals) throws MissingArgumentException {
List<String> suggestions = Lists.newArrayList();
boolean seenParameter = false;
for (CommandExecutor<?> parameter : getParameters()) {
try {
suggestions = parameter.getSuggestions(args, locals);
seenParameter = true;
} catch (MissingArgumentException e) {
if (seenParameter) {
return suggestions;
} else {
throw e;
}
}
}
return suggestions;
}
}