Skip to content

Commit

Permalink
Fall back to the map name if no player alias is set
Browse files Browse the repository at this point in the history
The behaviour of these function is back to their 1.12.2 behaviour, as they will no longer return `null` values for maps
that have no player alias set:
- `getMapDisplayName()`
- `getAllMapDisplayNames()`
- `getVisibleMapDisplayNames()`

Also `getMapName()` will once again be able to find maps by matching the name when no player alias is set.

The new method `Zone.getDisplayName()` can be used to get the name of the zone to show to players, whether that is the
player alias or the regular name.
  • Loading branch information
kwvanderlinde committed Mar 13, 2023
1 parent 06ba966 commit 715e9c2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Object childEvaluate(
FunctionUtil.blockUntrustedMacro(functionName);
FunctionUtil.checkNumberParam(functionName, parameters, 0, 1);
final var zr = FunctionUtil.getZoneRendererFromParam(functionName, parameters, 0);
return zr.getZone().getPlayerAlias();
return zr.getZone().getDisplayName();

} else if (functionName.equalsIgnoreCase("setCurrentMap")) {
FunctionUtil.blockUntrustedMacro(functionName);
Expand Down Expand Up @@ -116,21 +116,25 @@ public Object childEvaluate(
FunctionUtil.checkNumberParam(functionName, parameters, 2, 2);
String mapName = parameters.get(0).toString();
String newMapDisplayName = parameters.get(1).toString();

Zone zone = FunctionUtil.getZoneRenderer(functionName, mapName).getZone();
String oldName = zone.getPlayerAlias();
zone.setPlayerAlias(newMapDisplayName);
if (oldName.equals(newMapDisplayName)) {
return zone.getPlayerAlias();
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));
}

MapTool.serverCommand().changeZoneDispName(zone.getId(), newMapDisplayName);
if (zone == MapTool.getFrame().getCurrentZoneRenderer().getZone()) {
MapTool.getFrame().setCurrentZoneRenderer(MapTool.getFrame().getCurrentZoneRenderer());
}
if (oldName.equals(zone.getPlayerAlias())) {
throw new ParserException(
I18N.getText("macro.function.map.duplicateDisplay", functionName));
}
return zone.getPlayerAlias();

return zone.getDisplayName();

} else if ("copyMap".equalsIgnoreCase(functionName)) {
FunctionUtil.blockUntrustedMacro(functionName);
Expand Down Expand Up @@ -175,7 +179,7 @@ public Object childEvaluate(
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());
}
}

Expand All @@ -188,7 +192,7 @@ public Object childEvaluate(
FunctionUtil.blockUntrustedMacro(functionName);

for (ZoneRenderer zr : MapTool.getFrame().getZoneRenderers()) {
if (zr.getZone().getPlayerAlias().equals(displayName)) {
if (displayName.equals(zr.getZone().getDisplayName())) {
return zr.getZone().getName();
}
}
Expand Down
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

0 comments on commit 715e9c2

Please sign in to comment.