diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/info/BukkitInformationManager.java b/Plan/src/main/java/com/djrapitops/plan/systems/info/BukkitInformationManager.java index 2e5dbfd7b..6c0f96e79 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/info/BukkitInformationManager.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/info/BukkitInformationManager.java @@ -12,6 +12,7 @@ import main.java.com.djrapitops.plan.api.exceptions.*; import main.java.com.djrapitops.plan.command.commands.AnalyzeCommand; import main.java.com.djrapitops.plan.data.AnalysisData; import main.java.com.djrapitops.plan.data.additional.HookHandler; +import main.java.com.djrapitops.plan.data.additional.InspectContainer; import main.java.com.djrapitops.plan.data.additional.PluginData; import main.java.com.djrapitops.plan.systems.cache.DataCache; import main.java.com.djrapitops.plan.systems.info.parsing.AnalysisPageParser; @@ -30,6 +31,7 @@ import main.java.com.djrapitops.plan.systems.webserver.webapi.universal.PingWebA import main.java.com.djrapitops.plan.utilities.MiscUtils; import main.java.com.djrapitops.plan.utilities.analysis.Analysis; import main.java.com.djrapitops.plan.utilities.html.HtmlStructure; +import main.java.com.djrapitops.plan.utilities.html.structure.InspectPluginsTabContentCreator; import java.io.IOException; import java.sql.SQLException; @@ -52,7 +54,7 @@ public class BukkitInformationManager extends InformationManager { private String analysisPluginsTab; private Long refreshDate; - private final Map pluginsTabContents; + private final Map pluginsTabContents; public BukkitInformationManager(Plan plugin) { this.plugin = plugin; @@ -93,6 +95,10 @@ public class BukkitInformationManager extends InformationManager { @Override public void cachePlayer(UUID uuid) { + if (uuid == null) { + Log.debug("BukkitInformationManager.cachePlayer: UUID was null"); + return; + } if (usingAnotherWebServer) { try { getWebAPI().getAPI(PostHtmlWebAPI.class).sendInspectHtml(webServerAddress, uuid, getPlayerHtml(uuid)); @@ -131,14 +137,26 @@ public class BukkitInformationManager extends InformationManager { String serverName = plugin.getServerInfoManager().getServerName(); HookHandler hookHandler = plugin.getHookHandler(); List plugins = hookHandler.getAdditionalDataSources(); - // TODO Inspect Plugins Tab -// Map replaceMap = hookHandler.getAdditionalInspectReplaceRules(uuid); -// String contents = HtmlStructure.createInspectPluginsTabContent(serverName, plugins, replaceMap); - cacheInspectPluginsTab(uuid, ""); + Map containers = new HashMap<>(); + for (PluginData pluginData : plugins) { + InspectContainer inspectContainer = new InspectContainer(); + try { + InspectContainer container = pluginData.getPlayerData(uuid, inspectContainer); + if (container != null && !container.isEmpty()) { + containers.put(pluginData, container); + } + } catch (Exception e) { + String sourcePlugin = pluginData.getSourcePlugin(); + Log.error("PluginData caused exception: " + sourcePlugin); + Log.toLog(this.getClass().getName() + " " + sourcePlugin, e); + } + } + + cacheInspectPluginsTab(uuid, InspectPluginsTabContentCreator.createContent(containers)); } } - public void cacheInspectPluginsTab(UUID uuid, String contents) { + public void cacheInspectPluginsTab(UUID uuid, String[] contents) { if (usingAnotherWebServer) { try { getWebAPI().getAPI(PostInspectPluginsTabWebAPI.class).sendPluginsTab(webServerAddress, uuid, contents); @@ -158,8 +176,8 @@ public class BukkitInformationManager extends InformationManager { } @Override - public String getPluginsTabContent(UUID uuid) { - String calculating = HtmlStructure.createInspectPageTabContentCalculating(); + public String[] getPluginsTabContent(UUID uuid) { + String[] calculating = HtmlStructure.createInspectPageTabContentCalculating(); return pluginsTabContents.getOrDefault(uuid, calculating); } diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/info/BungeeInformationManager.java b/Plan/src/main/java/com/djrapitops/plan/systems/info/BungeeInformationManager.java index 2d7cc9e03..670941335 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/info/BungeeInformationManager.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/info/BungeeInformationManager.java @@ -43,7 +43,7 @@ public class BungeeInformationManager extends InformationManager { private Map bukkitServers; private final Map networkPageContent; - private final Map> pluginsTabContent; + private final Map> pluginsTabContent; private final BungeeServerInfoManager serverInfoManager; public BungeeInformationManager(PlanBungee plugin) throws SQLException { @@ -246,10 +246,10 @@ public class BungeeInformationManager extends InformationManager { */ @Override public String getPlayerHtml(UUID uuid) { - Response response = PageCache.loadPage("inspectPage:" + uuid, + Response response = PageCache.copyPage("inspectPage:" + uuid, () -> new NotFoundResponse("No Bukkit Servers were online to process this request")); if (response instanceof InspectPageResponse) { - ((InspectPageResponse) response).setInspectPagePluginsTab(pluginsTabContent.get(uuid)); + ((InspectPageResponse) response).setInspectPagePluginsTab(getPluginsTabContent(uuid)); } return response.getContent(); } @@ -275,17 +275,28 @@ public class BungeeInformationManager extends InformationManager { * @return Html string. */ @Override - public String getPluginsTabContent(UUID uuid) { - Map pluginsTab = pluginsTabContent.get(uuid); + public String[] getPluginsTabContent(UUID uuid) { + Map pluginsTab = pluginsTabContent.get(uuid); if (pluginsTab == null) { return HtmlStructure.createInspectPageTabContentCalculating(); } - StringBuilder builder = new StringBuilder(); - for (String tab : pluginsTab.values()) { - builder.append(tab); + List order = new ArrayList<>(pluginsTab.values()); + // Sort serverNames alphabetically + order.sort(new Comparator() { + @Override + public int compare(String[] o1, String[] o2) { + return o1[0].compareTo(o2[1]); + } + }); + + StringBuilder nav = new StringBuilder(); + StringBuilder tabs = new StringBuilder(); + for (String[] tab : order) { + nav.append(tab[0]); + tabs.append(tab[1]); } - return builder.toString(); + return new String[]{nav.toString(), tabs.toString()}; } /** @@ -295,8 +306,8 @@ public class BungeeInformationManager extends InformationManager { * @param uuid UUID of the player * @param html Plugins tab html for the player on the server */ - public void cachePluginsTabContent(UUID serverUUID, UUID uuid, String html) { - Map perServerPluginsTab = pluginsTabContent.getOrDefault(uuid, new HashMap<>()); + public void cachePluginsTabContent(UUID serverUUID, UUID uuid, String[] html) { + Map perServerPluginsTab = pluginsTabContent.getOrDefault(uuid, new HashMap<>()); perServerPluginsTab.put(serverUUID, html); pluginsTabContent.put(uuid, perServerPluginsTab); Response inspectResponse = PageCache.loadPage("inspectPage: " + uuid); diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/info/InformationManager.java b/Plan/src/main/java/com/djrapitops/plan/systems/info/InformationManager.java index 85e322001..35e32cb93 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/info/InformationManager.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/info/InformationManager.java @@ -70,7 +70,7 @@ public abstract class InformationManager { analysisNotification.put(serverUUID, notify); } - public abstract String getPluginsTabContent(UUID uuid); + public abstract String[] getPluginsTabContent(UUID uuid); public boolean isUsingAnotherWebServer() { return usingAnotherWebServer; diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/info/parsing/InspectPageParser.java b/Plan/src/main/java/com/djrapitops/plan/systems/info/parsing/InspectPageParser.java index bd289edf5..592bee6a4 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/info/parsing/InspectPageParser.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/info/parsing/InspectPageParser.java @@ -52,6 +52,9 @@ public class InspectPageParser extends PageParser { public String parse() throws ParseException { try { + if (uuid == null) { + throw new IllegalStateException("UUID was null!"); + } Log.logDebug("Database", "Inspect Parse Fetch"); Benchmark.start("Inspect Parse, Fetch"); Database db = plugin.getDB(); @@ -65,6 +68,9 @@ public class InspectPageParser extends PageParser { addValue("timeZone", MiscUtils.getTimeZoneOffsetHours()); PlayerProfile profile = db.getPlayerProfile(uuid); + if (profile == null) { + throw new IllegalStateException("Player profile was null!"); + } String online = "Offline"; Optional activeSession = plugin.getInfoManager().getDataCache().getCachedSession(uuid); diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/PageCache.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/PageCache.java index cd9b62609..03319fa12 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/PageCache.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/PageCache.java @@ -2,6 +2,7 @@ package main.java.com.djrapitops.plan.systems.webserver; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; +import main.java.com.djrapitops.plan.systems.webserver.response.InspectPageResponse; import main.java.com.djrapitops.plan.systems.webserver.response.Response; import java.util.Map; @@ -67,6 +68,22 @@ public class PageCache { return pageCache.getIfPresent(identifier); } + /** + * Returns a copy some responses + * + * Currently supported copyable responses: InspectPageResponse + * + * @param identifier The identifier of the page + * @return Copied Response of loadPage, so that cache contents are not changed. + */ + public static Response copyPage(String identifier, PageLoader loader) { + Response response = loadPage(identifier, loader); + if (response instanceof InspectPageResponse) { + return InspectPageResponse.copyOf((InspectPageResponse) response); + } + return response; + } + /** * Puts the page into the page cache. *

diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/InspectPageResponse.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/InspectPageResponse.java index f13e62e1a..73ed666b6 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/InspectPageResponse.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/InspectPageResponse.java @@ -2,7 +2,9 @@ package main.java.com.djrapitops.plan.systems.webserver.response; import main.java.com.djrapitops.plan.systems.info.InformationManager; import main.java.com.djrapitops.plan.systems.webserver.theme.Theme; +import org.apache.commons.lang3.text.StrSubstitutor; +import java.util.HashMap; import java.util.Map; import java.util.UUID; @@ -12,30 +14,37 @@ import java.util.UUID; */ public class InspectPageResponse extends Response { - private String inspectPagePluginsTab; + private final UUID uuid; public InspectPageResponse(InformationManager infoManager, UUID uuid) { + this.uuid = uuid; super.setHeader("HTTP/1.1 200 OK"); super.setContent(infoManager.getPlayerHtml(uuid)); setInspectPagePluginsTab(infoManager.getPluginsTabContent(uuid)); } public InspectPageResponse(InformationManager infoManager, UUID uuid, String html) { + this.uuid = uuid; super.setHeader("HTTP/1.1 200 OK"); super.setContent(Theme.replaceColors(html)); setInspectPagePluginsTab(infoManager.getPluginsTabContent(uuid)); } - public void setInspectPagePluginsTab(String inspectPagePluginsTab) { - if (this.inspectPagePluginsTab != null) { - setContent(getContent().replace(this.inspectPagePluginsTab, inspectPagePluginsTab)); - } else { - setContent(getContent().replace("${tabContentPlugins}", inspectPagePluginsTab)); - } - this.inspectPagePluginsTab = inspectPagePluginsTab; + private InspectPageResponse(InspectPageResponse response) { + this.uuid = response.uuid; + super.setHeader(response.getHeader()); + super.setContent(response.getContent()); } - public void setInspectPagePluginsTab(Map uuidStringMap) { - // TODO + public void setInspectPagePluginsTab(String[] inspectPagePluginsTab) { + Map replaceMap = new HashMap<>(); + replaceMap.put("navPluginsTabs", inspectPagePluginsTab[0]); + replaceMap.put("pluginsTabs", inspectPagePluginsTab[1]); + + setContent(StrSubstitutor.replace(getContent(), replaceMap)); + } + + public static InspectPageResponse copyOf(InspectPageResponse response) { + return new InspectPageResponse(response); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/Response.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/Response.java index 93ae3e365..03f82ecb2 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/Response.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/Response.java @@ -31,6 +31,10 @@ public abstract class Response { this.type = ResponseType.HTML.get(); } + protected String getHeader() { + return header; + } + public String getResponse() { return header + "\r\n" + "Content-Type: " + type + ";\r\n" diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bungee/PostInspectPluginsTabWebAPI.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bungee/PostInspectPluginsTabWebAPI.java index 685dedadc..3cf3062b9 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bungee/PostInspectPluginsTabWebAPI.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bungee/PostInspectPluginsTabWebAPI.java @@ -33,9 +33,17 @@ public class PostInspectPluginsTabWebAPI extends WebAPI { UUID uuid = UUID.fromString(uuidS); UUID serverUUID = UUID.fromString(variables.get("sender")); + String nav = variables.get("nav"); + if (nav == null) { + return badRequest("nav not included"); + } String html = variables.get("html"); + if (html == null) { + return badRequest("html not included"); + } + String[] content = new String[]{nav, html}; - ((BungeeInformationManager) plugin.getInfoManager()).cachePluginsTabContent(serverUUID, uuid, html); + ((BungeeInformationManager) plugin.getInfoManager()).cachePluginsTabContent(serverUUID, uuid, content); return success(); } @@ -45,9 +53,10 @@ public class PostInspectPluginsTabWebAPI extends WebAPI { throw new IllegalStateException("Wrong method call for this WebAPI, call sendRequest(String, UUID, UUID) instead."); } - public void sendPluginsTab(String address, UUID uuid, String html) throws WebAPIException { + public void sendPluginsTab(String address, UUID uuid, String[] html) throws WebAPIException { addVariable("uuid", uuid.toString()); - addVariable("html", html); + addVariable("nav", html[0]); + addVariable("html", html[1]); super.sendRequest(address); } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/html/HtmlStructure.java b/Plan/src/main/java/com/djrapitops/plan/utilities/html/HtmlStructure.java index 97796d60e..c820ad5bf 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/html/HtmlStructure.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/html/HtmlStructure.java @@ -13,7 +13,6 @@ import main.java.com.djrapitops.plan.data.Session; import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.systems.info.BukkitInformationManager; import main.java.com.djrapitops.plan.utilities.FormatUtils; -import main.java.com.djrapitops.plan.utilities.analysis.AnalysisUtils; import main.java.com.djrapitops.plan.utilities.html.graphs.PlayerActivityGraphCreator; import main.java.com.djrapitops.plan.utilities.html.structure.SessionTabStructureCreator; import main.java.com.djrapitops.plan.utilities.html.tables.SessionsTableCreator; @@ -72,26 +71,17 @@ public class HtmlStructure { } } - private static String[] getSessionsAsTable(Map> sessions, List allSessions, UUID uuid) { - Map uuidByID = new HashMap<>(); - for (List sessionList : sessions.values()) { - for (Session session : sessionList) { - uuidByID.put(session.getSessionID(), uuid); - } - } - - return new String[]{/*Html.TABLE_SESSIONS.parse(SessionsTableCreator.createTable(uuidByID, allSessions)[0]),*/"", ""}; - } - - public static String createInspectPageTabContentCalculating() { - return "

" + - "
" + - "
" + - "

No Plugins

" + - "
" + - "

Plugins tab is still being calculated, please refresh the page after a while (F5)

" + + public static String[] createInspectPageTabContentCalculating() { + String tab = "
" + + "
" + + "
" + + "
" + + "

Plugin Data

" + + "
" + + "

Calculating Plugins tab, refresh (F5) shortly..

" + "
" + - "
"; + "
"; + return new String[]{"
  • Calculating... Refresh shortly
  • ", tab}; } public static String createNetworkPageContent(Map networkPageContents) { diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/html/structure/AnalysisPluginsTabContentCreator.java b/Plan/src/main/java/com/djrapitops/plan/utilities/html/structure/AnalysisPluginsTabContentCreator.java index 4ec72d175..36aa2c248 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/html/structure/AnalysisPluginsTabContentCreator.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/html/structure/AnalysisPluginsTabContentCreator.java @@ -5,6 +5,7 @@ package main.java.com.djrapitops.plan.utilities.html.structure; import main.java.com.djrapitops.plan.data.additional.AnalysisContainer; +import main.java.com.djrapitops.plan.data.additional.InspectContainer; import main.java.com.djrapitops.plan.data.additional.PluginData; import main.java.com.djrapitops.plan.utilities.analysis.Analysis; import main.java.com.djrapitops.plan.utilities.comparators.PluginDataNameComparator; @@ -85,7 +86,7 @@ public class AnalysisPluginsTabContentCreator { }; } - private static void appendThird(PluginData pluginData, AnalysisContainer container, StringBuilder generalTab) { + public static void appendThird(PluginData pluginData, InspectContainer container, StringBuilder generalTab) { generalTab.append("
    " + "
    " + "
    " + diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/html/structure/InspectPluginsTabContentCreator.java b/Plan/src/main/java/com/djrapitops/plan/utilities/html/structure/InspectPluginsTabContentCreator.java new file mode 100644 index 000000000..965babed7 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/html/structure/InspectPluginsTabContentCreator.java @@ -0,0 +1,46 @@ +/* + * 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.utilities.html.structure; + +import main.java.com.djrapitops.plan.Plan; +import main.java.com.djrapitops.plan.data.additional.InspectContainer; +import main.java.com.djrapitops.plan.data.additional.PluginData; +import main.java.com.djrapitops.plan.systems.info.server.BukkitServerInfoManager; +import main.java.com.djrapitops.plan.utilities.comparators.PluginDataNameComparator; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * //TODO Class Javadoc Comment + * + * @author Rsl1122 + */ +public class InspectPluginsTabContentCreator { + + public static String[] createContent(Map containers) { + BukkitServerInfoManager serverInfoManager = Plan.getInstance().getServerInfoManager(); + String serverName = serverInfoManager.getServerName(); + String actualServerName = serverName.equals("Plan") ? "Server " + serverInfoManager.getServerID() : serverName; + String nav = "
  • " + actualServerName + "
  • "; + + StringBuilder tab = new StringBuilder(); + tab.append("
    "); + + List order = new ArrayList<>(containers.keySet()); + order.sort(new PluginDataNameComparator()); + + for (PluginData pluginData : order) { + InspectContainer container = containers.get(pluginData); + AnalysisPluginsTabContentCreator.appendThird(pluginData, container, tab); + } + + tab.append("
    "); + + return new String[]{nav, tab.toString()}; + } + +} \ No newline at end of file