diff --git a/docs/scarpet/api/Entities.md b/docs/scarpet/api/Entities.md index 34584e8b..7f03f748 100644 --- a/docs/scarpet/api/Entities.md +++ b/docs/scarpet/api/Entities.md @@ -8,7 +8,10 @@ In this case - entities would need to be re-fetched, or the code should account ### `player(), player(type), player(name)` -With no arguments, it returns the calling player or the player closest to the caller. Note that the main context +With no arguments, it returns the calling player or the player closest to the caller. +For player-scoped apps (which is a default) its always the owning player or `null` if it not present even if some code +still runs in their name. +Note that the main context will receive `p` variable pointing to this player. With `type` or `name` specified, it will try first to match a type, returning a list of players matching a type, and if this fails, will assume its player name query retuning player with that name, or `null` if no player was found. With `'all'`, list of all players in the game, in all dimensions, so end diff --git a/src/main/java/carpet/script/ScriptHost.java b/src/main/java/carpet/script/ScriptHost.java index d984673f..a59f62aa 100644 --- a/src/main/java/carpet/script/ScriptHost.java +++ b/src/main/java/carpet/script/ScriptHost.java @@ -91,7 +91,7 @@ public abstract class ScriptHost protected ScriptHost parent; protected boolean perUser; - protected String user; + public String user; public String getName() {return main ==null?null: main.getName();} diff --git a/src/main/java/carpet/script/api/Entities.java b/src/main/java/carpet/script/api/Entities.java index ccf35938..68a02904 100644 --- a/src/main/java/carpet/script/api/Entities.java +++ b/src/main/java/carpet/script/api/Entities.java @@ -45,10 +45,17 @@ public class Entities { { expression.addLazyFunction("player", -1, (c, t, lv) -> { - if (lv.size() ==0) + if (lv.size() == 0) { - - Entity callingEntity = ((CarpetContext)c).s.getEntity(); + CarpetContext cc = (CarpetContext)c; + if (cc.host.user != null) + { + ServerPlayerEntity player = cc.s.getMinecraftServer().getPlayerManager().getPlayer(cc.host.user); + if (player == null) return LazyValue.NULL; + Value ret = new EntityValue(player); + return (_c, _t) -> ret; + } + Entity callingEntity = cc.s.getEntity(); if (callingEntity instanceof PlayerEntity) { Value retval = new EntityValue(callingEntity); @@ -108,7 +115,7 @@ public class Entities { retval = new EntityValue(player); } Value finalVar = retval; - return (cc, tt) -> finalVar; + return (_c, _t) -> finalVar; }); expression.addLazyFunction("spawn", -1, (c, t, lv) ->