Skip to content

Commit

Permalink
Use AsyncCommandBuilder for /calc.
Browse files Browse the repository at this point in the history
  • Loading branch information
wizjany committed Jun 13, 2019
1 parent 213cadf commit 7787f2c
Showing 1 changed file with 10 additions and 9 deletions.
Expand Up @@ -34,6 +34,7 @@
import com.sk89q.worldedit.command.util.EntityRemover;
import com.sk89q.worldedit.command.util.Logging;
import com.sk89q.worldedit.command.util.PrintCommandHelp;
import com.sk89q.worldedit.command.util.WorldEditAsyncCommandBuilder;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.Actor;
Expand Down Expand Up @@ -494,20 +495,20 @@ private int killMatchingEntities(Integer radius, Player player, Supplier<EntityF
public void calc(Actor actor,
@Arg(desc = "Expression to evaluate", variable = true)
List<String> input) {
Expression expression;
try {
Expression expression = Expression.compile(String.join(" ", input));
double result = expression.evaluate(
new double[]{}, WorldEdit.getInstance().getSessionManager().get(actor).getTimeout());
String formatted = formatter.format(result);
actor.print(SubtleFormat.wrap(input + " = ")
.append(TextComponent.of(formatted, TextColor.LIGHT_PURPLE)));
} catch (EvaluationException e) {
actor.printError(String.format(
"'%s' could not be evaluated (error: %s)", input, e.getMessage()));
expression = Expression.compile(String.join(" ", input));
} catch (ExpressionException e) {
actor.printError(String.format(
"'%s' could not be parsed as a valid expression", input));
return;
}
WorldEditAsyncCommandBuilder.createAndSendMessage(actor, () -> {
double result = expression.evaluate(
new double[]{}, WorldEdit.getInstance().getSessionManager().get(actor).getTimeout());
String formatted = Double.isNaN(result) ? "NaN" : formatter.format(result);
return SubtleFormat.wrap(input + " = ").append(TextComponent.of(formatted, TextColor.LIGHT_PURPLE));
}, null);
}

@Command(
Expand Down

0 comments on commit 7787f2c

Please sign in to comment.