From f310e58245d28680ecc6a627568f93c2a78a47b4 Mon Sep 17 00:00:00 2001 From: Kenneth VanderLinde Date: Sun, 12 Mar 2023 14:40:02 -0700 Subject: [PATCH] Fall back to the map name if no player alias is set 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. --- .../client/functions/MapFunctions.java | 28 +++++++++++-------- .../client/functions/getInfoFunction.java | 2 +- .../maptool/client/ui/zone/ZoneRenderer.java | 2 +- .../java/net/rptools/maptool/model/Zone.java | 26 +++++++++++++++-- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/main/java/net/rptools/maptool/client/functions/MapFunctions.java b/src/main/java/net/rptools/maptool/client/functions/MapFunctions.java index fa24979dd4..d887b5ab92 100644 --- a/src/main/java/net/rptools/maptool/client/functions/MapFunctions.java +++ b/src/main/java/net/rptools/maptool/client/functions/MapFunctions.java @@ -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); @@ -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); @@ -175,7 +179,7 @@ public Object childEvaluate( List mapNames = new LinkedList<>(); for (ZoneRenderer zr : MapTool.getFrame().getZoneRenderers()) { if (allMaps || zr.getZone().isVisible()) { - mapNames.add(zr.getZone().getPlayerAlias()); + mapNames.add(zr.getZone().getDisplayName()); } } @@ -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(); } } diff --git a/src/main/java/net/rptools/maptool/client/functions/getInfoFunction.java b/src/main/java/net/rptools/maptool/client/functions/getInfoFunction.java index 8b515cad5b..82f2986be1 100644 --- a/src/main/java/net/rptools/maptool/client/functions/getInfoFunction.java +++ b/src/main/java/net/rptools/maptool/client/functions/getInfoFunction.java @@ -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); diff --git a/src/main/java/net/rptools/maptool/client/ui/zone/ZoneRenderer.java b/src/main/java/net/rptools/maptool/client/ui/zone/ZoneRenderer.java index 43d66882b0..171a69d8fd 100644 --- a/src/main/java/net/rptools/maptool/client/ui/zone/ZoneRenderer.java +++ b/src/main/java/net/rptools/maptool/client/ui/zone/ZoneRenderer.java @@ -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 diff --git a/src/main/java/net/rptools/maptool/model/Zone.java b/src/main/java/net/rptools/maptool/model/Zone.java index 6ebefd0faa..bb60723a94 100644 --- a/src/main/java/net/rptools/maptool/model/Zone.java +++ b/src/main/java/net/rptools/maptool/model/Zone.java @@ -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; @@ -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. + * + *

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; } @@ -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 { @@ -2051,6 +2065,12 @@ private void collapseDrawableLayer(List 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) {