From 9e6c342f8958a86c02394dcd4f58d57111bba322 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Mon, 20 Mar 2017 14:50:29 +0200 Subject: [PATCH] [2.9.0-DEV] Bugfixes Fix #61 #58 #56 #57 #59 - Fixed GamemodeTimesHandler giving NPE if a gamemode is null. - Fixed ArrayOutOfBounds if sessions is empty. - Fixed randomly caused NPE when transforming sessions to length values (also improved it) Demographics tab not yet finished. --- .../com/djrapitops/plan/data/UserData.java | 3 ++ .../data/handlers/GamemodeTimesHandler.java | 21 +++++++++----- .../djrapitops/plan/utilities/Analysis.java | 5 +++- .../plan/utilities/AnalysisUtils.java | 29 ++++++++++--------- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/data/UserData.java b/Plan/src/main/java/com/djrapitops/plan/data/UserData.java index 762b8b125..acf1aa380 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/UserData.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/UserData.java @@ -225,6 +225,9 @@ public class UserData { * @param time */ public void setGMTime(GameMode gm, long time) { + if (gmTimes == null) { + gmTimes = new HashMap<>(); + } gmTimes.put(gm, time); } diff --git a/Plan/src/main/java/com/djrapitops/plan/data/handlers/GamemodeTimesHandler.java b/Plan/src/main/java/com/djrapitops/plan/data/handlers/GamemodeTimesHandler.java index 30e98db36..534168979 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/handlers/GamemodeTimesHandler.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/handlers/GamemodeTimesHandler.java @@ -33,7 +33,7 @@ public class GamemodeTimesHandler { * @param data UserData matching the Player */ public void handleLogin(GameMode gm, UserData data) { - data.setLastGamemode(gm); + handleChangeEvent(gm, data); } /** @@ -45,15 +45,19 @@ public class GamemodeTimesHandler { * @param data UserData matching the Player */ public void handleChangeEvent(GameMode newGM, UserData data) { + if (newGM == null) { + return; + } + if (data.getLastGamemode() == null) { + data.setLastGamemode(GameMode.SURVIVAL); + } + GameMode oldGM = data.getLastGamemode(); HashMap times = data.getGmTimes(); + Long currentGMTime = times.get(oldGM); handler.getActivityHandler().saveToCache(data); - long lastSwap = data.getLastGmSwapTime(); long playTime = data.getPlayTime(); - GameMode oldGM = data.getLastGamemode(); - if (oldGM != null) { - data.setGMTime(oldGM, times.get(oldGM) + (playTime - lastSwap)); - } + data.setGMTime(oldGM, currentGMTime + (playTime - lastSwap)); data.setLastGamemode(newGM); data.setLastGmSwapTime(playTime); } @@ -65,13 +69,14 @@ public class GamemodeTimesHandler { * @param data UserData matching the Player */ public void saveToCache(GameMode currentGM, UserData data) { + if (currentGM == null) { + return; + } HashMap times = data.getGmTimes(); handler.getActivityHandler().saveToCache(data); - long lastSwap = data.getLastGmSwapTime(); long playtime = data.getPlayTime(); data.setGMTime(currentGM, times.get(currentGM) + (playtime - lastSwap)); - data.setLastGmSwapTime(playtime); } diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/Analysis.java b/Plan/src/main/java/com/djrapitops/plan/utilities/Analysis.java index b35bddc1f..acb041edf 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/Analysis.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/Analysis.java @@ -176,7 +176,10 @@ public class Analysis { sorted.addTotalKills(uData.getPlayerKills().size()); sorted.addTotalMobKills(uData.getMobKills()); sorted.addTotalDeaths(uData.getDeaths()); - sorted.getSessiondata().addAll(uData.getSessions()); + List sessions = uData.getSessions(); + if (!sessions.isEmpty()) { + sorted.getSessiondata().addAll(sessions); + } sorted.getRegistered().add(uData.getRegistered()); sorted.addGeoloc(uData.getDemData().getGeoLocation()); uData.stopAccessing(); diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/AnalysisUtils.java b/Plan/src/main/java/com/djrapitops/plan/utilities/AnalysisUtils.java index ac65ce9ca..6bdc87bb0 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/AnalysisUtils.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/AnalysisUtils.java @@ -5,6 +5,7 @@ import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.List; +import java.util.stream.Collectors; import main.java.com.djrapitops.plan.Settings; import main.java.com.djrapitops.plan.data.SessionData; import main.java.com.djrapitops.plan.data.UserData; @@ -43,33 +44,33 @@ public class AnalysisUtils { static String createTableOutOfHashMap(HashMap commandUse) { return SortableCommandUseTableCreator.createSortedCommandUseTable(commandUse); } - + static String createSortablePlayersTable(Collection data) { return SortablePlayersTableCreator.createSortablePlayersTable(data); } - public static int getNewPlayers(List registered, long scale, long now) { + public static int getNewPlayers(List registered, long scale, long now) { int newPlayers = 0; if (!registered.isEmpty()) { - newPlayers = registered.stream() - .filter((reg) -> (reg != null)) - .filter((reg) -> (reg > now - scale)) - .map((_item) -> 1).reduce(newPlayers, Integer::sum); + newPlayers = registered.stream() + .filter((reg) -> (reg != null)) + .filter((reg) -> (reg > now - scale)) + .map((_item) -> 1).reduce(newPlayers, Integer::sum); } // Filters out register dates before scale - + return newPlayers; } - + public static List transformSessionDataToLengths(Collection data) { - List d = new ArrayList<>(data); - List list = new ArrayList<>(); - d.stream().forEach((sData) -> { - list.add(sData.getSessionEnd()-sData.getSessionStart()); - }); + List list = data.stream() + .filter(session -> session != null) + .filter(session -> session.getSessionEnd() > session.getSessionStart()) + .map(session -> session.getSessionEnd() - session.getSessionStart()) + .collect(Collectors.toList()); return list; } - + public static long average(Collection list) { if (list.isEmpty()) { return 0;