From 51a540dab1251ab8ab2b4c8787f5b2015578ab2f Mon Sep 17 00:00:00 2001 From: sk89q Date: Thu, 3 Jul 2014 03:34:19 -0700 Subject: [PATCH] Added //calc, //eval, etc. to evaluate mathematical expressions. --- .../worldedit/command/UtilityCommands.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java b/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java index d57bedd28..d1e0852d8 100644 --- a/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java +++ b/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java @@ -22,6 +22,7 @@ package com.sk89q.worldedit.command; import com.google.common.base.Joiner; import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.CommandContext; +import com.sk89q.minecraft.util.commands.CommandException; import com.sk89q.minecraft.util.commands.CommandPermissions; import com.sk89q.minecraft.util.commands.Logging; import com.sk89q.worldedit.EditSession; @@ -36,6 +37,9 @@ import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extension.platform.CommandManager; +import com.sk89q.worldedit.internal.expression.Expression; +import com.sk89q.worldedit.internal.expression.ExpressionException; +import com.sk89q.worldedit.internal.expression.runtime.EvaluationException; import com.sk89q.worldedit.patterns.Pattern; import com.sk89q.worldedit.patterns.SingleBlockPattern; import com.sk89q.worldedit.regions.CuboidRegion; @@ -44,6 +48,7 @@ import com.sk89q.worldedit.util.command.CommandCallable; import com.sk89q.worldedit.util.command.CommandMapping; import com.sk89q.worldedit.util.command.Dispatcher; import com.sk89q.worldedit.util.command.PrimaryAliasComparator; +import com.sk89q.worldedit.util.command.binding.Text; import com.sk89q.worldedit.util.command.parametric.Optional; import com.sk89q.worldedit.util.formatting.ColorCodeBuilder; import com.sk89q.worldedit.util.formatting.Style; @@ -504,6 +509,24 @@ public class UtilityCommands { player.print("Marked " + removed + " entit(ies) for removal."); } + @Command( + aliases = { "/calc", "/calculate", "/eval", "/evaluate", "/solve" }, + usage = "", + desc = "Evaluate a mathematical expression" + ) + public void calc(Actor actor, @Text String input) throws CommandException { + try { + Expression expression = Expression.compile(input); + actor.print("= " + expression.evaluate()); + } catch (EvaluationException e) { + actor.printError(String.format( + "'%s' could not be parsed as a valid expression", input)); + } catch (ExpressionException e) { + actor.printError(String.format( + "'%s' could not be evaluated (error: %s)", input, e.getMessage())); + } + } + @Command( aliases = { "/help" }, usage = "[]",