From ded98540d84cd2f8ba5fdc742d5347f823ca09b2 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sun, 20 Aug 2017 16:09:52 +0300 Subject: [PATCH] Start of ServerInfo, Fix typo in server.html --- .../com/djrapitops/plan/data/SessionData.java | 1 + .../plan/data/server/ServerInfo.java | 60 ++++++++++++++ .../plan/data/server/ServerInfoFile.java | 80 +++++++++++++++++++ .../plan/data/server/ServerInfoManager.java | 68 ++++++++++++++++ .../djrapitops/plan/database/Database.java | 9 +++ .../djrapitops/plan/database/sql/Insert.java | 32 ++++++++ .../djrapitops/plan/database/sql/Select.java | 1 - .../plan/database/tables/ServerTable.java | 76 +++++++++++++++++- .../djrapitops/plan/utilities/HtmlUtils.java | 2 +- Plan/src/main/resources/html/server.html | 2 +- 10 files changed, 325 insertions(+), 6 deletions(-) create mode 100644 Plan/src/main/java/com/djrapitops/plan/data/server/ServerInfo.java create mode 100644 Plan/src/main/java/com/djrapitops/plan/data/server/ServerInfoFile.java create mode 100644 Plan/src/main/java/com/djrapitops/plan/data/server/ServerInfoManager.java create mode 100644 Plan/src/main/java/com/djrapitops/plan/database/sql/Insert.java diff --git a/Plan/src/main/java/com/djrapitops/plan/data/SessionData.java b/Plan/src/main/java/com/djrapitops/plan/data/SessionData.java index 36ddd3057..4a69b3538 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/SessionData.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/SessionData.java @@ -13,6 +13,7 @@ public class SessionData { private WorldTimes worldTimes; // TODO add World Times to SessionData private final long sessionStart; private long sessionEnd; + // TODO Add kills & deaths to SessionData /** * Creates a new session with given start and end of -1. 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 new file mode 100644 index 000000000..e3210a336 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/data/server/ServerInfo.java @@ -0,0 +1,60 @@ +/* + * 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.data.server; + +import java.util.UUID; + +/** + * Represents a Server that is running Plan. + * + * @author Rsl1122 + */ +public class ServerInfo { + private final int id; + 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) { + this.id = id; + this.uuid = uuid; + this.name = name; + this.webAddress = webAddress; + this.port = port; + } + + public int getId() { + return id; + } + + public UUID getUuid() { + return uuid; + } + + public String getName() { + return name; + } + + public String getWebAddress() { + return webAddress; + } + + public int getPort() { + return port; + } + + public void setName(String name) { + this.name = name; + } + + public void setWebAddress(String webAddress) { + 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 new file mode 100644 index 000000000..788fb23de --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/data/server/ServerInfoFile.java @@ -0,0 +1,80 @@ +/* + * 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.data.server; + +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 java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +/** + * Manages local server info file. + *

+ * ServerInfo.yml contains current server's ID, UUID & Bungee WebServer connection information. + * It + * + * @author Rsl1122 + */ +public class ServerInfoFile extends BukkitConfig { + public ServerInfoFile(Plan plugin) throws IOException, InvalidConfigurationException { + super(plugin, "ServerInfo"); + FileConfiguration config = getConfig(); + config.options().copyDefaults(true); + config.addDefault("Server.ID", "-1"); + config.addDefault("Server.UUID", ""); + config.addDefault("Bungee.WebAddress", ""); + config.addDefault("Bungee.Port", -1); + config.addDefault("Bungee.Fail", 0); + save(); + } + + public void saveInfo(ServerInfo thisServer, ServerInfo bungee) throws IOException { + Map serverMap = new HashMap<>(); + Map bungeeMap = new HashMap<>(); + + serverMap.put("ID", thisServer.getId()); + serverMap.put("UUID", thisServer.getUuid().toString()); + + bungeeMap.put("WebAddress", bungee.getWebAddress()); + bungeeMap.put("Port", bungee.getPort()); + + getConfig().set("Server", serverMap); + getConfig().set("Bungee", bungeeMap); + save(); + } + + public int getID() { + return getConfig().getInt("Server.ID"); + } + + public UUID getUUID() { + String uuidString = getConfig().getString("Server.UUID"); + if (uuidString == null) { + return null; + } + return UUID.fromString(uuidString); + } + + public String getBungeeWebAddress() { + 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"); + config.set("Bungee.Fail", fails + 1); + save(); + } +} \ No newline at end of file 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 new file mode 100644 index 000000000..12d94f308 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/data/server/ServerInfoManager.java @@ -0,0 +1,68 @@ +/* + * 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.data.server; + + +import main.java.com.djrapitops.plan.Log; +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.util.UUID; + +/** + * //TODO Class Javadoc Comment + * + * @author Rsl1122 + */ +public class ServerInfoManager { + + private ServerInfo serverInfo; + private ServerInfoFile serverInfoFile; + private ServerTable serverTable; + + public ServerInfoManager(Plan plugin) { + Database db = plugin.getDB(); + if ("sqlite".equals(db.getConfigName())) { + return; + } + try { + serverInfoFile = new ServerInfoFile(plugin); + } catch (IOException | InvalidConfigurationException e) { + Log.toLog(this.getClass().getName(), e); + Log.error("Failed to read server info from local file, disabling plugin."); + plugin.disablePlugin(); + } + + 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(); + serverTable.saveCurrentServerInfo(new ServerInfo(-1, serverUUID, name, webAddress, port)); + } + + public UUID generateNewUUID(Server server) { + String seed = server.getName() + server.getIp() + server.getPort() + server.getVersion() + server.getBukkitVersion(); + return UUID.nameUUIDFromBytes(seed.getBytes()); + } + + +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/database/Database.java b/Plan/src/main/java/com/djrapitops/plan/database/Database.java index 4c7ac5fea..8cb41e91b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/Database.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/Database.java @@ -91,6 +91,11 @@ public abstract class Database { */ protected WorldTimesTable worldTimesTable; + /** + * Table representing plan_servers in the database. + */ + protected ServerTable serverTable; + /** * Super constructor. * @@ -369,4 +374,8 @@ public abstract class Database { public WorldTimesTable getWorldTimesTable() { return worldTimesTable; } + + public ServerTable getServerTable() { + return serverTable; + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/database/sql/Insert.java b/Plan/src/main/java/com/djrapitops/plan/database/sql/Insert.java new file mode 100644 index 000000000..45423aa17 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/database/sql/Insert.java @@ -0,0 +1,32 @@ +package main.java.com.djrapitops.plan.database.sql; + +public class Insert extends SqlParser { + + private int conditions = 0; + + public Insert(String table) { + super("INSERT INTO " + table); + addSpace(); + } + + public static String values(String table, String... columns) { + Insert parser = new Insert(table); + parser.append("("); + int size = columns.length; + for (int i = 0; i < size; i++) { + if (size > 1 && i > 0) { + parser.append(", "); + } + parser.append(columns[i]); + } + parser.append(") VALUES ("); + for (int i = 0; i < size; i++) { + if (size > 1 && i > 0) { + parser.append(", "); + } + parser.append("?"); + } + parser.append(")"); + return parser.toString(); + } +} diff --git a/Plan/src/main/java/com/djrapitops/plan/database/sql/Select.java b/Plan/src/main/java/com/djrapitops/plan/database/sql/Select.java index 5d100051c..44290f9f7 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/sql/Select.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/sql/Select.java @@ -2,7 +2,6 @@ package main.java.com.djrapitops.plan.database.sql; import main.java.com.djrapitops.plan.Log; -@Deprecated public class Select extends SqlParser { private int conditions = 0; 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 0f26d8981..4a78a0a64 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,13 +4,83 @@ */ package main.java.com.djrapitops.plan.database.tables; +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 java.sql.PreparedStatement; +import java.sql.SQLException; + /** * //TODO Class Javadoc Comment * * @author Rsl1122 */ -public class ServerTable { - //TODO Server Table +public class ServerTable extends Table { - // id, uuid, webserver info, name + private final String columnServerID; + private final String columnServerUUID; + private final String columnServerName; + private final String columnWebserverAddress; + private final String columnWebserverPort; + + public ServerTable(String name, SQLDB db, boolean usingMySQL) { + super("plan_servers", db, usingMySQL); + columnServerID = "id"; + columnServerUUID = "uuid"; + columnServerName = "name"; + columnWebserverAddress = "web_address"; + columnWebserverPort = "web_port"; + } + + @Override + public boolean createTable() { + try { + execute(TableSqlParser.createTable(tableName) + .primaryKeyIDColumn(usingMySQL, columnServerID, Sql.INT) + .column(columnServerUUID, Sql.varchar(36)).notNull().unique() + .column(columnServerName, Sql.varchar(100)) + .column(columnWebserverAddress, Sql.varchar(100)) + .column(columnWebserverPort, Sql.INT) + .toString()); + return true; + } catch (SQLException ex) { + Log.toLog(this.getClass().getName(), ex); + return false; + } + } + + public void saveCurrentServerInfo(ServerInfo info) throws SQLException { + if (info.getId() == -1) { + saveNewServerInfo(info); + } else { + updateServerInfo(info); + } + + } + + private void updateServerInfo(ServerInfo info) { + //TODO Continue here, create Update SqlParser. + } + + public void saveNewServerInfo(ServerInfo info) throws SQLException { + 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()); + statement.execute(); + } finally { + close(statement); + } + } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/HtmlUtils.java b/Plan/src/main/java/com/djrapitops/plan/utilities/HtmlUtils.java index ef9537c15..15a380220 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/HtmlUtils.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/HtmlUtils.java @@ -77,7 +77,7 @@ public class HtmlUtils { return ip; } - private static String getProtocol() { + public static String getProtocol() { WebServer uiServer = Plan.getInstance().getUiServer(); return uiServer.isEnabled() ? uiServer.getProtocol() : Settings.ETERNAL_WEBSERVER_LINK_PROTOCOL.toString(); } diff --git a/Plan/src/main/resources/html/server.html b/Plan/src/main/resources/html/server.html index ee14fc76f..97af1248a 100644 --- a/Plan/src/main/resources/html/server.html +++ b/Plan/src/main/resources/html/server.html @@ -296,7 +296,7 @@

-

Playerlist

+

Command Usage