Skip to content

Commit

Permalink
Add property default and reset JS bindings
Browse files Browse the repository at this point in the history
resetProperty was conspicuously absent
and is necessary to support resetting a token to default
which is convenient for things like a Full Heal or removing expired
temporary HP.

Another reason to reset properties is if they have been restored to
their default and you wish to reduce memory usage and serialization size
but to do so without coupling to the campaign requires functions to get
the default.

getPropertyDefault gets the default value from the properties verbatim
and getEvaluatedPropertDefault evaluates the expression in the context
of the token.
  • Loading branch information
fishface60 committed Jan 30, 2025
1 parent ce41af3 commit 475caa6
Showing 1 changed file with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@ public String getProperty(String name) {
return "" + val;
}

@HostAccess.Export
public String getPropertyDefault(String name) {
boolean trusted = JSScriptEngine.inTrustedContext();
String playerId = MapTool.getPlayer().getName();
if (!trusted && !token.isOwner(playerId)) {
return null;
}

return this.token.getPropertyDefault(name);
}

@HostAccess.Export
public String getEvaluatedProperty(String name) {
boolean trusted = JSScriptEngine.inTrustedContext();
Expand All @@ -169,6 +180,22 @@ public String getEvaluatedProperty(String name) {
return "";
}

@HostAccess.Export
public String getEvaluatedPropertyDefault(String name) {
boolean trusted = JSScriptEngine.inTrustedContext();
String playerId = MapTool.getPlayer().getName();
if (!trusted && !token.isOwner(playerId)) {
return null;
}

var res = this.token.getPropertyDefault(name);
if (res == null) {
return null;
}

return "" + this.token.evaluateProperty(null, name, res);
}

@HostAccess.Export
public void setProperty(String name, Object value) {
boolean trusted = JSScriptEngine.inTrustedContext();
Expand All @@ -180,6 +207,19 @@ public void setProperty(String name, Object value) {
}
}

@HostAccess.Export
public boolean resetProperty(String name) {
boolean trusted = JSScriptEngine.inTrustedContext();
String playerId = MapTool.getPlayer().getName();
if (!trusted && !token.isOwner(playerId)) {
return false;
}

this.token.resetProperty(name);
MapTool.serverCommand().updateTokenProperty(token, Token.Update.resetProperty, name);
return true;
}

@HostAccess.Export
public int getX() throws ParserException {
boolean trusted = JSScriptEngine.inTrustedContext();
Expand Down

0 comments on commit 475caa6

Please sign in to comment.