Merge branch '4.0.0-BungeeCord-Support' of https://github.com/Rsl1122/Plan-PlayerAnalytics

This commit is contained in:
Fuzzlemann 2017-08-20 15:13:22 +02:00
commit 6102aa81bb
10 changed files with 325 additions and 6 deletions

View File

@ -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.

View File

@ -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;
}
}

View File

@ -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.
* <p>
* ServerInfo.yml contains current server's ID, UUID & Bungee WebServer connection information.
* It
*
* @author Rsl1122
*/
public class ServerInfoFile extends BukkitConfig<Plan> {
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<String, Serializable> serverMap = new HashMap<>();
Map<String, Serializable> 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();
}
}

View File

@ -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());
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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;

View File

@ -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);
}
}
}

View File

@ -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();
}

View File

@ -296,7 +296,7 @@
<div id="tab-command-usage" class="tab">
<div class="column">
<div class="box-header">
<h2><i class="fa fa-list" aria-hidden="true"></i> Playerlist</h2>
<h2><i class="fa fa-terminal" aria-hidden="true"></i> Command Usage</h2>
</div>
<div class="box-footer scrollbar" style="padding: 2px;">
<table class="sortable table">