mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-27 03:27:37 +01:00
Improve ServerInfo so that it will work when swapping from sqlite to mysql.
This commit is contained in:
parent
e44b20d74f
commit
54b9dc4374
@ -34,12 +34,12 @@ import main.java.com.djrapitops.plan.data.cache.DataCacheHandler;
|
||||
import main.java.com.djrapitops.plan.data.cache.InspectCacheHandler;
|
||||
import main.java.com.djrapitops.plan.data.cache.PageCacheHandler;
|
||||
import main.java.com.djrapitops.plan.data.listeners.*;
|
||||
import main.java.com.djrapitops.plan.data.server.ServerInfoManager;
|
||||
import main.java.com.djrapitops.plan.database.Database;
|
||||
import main.java.com.djrapitops.plan.database.databases.MySQLDB;
|
||||
import main.java.com.djrapitops.plan.database.databases.SQLiteDB;
|
||||
import main.java.com.djrapitops.plan.locale.Locale;
|
||||
import main.java.com.djrapitops.plan.locale.Msg;
|
||||
import main.java.com.djrapitops.plan.ui.theme.Theme;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.WebServer;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.api.bukkit.*;
|
||||
import main.java.com.djrapitops.plan.utilities.Benchmark;
|
||||
@ -77,6 +77,8 @@ public class Plan extends BukkitPlugin<Plan> {
|
||||
|
||||
private WebServer uiServer;
|
||||
|
||||
private ServerInfoManager serverInfoManager;
|
||||
|
||||
private ServerVariableHolder serverVariableHolder;
|
||||
private int bootAnalysisTaskID = -1;
|
||||
|
||||
@ -185,6 +187,11 @@ public class Plan extends BukkitPlugin<Plan> {
|
||||
if (!uiServer.isEnabled()) {
|
||||
Log.error("WebServer was not successfully initialized.");
|
||||
}
|
||||
|
||||
Benchmark.start("ServerInfo Registration");
|
||||
serverInfoManager = new ServerInfoManager(this);
|
||||
Benchmark.stop("Enable", "ServerInfo Registration");
|
||||
|
||||
setupFilter(); // TODO Move to RegisterCommand Constructor
|
||||
|
||||
// Data view settings // TODO Rewrite. (TextUI removed & webserver might be running on bungee
|
||||
@ -210,8 +217,6 @@ public class Plan extends BukkitPlugin<Plan> {
|
||||
// BStats bStats = new BStats(this);
|
||||
// bStats.registerMetrics();
|
||||
|
||||
Theme.test(); //TODO Remove
|
||||
|
||||
Log.debug("Verbose debug messages are enabled.");
|
||||
Log.logDebug("Enable", Benchmark.stop("Enable", "Enable"));
|
||||
Log.info(Locale.get(Msg.ENABLED).toString());
|
||||
|
@ -12,7 +12,7 @@ import java.util.UUID;
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class ServerInfo {
|
||||
private final int id;
|
||||
private int id;
|
||||
private final UUID uuid;
|
||||
private String name;
|
||||
private String webAddress;
|
||||
@ -48,4 +48,7 @@ public class ServerInfo {
|
||||
this.webAddress = webAddress;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@
|
||||
package main.java.com.djrapitops.plan.data.server;
|
||||
|
||||
import com.djrapitops.plugin.config.BukkitConfig;
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
@ -14,6 +15,7 @@ import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
@ -31,7 +33,6 @@ public class ServerInfoFile extends BukkitConfig<Plan> {
|
||||
FileConfigurationOptions options = config.options();
|
||||
options.copyDefaults(true);
|
||||
options.header("IMPORTANT: Do not edit this file unless you want to lose your data!");
|
||||
config.addDefault("Server.ID", -1);
|
||||
config.addDefault("Server.UUID", "");
|
||||
config.addDefault("Bungee.WebAddress", "");
|
||||
config.addDefault("Bungee.Fail", 0);
|
||||
@ -39,29 +40,30 @@ public class ServerInfoFile extends BukkitConfig<Plan> {
|
||||
}
|
||||
|
||||
public void saveInfo(ServerInfo thisServer, ServerInfo bungee) throws IOException {
|
||||
FileConfiguration config = getConfig();
|
||||
Map<String, Serializable> serverMap = new HashMap<>();
|
||||
Map<String, Serializable> bungeeMap = new HashMap<>();
|
||||
|
||||
serverMap.put("ID", thisServer.getId());
|
||||
serverMap.put("UUID", thisServer.getUuid().toString());
|
||||
config.set("Server", serverMap);
|
||||
|
||||
bungeeMap.put("WebAddress", bungee.getWebAddress());
|
||||
String oldAddress = config.getString("Bungee.WebAddress");
|
||||
String newAddress = bungee.getWebAddress();
|
||||
|
||||
getConfig().set("Server", serverMap);
|
||||
getConfig().set("Bungee", bungeeMap);
|
||||
if (!newAddress.equals(oldAddress)) {
|
||||
bungeeMap.put("Fail", 0);
|
||||
bungeeMap.put("WebAddress", newAddress);
|
||||
config.set("Bungee", bungeeMap);
|
||||
}
|
||||
save();
|
||||
}
|
||||
|
||||
public int getID() {
|
||||
return getConfig().getInt("Server.ID");
|
||||
}
|
||||
|
||||
public UUID getUUID() {
|
||||
public Optional<UUID> getUUID() {
|
||||
String uuidString = getConfig().getString("Server.UUID");
|
||||
if (uuidString == null) {
|
||||
return null;
|
||||
if (Verify.isEmpty(uuidString)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return UUID.fromString(uuidString);
|
||||
return Optional.of(UUID.fromString(uuidString));
|
||||
}
|
||||
|
||||
public String getBungeeWebAddress() {
|
||||
|
@ -19,7 +19,9 @@ import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* //TODO Class Javadoc Comment
|
||||
* Manages the Server information required for Bungee-Bukkit WebAPI connection.
|
||||
* <p>
|
||||
* Also manages Server ID required for MySQL database independence.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@ -33,9 +35,8 @@ public class ServerInfoManager {
|
||||
public ServerInfoManager(Plan plugin) {
|
||||
this.plugin = plugin;
|
||||
Database db = plugin.getDB();
|
||||
if ("sqlite".equals(db.getConfigName())) {
|
||||
return;
|
||||
}
|
||||
serverTable = db.getServerTable();
|
||||
|
||||
try {
|
||||
serverInfoFile = new ServerInfoFile(plugin);
|
||||
} catch (IOException | InvalidConfigurationException e) {
|
||||
@ -44,39 +45,52 @@ public class ServerInfoManager {
|
||||
plugin.disablePlugin();
|
||||
}
|
||||
|
||||
serverTable = db.getServerTable();
|
||||
Optional<UUID> serverUUID = serverInfoFile.getUUID();
|
||||
|
||||
int serverID = serverInfoFile.getID();
|
||||
try {
|
||||
if (serverID == -1) {
|
||||
registerServer();
|
||||
if (serverUUID.isPresent()) {
|
||||
updateDbInfo(serverUUID.get());
|
||||
} else {
|
||||
updateDbInfo(serverID);
|
||||
registerServer();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
} catch (Exception e) {
|
||||
Log.toLog(this.getClass().getName(), e);
|
||||
Log.error("Failed to register server info to database, disabling plugin.");
|
||||
plugin.disablePlugin();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void updateDbInfo(int serverID) throws SQLException {
|
||||
UUID uuid = serverInfoFile.getUUID();
|
||||
private void updateDbInfo(UUID serverUUID) throws SQLException {
|
||||
Optional<Integer> serverID = serverTable.getServerID(serverUUID);
|
||||
if (!serverID.isPresent()) {
|
||||
registerServer(serverUUID);
|
||||
return;
|
||||
}
|
||||
String name = Settings.SERVER_NAME.toString();
|
||||
String webAddress = plugin.getUiServer().getAccessAddress();
|
||||
if ("plan".equalsIgnoreCase(name)) {
|
||||
name = "Server" + Integer.toString(serverID);
|
||||
name = "Server" + serverID.get();
|
||||
}
|
||||
|
||||
serverInfo = new ServerInfo(serverID, uuid, name, webAddress);
|
||||
serverInfo = new ServerInfo(serverID.get(), serverUUID, name, webAddress);
|
||||
serverTable.saveCurrentServerInfo(serverInfo);
|
||||
}
|
||||
|
||||
private void registerServer() throws SQLException {
|
||||
UUID serverUUID = generateNewUUID(plugin.getServer());
|
||||
registerServer(generateNewUUID(plugin.getServer()));
|
||||
}
|
||||
|
||||
private void registerServer(UUID serverUUID) throws SQLException {
|
||||
String webAddress = plugin.getUiServer().getAccessAddress();
|
||||
String name = Settings.SERVER_NAME.toString();
|
||||
serverInfo = new ServerInfo(-1, serverUUID, name, webAddress);
|
||||
serverTable.saveCurrentServerInfo(serverInfo);
|
||||
Optional<Integer> serverID = serverTable.getServerID(serverUUID);
|
||||
if (serverID.isPresent()) {
|
||||
serverInfo.setId(serverID.get());
|
||||
} else {
|
||||
throw new IllegalStateException("Failed to Register Server (ID not found)");
|
||||
}
|
||||
}
|
||||
|
||||
private UUID generateNewUUID(Server server) {
|
||||
@ -96,10 +110,18 @@ public class ServerInfoManager {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public void saveBungeeConnectionAddress(String address) throws IOException {
|
||||
serverInfoFile.saveInfo(serverInfo, new ServerInfo(-1, null, "Bungee", address));
|
||||
}
|
||||
|
||||
public int getServerID() {
|
||||
return serverInfo.getId();
|
||||
}
|
||||
|
||||
public UUID getServerUUID() {
|
||||
return serverInfo.getUuid();
|
||||
}
|
||||
|
||||
public String getServerName() {
|
||||
return serverInfo.getName();
|
||||
}
|
||||
|
@ -56,6 +56,7 @@ public abstract class SQLDB extends Database {
|
||||
securityTable = new SecurityTable(this, usingMySQL);
|
||||
worldTable = new WorldTable(this, usingMySQL);
|
||||
worldTimesTable = new WorldTimesTable(this, usingMySQL);
|
||||
serverTable = new ServerTable(this, usingMySQL);
|
||||
|
||||
startConnectionPingTask();
|
||||
}
|
||||
|
@ -19,7 +19,9 @@ import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* //TODO Class Javadoc Comment
|
||||
* Table representing plan_servers in the database.
|
||||
* <p>
|
||||
* Used for managing multiple server's data in the database.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@ -31,7 +33,7 @@ public class ServerTable extends Table {
|
||||
private final String columnWebserverAddress;
|
||||
private final String columnInstalled;
|
||||
|
||||
public ServerTable(String name, SQLDB db, boolean usingMySQL) {
|
||||
public ServerTable(SQLDB db, boolean usingMySQL) {
|
||||
super("plan_servers", db, usingMySQL);
|
||||
columnServerID = "id";
|
||||
columnServerUUID = "uuid";
|
||||
|
@ -5,13 +5,6 @@
|
||||
package main.java.com.djrapitops.plan.ui.theme;
|
||||
|
||||
import main.java.com.djrapitops.plan.Log;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.utilities.HtmlUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* Enum that contains available themes.
|
||||
@ -78,23 +71,6 @@ public enum Theme {
|
||||
return replaced;
|
||||
}
|
||||
|
||||
// TODO Remove
|
||||
public static void test() throws IOException {
|
||||
String serverHtml = HtmlUtils.getStringFromResource("server - Example.html");
|
||||
String css = HtmlUtils.getStringFromResource("main.css");
|
||||
|
||||
File folder = new File(Plan.getInstance().getDataFolder(), "themes");
|
||||
folder.mkdirs();
|
||||
for (Theme t : Theme.values()) {
|
||||
File themeFolder = new File(folder, t.name());
|
||||
themeFolder.mkdirs();
|
||||
File themeHtml = new File(themeFolder, "server.html");
|
||||
File themeCss = new File(themeFolder, "main.css");
|
||||
Files.write(themeHtml.toPath(), Collections.singletonList(t.replaceThemeColors(serverHtml)));
|
||||
Files.write(themeCss.toPath(), Collections.singletonList(t.replaceThemeColors(css)));
|
||||
}
|
||||
}
|
||||
|
||||
public static String replaceColors(String resourceString) {
|
||||
Theme def = Theme.DEFAULT;
|
||||
String replaced = resourceString;
|
||||
|
Loading…
Reference in New Issue
Block a user