From c5d85d9ba56913f7856e3a06a712e93ddd8a13ac Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Mon, 30 Jan 2017 18:35:32 +0200 Subject: [PATCH] Added Recent Players, Top 20 active list, Fixes - Fixed manage command not giving explanation for errors when database connection is not establishable - Fixed hotswap causing plugin reload to fail if database connection is not establsihable. - Added Phrase.GRABBING_DATA_MESSAGE to Analysis, Inspect & search - Moved some methods from MiscUtils to DataCombineUtils --- .../main/java/com/djrapitops/plan/Phrase.java | 55 ++++--- .../main/java/com/djrapitops/plan/Plan.java | 2 +- .../plan/command/commands/AnalyzeCommand.java | 1 + .../plan/command/commands/InspectCommand.java | 6 +- .../plan/command/commands/SearchCommand.java | 3 +- .../commands/manage/ManageCombineCommand.java | 12 +- .../commands/manage/ManageHotswapCommand.java | 29 ++-- .../commands/manage/ManageImportCommand.java | 2 +- .../commands/manage/ManageMoveCommand.java | 42 ++--- .../commands/manage/ManageRemoveCommand.java | 1 + .../djrapitops/plan/data/AnalysisData.java | 20 +++ .../plan/data/cache/InspectCacheHandler.java | 39 +++-- .../djrapitops/plan/utilities/Analysis.java | 39 +++-- .../plan/utilities/AnalysisUtils.java | 52 ++++++- .../plan/utilities/DataCombineUtils.java | 144 ++++++++++++++++++ .../djrapitops/plan/utilities/MiscUtils.java | 110 ------------- .../utilities/comparators/MapComparator.java | 9 ++ Plan/src/main/resources/analysis.html | 6 +- Plan/src/main/resources/plugin.yml | 11 +- 19 files changed, 379 insertions(+), 204 deletions(-) create mode 100644 Plan/src/main/java/com/djrapitops/plan/utilities/DataCombineUtils.java diff --git a/Plan/src/main/java/com/djrapitops/plan/Phrase.java b/Plan/src/main/java/com/djrapitops/plan/Phrase.java index 007a28248..a17883096 100644 --- a/Plan/src/main/java/com/djrapitops/plan/Phrase.java +++ b/Plan/src/main/java/com/djrapitops/plan/Phrase.java @@ -8,13 +8,14 @@ import static org.bukkit.plugin.java.JavaPlugin.getPlugin; * @author Rsl1122 */ public enum Phrase { + PREFIX("[Plan] "), CONFIG_HEADER("Plan Config | More info at https://www.spigotmc.org/wiki/plan-configuration/"), DATABASE_TYPE_DOES_NOT_EXIST("That database type doesn't exist."), DATABASE_FAILURE_DISABLE("Database initialization has failed, disabling Plan."), - PLANLITE_REG_HOOK("Registered additional hook, passed on to PlanLite: "), - USERNAME_NOT_VALID(ChatColor.RED + "[Plan] This Player doesn't exist."), - USERNAME_NOT_SEEN(ChatColor.RED + "[Plan] This Player has not played on this server."), - USERNAME_NOT_KNOWN(ChatColor.RED + "[Plan] Player not found from the database."), + USERNAME_NOT_VALID(ChatColor.RED + "" + PREFIX + "This Player doesn't exist."), + USERNAME_NOT_SEEN(ChatColor.RED + "" + PREFIX + "This Player has not played on this server."), + USERNAME_NOT_KNOWN(ChatColor.RED + "" + PREFIX + "Player not found from the database."), + COLOR_MAIN(ChatColor.getByChar(getPlugin(Plan.class).getConfig().getString("Customization.Colors.Commands.Main").charAt(1))), COLOR_SEC(ChatColor.getByChar(getPlugin(Plan.class).getConfig().getString("Customization.Colors.Commands.Secondary").charAt(1))), COLOR_TER(ChatColor.getByChar(getPlugin(Plan.class).getConfig().getString("Customization.Colors.Commands.Highlight").charAt(1))), @@ -30,24 +31,38 @@ public enum Phrase { HCOLOR_ACTP_JON(getPlugin(Plan.class).getConfig().getString("Customization.Colors.HTML.ActivityPie.JoinedOnce")), ARROWS_RIGHT("»"), BALL("•"), + + GRABBING_DATA_MESSAGE(COLOR_TER+""+ARROWS_RIGHT+COLOR_MAIN+" Fetching data to cache.."), + + ANALYSIS("Analysis | "), + ANALYSIS_START(ANALYSIS + "Beginning analysis of user data.."), + ANALYSIS_FETCH_PLAYERS(ANALYSIS + "Checking for available players.."), + ANALYSIS_FETCH_DATA(ANALYSIS + "Fetching Data.."), + ANALYSIS_FAIL_NO_PLAYERS(ANALYSIS + "Analysis failed, no known players."), + ANALYSIS_FAIL_NO_DATA(ANALYSIS + "Analysis failed, no data in the database."), + ANALYSIS_BEGIN_ANALYSIS(ANALYSIS + "Data Fetched, beginning Analysis of data.."), + ANALYSIS_COMPLETE(ANALYSIS + "Analysis Complete."), + ERROR_PLANLITE("PlanLite not found, if you're have plugins using PlanAPI v1.6.0 download PlanLite."), ERROR_NO_DATA_VIEW(ChatColor.YELLOW + "Webserver disabled but Alternative IP/PlanLite not used, no way to view data!"), - ERROR_WEBSERVER_OFF_ANALYSIS(ChatColor.YELLOW + "[Plan] This command can be only used if the webserver is running on this server."), - ERROR_WEBSERVER_OFF_INSPECT(ChatColor.YELLOW + "[Plan] This command can be only used if webserver/planlite is enabled on this server."), - MANAGE_ERROR_INCORRECT_PLUGIN(ChatColor.RED+"[Plan] Plugin not supported: "), - MANAGE_ERROR_PLUGIN_NOT_ENABLED(ChatColor.RED+"[Plan] Plugin is not enabled: "), - MANAGE_ERROR_INCORRECT_DB(ChatColor.RED+"[Plan] Incorrect database! (sqlite/mysql accepted): "), - MANAGE_ERROR_SAME_DB(ChatColor.RED+"[Plan] Can't move to the same database!"), - MANAGE_DATABASE_FAILURE(ChatColor.RED+"[Plan] One of the databases was not initialized properly."), - MANAGE_DB_CONFIG_REMINDER(ChatColor.YELLOW+"[Plan] Remember to swap to the new database and reload plugin"), - MANAGE_ERROR_NO_PLAYERS(ChatColor.RED+"[Plan] Database has no player data!"), - MANAGE_MOVE_SUCCESS(ChatColor.GREEN+"[Plan] All data moved successfully!"), - MANAGE_CLEAR_SUCCESS(ChatColor.GREEN+"[Plan] All data cleared successfully!"), - COMMAND_SENDER_NOT_PLAYER(ChatColor.RED + "[Plan] This command can be only used as a player."), - COMMAND_REQUIRES_ARGUMENTS(ChatColor.RED + "[Plan] Command requires arguments."), - COMMAND_ADD_CONFIRMATION_ARGUMENT(ChatColor.RED + "[Plan] Add -a to confirm execution!"), - COMMAND_REQUIRES_ARGUMENTS_ONE(ChatColor.RED + "[Plan] Command requires one argument."), - COMMAND_NO_PERMISSION(ChatColor.RED + "[Plan] You do not have the required permmission."); + ERROR_WEBSERVER_OFF_ANALYSIS(ChatColor.YELLOW + "" + PREFIX + "This command can be only used if the webserver is running on this server."), + ERROR_WEBSERVER_OFF_INSPECT(ChatColor.YELLOW + "" + PREFIX + "This command can be only used if webserver/planlite is enabled on this server."), + + MANAGE_ERROR_INCORRECT_PLUGIN(ChatColor.RED + "" + PREFIX + "Plugin not supported: "), + MANAGE_ERROR_PLUGIN_NOT_ENABLED(ChatColor.RED + "" + PREFIX + "Plugin is not enabled: "), + MANAGE_ERROR_INCORRECT_DB(ChatColor.RED + "" + PREFIX + "Incorrect database! (sqlite/mysql accepted): "), + MANAGE_ERROR_SAME_DB(ChatColor.RED + "" + PREFIX + "Can't move to the same database!"), + MANAGE_DATABASE_FAILURE(ChatColor.RED + "" + PREFIX + "One of the databases was not initialized properly."), + MANAGE_DB_CONFIG_REMINDER(ChatColor.YELLOW + "" + PREFIX + "Remember to swap to the new database and reload plugin"), + MANAGE_ERROR_NO_PLAYERS(ChatColor.RED + "" + PREFIX + "Database has no player data!"), + MANAGE_MOVE_SUCCESS(ChatColor.GREEN + "" + PREFIX + "All data moved successfully!"), + MANAGE_CLEAR_SUCCESS(ChatColor.GREEN + "" + PREFIX + "All data cleared successfully!"), + + COMMAND_SENDER_NOT_PLAYER(ChatColor.RED + "" + PREFIX + "This command can be only used as a player."), + COMMAND_REQUIRES_ARGUMENTS(ChatColor.RED + "" + PREFIX + "Command requires arguments."), + COMMAND_ADD_CONFIRMATION_ARGUMENT(ChatColor.RED + "" + PREFIX + "Add -a to confirm execution!"), + COMMAND_REQUIRES_ARGUMENTS_ONE(ChatColor.RED + "" + PREFIX + "Command requires one argument."), + COMMAND_NO_PERMISSION(ChatColor.RED + "" + PREFIX + "You do not have the required permmission."); private final String text; private final ChatColor color; diff --git a/Plan/src/main/java/com/djrapitops/plan/Plan.java b/Plan/src/main/java/com/djrapitops/plan/Plan.java index 28e8bc181..335c91e86 100644 --- a/Plan/src/main/java/com/djrapitops/plan/Plan.java +++ b/Plan/src/main/java/com/djrapitops/plan/Plan.java @@ -70,7 +70,7 @@ public class Plan extends JavaPlugin { log(MiscUtils.checkVersion()); log("Database init.."); if (initDatabase()) { - log(db.getConfigName()+" Database initiated."); + log(db.getConfigName()+"-database connection established."); } else { logError(Phrase.DATABASE_FAILURE_DISABLE.toString()); getServer().getPluginManager().disablePlugin(this); diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/AnalyzeCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/AnalyzeCommand.java index a2871c456..d78fda18c 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/AnalyzeCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/AnalyzeCommand.java @@ -59,6 +59,7 @@ public class AnalyzeCommand extends SubCommand { return true; } } + sender.sendMessage(Phrase.GRABBING_DATA_MESSAGE+""); if (!analysisCache.isCached()) { analysisCache.updateCache(); } else if (new Date().getTime() - analysisCache.getData().getRefreshDate() > 60000) { diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/InspectCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/InspectCommand.java index 884ad0aef..c2d2a62af 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/InspectCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/InspectCommand.java @@ -20,7 +20,6 @@ import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; import static org.bukkit.Bukkit.getOfflinePlayer; -import static org.bukkit.Bukkit.getOfflinePlayer; /** * @@ -93,11 +92,12 @@ public class InspectCommand extends SubCommand { return true; } - inspectCache.cache(uuid); + ChatColor oColor = Phrase.COLOR_MAIN.color(); ChatColor tColor = Phrase.COLOR_SEC.color(); ChatColor hColor = Phrase.COLOR_TER.color(); - + sender.sendMessage(Phrase.GRABBING_DATA_MESSAGE+""); + inspectCache.cache(uuid); final boolean useAlternativeIP = config.getBoolean("Settings.WebServer.ShowAlternativeServerIP"); final int port = config.getInt("Settings.WebServer.Port"); final String alternativeIP = config.getString("Settings.WebServer.AlternativeIP").replaceAll("%port%", "" + port); diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/SearchCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/SearchCommand.java index 3ce9e2dc6..1de772d8c 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/SearchCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/SearchCommand.java @@ -64,7 +64,8 @@ public class SearchCommand extends SubCommand { ChatColor oColor = Phrase.COLOR_MAIN.color(); ChatColor tColor = Phrase.COLOR_SEC.color(); ChatColor hColor = Phrase.COLOR_TER.color(); - + + sender.sendMessage(Phrase.GRABBING_DATA_MESSAGE+""); Set matches = MiscUtils.getMatchingDisplaynames(args[0]); Set uuids = new HashSet<>(); for (OfflinePlayer match : matches) { diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageCombineCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageCombineCommand.java index 91e474417..820cdcce5 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageCombineCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageCombineCommand.java @@ -16,6 +16,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.UUID; +import main.java.com.djrapitops.plan.utilities.DataCombineUtils; import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; @@ -110,7 +111,7 @@ public class ManageCombineCommand extends SubCommand { } final List fromUUIDS = new ArrayList<>(); final List toUUIDS = new ArrayList<>(); - for (OfflinePlayer p : offlinePlayers) { + try {for (OfflinePlayer p : offlinePlayers) { UUID uuid = p.getUniqueId(); if (fromDatabase.wasSeenBefore(uuid)) { fromUUIDS.add(uuid); @@ -141,13 +142,13 @@ public class ManageCombineCommand extends SubCommand { uuids.addAll(toUUIDS); uuids.addAll(fromUUIDS); - List combinedUserData = MiscUtils.combineUserDatas(allFromUserData, allToUserData, uuids); + List combinedUserData = DataCombineUtils.combineUserDatas(allFromUserData, allToUserData, uuids); HashMap fromServerData = moveFromDB.getServerDataHashMap(); HashMap toServerData = moveToDB.getServerDataHashMap(); - HashMap combinedServerData = MiscUtils.combineServerDatas(fromServerData, toServerData); + HashMap combinedServerData = DataCombineUtils.combineServerDatas(fromServerData, toServerData); - HashMap commandUse = MiscUtils.combineCommandUses(getCommandUse(fromServerData), getCommandUse(toServerData)); + HashMap commandUse = DataCombineUtils.combineCommandUses(getCommandUse(fromServerData), getCommandUse(toServerData)); moveToDB.removeAllData(); @@ -175,6 +176,9 @@ public class ManageCombineCommand extends SubCommand { return fromCommandUse; } }).runTaskAsynchronously(plugin); + } catch (NullPointerException e) { + sender.sendMessage(Phrase.MANAGE_DATABASE_FAILURE+""); + } return true; } } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageHotswapCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageHotswapCommand.java index 5e5f43e4d..9ff8fbddb 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageHotswapCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageHotswapCommand.java @@ -4,25 +4,9 @@ import com.djrapitops.plan.Phrase; import com.djrapitops.plan.Plan; import com.djrapitops.plan.command.CommandType; import com.djrapitops.plan.command.SubCommand; -import com.djrapitops.plan.data.ServerData; -import com.djrapitops.plan.data.UserData; - import com.djrapitops.plan.database.Database; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.UUID; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.bukkit.ChatColor; -import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; -import org.bukkit.scheduler.BukkitRunnable; /** * @@ -67,6 +51,19 @@ public class ManageHotswapCommand extends SubCommand { sender.sendMessage(Phrase.MANAGE_ERROR_INCORRECT_DB + dbToSwapTo); return true; } + try { + Database db = null; + for (Database database : plugin.getDatabases()) { + if (dbToSwapTo.equalsIgnoreCase(database.getConfigName())) { + db = database; + db.init(); + db.getNewestServerData(); + } + } + } catch (NullPointerException e) { + sender.sendMessage(Phrase.MANAGE_DATABASE_FAILURE + ""); + return true; + } plugin.getConfig().set("database.type", dbToSwapTo); plugin.saveConfig(); plugin.onDisable(); diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageImportCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageImportCommand.java index 50ec99a7e..b30b1a9b2 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageImportCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageImportCommand.java @@ -8,7 +8,6 @@ import com.djrapitops.plan.command.SubCommand; import com.djrapitops.plan.data.UserData; import com.djrapitops.plan.data.cache.DataCacheHandler; import java.util.Arrays; -import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -24,6 +23,7 @@ import org.bukkit.GameMode; import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; +import static org.bukkit.Bukkit.getOfflinePlayer; /** * diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageMoveCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageMoveCommand.java index 95ced9542..9778312cd 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageMoveCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageMoveCommand.java @@ -123,25 +123,29 @@ public class ManageMoveCommand extends SubCommand { (new BukkitRunnable() { @Override public void run() { - moveToDB.removeAllData(); - List allUserData = new ArrayList<>(); - for (UUID uuid : uuids) { - allUserData.add(moveFromDB.getUserData(uuid)); - } - moveToDB.saveMultipleUserData(allUserData); - HashMap serverData = moveFromDB.getServerDataHashMap(); - moveToDB.saveServerDataHashMap(serverData); - ServerData sData = null; - for (long sDataKey : serverData.keySet()) { - sData = serverData.get(sDataKey); - break; - } - if (sData != null) { - moveToDB.saveCommandUse(sData.getCommandUsage()); - } - sender.sendMessage(Phrase.MANAGE_MOVE_SUCCESS+""); - if (!toDB.equals(plugin.getDB().getConfigName())) { - sender.sendMessage(Phrase.MANAGE_DB_CONFIG_REMINDER+""); + try { + moveToDB.removeAllData(); + List allUserData = new ArrayList<>(); + for (UUID uuid : uuids) { + allUserData.add(moveFromDB.getUserData(uuid)); + } + moveToDB.saveMultipleUserData(allUserData); + HashMap serverData = moveFromDB.getServerDataHashMap(); + moveToDB.saveServerDataHashMap(serverData); + ServerData sData = null; + for (long sDataKey : serverData.keySet()) { + sData = serverData.get(sDataKey); + break; + } + if (sData != null) { + moveToDB.saveCommandUse(sData.getCommandUsage()); + } + sender.sendMessage(Phrase.MANAGE_MOVE_SUCCESS + ""); + if (!toDB.equals(plugin.getDB().getConfigName())) { + sender.sendMessage(Phrase.MANAGE_DB_CONFIG_REMINDER + ""); + } + } catch (NullPointerException e) { + sender.sendMessage(Phrase.MANAGE_DATABASE_FAILURE + ""); } this.cancel(); } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRemoveCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRemoveCommand.java index a2926bb16..82a9ba10d 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRemoveCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRemoveCommand.java @@ -16,6 +16,7 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.scheduler.BukkitRunnable; import static org.bukkit.Bukkit.getOfflinePlayer; +import static org.bukkit.Bukkit.getOfflinePlayer; /** * diff --git a/Plan/src/main/java/com/djrapitops/plan/data/AnalysisData.java b/Plan/src/main/java/com/djrapitops/plan/data/AnalysisData.java index 58664e6c6..113a38a60 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/AnalysisData.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/AnalysisData.java @@ -19,6 +19,8 @@ public class AnalysisData { private String playersChartImgHtmlDay; private String activityChartImgHtml; private String top50CommandsListHtml; + private String top20ActivePlayers; + private String recentPlayers; private double gm0Perc; private double gm1Perc; @@ -395,4 +397,22 @@ public class AnalysisData { this.ops = ops; } + public String getTop20ActivePlayers() { + return top20ActivePlayers; + } + + public void setTop20ActivePlayers(String top20ActivePlayers) { + this.top20ActivePlayers = top20ActivePlayers; + } + + public String getRecentPlayers() { + return recentPlayers; + } + + public void setRecentPlayers(String recentPlayers) { + this.recentPlayers = recentPlayers; + } + + + } diff --git a/Plan/src/main/java/com/djrapitops/plan/data/cache/InspectCacheHandler.java b/Plan/src/main/java/com/djrapitops/plan/data/cache/InspectCacheHandler.java index 9d0d989e3..8e061b5aa 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/cache/InspectCacheHandler.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/cache/InspectCacheHandler.java @@ -2,6 +2,7 @@ package com.djrapitops.plan.data.cache; import com.djrapitops.plan.Plan; import com.djrapitops.plan.data.UserData; +import java.util.Date; import java.util.HashMap; import java.util.UUID; import org.bukkit.scheduler.BukkitRunnable; @@ -15,6 +16,7 @@ public class InspectCacheHandler { private DataCacheHandler handler; private Plan plugin; private HashMap cache; + private HashMap clearTimes; /** * Class constructor @@ -25,6 +27,7 @@ public class InspectCacheHandler { this.handler = plugin.getHandler(); this.plugin = plugin; this.cache = new HashMap<>(); + this.clearTimes = new HashMap<>(); } /** @@ -34,20 +37,36 @@ public class InspectCacheHandler { * @param uuid UUID of the player */ public void cache(UUID uuid) { - if (!handler.getDB().wasSeenBefore(uuid)) { - return; - } - cache.put(uuid, handler.getCurrentData(uuid, false)); int minutes = plugin.getConfig().getInt("Settings.Cache.InspectCache.ClearFromInspectCacheAfterXMinutes"); if (minutes <= 0) { minutes = 3; } - (new BukkitRunnable() { - @Override - public void run() { - clearFomCache(uuid); - } - }).runTaskLater(plugin, 60 * 20 * minutes); + cache(uuid, minutes); + } + + public void cache(UUID uuid, int minutes) { + if (!handler.getDB().wasSeenBefore(uuid)) { + return; + } + cache.put(uuid, handler.getCurrentData(uuid, false)); + long clearTime = new Date().toInstant().getEpochSecond() + (long) 60 * (long) minutes; + if (clearTimes.get(uuid) == null) { + clearTimes.put(uuid, (long) 0); + } + if (clearTimes.get(uuid) < clearTime) { + clearTimes.put(uuid, clearTime); + (new BukkitRunnable() { + @Override + public void run() { + if (new Date().toInstant().getEpochSecond() - clearTimes.get(uuid) < 30) { + clearFomCache(uuid); + } else { + this.cancel(); + } + this.cancel(); + } + }).runTaskLater(plugin, 60 * 20 * minutes); + } } private void clearFomCache(UUID uuid) { 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 1f1d9be61..86fd4bb9e 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/Analysis.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/Analysis.java @@ -1,5 +1,6 @@ package com.djrapitops.plan.utilities; +import com.djrapitops.plan.Phrase; import com.djrapitops.plan.Plan; import com.djrapitops.plan.PlanLiteHook; import com.djrapitops.plan.data.AnalysisData; @@ -14,9 +15,9 @@ import java.util.List; import java.util.UUID; import main.java.com.djrapitops.plan.data.PlanLiteAnalyzedData; import main.java.com.djrapitops.plan.data.PlanLitePlayerData; -import main.java.com.djrapitops.plan.ui.graphs.HeatMapCreator; import org.bukkit.GameMode; import org.bukkit.OfflinePlayer; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.scheduler.BukkitRunnable; /** @@ -48,22 +49,23 @@ public class Analysis { * * First retrieves all Offlineplayers and checks those that are in the * database. Then Runs a new Analysis Task Asyncronously. Saves AnalysisData - * to the provided Cache. + * to the provided Cache. Saves all UserData to InspectCache for 8 minutes. * * @param analysisCache Cache that the data is saved to. */ public void analyze(AnalysisCacheHandler analysisCache) { rawData.clear(); added.clear(); - plugin.log("Analysis | Beginning analysis of user data.."); + plugin.log(Phrase.ANALYSIS_START + ""); OfflinePlayer[] offlinePlayers; try { offlinePlayers = plugin.getServer().getOfflinePlayers(); } catch (IndexOutOfBoundsException e) { - plugin.log("Analysis | Analysis failed, no known players."); + plugin.log(Phrase.ANALYSIS_FAIL_NO_PLAYERS + ""); return; } final List uuids = new ArrayList<>(); + plugin.log(Phrase.ANALYSIS_FETCH_PLAYERS + ""); for (OfflinePlayer p : offlinePlayers) { UUID uuid = p.getUniqueId(); if (plugin.getDB().wasSeenBefore(uuid)) { @@ -71,16 +73,20 @@ public class Analysis { } } if (uuids.isEmpty()) { - plugin.log("Analysis | Analysis failed, no data in the database."); + plugin.log(Phrase.ANALYSIS_FAIL_NO_DATA + ""); return; } + FileConfiguration config = plugin.getConfig(); + final boolean useAlternativeIP = config.getBoolean("Settings.WebServer.ShowAlternativeServerIP"); + final int port = config.getInt("Settings.WebServer.Port"); + final String alternativeIP = config.getString("Settings.WebServer.AlternativeIP").replaceAll("%port%", "" + port); (new BukkitRunnable() { @Override public void run() { uuids.stream().forEach((uuid) -> { - inspectCache.cache(uuid); + inspectCache.cache(uuid, 8); }); - plugin.log("Analysis | Fetching Data.."); + plugin.log(Phrase.ANALYSIS_FETCH_DATA + ""); while (rawData.size() != uuids.size()) { uuids.stream() .filter((uuid) -> (!added.contains(uuid))) @@ -93,7 +99,7 @@ public class Analysis { }); } rawServerData = plugin.getDB().getServerDataHashMap(); - plugin.log("Analysis | Data Fetched, beginning Analysis of data.."); + plugin.log(Phrase.ANALYSIS_BEGIN_ANALYSIS + ""); AnalysisData data = new AnalysisData(); createPlayerActivityGraphs(data); @@ -129,6 +135,8 @@ public class Analysis { int totalVotes = 0; int totalMoney = 0; + HashMap latestLogins = new HashMap<>(); + HashMap playtimes = new HashMap<>(); // Fill Dataset with userdata. for (UserData uData : rawData) { if (planLiteEnabled) { @@ -157,7 +165,13 @@ public class Analysis { } } catch (NoSuchFieldError e) { } - totalPlaytime += uData.getPlayTime(); + long playTime = uData.getPlayTime(); + totalPlaytime += playTime; + String playerName = uData.getName(); + String url = ""+playerName+""; + playtimes.put(url, playTime); + latestLogins.put(url, uData.getLastPlayed()); totalLoginTimes += uData.getLoginTimes(); int age = uData.getDemData().getAge(); if (age != -1) { @@ -171,13 +185,16 @@ public class Analysis { totalBanned++; } else if (uData.getLoginTimes() == 1) { joinleaver++; - } else if (AnalysisUtils.isActive(uData.getLastPlayed(), uData.getPlayTime(), uData.getLoginTimes())) { + } else if (AnalysisUtils.isActive(uData.getLastPlayed(), playTime, uData.getLoginTimes())) { active++; } else { inactive++; } } + // Save Dataset to AnalysisData + data.setTop20ActivePlayers(AnalysisUtils.createActivePlayersTable(playtimes, 20)); + data.setRecentPlayers(AnalysisUtils.createListStringOutOfHashMapLong(latestLogins, 20)); if (planLiteEnabled) { plData.setFactionMap(factionMap); plData.setTownMap(townMap); @@ -246,7 +263,7 @@ public class Analysis { data.setRefreshDate(new Date().getTime()); analysisCache.cache(data); - plugin.log("Analysis | Analysis Complete."); + plugin.log(Phrase.ANALYSIS_COMPLETE + ""); this.cancel(); } 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 33927565a..c188b886b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/AnalysisUtils.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/AnalysisUtils.java @@ -80,7 +80,7 @@ public class AnalysisUtils { gm3 = (long) 0; } gmThree = gm3; - } catch (NoSuchFieldError e) { + } catch (NoSuchFieldError e) { gmThree = 0; } long total = gmZero + gmOne + gmTwo + gmThree; @@ -143,6 +143,8 @@ public class AnalysisUtils { replaceMap.put("%ops%", "" + data.getOps()); replaceMap.put("%refresh%", FormatUtils.formatTimeAmountSinceString("" + data.getRefreshDate(), new Date())); replaceMap.put("%totallogins%", "" + data.getTotalLoginTimes()); + replaceMap.put("%top20mostactive%", data.getTop20ActivePlayers()); + replaceMap.put("%recentlogins%", data.getRecentPlayers()); PlanLiteHook hook = getPlugin(Plan.class).getPlanLiteHook(); if (hook != null) { replaceMap.put("%planlite%", hook.isEnabled() ? getPlanLiteAnalysisHtml(data.getPlanLiteData()) : ""); @@ -178,17 +180,21 @@ public class AnalysisUtils { return createTableOutOfHashMap(commandUse, 50); } + static String createTableOutOfHashMapLong(HashMap players) { + return createActivePlayersTable(players, 20); + } + static String createTableOutOfHashMap(HashMap map, int limit) { List sorted = MapComparator.sortByValue(map); String html = ""; if (sorted.isEmpty()) { - html = "

Error Calcuclating Command usages

"; + html = "

Error Calcuclating Table usages (sorted data was empty)

"; return html; } Collections.reverse(sorted); int i = 1; for (String[] values : sorted) { - if (i >= 50) { + if (i >= limit) { break; } html += "\r\n"; @@ -198,6 +204,46 @@ public class AnalysisUtils { return html; } + static String createActivePlayersTable(HashMap map, int limit) { + List sorted = MapComparator.sortByValueLong(map); + String html = "
" + values[1] + "" + values[0] + "
"; + if (sorted.isEmpty()) { + html = "

Error Calculating Active players (sorted list was empty)

"; + return html; + } + Collections.reverse(sorted); + int i = 1; + for (String[] values : sorted) { + if (i >= limit) { + break; + } + html += "\r\n"; + i++; + } + html += "
" + values[1] + "" + FormatUtils.formatTimeAmount(values[0]) + "
"; + return html; + } + + static String createListStringOutOfHashMapLong(HashMap map, int limit) { + List sorted = MapComparator.sortByValueLong(map); + String html = "

"; + if (sorted.isEmpty()) { + html = "Error Creating List

"; + return html; + } + Collections.reverse(sorted); + int i = 1; + for (String[] values : sorted) { + if (i >= limit) { + break; + } + html += values[1]+" "; + i++; + } + html += "

"; + return html; + } + private static String getPlanLiteAnalysisHtml(PlanLiteAnalyzedData planLiteData) { Scanner scanner = new Scanner(getPlugin(Plan.class).getResource("planlite.html")); String html = ""; diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/DataCombineUtils.java b/Plan/src/main/java/com/djrapitops/plan/utilities/DataCombineUtils.java new file mode 100644 index 000000000..72095d3d7 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/DataCombineUtils.java @@ -0,0 +1,144 @@ +package main.java.com.djrapitops.plan.utilities; + +import com.djrapitops.plan.data.DemographicsData; +import com.djrapitops.plan.data.ServerData; +import com.djrapitops.plan.data.UserData; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.UUID; +import org.bukkit.GameMode; + +/** + * + * @author Rsl1122 + */ +public class DataCombineUtils { + + /** + * Combines two conflicting UserDatasets from databases. + * + * @param allFromUserData First Dataset + * @param allToUserData Second Dataset + * @param uuids UUIDs from both Datasets + * @return Combined UserDataset + */ + public static List combineUserDatas(HashMap allFromUserData, HashMap allToUserData, Set uuids) { + List combinedData = new ArrayList<>(); + uuids.forEach((UUID uuid) -> { + UserData fData = allFromUserData.get(uuid); + UserData tData = allToUserData.get(uuid); + if (fData == null) { + combinedData.add(tData); + } else if (tData == null) { + combinedData.add(fData); + } else { + combinedData.add(combineUserData(fData, tData)); + } + }); + return combinedData; + } + + private static UserData combineUserData(UserData fData, UserData tData) { + if (fData.getLastGmSwapTime() < tData.getLastGmSwapTime()) { + fData.setLastGmSwapTime(tData.getLastGmSwapTime()); + fData.setLastGamemode(tData.getLastGamemode()); + } + HashMap gmTimes = fData.getGmTimes(); + HashMap tGmTimes = tData.getGmTimes(); + gmTimes.keySet().stream().forEach((GameMode gm) -> { + long fTime = gmTimes.get(gm); + if (tGmTimes.get(gm) != null) { + long tTime = tGmTimes.get(gm); + gmTimes.put(gm, fTime + tTime); + } + }); + if (fData.getLastPlayed() < tData.getLastPlayed()) { + fData.setLastPlayed(tData.getLastPlayed()); + } + fData.setPlayTime(fData.getPlayTime() + tData.getPlayTime()); + fData.setTimesKicked(fData.getTimesKicked() + tData.getTimesKicked()); + fData.setLoginTimes(fData.getLoginTimes() + tData.getLoginTimes()); + fData.addLocations(tData.getLocations()); + fData.addNicknames(tData.getNicknames()); + fData.addIpAddresses(tData.getIps()); + DemographicsData tDemData = tData.getDemData(); + DemographicsData fDemData = fData.getDemData(); + if (tDemData.getAge() > fDemData.getAge()) { + fDemData.setAge(tDemData.getAge()); + } + if (fDemData.getGeoLocation().equals("Not Known")) { + fDemData.setGeoLocation(tDemData.getGeoLocation()); + } + fData.setDemData(fDemData); + return fData; + } + + /** + * Combines two conflicting ServerDatasets + * + * @param fData First Dataset + * @param tData Second Dataset + * @return Combined ServerDataset + */ + public static HashMap combineServerDatas(HashMap fData, HashMap tData) { + HashMap combinedData = new HashMap<>(); + Set allDates = new HashSet<>(); + allDates.addAll(fData.keySet()); + allDates.addAll(tData.keySet()); + allDates.parallelStream().forEach((Long date) -> { + ServerData fServerData = fData.get(date); + ServerData tServerData = tData.get(date); + if (fServerData == null) { + combinedData.put(date, tServerData); + } else if (tServerData == null) { + combinedData.put(date, fServerData); + } else if (fServerData.getPlayersOnline() > tServerData.getPlayersOnline()) { + combinedData.put(date, fServerData); + } else { + combinedData.put(date, tServerData); + } + }); + return combinedData; + } + + /** + * Combines Two conflicting command usage datasets. + * + * @param fData First Dataset + * @param tData Second Dataset + * @return Combined Dataset + */ + public static HashMap combineCommandUses(HashMap fData, HashMap tData) { + HashMap combinedData = new HashMap<>(); + Set allCommands = new HashSet<>(); + if (fData != null) { + allCommands.addAll(fData.keySet()); + } + if (tData != null) { + allCommands.addAll(tData.keySet()); + } + for (String command : allCommands) { + boolean fDataHasCommand = false; + if (fData != null) { + fDataHasCommand = fData.keySet().contains(command); + } + boolean tDataHasCommand = false; + if (tData != null) { + tDataHasCommand = tData.keySet().contains(command); + } + int value = 0; + if (fDataHasCommand) { + value += fData.get(command); + } + if (tDataHasCommand) { + value += tData.get(command); + } + combinedData.put(command, value); + } + return combinedData; + } + +} diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java b/Plan/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java index 685ee3c2b..16416f11b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java @@ -2,20 +2,14 @@ package com.djrapitops.plan.utilities; import com.djrapitops.plan.Phrase; import com.djrapitops.plan.Plan; -import com.djrapitops.plan.data.DemographicsData; -import com.djrapitops.plan.data.ServerData; -import com.djrapitops.plan.data.UserData; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; -import java.util.UUID; import org.bukkit.Bukkit; -import org.bukkit.GameMode; import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -110,108 +104,4 @@ public class MiscUtils { }); return matches; } - - public static List combineUserDatas(HashMap allFromUserData, HashMap allToUserData, Set uuids) { - List combinedData = new ArrayList<>(); - uuids.forEach((uuid) -> { - UserData fData = allFromUserData.get(uuid); - UserData tData = allToUserData.get(uuid); - if (fData == null) { - combinedData.add(tData); - } else if (tData == null) { - combinedData.add(fData); - } else { - combinedData.add(combineUserData(fData, tData)); - } - }); - return combinedData; - } - - private static UserData combineUserData(UserData fData, UserData tData) { - if (fData.getLastGmSwapTime() < tData.getLastGmSwapTime()) { - fData.setLastGmSwapTime(tData.getLastGmSwapTime()); - fData.setLastGamemode(tData.getLastGamemode()); - } - HashMap gmTimes = fData.getGmTimes(); - HashMap tGmTimes = tData.getGmTimes(); - gmTimes.keySet().stream().forEach((gm) -> { - long fTime = gmTimes.get(gm); - if (tGmTimes.get(gm) != null) { - long tTime = tGmTimes.get(gm); - gmTimes.put(gm, fTime + tTime); - } - }); - if (fData.getLastPlayed() < tData.getLastPlayed()) { - fData.setLastPlayed(tData.getLastPlayed()); - } - fData.setPlayTime(fData.getPlayTime() + tData.getPlayTime()); - fData.setTimesKicked(fData.getTimesKicked() + tData.getTimesKicked()); - fData.setLoginTimes(fData.getLoginTimes() + tData.getLoginTimes()); - fData.addLocations(tData.getLocations()); - fData.addNicknames(tData.getNicknames()); - fData.addIpAddresses(tData.getIps()); - DemographicsData tDemData = tData.getDemData(); - DemographicsData fDemData = fData.getDemData(); - if (tDemData.getAge() > fDemData.getAge()) { - fDemData.setAge(tDemData.getAge()); - } - if (fDemData.getGeoLocation().equals("Not Known")) { - fDemData.setGeoLocation(tDemData.getGeoLocation()); - } - fData.setDemData(fDemData); - return fData; - } - - public static HashMap combineServerDatas(HashMap fData, HashMap tData) { - HashMap combinedData = new HashMap<>(); - Set allDates = new HashSet<>(); - allDates.addAll(fData.keySet()); - allDates.addAll(tData.keySet()); - allDates.parallelStream().forEach((date) -> { - ServerData fServerData = fData.get(date); - ServerData tServerData = tData.get(date); - if (fServerData == null) { - combinedData.put(date, tServerData); - } else if (tServerData == null) { - combinedData.put(date, fServerData); - } else { - if (fServerData.getPlayersOnline() > tServerData.getPlayersOnline()) { - combinedData.put(date, fServerData); - } else { - combinedData.put(date, tServerData); - } - } - }); - return combinedData; - } - - public static HashMap combineCommandUses(HashMap fData, HashMap tData) { - HashMap combinedData = new HashMap<>(); - Set allCommands = new HashSet<>(); - if (fData != null) { - allCommands.addAll(fData.keySet()); - } - if (tData != null) { - allCommands.addAll(tData.keySet()); - } - for (String command : allCommands) { - boolean fDataHasCommand = false; - if (fData != null) { - fDataHasCommand = fData.keySet().contains(command); - } - boolean tDataHasCommand = false; - if (tData != null) { - tDataHasCommand = tData.keySet().contains(command); - } - int value = 0; - if (fDataHasCommand) { - value += fData.get(command); - } - if (tDataHasCommand) { - value += tData.get(command); - } - combinedData.put(command, value); - } - return combinedData; - } } diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/comparators/MapComparator.java b/Plan/src/main/java/com/djrapitops/plan/utilities/comparators/MapComparator.java index c6dd29361..bb394fe7d 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/comparators/MapComparator.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/comparators/MapComparator.java @@ -27,4 +27,13 @@ public class MapComparator { return sortedList; } + public static List sortByValueLong(HashMap hashMap) { + List sortedList = new ArrayList<>(); + hashMap.keySet().stream().forEach((key) -> { + sortedList.add(new String[]{"" + hashMap.get(key), key}); + }); + Collections.sort(sortedList, (String[] strings, String[] otherStrings) -> (int) (Long.parseLong(strings[0]) - (Long.parseLong(otherStrings[0])))); + return sortedList; + } + } diff --git a/Plan/src/main/resources/analysis.html b/Plan/src/main/resources/analysis.html index ebbc4258f..efefba79d 100644 --- a/Plan/src/main/resources/analysis.html +++ b/Plan/src/main/resources/analysis.html @@ -28,9 +28,9 @@ %playerchartweek%

Player Activity - Last 30 days

%playerchartmonth% - +

Most recent logins: %recentlogins%

+

Top 20 Most Active

+ %top20mostactive%