diff --git a/src/main/java/net/rptools/maptool/client/script/javascript/api/JSAPIToken.java b/src/main/java/net/rptools/maptool/client/script/javascript/api/JSAPIToken.java index 3045e415e9..f410b5d896 100644 --- a/src/main/java/net/rptools/maptool/client/script/javascript/api/JSAPIToken.java +++ b/src/main/java/net/rptools/maptool/client/script/javascript/api/JSAPIToken.java @@ -22,6 +22,7 @@ import net.rptools.maptool.language.I18N; import net.rptools.maptool.model.GUID; import net.rptools.maptool.model.Token; +import net.rptools.maptool.model.TokenProperty; import net.rptools.maptool.model.Zone; import net.rptools.parser.ParserException; import org.graalvm.polyglot.HostAccess; @@ -119,12 +120,61 @@ public String getId() { return "" + token.getId(); } + @HostAccess.Export + public String getRawProperty(String name) { + boolean trusted = JSScriptEngine.inTrustedContext(); + String playerId = MapTool.getPlayer().getName(); + if (trusted || token.isOwner(playerId)) { + Object val = this.token.getProperty(name); + // Short-circuit to returning null so we don't return "null" + if (val == null) { + return null; + } + return "" + val; + } + return null; + } + @HostAccess.Export public String getProperty(String name) { boolean trusted = JSScriptEngine.inTrustedContext(); String playerId = MapTool.getPlayer().getName(); if (trusted || token.isOwner(playerId)) { - return "" + this.token.getProperty(name); + Object val = this.token.getProperty(name); + // Fall back to the property type's default value + // since it's not useful to return nulls and require + // javascript to have to handle defaults when getInfo isn't even bound, + // especially when the value gets unset if it matches the default. + // Evaluation is not performed automatically, use getEvaluatedProperty for that. + if (val == null) { + List propertyList = + MapTool.getCampaign() + .getCampaignProperties() + .getTokenPropertyList(this.token.getPropertyType()); + if (propertyList != null) { + for (TokenProperty property : propertyList) { + if (name.equalsIgnoreCase(property.getName())) { + val = property.getDefaultValue(); + break; + } + } + } + } + if (val == null) { + return null; + } + return "" + val; + } + return null; + } + + @HostAccess.Export + public String getEvaluatedProperty(String name) { + boolean trusted = JSScriptEngine.inTrustedContext(); + String playerId = MapTool.getPlayer().getName(); + if (trusted || token.isOwner(playerId)) { + Object val = this.token.getEvaluatedProperty(name); + return "" + val; } return ""; }