From 59c339c6b0f10e68cb720a3a1ffe0abf28cb39f5 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Mon, 25 Sep 2017 15:54:35 +0300 Subject: [PATCH] Configuration WebAPI now functional --- .../plan/ServerSpecificSettings.java | 60 ++++++++++++------- .../java/com/djrapitops/plan/Settings.java | 4 +- .../info/BukkitInformationManager.java | 4 +- .../systems/webserver/APIResponseHandler.java | 3 +- .../plan/systems/webserver/webapi/WebAPI.java | 2 + .../webapi/bukkit/ConfigurationWebAPI.java | 17 ++++-- .../webapi/universal/PingWebAPI.java | 3 + Plan/src/main/resources/html/player.html | 4 +- Plan/src/main/resources/html/server.html | 6 +- 9 files changed, 69 insertions(+), 34 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/ServerSpecificSettings.java b/Plan/src/main/java/com/djrapitops/plan/ServerSpecificSettings.java index 6cb7f6332..752737fa1 100644 --- a/Plan/src/main/java/com/djrapitops/plan/ServerSpecificSettings.java +++ b/Plan/src/main/java/com/djrapitops/plan/ServerSpecificSettings.java @@ -7,6 +7,7 @@ package main.java.com.djrapitops.plan; import com.djrapitops.plugin.config.IConfig; import com.djrapitops.plugin.config.fileconfig.IFileConfig; import com.djrapitops.plugin.utilities.Verify; +import org.bukkit.configuration.file.FileConfiguration; import java.io.IOException; import java.util.Map; @@ -38,30 +39,47 @@ public class ServerSpecificSettings { } } - public void updateSettings(Plan plugin, Map settings) { - try { - IFileConfig config = plugin.getIConfig().getConfig(); - boolean changedSomething = false; - for (Map.Entry setting : settings.entrySet()) { - String path = setting.getKey(); - String value = setting.getValue(); - String currentValue = config.getString(path); - if (currentValue.equals(value)) { - continue; - } - config.set(path, value); - changedSomething = true; + public static void updateSettings(Plan plugin, Map settings) { + Log.debug("Checking new settings.."); + FileConfiguration config = plugin.getConfig(); + boolean changedSomething = false; + for (Map.Entry setting : settings.entrySet()) { + String path = setting.getKey(); + if ("sender".equals(path)) { + continue; } - if (changedSomething) { - plugin.getIConfig().save(); - Log.info("----------------------------------"); - Log.info("The Received Bungee Settings changed the config values, restarting Plan.."); - Log.info("----------------------------------"); - plugin.restart(); + String stringValue = setting.getValue(); + Object value = getValue(stringValue); + String currentValue = config.getString(path); + if (currentValue.equals(stringValue)) { + continue; } - } catch (IOException e) { - Log.toLog(this.getClass().getName(), e); + config.set(path, value); + Log.debug(" " + path + ": " + value); + changedSomething = true; } + if (changedSomething) { + plugin.saveConfig(); + Log.info("----------------------------------"); + Log.info("The Received Bungee Settings changed the config values, restarting Plan.."); + Log.info("----------------------------------"); + plugin.restart(); + } else { + Log.debug("Settings up to date"); + } + } + + private static Object getValue(String value) { + try { + return Integer.parseInt(value); + } catch (Exception ignored) { + } + if ("true".equalsIgnoreCase(value)) { + return true; + } else if ("false".equalsIgnoreCase(value)) { + return false; + } + return value; } private String getPath(UUID serverUUID, Settings setting) { diff --git a/Plan/src/main/java/com/djrapitops/plan/Settings.java b/Plan/src/main/java/com/djrapitops/plan/Settings.java index 1da9e66ef..a92bbde1d 100644 --- a/Plan/src/main/java/com/djrapitops/plan/Settings.java +++ b/Plan/src/main/java/com/djrapitops/plan/Settings.java @@ -16,8 +16,8 @@ import java.util.List; */ public enum Settings { // Boolean - BUNGEE_COPY_CONFIG("Bungee-Override.CopyBungeeConfig"), - BUNGEE_OVERRIDE_STANDALONE_MODE("Bungee-Override.StandaloneMode"), + BUNGEE_COPY_CONFIG("Plugin.Bungee-Override.CopyBungeeConfig"), + BUNGEE_OVERRIDE_STANDALONE_MODE("Plugin.Bungee-Override.StandaloneMode"), ANALYSIS_EXPORT("Analysis.Export.Enabled"), SHOW_ALTERNATIVE_IP("Commands.AlternativeIP.Enabled"), LOG_UNKNOWN_COMMANDS("Data.Commands.LogUnknownCommands"), 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 04b9d511a..bc93193ad 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 @@ -61,7 +61,7 @@ public class BukkitInformationManager extends InformationManager { Optional bungeeConnectionAddress = plugin.getServerInfoManager().getBungeeConnectionAddress(); if (bungeeConnectionAddress.isPresent()) { webServerAddress = bungeeConnectionAddress.get(); - usingAnotherWebServer = attemptConnection(); + attemptConnection(); } else { usingAnotherWebServer = false; } @@ -226,6 +226,7 @@ public class BukkitInformationManager extends InformationManager { getWebAPI().getAPI(PostOriginalBukkitSettingsWebAPI.class).sendRequest(webServerAddress); Log.info("Bungee Connection OK"); plugin.getServerInfoManager().resetConnectionFails(); + usingAnotherWebServer = true; return true; } catch (WebAPIConnectionFailException e) { plugin.getServerInfoManager().markConnectionFail(); @@ -233,6 +234,7 @@ public class BukkitInformationManager extends InformationManager { Log.toLog(this.getClass().getName(), e); } Log.info("Bungee Connection Failed."); + usingAnotherWebServer = false; return false; } diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/APIResponseHandler.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/APIResponseHandler.java index 70b8bfa76..20b5b94f9 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/APIResponseHandler.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/APIResponseHandler.java @@ -86,12 +86,13 @@ public class APIResponseHandler { if (api == null) { String error = "API Method not found"; + Log.debug(error); return PageCache.loadPage(error, () -> new BadRequestResponse(error)); } Response response = api.processRequest(MiscUtils.getIPlan(), variables); - Log.debug(response.toString()); + Log.debug("Response: " + response.getResponse().split("\r\n")[0]); return response; } diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/WebAPI.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/WebAPI.java index e16003423..3fd26b76c 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/WebAPI.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/WebAPI.java @@ -46,11 +46,13 @@ public abstract class WebAPI { public Response processRequest(IPlan plugin, Map variables) { String sender = variables.get("sender"); if (sender == null) { + Log.debug(getClass().getSimpleName() + ": Sender not Found"); return badRequest("Sender not present"); } try { UUID.fromString(sender); } catch (Exception e) { + Log.debug(getClass().getSimpleName() + ": Invalid Sender UUID"); return badRequest("Faulty Sender value"); } return onRequest(plugin, variables); diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/ConfigurationWebAPI.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/ConfigurationWebAPI.java index 4e01e1642..31aa5881e 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/ConfigurationWebAPI.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/ConfigurationWebAPI.java @@ -5,6 +5,7 @@ package main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit; import com.djrapitops.plugin.utilities.Compatibility; +import com.djrapitops.plugin.utilities.Verify; import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.ServerSpecificSettings; @@ -26,14 +27,14 @@ public class ConfigurationWebAPI extends WebAPI { @Override public Response onRequest(IPlan plugin, Map variables) { if (!Compatibility.isBukkitAvailable()) { + Log.debug("Called a wrong server type"); return badRequest("Called a Bungee Server"); } if (Settings.BUNGEE_COPY_CONFIG.isFalse() || Settings.BUNGEE_OVERRIDE_STANDALONE_MODE.isTrue()) { Log.debug("Bungee Config settings overridden on this server."); return success(); } - variables.remove("sender"); - Settings.serverSpecific().updateSettings((Plan) plugin, variables); + ServerSpecificSettings.updateSettings((Plan) plugin, variables); return success(); } @@ -44,14 +45,22 @@ public class ConfigurationWebAPI extends WebAPI { public void sendRequest(String address, UUID serverUUID) throws WebAPIException { Map configValues = getConfigValues(serverUUID); + Log.debug("Sending config values: " + configValues); for (Map.Entry entry : configValues.entrySet()) { - addVariable(entry.getKey(), entry.getValue().toString()); + String key = entry.getKey(); + Object value = entry.getValue(); + if (!Verify.notNull(key, value)) { + continue; + } + addVariable(key, value.toString()); } super.sendRequest(address); } private void addConfigValue(Map configValues, Settings setting, Object value) { - configValues.put(setting.getPath(), value); + if (value != null) { + configValues.put(setting.getPath(), value); + } } private Map getConfigValues(UUID serverUUID) throws WebAPIException { diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/universal/PingWebAPI.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/universal/PingWebAPI.java index 6adce8b04..a7f7a7147 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/universal/PingWebAPI.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/universal/PingWebAPI.java @@ -22,6 +22,9 @@ public class PingWebAPI extends WebAPI { if (Compatibility.isBungeeAvailable()) { ((PlanBungee) plugin).getServerInfoManager().serverConnected(UUID.fromString(variables.get("sender"))); } + if (Compatibility.isBukkitAvailable() && !plugin.getInfoManager().isUsingAnotherWebServer()) { + plugin.getInfoManager().attemptConnection(); + } return success(); } } \ No newline at end of file diff --git a/Plan/src/main/resources/html/player.html b/Plan/src/main/resources/html/player.html index dfa9c6aec..7f3c61556 100644 --- a/Plan/src/main/resources/html/player.html +++ b/Plan/src/main/resources/html/player.html @@ -199,13 +199,13 @@ var serverSeries = { name:'Server Playtime', colorByPoint:true, - colors: ${serverPieColors}, + colors: [${serverPieColors}], data: ${serverPieSeries} } var worldSeries = { name:'World Playtime', colorByPoint:true, - colors: ${worldPieColors}, + colors: [${worldPieColors}], data: ${worldPieSeries} }; var gmSeries = ${gmSeries}; diff --git a/Plan/src/main/resources/html/server.html b/Plan/src/main/resources/html/server.html index ceb5fb587..3a54f3ea6 100644 --- a/Plan/src/main/resources/html/server.html +++ b/Plan/src/main/resources/html/server.html @@ -409,7 +409,7 @@ var activitySeries = { name: 'Players', colorByPoint: true, - colors: ${activityPieColors}, + colors: [${activityPieColors}], data: [{ name: 'Active', y: ${active} @@ -429,7 +429,7 @@ var worldSeries = { name: 'World Playtime', colorByPoint: true, - colors: ${worldPieColors}, + colors: [${worldPieColors}], data: ${worldSeries} }; var gmSeries = ${gmSeries}; @@ -464,7 +464,7 @@ } x.style.opacity = "1"; openFunc(slideIndex)(); - activityPie('activityPie', activitySeries, ${playersTotal}, [${activityColors}]); + activityPie('activityPie', activitySeries, ${playersTotal}, [${activityPieColors}]); worldPie('worldPie', worldSeries, gmSeries); playersChart('playerChartDay', playersOnlineSeries, 1); playersChart('playerChartMonth', playersOnlineSeries, 4);