From 913766214c0446fb31682292ed171ccd5930d349 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Tue, 31 Oct 2017 19:37:19 +0200 Subject: [PATCH] Implemented World Pie Alias Setting system for #390 & #340 --- .../java/com/djrapitops/plan/Settings.java | 2 + .../djrapitops/plan/WorldAliasSettings.java | 77 ++++++++++++++++++ .../djrapitops/plan/data/time/TimeKeeper.java | 7 +- .../listeners/PlanGamemodeChangeListener.java | 3 + .../listeners/PlanWorldChangeListener.java | 3 + .../html/graphs/WorldPieCreator.java | 80 +++++++++++++++---- Plan/src/main/resources/config.yml | 2 + 7 files changed, 158 insertions(+), 16 deletions(-) create mode 100644 Plan/src/main/java/com/djrapitops/plan/WorldAliasSettings.java diff --git a/Plan/src/main/java/com/djrapitops/plan/Settings.java b/Plan/src/main/java/com/djrapitops/plan/Settings.java index 35ea08341..2246b575a 100644 --- a/Plan/src/main/java/com/djrapitops/plan/Settings.java +++ b/Plan/src/main/java/com/djrapitops/plan/Settings.java @@ -99,6 +99,8 @@ public enum Settings { // StringList HIDE_FACTIONS("Plugins.Factions.HideFactions"), HIDE_TOWNS("Plugins.Towny.HideTowns"), + // Config section + WORLD_ALIASES("Customization.WorldAliases"), // // Bungee BUNGEE_IP("Server.IP"), diff --git a/Plan/src/main/java/com/djrapitops/plan/WorldAliasSettings.java b/Plan/src/main/java/com/djrapitops/plan/WorldAliasSettings.java new file mode 100644 index 000000000..4bfb9ad9d --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/WorldAliasSettings.java @@ -0,0 +1,77 @@ +/* + * Licence is provided in the jar as license.yml also here: + * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml + */ +package main.java.com.djrapitops.plan; + +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; + +import java.util.HashMap; +import java.util.Map; + +/** + * Class responsible for managing Bukkit side config settings for World Aliases. + * + * @author Rsl1122 + */ +public class WorldAliasSettings { + + private final Plan plugin; + + /** + * Constructor. + * + * @param plugin Current instance of Plan. + */ + public WorldAliasSettings(Plan plugin) { + this.plugin = plugin; + } + + /** + * Used to get all World aliases in the config + * + * @return Map: Original name, Alias + */ + public Map getAliases() { + ConfigurationSection aliasSect = getAliasSection(); + + Map aliasMap = new HashMap<>(); + for (String world : aliasSect.getKeys(false)) { + aliasMap.put(world, aliasSect.getString(world)); + } + return aliasMap; + } + + private ConfigurationSection getAliasSection() { + FileConfiguration config = plugin.getConfig(); + return config.getConfigurationSection(Settings.WORLD_ALIASES.getPath()); + } + + /** + * Adds a new World to the config section. + *

+ * If exists does not override old value. + * + * @param world World name + */ + public void addWorld(String world) { + ConfigurationSection aliasSect = getAliasSection(); + + Object previousValue = aliasSect.get(world); + if (previousValue == null) { + aliasSect.set(world, world); + } + plugin.saveConfig(); + } + + /** + * Used to get alias of a single world. + * + * @param world World name. + * @return Alias. + */ + public String getAlias(String world) { + return getAliasSection().getString(world); + } +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/data/time/TimeKeeper.java b/Plan/src/main/java/com/djrapitops/plan/data/time/TimeKeeper.java index a5c29b2d2..2be496f5d 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/time/TimeKeeper.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/time/TimeKeeper.java @@ -103,8 +103,11 @@ public abstract class TimeKeeper { } public long getTime(String state) { - Long time = times.get(state); - return time != null ? time : 0L; + return times.getOrDefault(state, 0L); + } + + public void addTime(String state, long time) { + times.put(state, times.getOrDefault(state, 0L) + time); } public long getTotal() { diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/listeners/PlanGamemodeChangeListener.java b/Plan/src/main/java/com/djrapitops/plan/systems/listeners/PlanGamemodeChangeListener.java index c9eeca62c..0e39ab8cf 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/listeners/PlanGamemodeChangeListener.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/listeners/PlanGamemodeChangeListener.java @@ -1,6 +1,7 @@ package main.java.com.djrapitops.plan.systems.listeners; import main.java.com.djrapitops.plan.Plan; +import main.java.com.djrapitops.plan.WorldAliasSettings; import main.java.com.djrapitops.plan.data.Session; import main.java.com.djrapitops.plan.utilities.MiscUtils; import org.bukkit.entity.Player; @@ -46,6 +47,8 @@ public class PlanGamemodeChangeListener implements Listener { String gameMode = event.getNewGameMode().name(); String worldName = p.getWorld().getName(); + new WorldAliasSettings(plugin).addWorld(worldName); + Optional cachedSession = plugin.getDataCache().getCachedSession(uuid); cachedSession.ifPresent(session -> session.changeState(worldName, gameMode, time)); } diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/listeners/PlanWorldChangeListener.java b/Plan/src/main/java/com/djrapitops/plan/systems/listeners/PlanWorldChangeListener.java index cd41aa38f..87cc948ab 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/listeners/PlanWorldChangeListener.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/listeners/PlanWorldChangeListener.java @@ -1,6 +1,7 @@ package main.java.com.djrapitops.plan.systems.listeners; import main.java.com.djrapitops.plan.Plan; +import main.java.com.djrapitops.plan.WorldAliasSettings; import main.java.com.djrapitops.plan.data.Session; import main.java.com.djrapitops.plan.utilities.MiscUtils; import org.bukkit.entity.Player; @@ -28,6 +29,8 @@ public class PlanWorldChangeListener implements Listener { String gameMode = p.getGameMode().name(); long time = MiscUtils.getTime(); + new WorldAliasSettings(plugin).addWorld(worldName); + Optional cachedSession = plugin.getDataCache().getCachedSession(uuid); cachedSession.ifPresent(session -> session.changeState(worldName, gameMode, time)); } diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/html/graphs/WorldPieCreator.java b/Plan/src/main/java/com/djrapitops/plan/utilities/html/graphs/WorldPieCreator.java index d140978c0..6af500bae 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/html/graphs/WorldPieCreator.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/html/graphs/WorldPieCreator.java @@ -1,12 +1,11 @@ package main.java.com.djrapitops.plan.utilities.html.graphs; +import main.java.com.djrapitops.plan.Plan; +import main.java.com.djrapitops.plan.WorldAliasSettings; import main.java.com.djrapitops.plan.data.time.GMTimes; import main.java.com.djrapitops.plan.data.time.WorldTimes; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; public class WorldPieCreator { @@ -28,14 +27,16 @@ public class WorldPieCreator { Map playtimePerWorld = worldTimes.getWorldTimes().entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getTotal())); - List worlds = new ArrayList<>(playtimePerWorld.keySet()); + Map playtimePerAlias = transformToAliases(playtimePerWorld); + + List worlds = new ArrayList<>(playtimePerAlias.keySet()); Collections.sort(worlds); - int size = playtimePerWorld.size(); - for (String worldName : worlds) { - seriesBuilder.append("{name:'").append(worldName) - .append("',y:").append(playtimePerWorld.getOrDefault(worldName, 0L)) - .append(",drilldown: '").append(worldName).append("'"); + int size = playtimePerAlias.size(); + for (String alias : worlds) { + seriesBuilder.append("{name:'").append(alias) + .append("',y:").append(playtimePerAlias.getOrDefault(alias, 0L)) + .append(",drilldown: '").append(alias).append("'"); seriesBuilder.append("}"); if (i < size - 1) { @@ -52,6 +53,27 @@ public class WorldPieCreator { return new String[]{seriesData, drilldownData}; } + private static Map transformToAliases(Map playtimePerWorld) { + WorldAliasSettings aliasSettings = new WorldAliasSettings(Plan.getInstance()); + Map aliases = aliasSettings.getAliases(); + + Map playtimePerAlias = new HashMap<>(); + for (Map.Entry entry : playtimePerWorld.entrySet()) { + String worldName = entry.getKey(); + long playtime = entry.getValue(); + + if (!aliases.containsKey(worldName)) { + aliases.put(worldName, worldName); + aliasSettings.addWorld(worldName); + } + + String alias = aliases.get(worldName); + + playtimePerAlias.put(alias, playtimePerAlias.getOrDefault(alias, 0L) + playtime); + } + return playtimePerAlias; + } + private static String createDrilldownData(WorldTimes worldTimes) { StringBuilder drilldownBuilder = new StringBuilder(); int i = 0; @@ -60,15 +82,17 @@ public class WorldPieCreator { if (gmTimesMap.isEmpty()) { return "[]"; } + Map gmTimesAliasMap = transformToGMAliases(gmTimesMap); + int size = gmTimesMap.size(); drilldownBuilder.append("["); - for (Map.Entry world : gmTimesMap.entrySet()) { - drilldownBuilder.append("{name:'").append(world.getKey()) - .append("', id:'").append(world.getKey()) + for (Map.Entry worldAlias : gmTimesAliasMap.entrySet()) { + drilldownBuilder.append("{name:'").append(worldAlias.getKey()) + .append("', id:'").append(worldAlias.getKey()) .append("',colors: gmPieColors,"); drilldownBuilder.append("data: ["); - appendGMTimesForWorld(drilldownBuilder, world); + appendGMTimesForWorld(drilldownBuilder, worldAlias); if (i < size - 1) { drilldownBuilder.append(","); @@ -79,6 +103,34 @@ public class WorldPieCreator { return drilldownBuilder.toString(); } + private static Map transformToGMAliases(Map gmTimesMap) { + WorldAliasSettings aliasSettings = new WorldAliasSettings(Plan.getInstance()); + Map aliases = aliasSettings.getAliases(); + + Map gmTimesPerAlias = new HashMap<>(); + + String[] gms = GMTimes.getGMKeyArray(); + + for (Map.Entry entry : gmTimesMap.entrySet()) { + String worldName = entry.getKey(); + GMTimes gmTimes = entry.getValue(); + + if (!aliases.containsKey(worldName)) { + aliases.put(worldName, worldName); + aliasSettings.addWorld(worldName); + } + + String alias = aliases.get(worldName); + + GMTimes aliasGMtimes = gmTimesPerAlias.getOrDefault(alias, new GMTimes()); + for (String gm : gms) { + aliasGMtimes.addTime(gm, gmTimes.getTime(gm)); + } + gmTimesPerAlias.put(alias, aliasGMtimes); + } + return gmTimesPerAlias; + } + private static void appendGMTimesForWorld(StringBuilder drilldownBuilder, Map.Entry world) { Map gmTimes = world.getValue().getTimes(); int smallSize = gmTimes.size(); diff --git a/Plan/src/main/resources/config.yml b/Plan/src/main/resources/config.yml index 67836116f..3eff7fa20 100644 --- a/Plan/src/main/resources/config.yml +++ b/Plan/src/main/resources/config.yml @@ -67,6 +67,8 @@ Customization: Minutes: '%minutes%m ' Seconds: '%seconds%s' Zero: '0s' + WorldAliases: + world: world Theme: Base: Default