diff --git a/Plan/src/main/java/com/djrapitops/plan/data/server/ServerInfo.java b/Plan/src/main/java/com/djrapitops/plan/data/server/ServerInfo.java index e3210a336..eb0d00b2f 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/server/ServerInfo.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/server/ServerInfo.java @@ -16,14 +16,12 @@ public class ServerInfo { private final UUID uuid; private String name; private String webAddress; - private int port; - public ServerInfo(int id, UUID uuid, String name, String webAddress, int port) { + public ServerInfo(int id, UUID uuid, String name, String webAddress) { this.id = id; this.uuid = uuid; this.name = name; this.webAddress = webAddress; - this.port = port; } public int getId() { @@ -42,10 +40,6 @@ public class ServerInfo { return webAddress; } - public int getPort() { - return port; - } - public void setName(String name) { this.name = name; } @@ -54,7 +48,4 @@ public class ServerInfo { this.webAddress = webAddress; } - public void setPort(int port) { - this.port = port; - } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/data/server/ServerInfoFile.java b/Plan/src/main/java/com/djrapitops/plan/data/server/ServerInfoFile.java index 788fb23de..d1a75a4e7 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/server/ServerInfoFile.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/server/ServerInfoFile.java @@ -8,6 +8,7 @@ import com.djrapitops.plugin.config.BukkitConfig; import main.java.com.djrapitops.plan.Plan; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.FileConfigurationOptions; import java.io.IOException; import java.io.Serializable; @@ -25,13 +26,14 @@ import java.util.UUID; */ public class ServerInfoFile extends BukkitConfig { public ServerInfoFile(Plan plugin) throws IOException, InvalidConfigurationException { - super(plugin, "ServerInfo"); + super(plugin, "ServerInfoFile"); FileConfiguration config = getConfig(); - config.options().copyDefaults(true); - config.addDefault("Server.ID", "-1"); + 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.Port", -1); config.addDefault("Bungee.Fail", 0); save(); } @@ -44,7 +46,6 @@ public class ServerInfoFile extends BukkitConfig { serverMap.put("UUID", thisServer.getUuid().toString()); bungeeMap.put("WebAddress", bungee.getWebAddress()); - bungeeMap.put("Port", bungee.getPort()); getConfig().set("Server", serverMap); getConfig().set("Bungee", bungeeMap); @@ -67,10 +68,6 @@ public class ServerInfoFile extends BukkitConfig { return getConfig().getString("Bungee.WebAddress"); } - public int getBungeePort() { - return getConfig().getInt("Bungee.Port"); - } - public void markConnectionFail() throws IOException { FileConfiguration config = getConfig(); int fails = config.getInt("Bungee.Fail"); diff --git a/Plan/src/main/java/com/djrapitops/plan/data/server/ServerInfoManager.java b/Plan/src/main/java/com/djrapitops/plan/data/server/ServerInfoManager.java index 6cc1d6895..956dbb6dc 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/server/ServerInfoManager.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/server/ServerInfoManager.java @@ -10,12 +10,12 @@ import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Settings; import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.database.tables.ServerTable; -import main.java.com.djrapitops.plan.utilities.HtmlUtils; import org.bukkit.Server; import org.bukkit.configuration.InvalidConfigurationException; import java.io.IOException; import java.sql.SQLException; +import java.util.Optional; import java.util.UUID; /** @@ -25,11 +25,13 @@ import java.util.UUID; */ public class ServerInfoManager { + private Plan plugin; private ServerInfo serverInfo; private ServerInfoFile serverInfoFile; private ServerTable serverTable; public ServerInfoManager(Plan plugin) { + this.plugin = plugin; Database db = plugin.getDB(); if ("sqlite".equals(db.getConfigName())) { return; @@ -45,27 +47,60 @@ public class ServerInfoManager { serverTable = db.getServerTable(); int serverID = serverInfoFile.getID(); - if (serverID == -1) { - registerServer(plugin); - } - } - - private void registerServer(Plan plugin) { - UUID serverUUID = generateNewUUID(plugin.getServer()); - // TODO Clean Up HtmlUtils so this method can make sense - String[] address = (HtmlUtils.getProtocol() + "/" + HtmlUtils.getIP()).split(":"); - String webAddress = address[0]; - int port = Integer.parseInt(address[1]); - String name = Settings.SERVER_NAME.toString(); try { - serverTable.saveCurrentServerInfo(new ServerInfo(-1, serverUUID, name, webAddress, port)); + if (serverID == -1) { + registerServer(); + } else { + updateDbInfo(serverID); + } } catch (SQLException e) { Log.toLog(this.getClass().getName(), e); } + } - public UUID generateNewUUID(Server server) { + private void updateDbInfo(int serverID) throws SQLException { + UUID uuid = serverInfoFile.getUUID(); + String name = Settings.SERVER_NAME.toString(); + String webAddress = plugin.getUiServer().getAccessAddress(); + if ("plan".equalsIgnoreCase(name)) { + name = "Server" + Integer.toString(serverID); + } + + serverInfo = new ServerInfo(serverID, uuid, name, webAddress); + serverTable.saveCurrentServerInfo(serverInfo); + } + + private void registerServer() throws SQLException { + UUID serverUUID = generateNewUUID(plugin.getServer()); + String webAddress = plugin.getUiServer().getAccessAddress(); + String name = Settings.SERVER_NAME.toString(); + serverInfo = new ServerInfo(-1, serverUUID, name, webAddress); + serverTable.saveCurrentServerInfo(serverInfo); + } + + private UUID generateNewUUID(Server server) { String seed = server.getName() + server.getIp() + server.getPort() + server.getVersion() + server.getBukkitVersion(); return UUID.nameUUIDFromBytes(seed.getBytes()); } + + public Optional getBungeeConnectionAddress() { + try { + String bungeeWebAddress = serverInfoFile.getBungeeWebAddress(); + if (!bungeeWebAddress.isEmpty()) { + return Optional.of(bungeeWebAddress); + } + } catch (Exception ignored) { + /* Ignored */ + } + return Optional.empty(); + } + + public int getServerID() { + return serverInfo.getId(); + } + + public String getServerName() { + return serverInfo.getName(); + } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/database/sql/WhereParser.java b/Plan/src/main/java/com/djrapitops/plan/database/sql/WhereParser.java index 2462d9e1f..d94d5f636 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/sql/WhereParser.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/sql/WhereParser.java @@ -20,28 +20,29 @@ public abstract class WhereParser extends SqlParser { private int conditions = 0; public WhereParser where(String... conditions) { - return and(conditions); - } - - public WhereParser and(String... conditions) { - return whereOperator("AND", conditions); - } - - public WhereParser or(String... conditions) { - return whereOperator("OR", conditions); - } - - private WhereParser whereOperator(String operator, String... conditions) { append(" WHERE "); for (String condition : conditions) { if (this.conditions > 0) { - addSpace().append(operator).addSpace(); + append(" AND "); } - append("(").append(condition).append(")"); this.conditions++; } return this; } + + public WhereParser and(String condition) { + append(" AND "); + append("(").append(condition).append(")"); + this.conditions++; + return this; + } + + public WhereParser or(String condition) { + append(" OR "); + append("(").append(condition).append(")"); + this.conditions++; + return this; + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/ServerTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/ServerTable.java index 4a78a0a64..b352b5ff4 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/ServerTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/ServerTable.java @@ -4,15 +4,19 @@ */ package main.java.com.djrapitops.plan.database.tables; +import com.djrapitops.plugin.utilities.Verify; import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.data.server.ServerInfo; import main.java.com.djrapitops.plan.database.databases.SQLDB; -import main.java.com.djrapitops.plan.database.sql.Insert; -import main.java.com.djrapitops.plan.database.sql.Sql; -import main.java.com.djrapitops.plan.database.sql.TableSqlParser; +import main.java.com.djrapitops.plan.database.sql.*; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.UUID; /** * //TODO Class Javadoc Comment @@ -25,7 +29,7 @@ public class ServerTable extends Table { private final String columnServerUUID; private final String columnServerName; private final String columnWebserverAddress; - private final String columnWebserverPort; + private final String columnInstalled; public ServerTable(String name, SQLDB db, boolean usingMySQL) { super("plan_servers", db, usingMySQL); @@ -33,7 +37,7 @@ public class ServerTable extends Table { columnServerUUID = "uuid"; columnServerName = "name"; columnWebserverAddress = "web_address"; - columnWebserverPort = "web_port"; + columnInstalled = "is_installed"; } @Override @@ -44,7 +48,8 @@ public class ServerTable extends Table { .column(columnServerUUID, Sql.varchar(36)).notNull().unique() .column(columnServerName, Sql.varchar(100)) .column(columnWebserverAddress, Sql.varchar(100)) - .column(columnWebserverPort, Sql.INT) + .column(columnInstalled, Sql.BOOL).notNull().defaultValue(false) + .primaryKey(usingMySQL, columnServerID) .toString()); return true; } catch (SQLException ex) { @@ -62,25 +67,161 @@ public class ServerTable extends Table { } - private void updateServerInfo(ServerInfo info) { - //TODO Continue here, create Update SqlParser. + private void updateServerInfo(ServerInfo info) throws SQLException { + PreparedStatement statement = null; + try { + statement = prepareStatement(Update.values(tableName, + columnServerUUID, + columnServerName, + columnWebserverAddress, + columnInstalled) + .where(columnServerID + "=?") + .toString() + ); + statement.setString(1, info.getUuid().toString()); + statement.setString(2, info.getName()); + statement.setString(3, info.getWebAddress()); + statement.setBoolean(4, true); + statement.setInt(5, info.getId()); + statement.executeUpdate(); + } finally { + close(statement); + } } - public void saveNewServerInfo(ServerInfo info) throws SQLException { + /** + * Inserts new row for a server into the table. + * + * @param info Info to instert (All variables should be present. + * @throws IllegalStateException if one of the ServerInfo variables is null + * @throws SQLException + */ + private void saveNewServerInfo(ServerInfo info) throws SQLException { + UUID uuid = info.getUuid(); + String name = info.getName(); + String webAddress = info.getWebAddress(); + Verify.nullCheck(uuid, name, webAddress); PreparedStatement statement = null; try { statement = prepareStatement(Insert.values(tableName, columnServerUUID, columnServerName, columnWebserverAddress, - columnWebserverPort)); - statement.setString(1, info.getUuid().toString()); - statement.setString(2, info.getName()); - statement.setString(3, info.getWebAddress()); - statement.setInt(4, info.getPort()); + columnInstalled)); + + statement.setString(1, uuid.toString()); + statement.setString(2, name); + statement.setString(3, webAddress); + statement.setBoolean(4, true); statement.execute(); } finally { close(statement); } } + + /** + * Returns server ID for a matching UUID + * + * @param serverUUID UUID of the server. + * @return ID or or empty optional. + * @throws SQLException + */ + public Optional getServerID(UUID serverUUID) throws SQLException { + PreparedStatement statement = null; + ResultSet set = null; + try { + statement = prepareStatement(Select.from(tableName, + columnServerID) + .where(columnServerUUID + "=?") + .toString()); + statement.setString(1, serverUUID.toString()); + set = statement.executeQuery(); + if (set.next()) { + return Optional.of(set.getInt(columnServerID)); + } else { + return Optional.empty(); + } + } finally { + close(set, statement); + } + } + + /** + * Returns server Name for a matching UUID + * + * @param serverUUID UUID of the server. + * @return Name or empty optional. + * @throws SQLException + */ + public Optional getServerName(UUID serverUUID) throws SQLException { + PreparedStatement statement = null; + ResultSet set = null; + try { + statement = prepareStatement(Select.from(tableName, + columnServerName) + .where(columnServerUUID + "=?") + .toString()); + statement.setString(1, serverUUID.toString()); + set = statement.executeQuery(); + if (set.next()) { + return Optional.of(set.getString(columnServerName)); + } else { + return Optional.empty(); + } + } finally { + close(set, statement); + } + } + + /** + * Used to get BungeeCord WebServer info if present. + * + * @return information about Bungee server. + * @throws SQLException + */ + public Optional getBungeeInfo() throws SQLException { + PreparedStatement statement = null; + ResultSet set = null; + try { + statement = prepareStatement(Select.from(tableName, "*") + .where(columnServerName + "=?") + .toString()); + statement.setString(1, "BungeeCord"); + set = statement.executeQuery(); + if (set.next()) { + return Optional.of(new ServerInfo( + set.getInt(columnServerID), + UUID.fromString(set.getString(columnServerUUID)), + set.getString(columnServerName), + set.getString(columnWebserverAddress))); + } else { + return Optional.empty(); + } + } finally { + close(set, statement); + } + } + + public List getBukkitServers() throws SQLException { + PreparedStatement statement = null; + ResultSet set = null; + try { + statement = prepareStatement(Select.from(tableName, "*") + .where(columnServerName + "!=?") + .toString()); + statement.setString(1, "BungeeCord"); + set = statement.executeQuery(); + List servers = new ArrayList<>(); + while (set.next()) { + servers.add(new ServerInfo( + set.getInt(columnServerID), + UUID.fromString(set.getString(columnServerUUID)), + set.getString(columnServerName), + set.getString(columnWebserverAddress))); + } + return servers; + } finally { + close(set, statement); + } + } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebServer.java b/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebServer.java index c258feb76..60fb49a9e 100644 --- a/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebServer.java +++ b/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebServer.java @@ -531,4 +531,8 @@ public class WebServer { public boolean isAuthRequired() { return usingHttps; } + + public String getAccessAddress() { + return getProtocol()+":/"+ HtmlUtils.getIP(); + } }