Reworked WorldAliasSettings to not use a Map

It does not make any sense to copy the values to a Map when they are
already stored in memory in a tree like structure accessible in similar
manner.

Prevents NPE in getAliases() if World_aliases contains two level setting
1:
  14: "1.14"

Now the alias is read properly even with dots in the name.

Accidental fix to Most played world String:
"Unknown" makes no sense for a world alias if the actual name of the
world is known. Thus it is now displayed instead.

Affects issues:
- Close #1089
This commit is contained in:
Rsl1122 2019-07-08 11:04:15 +03:00
parent 092f43b129
commit ec31108f7f

View File

@ -68,15 +68,6 @@ public class WorldAliasSettings {
return config.get().get(DisplaySettings.WORLD_ALIASES); return config.get().get(DisplaySettings.WORLD_ALIASES);
} }
/**
* Used to get all World aliases in the config
*
* @return Map: Original name, Alias
*/
public Map<String, String> getAliases() {
return getAliasSection().getStringMap(false);
}
/** /**
* Adds a new World to the config section. * Adds a new World to the config section.
* <p> * <p>
@ -85,7 +76,7 @@ public class WorldAliasSettings {
* @param world World name * @param world World name
*/ */
public void addWorld(String world) { public void addWorld(String world) {
Verify.isFalse(Verify.isEmpty(world), () -> new IllegalArgumentException("Attempted to save a world alias '" + world + "'")); Verify.isFalse(Verify.isEmpty(world), () -> new IllegalArgumentException("Attempted to save empty world alias"));
ConfigNode aliasSect = getAliasSection(); ConfigNode aliasSect = getAliasSection();
@ -114,19 +105,18 @@ public class WorldAliasSettings {
entry -> entry.getValue().getTotal() // GMTimes.getTotal entry -> entry.getValue().getTotal() // GMTimes.getTotal
)); ));
Map<String, String> aliases = getAliases(); ConfigNode aliases = getAliasSection();
Map<String, Long> playtimePerAlias = new HashMap<>(); Map<String, Long> playtimePerAlias = new HashMap<>();
for (Map.Entry<String, Long> entry : playtimePerWorld.entrySet()) { for (Map.Entry<String, Long> entry : playtimePerWorld.entrySet()) {
String worldName = entry.getKey(); String worldName = entry.getKey();
long playtime = entry.getValue(); long playtime = entry.getValue();
if (!aliases.containsKey(worldName)) { if (!aliases.contains(worldName)) {
aliases.put(worldName, worldName);
addWorld(worldName); addWorld(worldName);
} }
String alias = aliases.get(worldName); String alias = aliases.getString(worldName);
playtimePerAlias.put(alias, playtimePerAlias.getOrDefault(alias, 0L) + playtime); playtimePerAlias.put(alias, playtimePerAlias.getOrDefault(alias, 0L) + playtime);
} }
@ -134,7 +124,7 @@ public class WorldAliasSettings {
} }
public Map<String, GMTimes> getGMTimesPerAlias(WorldTimes worldTimes) { public Map<String, GMTimes> getGMTimesPerAlias(WorldTimes worldTimes) {
Map<String, String> aliases = getAliases(); ConfigNode aliases = getAliasSection();
Map<String, GMTimes> gmTimesPerAlias = new HashMap<>(); Map<String, GMTimes> gmTimesPerAlias = new HashMap<>();
@ -144,12 +134,11 @@ public class WorldAliasSettings {
String worldName = entry.getKey(); String worldName = entry.getKey();
GMTimes gmTimes = entry.getValue(); GMTimes gmTimes = entry.getValue();
if (!aliases.containsKey(worldName)) { if (!aliases.contains(worldName)) {
aliases.put(worldName, worldName);
addWorld(worldName); addWorld(worldName);
} }
String alias = aliases.get(worldName); String alias = aliases.getString(worldName);
GMTimes aliasGMTimes = gmTimesPerAlias.getOrDefault(alias, new GMTimes()); GMTimes aliasGMTimes = gmTimesPerAlias.getOrDefault(alias, new GMTimes());
for (String gm : gms) { for (String gm : gms) {
@ -161,13 +150,15 @@ public class WorldAliasSettings {
} }
public String getLongestWorldPlayed(Session session) { public String getLongestWorldPlayed(Session session) {
Map<String, String> aliases = getAliases(); ConfigNode aliases = getAliasSection();
if (!session.supports(SessionKeys.WORLD_TIMES)) { if (!session.supports(SessionKeys.WORLD_TIMES)) {
return "No World Time Data"; return "No World Time Data";
} }
WorldTimes worldTimes = session.getValue(SessionKeys.WORLD_TIMES).orElse(new WorldTimes()); WorldTimes worldTimes = session.getValue(SessionKeys.WORLD_TIMES).orElse(new WorldTimes());
if (!session.supports(SessionKeys.END)) { if (!session.supports(SessionKeys.END)) {
return "Current: " + aliases.getOrDefault(worldTimes.getCurrentWorld(), "Unknown"); String currentWorld = worldTimes.getCurrentWorld();
return "Current: " + (aliases.contains(currentWorld) ? aliases.getString(currentWorld) : currentWorld);
} }
Map<String, Long> playtimePerAlias = getPlaytimePerAlias(worldTimes); Map<String, Long> playtimePerAlias = getPlaytimePerAlias(worldTimes);