summaryrefslogtreecommitdiff
path: root/serverlist-server
diff options
context:
space:
mode:
authorPugmatt <pugmatt@gmail.com>2019-06-19 23:54:13 -0400
committerPugmatt <pugmatt@gmail.com>2019-06-19 23:54:13 -0400
commitf01bb1fd9a5e1903878aa790e0f5505512031012 (patch)
treec07b71890eecc05219349e70c2ce87a275c0d5eb /serverlist-server
parentd5b97281d1f3fac79be585ce0c049163f971c876 (diff)
downloadBedrockConnect-f01bb1fd9a5e1903878aa790e0f5505512031012.tar.gz
BedrockConnect-f01bb1fd9a5e1903878aa790e0f5505512031012.tar.bz2
BedrockConnect-f01bb1fd9a5e1903878aa790e0f5505512031012.zip
Added MySQL
Diffstat (limited to 'serverlist-server')
-rw-r--r--serverlist-server/BedrockConnect.iml1
-rw-r--r--serverlist-server/pom.xml7
-rw-r--r--serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/BedrockConnect.java62
-rw-r--r--serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/listeners/PacketHandler.java6
-rw-r--r--serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/sql/Database.java40
-rw-r--r--serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/sql/MySQL.java137
6 files changed, 252 insertions, 1 deletions
diff --git a/serverlist-server/BedrockConnect.iml b/serverlist-server/BedrockConnect.iml
index 99df940..919c761 100644
--- a/serverlist-server/BedrockConnect.iml
+++ b/serverlist-server/BedrockConnect.iml
@@ -46,5 +46,6 @@
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.8" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.9.8" level="project" />
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.23" level="project" />
+ <orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.17" level="project" />
</component>
</module> \ No newline at end of file
diff --git a/serverlist-server/pom.xml b/serverlist-server/pom.xml
index 240d4d8..54f1b73 100644
--- a/serverlist-server/pom.xml
+++ b/serverlist-server/pom.xml
@@ -115,6 +115,13 @@
<version>2.9.8</version>
<scope>compile</scope>
</dependency>
+ <dependency>
+ <groupId>mysql</groupId>
+ <artifactId>mysql-connector-java</artifactId>
+ <version>5.1.17</version>
+ <type>jar</type>
+ <scope>compile</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/BedrockConnect.java b/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/BedrockConnect.java
index bda3b38..d7a92f7 100644
--- a/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/BedrockConnect.java
+++ b/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/BedrockConnect.java
@@ -1,16 +1,76 @@
package main.com.pyratron.pugmatt.bedrockconnect;
+import main.com.pyratron.pugmatt.bedrockconnect.sql.MySQL;
import main.com.pyratron.pugmatt.bedrockconnect.utils.PaletteManager;
+import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Timer;
+import java.util.TimerTask;
+
public class BedrockConnect {
public static PaletteManager paletteManager;
+ public static MySQL MySQL;
+ public static Connection connection;
+
public static void main(String[] args) {
paletteManager = new PaletteManager();
- Server server = new Server();
+ try {
+ String password = new String(Files.readAllBytes(Paths.get("mysql.txt")));
+
+ MySQL = new MySQL("localhost", "bedrock-connect", "root", password);
+
+ connection = null;
+
+ connection = MySQL.openConnection();
+
+ // Keep MySQL connection alive
+ Timer timer = new Timer();
+ TimerTask task = new TimerTask() {
+ int sec;
+
+ public void run() {
+ try {
+ if (connection == null || connection.isClosed()) {
+ connection = MySQL.openConnection();
+ } else {
+ if (sec == 600) {
+ try {
+ ResultSet rs = connection
+ .createStatement()
+ .executeQuery(
+ "SELECT 1");
+ rs.next();
+ } catch (SQLException e) {
+ // TODO Auto-generated
+ // catch block
+ e.printStackTrace();
+ }
+ sec = 0;
+ }
+ }
+ } catch (SQLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ sec++;
+ }
+ };
+ timer.scheduleAtFixedRate(task, 0L, 1200L);
+
+
+ Server server = new Server();
+ } catch(IOException e) {
+ e.printStackTrace();
+ }
}
}
diff --git a/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/listeners/PacketHandler.java b/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/listeners/PacketHandler.java
index c808ffa..a9e4b16 100644
--- a/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/listeners/PacketHandler.java
+++ b/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/listeners/PacketHandler.java
@@ -41,6 +41,9 @@ public class PacketHandler implements BedrockPacketHandler {
private BedrockServerSession session;
private PipePlayer player;
+ private String name;
+ private String uuid;
+
private JSONObject skinData;
private JSONObject extraData;
private ArrayNode chainData;
@@ -983,6 +986,9 @@ public class PacketHandler implements BedrockPacketHandler {
System.out.println("Made it through login - " + "User: " + extraData.getAsString("displayName") + " (" + extraData.getAsString("identity") + ")");
+ name = extraData.getAsString("displayName");
+ uuid = extraData.getAsString("identity");
+
PlayStatusPacket status = new PlayStatusPacket();
status.setStatus(PlayStatusPacket.Status.LOGIN_SUCCESS);
session.sendPacketImmediately(status);
diff --git a/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/sql/Database.java b/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/sql/Database.java
new file mode 100644
index 0000000..ea37439
--- /dev/null
+++ b/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/sql/Database.java
@@ -0,0 +1,40 @@
+package main.com.pyratron.pugmatt.bedrockconnect.sql;
+
+
+import java.sql.Connection;
+
+/**
+ * Abstract Database class, serves as a base for any connection method (MySQL, SQLite, etc.)
+ *
+ * @author -_Husky_-
+ * @author tips48
+ */
+public abstract class Database {
+
+
+ /**
+ * Opens a connection with the database
+ *
+ * @return Connection opened
+ */
+ public abstract Connection openConnection();
+
+ /**
+ * Checks if a connection is open with the database
+ *
+ * @return true if a connection is open
+ */
+ public abstract boolean checkConnection();
+
+ /**
+ * Gets the connection with the database
+ *
+ * @return Connection with the database, null if none
+ */
+ public abstract Connection getConnection();
+
+ /**
+ * Closes the connection with the database
+ */
+ public abstract void closeConnection();
+} \ No newline at end of file
diff --git a/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/sql/MySQL.java b/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/sql/MySQL.java
new file mode 100644
index 0000000..0a7798f
--- /dev/null
+++ b/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/sql/MySQL.java
@@ -0,0 +1,137 @@
+package main.com.pyratron.pugmatt.bedrockconnect.sql;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.logging.Level;
+
+
+
+
+/**
+ * Connects to and uses a MySQL database
+ *
+ * @author -_Husky_-
+ * @author tips48
+ */
+public class MySQL extends Database {
+ private final String user;
+ private final String database;
+ private final String password;
+ private final String hostname;
+
+ private Connection connection;
+
+ /**
+ * Creates a new MySQL instance
+ *
+ * @param hostname
+ * Name of the host
+ * @param database
+ * Database name
+ * @param username
+ * Username
+ * @param password
+ * Password
+ */
+ public MySQL(String hostname, String database, String username, String password) {
+ this.hostname = hostname;
+ this.database = database;
+ this.user = username;
+ this.password = password;
+ this.connection = null;
+ }
+
+ @Override
+ public Connection openConnection() {
+ try {
+ Class.forName("com.mysql.jdbc.Driver");
+ connection = DriverManager.getConnection("jdbc:mysql://" + this.hostname + "/" + this.database, this.user, this.password);
+ System.out.println("- MySQL Connection Started -");
+
+ } catch (SQLException e) {
+ System.out.println("ERROR: Could not connect to MySQL server! because: " + e.getMessage());
+ } catch (ClassNotFoundException e) {
+ System.out.println("ERROR: JDBC Driver not found!");
+ }
+ return connection;
+ }
+
+ @Override
+ public boolean checkConnection() {
+ return connection != null;
+ }
+
+ @Override
+ public Connection getConnection() {
+ return connection;
+ }
+
+ @Override
+ public void closeConnection() {
+ if (connection != null) {
+ try {
+ connection.close();
+ } catch (SQLException e) {
+ System.out.println("ERROR: Error closing the MySQL Connection!");
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public ResultSet querySQL(String query) {
+ Connection c = null;
+
+ if (checkConnection()) {
+ c = getConnection();
+ } else {
+ c = openConnection();
+ }
+
+ Statement s = null;
+
+ try {
+ s = c.createStatement();
+ } catch (SQLException e1) {
+ e1.printStackTrace();
+ }
+
+ ResultSet ret = null;
+
+ try {
+ ret = s.executeQuery(query);
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+
+ closeConnection();
+
+ return ret;
+ }
+
+ public void updateSQL(String update) {
+
+ Connection c = null;
+
+ if (checkConnection()) {
+ c = getConnection();
+ } else {
+ c = openConnection();
+ }
+
+ Statement s = null;
+
+ try {
+ s = c.createStatement();
+ s.executeUpdate(update);
+ } catch (SQLException e1) {
+ e1.printStackTrace();
+ }
+
+ closeConnection();
+
+ }
+
+} \ No newline at end of file