From 7787f2c15e5243c963873a9975e944d5b97882e9 Mon Sep 17 00:00:00 2001 From: wizjany Date: Thu, 13 Jun 2019 10:17:37 -0400 Subject: [PATCH] Use AsyncCommandBuilder for /calc. --- .../worldedit/command/UtilityCommands.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java index 4388b129e..76e5d441e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java @@ -34,6 +34,7 @@ import com.sk89q.worldedit.command.util.CreatureButcher; 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; @@ -494,20 +495,20 @@ public class UtilityCommands { public void calc(Actor actor, @Arg(desc = "Expression to evaluate", variable = true) List 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(