Fixed NPE when player has no 'favorite server'

Favorite server is determined by playtime and if player has no sessions
the player's favorite server will be null, leading to NPE while parsing
JSON.

Added Optional to the source of the null value
This commit is contained in:
Rsl1122 2019-10-25 15:08:40 +03:00
parent e5a233ae0e
commit 64f26f1df6
2 changed files with 4 additions and 5 deletions

View File

@ -73,7 +73,7 @@ public class PerServerMutator {
return timesMap; return timesMap;
} }
public UUID favoriteServer() { public Optional<UUID> favoriteServer() {
long max = 0; long max = 0;
UUID maxServer = null; UUID maxServer = null;
@ -85,7 +85,7 @@ public class PerServerMutator {
} }
} }
return maxServer; return Optional.ofNullable(maxServer);
} }
public Map<UUID, List<Session>> sessionsPerServer() { public Map<UUID, List<Session>> sessionsPerServer() {

View File

@ -92,7 +92,7 @@ public class PlayerJSONParser {
PlayerContainer player = db.query(new PlayerContainerQuery(playerUUID)); PlayerContainer player = db.query(new PlayerContainerQuery(playerUUID));
SessionsMutator sessionsMutator = SessionsMutator.forContainer(player); SessionsMutator sessionsMutator = SessionsMutator.forContainer(player);
Map<UUID, WorldTimes> worldTimesPerServer = PerServerMutator.forContainer(player).worldTimesPerServer(); Map<UUID, WorldTimes> worldTimesPerServer = PerServerMutator.forContainer(player).worldTimesPerServer();
List<Map<String, Object>> serverAccordion = new ServerAccordion(player, serverNames, graphs, year, timeAmount, locale.get(GenericLang.UNKNOWN).toString()).asMaps(); List<Map<String, Object>> serverAccordion = new ServerAccordion(player, serverNames, graphs, year, timeAmount, locale.getString(GenericLang.UNKNOWN)).asMaps();
List<PlayerKill> kills = player.getValue(PlayerKeys.PLAYER_KILLS).orElse(Collections.emptyList()); List<PlayerKill> kills = player.getValue(PlayerKeys.PLAYER_KILLS).orElse(Collections.emptyList());
List<PlayerKill> deaths = player.getValue(PlayerKeys.PLAYER_DEATHS_KILLS).orElse(Collections.emptyList()); List<PlayerKill> deaths = player.getValue(PlayerKeys.PLAYER_DEATHS_KILLS).orElse(Collections.emptyList());
@ -176,8 +176,7 @@ public class PlayerJSONParser {
info.put("session_median", timeAmount.apply(sessions.toMedianSessionLength())); info.put("session_median", timeAmount.apply(sessions.toMedianSessionLength()));
info.put("activity_index", decimals.apply(activityIndex.getValue())); info.put("activity_index", decimals.apply(activityIndex.getValue()));
info.put("activity_index_group", activityIndex.getGroup()); info.put("activity_index_group", activityIndex.getGroup());
UUID favoriteServer = perServer.favoriteServer(); info.put("favorite_server", perServer.favoriteServer().map(favoriteServer -> serverNames.getOrDefault(favoriteServer, favoriteServer.toString())).orElse(locale.getString(GenericLang.UNKNOWN)));
info.put("favorite_server", serverNames.getOrDefault(favoriteServer, favoriteServer.toString()));
double averagePing = ping.average(); double averagePing = ping.average();
int worstPing = ping.max(); int worstPing = ping.max();
int bestPing = ping.min(); int bestPing = ping.min();