Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional JavaScript API Token Functions #4323

Merged
merged 13 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package net.rptools.maptool.client.script.javascript.api;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.script.javascript.*;
Expand Down Expand Up @@ -199,4 +200,70 @@ public void setMap(Zone m) {
public String getMapName() {
return this.map.getDisplayName();
}

@HostAccess.Export
public boolean getState(String stateName) {
Object currentState = this.token.getState(stateName);
return (currentState instanceof Boolean && (Boolean) currentState);
}

@HostAccess.Export
public void setState(String stateName, boolean aValue) {
boolean trusted = JSScriptEngine.inTrustedContext();
String playerId = MapTool.getPlayer().getName();
if (trusted || token.isOwner(playerId)) {
this.token.setState(stateName, aValue);
MapTool.serverCommand().updateTokenProperty(token, Token.Update.setState, stateName, aValue);
}
}

@HostAccess.Export
public void setAllStates(boolean aValue) {
boolean trusted = JSScriptEngine.inTrustedContext();
String playerId = MapTool.getPlayer().getName();
if (trusted || token.isOwner(playerId)) {
this.token.setAllStates(aValue);
MapTool.serverCommand().updateTokenProperty(token, Token.Update.setAllStates, aValue);
}
}

@HostAccess.Export
public List<String> getActiveStates() {
return this.token.getSetStates();
}

@HostAccess.Export
public boolean isPC() {
return this.token.getType() == Token.Type.PC;
}

@HostAccess.Export
public void setPC() {
boolean trusted = JSScriptEngine.inTrustedContext();
String playerId = MapTool.getPlayer().getName();
if (trusted || token.isOwner(playerId)) {
this.token.setType(Token.Type.PC);
MapTool.serverCommand().updateTokenProperty(token, Token.Update.setPC);
}
}

@HostAccess.Export
public boolean isNPC() {
return this.token.getType() == Token.Type.NPC;
}

@HostAccess.Export
public void setNPC() {
boolean trusted = JSScriptEngine.inTrustedContext();
String playerId = MapTool.getPlayer().getName();
if (trusted || token.isOwner(playerId)) {
this.token.setType(Token.Type.NPC);
MapTool.serverCommand().updateTokenProperty(token, Token.Update.setNPC);
}
}

@HostAccess.Export
public String getType() {
return this.token.getType().name();
}
}
12 changes: 12 additions & 0 deletions src/main/java/net/rptools/maptool/model/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -1738,6 +1738,18 @@ public Object getState(String property) {
return state.get(property);
}

public List<String> getSetStates() {
List<String> setStates = new ArrayList<String>();
for (Map.Entry<String, Object> entry : state.entrySet()) {
if (entry.getValue() instanceof Boolean) {
if ((Boolean) entry.getValue()) {
setStates.add(entry.getKey());
}
}
}
return setStates;
}

/**
* Set the value of state for this Token.
*
Expand Down