From 51fae27708824e01391fc638cf877579609425fb Mon Sep 17 00:00:00 2001 From: gnembon <41132274+gnembon@users.noreply.github.com> Date: Thu, 20 Apr 2023 20:46:33 +0200 Subject: [PATCH] single argument print(foo) in player scoped apps will always target the player instance for chat message output. --- docs/scarpet/api/Auxiliary.md | 1 + src/main/java/carpet/script/api/Auxiliary.java | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/docs/scarpet/api/Auxiliary.md b/docs/scarpet/api/Auxiliary.md index 3fad5826..544ed6c2 100644 --- a/docs/scarpet/api/Auxiliary.md +++ b/docs/scarpet/api/Auxiliary.md @@ -245,6 +245,7 @@ produce an exception. ### `print(expr)`, `print(player/player_list, expr)` Displays the result of the expression to the chat. Overrides default `scarpet` behaviour of sending everything to stderr. +For player scoped apps it always by default targets the player for whom the app runs on behalf. Can optionally define player or list of players to send the message to. ### `format(components, ...)`, `format([components, ...])` diff --git a/src/main/java/carpet/script/api/Auxiliary.java b/src/main/java/carpet/script/api/Auxiliary.java index 61dcd5dc..c9818c29 100644 --- a/src/main/java/carpet/script/api/Auxiliary.java +++ b/src/main/java/carpet/script/api/Auxiliary.java @@ -505,25 +505,31 @@ public class Auxiliary { throw new InternalExpressionException("'print' takes one or two arguments"); } - CommandSourceStack s = ((CarpetContext) c).source(); + CarpetContext cc = (CarpetContext) c; + CommandSourceStack s = cc.source(); MinecraftServer server = s.getServer(); Value res = lv.get(0); - List targets = null; + List targets = null; if (lv.size() == 2) { List playerValues = (res instanceof ListValue list) ? list.getItems() : Collections.singletonList(res); - List playerTargets = new ArrayList<>(); + List playerTargets = new ArrayList<>(); playerValues.forEach(pv -> { ServerPlayer player = EntityValue.getPlayerByValue(server, pv); if (player == null) { throw new InternalExpressionException("Cannot target player " + pv.getString() + " in print"); } - playerTargets.add(player); + playerTargets.add(player.createCommandSourceStack()); }); targets = playerTargets; res = lv.get(1); - } + } else if (c.host.user != null) { + ServerPlayer player = cc.server().getPlayerList().getPlayerByName(cc.host.user); + if (player != null) { + targets = Collections.singletonList(player.createCommandSourceStack()); + } + } // optionally retrieve from CC.host.responsibleSource to print? Component message = FormattedTextValue.getTextByValue(res); if (targets == null) { @@ -531,7 +537,7 @@ public class Auxiliary } else { - targets.forEach(p -> p.createCommandSourceStack().sendSuccess(message, false)); + targets.forEach(p -> p.sendSuccess(message, false)); } return res; // pass through for variables });