Bungee->Bukkit Configuration WebAPI

This commit is contained in:
Rsl1122 2017-09-17 12:56:05 +03:00
parent 7c154bbb5e
commit 4918fea0b0
2 changed files with 57 additions and 31 deletions

View File

@ -4,7 +4,14 @@
*/
package main.java.com.djrapitops.plan;
import java.util.UUID; /**
import com.djrapitops.plugin.config.fileconfig.IFileConfig;
import main.java.com.djrapitops.plan.api.IPlan;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
/**
* Bungee Config manager for Server Settings such as:
* - WebServer Port
* - ServerName
@ -13,6 +20,34 @@ import java.util.UUID; /**
* @author Rsl1122
*/
public class ServerSpecificSettings {
public void updateSettings(IPlan plugin, Map<String, String> settings) {
try {
IFileConfig config = plugin.getIConfig().getConfig();
boolean changedSomething = false;
for (Map.Entry<String, String> 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;
}
if (changedSomething) {
plugin.getIConfig().save();
Log.info("----------------------------------");
Log.info("The Received Bungee Settings changed the config values, restarting Plan..");
Log.info("----------------------------------");
plugin.onDisable();
plugin.onEnable();
}
} catch (IOException e) {
Log.toLog(this.getClass().getName(), e);
}
}
public Object get(UUID serverUUID, Settings setting) {
return null;
}

View File

@ -4,9 +4,7 @@
*/
package main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit;
import com.djrapitops.plugin.config.fileconfig.IFileConfig;
import com.djrapitops.plugin.utilities.Compatibility;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.ServerSpecificSettings;
import main.java.com.djrapitops.plan.Settings;
import main.java.com.djrapitops.plan.api.IPlan;
@ -14,7 +12,6 @@ import main.java.com.djrapitops.plan.api.exceptions.WebAPIException;
import main.java.com.djrapitops.plan.systems.webserver.response.Response;
import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPI;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@ -28,41 +25,35 @@ public class ConfigurationWebAPI extends WebAPI {
@Override
public Response onRequest(IPlan plugin, Map<String, String> variables) {
if (!Compatibility.isBukkitAvailable()) {
return badRequest("Called a Bungee Server");
}
if (Settings.BUNGEE_COPY_CONFIG.isFalse() || Settings.BUNGEE_OVERRIDE_STANDALONE_MODE.isTrue()) {
return success();
}
String key = variables.get("configKey");
if (key == null) {
return badRequest("Config Key null");
}
String value = variables.get("configValue");
if (value == null) {
return badRequest("Config Value null");
}
if (value.equals("null")) {
value = null;
}
try {
IFileConfig config = plugin.getIConfig().getConfig();
config.set(key, value);
plugin.getIConfig().save();
} catch (IOException e) {
Log.toLog(this.getClass().getName(), e);
}
variables.remove("sender");
Settings.serverSpecific().updateSettings(plugin, variables);
return success();
}
public void addConfigValue(Settings setting, Object value) {
@Override
public void sendRequest(String address) throws WebAPIException {
throw new IllegalStateException("Wrong method call for this WebAPI, call sendRequest(String, UUID, UUID) instead.");
}
public void sendRequest(String address, UUID serverUUID, int newPort) throws WebAPIException {
setConfigValues(serverUUID, newPort);
for (Map.Entry<String, Object> entry : configValues.entrySet()) {
addVariable(entry.getKey(), entry.getValue().toString());
}
super.sendRequest(address);
}
private void addConfigValue(Settings setting, Object value) {
configValues.put(setting.getPath(), value);
}
public void setConfigValues(UUID serverUUID, int port) throws WebAPIException {
public void setConfigValues(UUID serverUUID, int newPort) throws WebAPIException {
if (!Compatibility.isBungeeAvailable()) {
throw new WebAPIException("Attempted to send config values from Bukkit to Bungee.");
}
@ -78,7 +69,7 @@ public class ConfigurationWebAPI extends WebAPI {
addConfigValue(setting, setting.toString());
}
addConfigValue(Settings.DB_PORT, Settings.DB_PORT.getNumber());
addConfigValue(Settings.WEBSERVER_PORT, port);
addConfigValue(Settings.WEBSERVER_PORT, newPort);
addServerSpecificValues(serverUUID);
}