single argument print(foo) in player scoped apps will always target the player instance for chat message output.
This commit is contained in:
1 parent
c9b93e598a
commit
51fae27708
2 files changed
+12
-5
No files matched your search
@@ -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, ...])`
|
||||
|
||||
@@ -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<ServerPlayer> targets = null;
|
||||
List<CommandSourceStack> targets = null;
|
||||
if (lv.size() == 2)
|
||||
{
|
||||
List<Value> playerValues = (res instanceof ListValue list) ? list.getItems() : Collections.singletonList(res);
|
||||
List<ServerPlayer> playerTargets = new ArrayList<>();
|
||||
List<CommandSourceStack> 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
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user