From 72e7ba5e7e4c7d3bce4843ebe9d535faf40ef48d Mon Sep 17 00:00:00 2001 From: gnembon Date: Sun, 14 Jun 2020 23:48:26 -0300 Subject: [PATCH] added optional player to print as target. --- docs/scarpet/language/SystemFunctions.md | 7 +++++-- src/main/java/carpet/script/CarpetExpression.java | 12 ++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/docs/scarpet/language/SystemFunctions.md b/docs/scarpet/language/SystemFunctions.md index a1efa6d6..06e5ccdb 100644 --- a/docs/scarpet/language/SystemFunctions.md +++ b/docs/scarpet/language/SystemFunctions.md @@ -239,10 +239,13 @@ Thou shall not sample from noise changing seed frequently. Scarpet will keep tra used for sampling providing similar speed comparing to the default seed of `0`. In case the app engine uses more than 256 seeds at the same time, switching between them can get much more expensive. -### `print(expr)` +### `print(expr)`, `print(player, expr)` prints the value of the expression to chat. Passes the result of the argument to the output unchanged, -so `print`-statements can be weaved in code to debug programming issues +so `print`-statements can be weaved in code to debug programming issues. By default it uses the same communication +channels that most vanilla commands are using. + +In case player is directly specified, it only sends the message to that player, like `tell` command.
 print('foo') => results in foo, prints: foo
diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java
index a0d63d6a..0493460b 100644
--- a/src/main/java/carpet/script/CarpetExpression.java
+++ b/src/main/java/carpet/script/CarpetExpression.java
@@ -2896,10 +2896,17 @@ public class CarpetExpression
         });
 
         //"overridden" native call that prints to stderr
-        this.expr.addLazyFunction("print", 1, (c, t, lv) ->
+        this.expr.addLazyFunction("print", -1, (c, t, lv) ->
         {
+            if (lv.size() == 0 || lv.size() > 2) throw new InternalExpressionException("'print' takes one or two arguments");
             ServerCommandSource s = ((CarpetContext)c).s;
             Value res = lv.get(0).evalValue(c);
+            if (lv.size() == 2)
+            {
+                ServerPlayerEntity player = EntityValue.getPlayerByValue(s.getMinecraftServer(), res);
+                s = player.getCommandSource();
+                res = lv.get(1).evalValue(c);
+            }
             if (res instanceof FormattedTextValue)
             {
                 s.sendFeedback(((FormattedTextValue) res).getText(), false);
@@ -2915,7 +2922,8 @@ public class CarpetExpression
                     Messenger.m(s, "w " + res.getString());
                 }
             }
-            return (_c, _t) -> res; // pass through for variables
+            Value finalRes = res;
+            return (_c, _t) -> finalRes; // pass through for variables
         });
 
         this.expr.addLazyFunction("format", -1, (c, t, lv) -> {