Add some experimental brush commands.

/br set <shape> <radius> <pattern>
/br deform <shape> <expression>
/br scatter <shape> <density> <generator>
/br apply <shape> <generator>

<shape> can be: cuboid, cyl[inder], sphere

<density> is 0-100

<generator> can be:
forest|tree <type>
item <item>[:<data>] (ONLY WORKS ON FORGE)

Examples:

/br deform cuboid 5 y-=0.2
/br scatter sphere 5 100 minecraft:dye:15
This commit is contained in:
sk89q
2015-10-26 23:14:30 -07:00
parent b19cd9bec4
commit 935de4c93d
59 changed files with 2239 additions and 99 deletions

View File

@ -39,7 +39,7 @@ public interface CommandCallable extends CommandCompleter {
* @return the called command, or null if there was no command found
* @throws CommandException thrown on a command error
*/
boolean call(String arguments, CommandLocals locals, String[] parentCommands) throws CommandException;
Object call(String arguments, CommandLocals locals, String[] parentCommands) throws CommandException;
/**
* Get an object describing this command.

View File

@ -0,0 +1,56 @@
/*
* 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;
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 java.util.List;
public abstract class CommandExecutor<T> implements CommandCallable {
@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;
}
public abstract T call(CommandArgs args, CommandLocals locals, String[] parentCommands) throws CommandException;
@Override
public Description getDescription() {
return new SimpleDescription();
}
@Override
public boolean testPermission(CommandLocals locals) {
return false;
}
@Override
public List<String> getSuggestions(String arguments, CommandLocals locals) throws CommandException {
return Lists.newArrayList();
}
}

View File

@ -38,7 +38,7 @@ public interface Description {
*
* @return a description, or null if no description is available
*/
String getShortDescription();
String getDescription();
/**
* Get a longer help text about this command.
@ -67,4 +67,4 @@ public interface Description {
*/
List<String> getPermissions();
}
}

View File

@ -63,4 +63,4 @@ public interface Parameter {
*/
public String[] getDefaultValue();
}
}

View File

@ -45,12 +45,13 @@ public class SimpleDescription implements Description {
* @param parameters the list of parameters
* @see #getParameters()
*/
public void setParameters(List<Parameter> parameters) {
public SimpleDescription setParameters(List<Parameter> parameters) {
this.parameters = Collections.unmodifiableList(parameters);
return this;
}
@Override
public String getShortDescription() {
public String getDescription() {
return description;
}
@ -58,10 +59,11 @@ public class SimpleDescription implements Description {
* Set the description of the command.
*
* @param description the description
* @see #getShortDescription()
* @see #getDescription()
*/
public void setDescription(String description) {
public SimpleDescription setDescription(String description) {
this.description = description;
return this;
}
@Override
@ -75,8 +77,9 @@ public class SimpleDescription implements Description {
* @param help the help text
* @see #getHelp()
*/
public void setHelp(String help) {
public SimpleDescription setHelp(String help) {
this.help = help;
return this;
}
@Override
@ -86,11 +89,12 @@ public class SimpleDescription implements Description {
/**
* Set the permissions of this command.
*
*
* @param permissions the permissions
*/
public void setPermissions(List<String> permissions) {
public SimpleDescription setPermissions(List<String> permissions) {
this.permissions = Collections.unmodifiableList(permissions);
return this;
}
/**

View File

@ -102,7 +102,7 @@ public class SimpleDispatcher implements Dispatcher {
}
@Override
public boolean call(String arguments, CommandLocals locals, String[] parentCommands) throws CommandException {
public Object call(String arguments, CommandLocals locals, String[] parentCommands) throws CommandException {
// We have permission for this command if we have permissions for subcommands
if (!testPermission(locals)) {
throw new CommandPermissionsException();
@ -122,15 +122,13 @@ public class SimpleDispatcher implements Dispatcher {
if (mapping != null) {
try {
mapping.getCallable().call(subArguments, locals, subParents);
return mapping.getCallable().call(subArguments, locals, subParents);
} catch (CommandException e) {
e.prependStack(subCommand);
throw e;
} catch (Throwable t) {
throw new WrappedCommandException(t);
}
return true;
}
}

View File

@ -52,11 +52,12 @@ public class SimpleParameter implements Parameter {
/**
* Set the name of the parameter.
*
*
* @param name the parameter name
*/
public void setName(String name) {
public SimpleParameter setName(String name) {
this.name = name;
return this;
}
@Override
@ -71,13 +72,13 @@ public class SimpleParameter implements Parameter {
/**
* Set the flag used by this parameter.
*
* @param flag the flag, or null if there is no flag
* @param flag the flag, or null if there is no flag
* @param isValue true if the flag is a value flag
*/
public void setFlag(Character flag, boolean isValue) {
public SimpleParameter setFlag(Character flag, boolean isValue) {
this.flag = flag;
this.isValue = isValue;
return this;
}
@Override
@ -87,11 +88,12 @@ public class SimpleParameter implements Parameter {
/**
* Set whether this parameter is optional.
*
*
* @param isOptional true if this parameter is optional
*/
public void setOptional(boolean isOptional) {
public SimpleParameter setOptional(boolean isOptional) {
this.isOptional = isOptional;
return this;
}
@Override
@ -101,11 +103,12 @@ public class SimpleParameter implements Parameter {
/**
* Set the default value.
*
*
* @param defaultValue a default value, or null if none
*/
public void setDefaultValue(String[] defaultValue) {
public SimpleParameter setDefaultValue(String[] defaultValue) {
this.defaultValue = defaultValue;
return this;
}
@Override

View File

@ -0,0 +1,329 @@
/*
* 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 com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class CommandArgs {
private final List<String> arguments;
private final Map<Character, String> flags;
private int position = 0;
private Set<Character> consumedFlags = Sets.newHashSet();
public CommandArgs(List<String> arguments, Map<Character, String> flags) {
this.arguments = arguments;
this.flags = Maps.newHashMap(flags);
}
public boolean hasNext() {
return position < arguments.size();
}
public String next() throws CommandException {
try {
return arguments.get(position++);
} catch (IndexOutOfBoundsException ignored) {
throw new CommandException("Too few arguments specified.");
}
}
public String peek() throws CommandException {
try {
return arguments.get(position);
} catch (IndexOutOfBoundsException ignored) {
throw new CommandException("Too few arguments specified.");
}
}
public String remaining() throws CommandException {
if (hasNext()) {
StringBuilder builder = new StringBuilder();
boolean first = true;
while (hasNext()) {
if (!first) {
builder.append(" ");
}
builder.append(next());
first = false;
}
return builder.toString();
} else {
throw new CommandException("Too few arguments specified.");
}
}
public String peekRemaining() throws CommandException {
if (hasNext()) {
StringBuilder builder = new StringBuilder();
boolean first = true;
while (hasNext()) {
if (!first) {
builder.append(" ");
}
builder.append(next());
first = false;
}
return builder.toString();
} else {
throw new CommandException("Too few arguments specified.");
}
}
public int position() {
return position;
}
public int size() {
return arguments.size();
}
public void markConsumed() {
position = arguments.size();
}
public CommandArgs split() {
return new CommandArgs(getUnusedArguments(), getUnusedFlags());
}
private List<String> getUnusedArguments() {
List<String> args = Lists.newArrayList();
while (position < arguments.size()) {
args.add(arguments.get(position++));
}
return args;
}
private Map<Character, String> getUnusedFlags() {
Map<Character, String> flags = Maps.newHashMap();
for (Entry<Character, String> entry : this.flags.entrySet()) {
if (!consumedFlags.contains(entry.getKey())) {
flags.put(entry.getKey(), entry.getValue());
consumedFlags.add(entry.getKey());
}
}
return flags;
}
public void requireAllConsumed() throws CommandException {
StringBuilder builder = new StringBuilder();
boolean hasUnconsumed = false;
if (flags.size() > consumedFlags.size()) {
hasUnconsumed = true;
for (Entry<Character, String> entry : flags.entrySet()) {
if (!consumedFlags.contains(entry.getKey())) {
builder.append("-").append(entry.getKey()).append(" ");
if (entry.getValue() != null) {
builder.append(entry.getValue()).append(" ");
}
}
}
}
if (hasNext()) {
hasUnconsumed = true;
builder.append(peekRemaining());
}
if (hasUnconsumed) {
throw new CommandException("There were unused arguments: " + builder);
}
}
public int nextInt() throws CommandException {
String next = next();
try {
return Integer.parseInt(next);
} catch (NumberFormatException ignored) {
throw new CommandException("Expected a number, got '" + next + "'");
}
}
public short nextShort() throws CommandException {
String next = next();
try {
return Short.parseShort(next);
} catch (NumberFormatException ignored) {
throw new CommandException("Expected a number, got '" + next + "'");
}
}
public byte nextByte() throws CommandException {
String next = next();
try {
return Byte.parseByte(next);
} catch (NumberFormatException ignored) {
throw new CommandException("Expected a number, got '" + next + "'");
}
}
public double nextDouble() throws CommandException {
String next = next();
try {
return Double.parseDouble(next);
} catch (NumberFormatException ignored) {
throw new CommandException("Expected a number, got '" + next + "'");
}
}
public float nextFloat() throws CommandException {
String next = next();
try {
return Float.parseFloat(next);
} catch (NumberFormatException ignored) {
throw new CommandException("Expected a number, got '" + next + "'");
}
}
public boolean nextBoolean() throws CommandException {
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 + "'");
}
}
public boolean containsFlag(char c) {
boolean contains = flags.containsKey(c);
if (contains) {
consumedFlags.add(c);
}
return contains;
}
public String getFlag(char c, String fallback) {
boolean contains = flags.containsKey(c);
if (contains) {
consumedFlags.add(c);
String value = flags.get(c);
if (value == null) {
return fallback;
} else {
return value;
}
}
return fallback;
}
public int getIntFlag(char c, int fallback) throws CommandException {
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 + "'");
}
}
public short getShortFlag(char c, short fallback) throws CommandException {
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 + "'");
}
}
public byte getByteFlag(char c, byte fallback) throws CommandException {
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 + "'");
}
}
public double getDoubleFlag(char c, double fallback) throws CommandException {
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 + "'");
}
}
public float getFloatFlag(char c, float fallback) throws CommandException {
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 + "'");
}
}
public boolean getBooleanFlag(char c, boolean fallback) throws CommandException {
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 + "'");
}
}
public static class Parser {
private boolean parseFlags = true;
private Set<Character> valueFlags = Sets.newHashSet();
public boolean isParseFlags() {
return parseFlags;
}
public Parser setParseFlags(boolean parseFlags) {
this.parseFlags = parseFlags;
return this;
}
public Set<Character> getValueFlags() {
return valueFlags;
}
public Parser setValueFlags(Set<Character> valueFlags) {
this.valueFlags = valueFlags;
return this;
}
public CommandArgs parse(String arguments) throws CommandException {
CommandContext context = new CommandContext(CommandContext.split("_ " + arguments), valueFlags, false, null, parseFlags);
List<String> args = Lists.newArrayList();
for (int i = 0; i < context.argsLength(); i++) {
args.add(context.getString(i));
}
Map<Character, String> flags = Maps.newHashMap(context.getValueFlags());
for (Character c : context.getFlags()) {
flags.put(c, null);
}
return new CommandArgs(args, flags);
}
}
}

View File

@ -20,10 +20,10 @@
package com.sk89q.worldedit.util.command.binding;
import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.worldedit.util.command.parametric.ArgumentStack;
import com.sk89q.worldedit.util.command.parametric.BindingBehavior;
import com.sk89q.worldedit.util.command.parametric.BindingHelper;
import com.sk89q.worldedit.util.command.parametric.BindingMatch;
import com.sk89q.worldedit.util.command.parametric.ArgumentStack;
/**
* Standard bindings that should be available to most configurations.
@ -32,15 +32,15 @@ public final class StandardBindings extends BindingHelper {
/**
* Gets a {@link CommandContext} from a {@link ArgumentStack}.
*
*
* @param context the context
* @return a selection
*/
@BindingMatch(type = CommandContext.class,
behavior = BindingBehavior.PROVIDES)
behavior = BindingBehavior.PROVIDES)
public CommandContext getCommandContext(ArgumentStack context) {
context.markConsumed(); // Consume entire stack
return context.getContext();
}
}
}

View File

@ -67,8 +67,9 @@ public class DispatcherNode {
* @param callable the executor
* @param alias the list of aliases, where the first alias is the primary one
*/
public void register(CommandCallable callable, String... alias) {
dispatcher.registerCommand(callable, alias);
public DispatcherNode register(CommandCallable callable, String... alias) {
dispatcher.registerCommand(callable, alias);;
return this;
}
/**

View File

@ -180,7 +180,7 @@ class ParametricCallable implements CommandCallable {
}
@Override
public boolean call(String stringArguments, CommandLocals locals, String[] parentCommands) throws CommandException {
public Object call(String stringArguments, CommandLocals locals, String[] parentCommands) throws CommandException {
// Test permission
if (!testPermission(locals)) {
throw new CommandPermissionsException();
@ -480,4 +480,4 @@ class ParametricCallable implements CommandCallable {
}
}
}
}

View File

@ -76,7 +76,7 @@ public class CommandUsageBox extends StyledFragment {
for (CommandMapping mapping : list) {
if (locals == null || mapping.getCallable().testPermission(locals)) {
box.appendCommand(prefix + mapping.getPrimaryAlias(), mapping.getDescription().getShortDescription());
box.appendCommand(prefix + mapping.getPrimaryAlias(), mapping.getDescription().getDescription());
}
}
@ -98,8 +98,8 @@ public class CommandUsageBox extends StyledFragment {
if (description.getHelp() != null) {
contents.append(description.getHelp());
} else if (description.getShortDescription() != null) {
contents.append(description.getShortDescription());
} else if (description.getDescription() != null) {
contents.append(description.getDescription());
} else {
contents.append(new Subtle().append("No further help is available."));
}