Add more variable args for expressions, quoting

This commit is contained in:
Kenzie Togami 2019-04-28 22:03:54 -07:00
parent d4fce65abc
commit 82c4846436
No known key found for this signature in database
GPG Key ID: 5D200B325E157A81
5 changed files with 103 additions and 14 deletions

View File

@ -273,8 +273,8 @@ public class GenerationCommands {
@Selection Region region,
@Arg(desc = "The pattern of blocks to set")
Pattern pattern,
@Arg(desc = "Expression to test block placement locations and set block type")
String expression,
@Arg(desc = "Expression to test block placement locations and set block type", variable = true)
List<String> expression,
@Switch(name = 'h', desc = "Generate a hollow shape")
boolean hollow,
@Switch(name = 'r', desc = "Use the game's coordinate origin")
@ -312,7 +312,7 @@ public class GenerationCommands {
}
try {
final int affected = editSession.makeShape(region, zero, unit, pattern, expression, hollow, session.getTimeout());
final int affected = editSession.makeShape(region, zero, unit, pattern, String.join(" ", expression), hollow, session.getTimeout());
player.findFreePosition();
player.print(affected + " block(s) have been created.");
return affected;
@ -334,8 +334,8 @@ public class GenerationCommands {
@Selection Region region,
@Arg(desc = "The biome type to set")
BiomeType target,
@Arg(desc = "Expression to test block placement locations and set biome type")
String expression,
@Arg(desc = "Expression to test block placement locations and set biome type", variable = true)
List<String> expression,
@Switch(name = 'h', desc = "Generate a hollow shape")
boolean hollow,
@Switch(name = 'r', desc = "Use the game's coordinate origin")
@ -372,7 +372,7 @@ public class GenerationCommands {
}
try {
final int affected = editSession.makeBiomeShape(region, zero, unit, target, expression, hollow, session.getTimeout());
final int affected = editSession.makeBiomeShape(region, zero, unit, target, String.join(" ", expression), hollow, session.getTimeout());
player.findFreePosition();
player.print("" + affected + " columns affected.");
return affected;

View File

@ -382,8 +382,8 @@ public class RegionCommands {
@Logging(ALL)
public int deform(Player player, LocalSession session, EditSession editSession,
@Selection Region region,
@Arg(desc = "The expression to use")
String expression,
@Arg(desc = "The expression to use", variable = true)
List<String> expression,
@Switch(name = 'r', desc = "Use the game's coordinate origin")
boolean useRawCoords,
@Switch(name = 'o', desc = "Use the selection's center as origin")
@ -410,7 +410,7 @@ public class RegionCommands {
}
try {
final int affected = editSession.deformRegion(region, zero, unit, expression, session.getTimeout());
final int affected = editSession.deformRegion(region, zero, unit, String.join(" ", expression), session.getTimeout());
player.findFreePosition();
player.print(affected + " block(s) have been deformed.");
return affected;

View File

@ -492,10 +492,10 @@ public class UtilityCommands {
)
@CommandPermissions("worldedit.calc")
public void calc(Actor actor,
@Arg(desc = "Expression to evaluate")
String input) {
@Arg(desc = "Expression to evaluate", variable = true)
List<String> input) {
try {
Expression expression = Expression.compile(input);
Expression expression = Expression.compile(String.join(" ", input));
double result = expression.evaluate(
new double[]{}, WorldEdit.getInstance().getSessionManager().get(actor).getTimeout());
String formatted = formatter.format(result);

View File

@ -89,12 +89,12 @@ import com.sk89q.worldedit.internal.command.CommandLoggingHandler;
import com.sk89q.worldedit.internal.command.WorldEditExceptionConverter;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.session.request.Request;
import com.sk89q.worldedit.util.command.CommandArgParser;
import com.sk89q.worldedit.util.command.CommandRegistrationHandler;
import com.sk89q.worldedit.util.command.parametric.ExceptionConverter;
import com.sk89q.worldedit.util.eventbus.Subscribe;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
import com.sk89q.worldedit.util.formatting.text.event.HoverEvent;
import com.sk89q.worldedit.util.formatting.text.format.TextColor;
import com.sk89q.worldedit.util.logging.DynamicStreamHandler;
import com.sk89q.worldedit.util.logging.LogFormat;
@ -439,12 +439,16 @@ public final class PlatformCommandManager {
return split;
}
private String[] parseArgs(String input) {
return new CommandArgParser(input).parseArgs().toArray(String[]::new);
}
@Subscribe
public void handleCommand(CommandEvent event) {
Request.reset();
Actor actor = platformManager.createProxyActor(event.getActor());
String[] split = commandDetection(event.getArguments().substring(1).split(" "));
String[] split = commandDetection(parseArgs(event.getArguments().substring(1)));
// No command found!
if (!commandManager.containsCommand(split[0])) {

View File

@ -0,0 +1,85 @@
package com.sk89q.worldedit.util.command;
import java.util.stream.Stream;
public class CommandArgParser {
private enum State {
NORMAL,
QUOTE
}
private final Stream.Builder<String> args = Stream.builder();
private final StringBuilder currentArg = new StringBuilder();
private final String input;
private int index = 0;
private State state = State.NORMAL;
public CommandArgParser(String input) {
this.input = input;
}
public Stream<String> parseArgs() {
for (; index < input.length(); index++) {
char c = input.charAt(index);
switch (state) {
case NORMAL:
handleNormal(c);
break;
case QUOTE:
handleQuote(c);
}
}
finishArg(true);
return args.build();
}
private void handleNormal(char c) {
switch (c) {
case '"':
state = State.QUOTE;
break;
case ' ':
finishArg(true);
break;
case '\\':
if (index + 1 < input.length()) {
index++;
}
appendChar(input.charAt(index));
break;
default:
appendChar(c);
}
}
private void handleQuote(char c) {
switch (c) {
case '"':
state = State.NORMAL;
finishArg(false);
break;
case '\\':
if (index + 1 < input.length()) {
index++;
}
appendChar(input.charAt(index));
break;
default:
appendChar(c);
}
}
private void finishArg(boolean requireText) {
if (currentArg.length() == 0 && requireText) {
return;
}
args.add(currentArg.toString());
currentArg.setLength(0);
}
private void appendChar(char c) {
currentArg.append(c);
}
}