DataCache now cleared on disable

This commit is contained in:
Rsl1122 2018-08-12 12:19:19 +03:00
parent c6b2f89728
commit 2e92ba2822
3 changed files with 21 additions and 25 deletions

View File

@ -2,7 +2,6 @@ package com.djrapitops.plan.data.time;
import com.djrapitops.plugin.utilities.Verify; import com.djrapitops.plugin.utilities.Verify;
import java.io.Serializable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -12,7 +11,7 @@ import java.util.Objects;
* *
* @author Rsl1122 * @author Rsl1122
*/ */
public abstract class TimeKeeper implements Serializable { public abstract class TimeKeeper {
protected Map<String, Long> times; protected Map<String, Long> times;
protected String state; protected String state;

View File

@ -2,7 +2,6 @@ package com.djrapitops.plan.data.time;
import com.djrapitops.plan.system.settings.WorldAliasSettings; import com.djrapitops.plan.system.settings.WorldAliasSettings;
import java.io.Serializable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -14,9 +13,9 @@ import java.util.stream.Collectors;
* @author Rsl1122 * @author Rsl1122
* @since 4.0.0 * @since 4.0.0
*/ */
public class WorldTimes implements Serializable { public class WorldTimes {
private final Map<String, GMTimes> worldTimes; private final Map<String, GMTimes> times;
private String currentWorld; private String currentWorld;
private String currentGamemode; private String currentGamemode;
@ -28,7 +27,7 @@ public class WorldTimes implements Serializable {
* @param time Epoch ms the time calculation should start * @param time Epoch ms the time calculation should start
*/ */
public WorldTimes(String startingWorld, String startingGM, long time) { public WorldTimes(String startingWorld, String startingGM, long time) {
worldTimes = new HashMap<>(); times = new HashMap<>();
currentWorld = startingWorld; currentWorld = startingWorld;
currentGamemode = startingGM; currentGamemode = startingGM;
addWorld(startingWorld, startingGM, time); addWorld(startingWorld, startingGM, time);
@ -40,11 +39,11 @@ public class WorldTimes implements Serializable {
* @param times Map of each World's GMTimes object. * @param times Map of each World's GMTimes object.
*/ */
public WorldTimes(Map<String, GMTimes> times) { public WorldTimes(Map<String, GMTimes> times) {
worldTimes = times; this.times = times;
} }
private void addWorld(String worldName, String gameMode, long changeTime) { private void addWorld(String worldName, String gameMode, long changeTime) {
worldTimes.put(worldName, new GMTimes(gameMode, changeTime)); times.put(worldName, new GMTimes(gameMode, changeTime));
} }
/** /**
@ -65,18 +64,18 @@ public class WorldTimes implements Serializable {
* @param changeTime Epoch ms the change occurred. * @param changeTime Epoch ms the change occurred.
*/ */
public void updateState(String worldName, String gameMode, long changeTime) { public void updateState(String worldName, String gameMode, long changeTime) {
GMTimes currentGMTimes = worldTimes.get(currentWorld); GMTimes currentGMTimes = times.get(currentWorld);
if (worldName.equals(currentWorld)) { if (worldName.equals(currentWorld)) {
currentGMTimes.changeState(gameMode, changeTime); currentGMTimes.changeState(gameMode, changeTime);
} else { } else {
GMTimes newGMTimes = worldTimes.get(worldName); GMTimes newGMTimes = times.get(worldName);
if (newGMTimes == null) { if (newGMTimes == null) {
addWorld(worldName, gameMode, currentGMTimes.getLastStateChange()); addWorld(worldName, gameMode, currentGMTimes.getLastStateChange());
} }
currentGMTimes.changeState(currentGamemode, changeTime); currentGMTimes.changeState(currentGamemode, changeTime);
} }
for (GMTimes gmTimes : worldTimes.values()) { for (GMTimes gmTimes : times.values()) {
gmTimes.setLastStateChange(changeTime); gmTimes.setLastStateChange(changeTime);
} }
@ -91,12 +90,12 @@ public class WorldTimes implements Serializable {
* @return total milliseconds spent in a world. * @return total milliseconds spent in a world.
*/ */
public long getWorldPlaytime(String world) { public long getWorldPlaytime(String world) {
GMTimes gmTimes = worldTimes.get(world); GMTimes gmTimes = times.get(world);
return gmTimes != null ? gmTimes.getTotal() : 0; return gmTimes != null ? gmTimes.getTotal() : 0;
} }
public long getTotal() { public long getTotal() {
return worldTimes.values().stream() return times.values().stream()
.mapToLong(GMTimes::getTotal) .mapToLong(GMTimes::getTotal)
.sum(); .sum();
} }
@ -112,20 +111,15 @@ public class WorldTimes implements Serializable {
* @return GMTimes object with play times of each GameMode. * @return GMTimes object with play times of each GameMode.
*/ */
public GMTimes getGMTimes(String world) { public GMTimes getGMTimes(String world) {
return worldTimes.getOrDefault(world, new GMTimes()); return times.getOrDefault(world, new GMTimes());
} }
/**
* Used to get the Map for saving.
*
* @return Current time map.
*/
public Map<String, GMTimes> getWorldTimes() { public Map<String, GMTimes> getWorldTimes() {
return worldTimes; return times;
} }
public void setGMTimesForWorld(String world, GMTimes gmTimes) { public void setGMTimesForWorld(String world, GMTimes gmTimes) {
worldTimes.put(world, gmTimes); times.put(world, gmTimes);
} }
@Override @Override
@ -133,21 +127,21 @@ public class WorldTimes implements Serializable {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
WorldTimes that = (WorldTimes) o; WorldTimes that = (WorldTimes) o;
return Objects.equals(worldTimes, that.worldTimes) && return Objects.equals(times, that.times) &&
Objects.equals(currentWorld, that.currentWorld) && Objects.equals(currentWorld, that.currentWorld) &&
Objects.equals(currentGamemode, that.currentGamemode); Objects.equals(currentGamemode, that.currentGamemode);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(worldTimes, currentWorld, currentGamemode); return Objects.hash(times, currentWorld, currentGamemode);
} }
@Override @Override
public String toString() { public String toString() {
StringBuilder b = new StringBuilder("WorldTimes (Current: " + currentWorld + "){\n"); StringBuilder b = new StringBuilder("WorldTimes (Current: " + currentWorld + "){\n");
for (Map.Entry<String, GMTimes> entry : worldTimes.entrySet()) { for (Map.Entry<String, GMTimes> entry : times.entrySet()) {
GMTimes value = entry.getValue(); GMTimes value = entry.getValue();
b.append("World '").append(entry.getKey()).append("':\n") b.append("World '").append(entry.getKey()).append("':\n")
.append(" Total: ").append(value.getTotal()).append("\n") .append(" Total: ").append(value.getTotal()).append("\n")
@ -172,7 +166,7 @@ public class WorldTimes implements Serializable {
for (String gm : GMTimes.getGMKeyArray()) { for (String gm : GMTimes.getGMKeyArray()) {
currentGMTimes.addTime(gm, gmTimes.getTime(gm)); currentGMTimes.addTime(gm, gmTimes.getTime(gm));
} }
worldTimes.put(worldName, currentGMTimes); this.times.put(worldName, currentGMTimes);
} }
} }

View File

@ -44,6 +44,9 @@ public class DataCache extends SessionCache implements SubSystem {
@Override @Override
public void disable() { public void disable() {
playerNames.clear();
uuids.clear();
displayNames.clear();
} }
public static DataCache getInstance() { public static DataCache getInstance() {