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

Fall back to the map name when the display name is equal or set empty #3861

Merged
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
190 changes: 61 additions & 129 deletions src/main/java/net/rptools/maptool/client/functions/MapFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/
package net.rptools.maptool.client.functions;

import com.google.gson.JsonArray;
import com.google.gson.JsonPrimitive;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -65,76 +63,34 @@ public Object childEvaluate(
ZoneRenderer currentZR = MapTool.getFrame().getCurrentZoneRenderer();
if (currentZR == null) {
throw new ParserException(I18N.getText("macro.function.map.none", functionName));
} else {
return currentZR.getZone().getName();
}
return currentZR.getZone().getName();

} else if (functionName.equalsIgnoreCase("getMapDisplayName")) {
checkTrusted(functionName);
FunctionUtil.blockUntrustedMacro(functionName);
FunctionUtil.checkNumberParam(functionName, parameters, 0, 1);
if (parameters.size() == 0) {
ZoneRenderer currentZR = MapTool.getFrame().getCurrentZoneRenderer();
if (currentZR == null) {
throw new ParserException(I18N.getText("macro.function.map.none", functionName));
} else {
return currentZR.getZone().getPlayerAlias();
}
} else {
List<ZoneRenderer> rendererList =
new LinkedList<ZoneRenderer>(
MapTool.getFrame().getZoneRenderers()); // copied from ZoneSelectionPopup
String searchMap = parameters.get(0).toString();
String foundMap = null;
for (int i = 0; i < rendererList.size(); i++) {
if (rendererList.get(i).getZone().getName().equals(searchMap)) {
foundMap = rendererList.get(i).getZone().getPlayerAlias();
break;
}
}
if (foundMap == null) {
throw new ParserException(I18N.getText("macro.function.map.notFound", functionName));
} else {
return foundMap;
}
}
final var zr = FunctionUtil.getZoneRendererFromParam(functionName, parameters, 0);
return zr.getZone().getDisplayName();

} else if (functionName.equalsIgnoreCase("setCurrentMap")) {
checkTrusted(functionName);
FunctionUtil.blockUntrustedMacro(functionName);
FunctionUtil.checkNumberParam(functionName, parameters, 1, 1);
String mapName = parameters.get(0).toString();
ZoneRenderer zr = getNamedMap(functionName, mapName);
final var zr = FunctionUtil.getZoneRenderer(functionName, mapName);
MapTool.getFrame().setCurrentZoneRenderer(zr);
return mapName;

} else if ("getMapVisible".equalsIgnoreCase(functionName)) {
FunctionUtil.checkNumberParam(functionName, parameters, 0, 1);
if (parameters.size() > 0) {
String mapName = parameters.get(0).toString();
return getNamedMap(functionName, mapName).getZone().isVisible()
? BigDecimal.ONE
: BigDecimal.ZERO;
} else {
// Return the visibility of the current map/zone
ZoneRenderer currentZR = MapTool.getFrame().getCurrentZoneRenderer();
if (currentZR == null) {
throw new ParserException(I18N.getText("macro.function.map.none", functionName));
} else {
return currentZR.getZone().isVisible() ? BigDecimal.ONE : BigDecimal.ZERO;
}
}
final var zr = FunctionUtil.getZoneRendererFromParam(functionName, parameters, 0);
return zr.getZone().isVisible() ? BigDecimal.ONE : BigDecimal.ZERO;

} else if ("setMapVisible".equalsIgnoreCase(functionName)) {
checkTrusted(functionName);
FunctionUtil.blockUntrustedMacro(functionName);
FunctionUtil.checkNumberParam(functionName, parameters, 1, 2);
boolean visible = FunctionUtil.getBooleanValue(parameters.get(0).toString());
Zone zone;
if (parameters.size() > 1) {
String mapName = parameters.get(1).toString();
zone = getNamedMap(functionName, mapName).getZone();
} else {
ZoneRenderer currentZR = MapTool.getFrame().getCurrentZoneRenderer();
if (currentZR == null) {
throw new ParserException(I18N.getText("macro.function.map.none", functionName));
} else {
zone = currentZR.getZone();
}
}
final var zr = FunctionUtil.getZoneRendererFromParam(functionName, parameters, 1);
final var zone = zr.getZone();
// Set the zone and return the visibility of the current map/zone
zone.setVisible(visible);
MapTool.serverCommand().setZoneVisibility(zone.getId(), zone.isVisible());
Expand All @@ -143,41 +99,49 @@ public Object childEvaluate(
return zone.isVisible() ? BigDecimal.ONE : BigDecimal.ZERO;

} else if ("setMapName".equalsIgnoreCase(functionName)) {
checkTrusted(functionName);
FunctionUtil.blockUntrustedMacro(functionName);
FunctionUtil.checkNumberParam(functionName, parameters, 2, 2);
String oldMapName = parameters.get(0).toString();
String newMapName = parameters.get(1).toString();
Zone zone = getNamedMap(functionName, oldMapName).getZone();
Zone zone = FunctionUtil.getZoneRenderer(functionName, oldMapName).getZone();
zone.setName(newMapName);
MapTool.serverCommand().renameZone(zone.getId(), newMapName);
if (zone == MapTool.getFrame().getCurrentZoneRenderer().getZone())
if (zone == MapTool.getFrame().getCurrentZoneRenderer().getZone()) {
MapTool.getFrame().setCurrentZoneRenderer(MapTool.getFrame().getCurrentZoneRenderer());
}
return zone.getName();

} else if ("setMapDisplayName".equalsIgnoreCase(functionName)) {
checkTrusted(functionName);
FunctionUtil.blockUntrustedMacro(functionName);
FunctionUtil.checkNumberParam(functionName, parameters, 2, 2);
String mapName = parameters.get(0).toString();
String newMapDisplayName = parameters.get(1).toString();
Zone zone = getNamedMap(functionName, mapName).getZone();
String oldName;
oldName = zone.getPlayerAlias();
zone.setPlayerAlias(newMapDisplayName);
if (oldName.equals(newMapDisplayName)) return zone.getPlayerAlias();
MapTool.serverCommand().changeZoneDispName(zone.getId(), newMapDisplayName);
if (zone == MapTool.getFrame().getCurrentZoneRenderer().getZone())
MapTool.getFrame().setCurrentZoneRenderer(MapTool.getFrame().getCurrentZoneRenderer());
if (oldName.equals(zone.getPlayerAlias()))

Zone zone = FunctionUtil.getZoneRenderer(functionName, mapName).getZone();
if (newMapDisplayName.equals(zone.getDisplayName())) {
// The name is the same, so nothing to do.
return newMapDisplayName;
}

final var nameChangeAccepted = zone.setPlayerAlias(newMapDisplayName);
if (!nameChangeAccepted) {
throw new ParserException(
I18N.getText("macro.function.map.duplicateDisplay", functionName));
return zone.getPlayerAlias();
}

MapTool.serverCommand().changeZoneDispName(zone.getId(), newMapDisplayName);
if (zone == MapTool.getFrame().getCurrentZoneRenderer().getZone()) {
MapTool.getFrame().setCurrentZoneRenderer(MapTool.getFrame().getCurrentZoneRenderer());
}

return zone.getDisplayName();

} else if ("copyMap".equalsIgnoreCase(functionName)) {
checkTrusted(functionName);
FunctionUtil.blockUntrustedMacro(functionName);
FunctionUtil.checkNumberParam(functionName, parameters, 2, 2);
String oldName = parameters.get(0).toString();
String newName = parameters.get(1).toString();
Zone oldMap = getNamedMap(functionName, oldName).getZone();
Zone oldMap = FunctionUtil.getZoneRenderer(functionName, oldName).getZone();
Zone newMap = new Zone(oldMap);
newMap.setName(newName);
MapTool.addZone(newMap, false);
Expand All @@ -189,97 +153,65 @@ public Object childEvaluate(
FunctionUtil.checkNumberParam(functionName, parameters, 0, 1);
boolean allMaps = functionName.equalsIgnoreCase("getAllMapNames");

if (allMaps) checkTrusted(functionName);
if (allMaps) {
FunctionUtil.blockUntrustedMacro(functionName);
}

List<String> mapNames = new LinkedList<String>();
List<String> mapNames = new LinkedList<>();
for (ZoneRenderer zr : MapTool.getFrame().getZoneRenderers()) {
if (allMaps || zr.getZone().isVisible()) {
mapNames.add(zr.getZone().getName());
}
}

String delim = parameters.size() > 0 ? parameters.get(0).toString() : ",";
if ("json".equals(delim)) {
JsonArray jarr = new JsonArray();
mapNames.forEach(m -> jarr.add(new JsonPrimitive(m)));
return jarr;
} else {
return StringFunctions.getInstance().join(mapNames, delim);
}
return FunctionUtil.delimitedResult(delim, mapNames);

} else if ("getVisibleMapDisplayNames".equalsIgnoreCase(functionName)
|| "getAllMapDisplayNames".equalsIgnoreCase(functionName)) {
FunctionUtil.checkNumberParam(functionName, parameters, 0, 1);
boolean allMaps = functionName.equalsIgnoreCase("getAllMapDisplayNames");

if (allMaps) checkTrusted(functionName);
if (allMaps) {
FunctionUtil.blockUntrustedMacro(functionName);
}

List<String> mapNames = new LinkedList<String>();
List<String> mapNames = new LinkedList<>();
for (ZoneRenderer zr : MapTool.getFrame().getZoneRenderers()) {
if (allMaps || zr.getZone().isVisible()) {
mapNames.add(zr.getZone().getPlayerAlias());
mapNames.add(zr.getZone().getDisplayName());
}
}

String delim = parameters.size() > 0 ? parameters.get(0).toString() : ",";
if ("json".equals(delim)) {
JsonArray jarr = new JsonArray();
mapNames.forEach(m -> jarr.add(new JsonPrimitive(m)));
return jarr;
} else {
return StringFunctions.getInstance().join(mapNames, delim);
}
return FunctionUtil.delimitedResult(delim, mapNames);

} else if ("getMapName".equalsIgnoreCase(functionName)) {
FunctionUtil.checkNumberParam(functionName, parameters, 1, 1);
String dispName = parameters.get(0).toString();
checkTrusted(functionName);
String displayName = parameters.get(0).toString();
FunctionUtil.blockUntrustedMacro(functionName);

for (ZoneRenderer zr : MapTool.getFrame().getZoneRenderers()) {
if (zr.getZone().getPlayerAlias().equals(dispName)) {
if (displayName.equals(zr.getZone().getDisplayName())) {
return zr.getZone().getName();
}
}
throw new ParserException(I18N.getText("macro.function.map.notFound", functionName));

} else if ("setMapSelectButton".equalsIgnoreCase(functionName)) {
// this is kind of a map function? :)
checkTrusted(functionName);
FunctionUtil.blockUntrustedMacro(functionName);
FunctionUtil.checkNumberParam(functionName, parameters, 1, 1);
boolean vis = !parameters.get(0).toString().equals("0");
if (MapTool.getFrame().getFullsZoneButton() != null)
if (MapTool.getFrame().getFullsZoneButton() != null) {
MapTool.getFrame().getFullsZoneButton().setVisible(vis);
}
MapTool.getFrame().getToolbarPanel().getMapselect().setVisible(vis);
return (MapTool.getFrame().getToolbarPanel().getMapselect().isVisible()
? BigDecimal.ONE
: BigDecimal.ZERO);
}
throw new ParserException(I18N.getText("macro.function.general.unknownFunction", functionName));
}

/**
* Find the map/zone for a given map name
*
* @param functionName String Name of the calling function.
* @param mapName String Name of the searched for map.
* @return ZoneRenderer The map/zone.
* @throws ParserException if the map is not found
*/
private ZoneRenderer getNamedMap(final String functionName, final String mapName)
throws ParserException {
ZoneRenderer zr = MapTool.getFrame().getZoneRenderer(mapName);

if (zr != null) return zr;

throw new ParserException(
I18N.getText("macro.function.moveTokenMap.unknownMap", functionName, mapName));
}

/**
* Checks whether or not the function is trusted
*
* @param functionName Name of the macro function
* @throws ParserException Returns trust error message and function name
*/
private void checkTrusted(String functionName) throws ParserException {
if (!MapTool.getParser().isMacroTrusted()) {
throw new ParserException(I18N.getText("macro.function.general.noPerm", functionName));
}
throw new ParserException(I18N.getText("macro.function.general.unknownFunction", functionName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private JsonObject getMapInfo() throws ParserException {
}

minfo.addProperty("name", zone.getName());
minfo.addProperty("display name", zone.getPlayerAlias());
minfo.addProperty("display name", zone.getDisplayName());
minfo.addProperty("image x scale", zone.getImageScaleX());
minfo.addProperty("image y scale", zone.getImageScaleY());
minfo.addProperty("player visible", zone.isVisible() ? 1 : 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1985,7 +1985,7 @@ public boolean isLoading() {
loadingProgress =
String.format(
" Loading Map '%s' - %d/%d Loaded %d/%d Cached",
zone.getPlayerAlias(), downloadCount, assetSet.size(), cacheCount, assetSet.size());
zone.getDisplayName(), downloadCount, assetSet.size(), cacheCount, assetSet.size());
isLoaded = loaded;
if (isLoaded) {
// Notify the token tree that it should update
Expand Down
26 changes: 23 additions & 3 deletions src/main/java/net/rptools/maptool/model/Zone.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.rptools.lib.MD5Key;
import net.rptools.maptool.client.AppPreferences;
import net.rptools.maptool.client.AppUtil;
Expand Down Expand Up @@ -368,14 +369,27 @@ public void setFogPaint(DrawablePaint paint) {
}

/** @return name of the zone */
public String getName() {
public @Nonnull String getName() {
return name;
}

public String getPlayerAlias() {
/** @return The zone's player alias, if set. Otherwise {@code null}. */
public @Nullable String getPlayerAlias() {
return playerAlias;
}

/**
* Get the name of the map to show to players.
*
* <p>If a player alias is set, that will be used as the display name. Otherwise the name is used
* as the display name.
*
* @return The name of the map that should be shown to players.
*/
public @Nonnull String getDisplayName() {
return Objects.requireNonNullElse(playerAlias, name);
}

public void setName(String name) {
this.name = name;
}
Expand All @@ -399,7 +413,7 @@ public boolean setPlayerAlias(String playerAlias) {
@Override
public String toString() {
if (!MapTool.getPlayer().isGM()) {
return playerAlias != null ? playerAlias : name;
return getDisplayName();
} else if (playerAlias == null || name.equals(playerAlias)) {
return name;
} else {
Expand Down Expand Up @@ -2051,6 +2065,12 @@ private void collapseDrawableLayer(List<DrawnElement> layer) {
////
// Backward compatibility
protected Object readResolve() {
if ("".equals(playerAlias) || name.equals(playerAlias)) {
// Don't keep redundant player aliases around. The display name will default to the name if
// no player alias is set.
playerAlias = null;
}

// 1.3b76 -> 1.3b77
// adding the exposed area for Individual FOW
if (exposedAreaMeta == null) {
Expand Down
Loading