From f01bb1fd9a5e1903878aa790e0f5505512031012 Mon Sep 17 00:00:00 2001 From: Pugmatt Date: Wed, 19 Jun 2019 23:54:13 -0400 Subject: Added MySQL --- serverlist-server/BedrockConnect.iml | 1 + serverlist-server/pom.xml | 7 ++ .../pugmatt/bedrockconnect/BedrockConnect.java | 62 +++++++++- .../bedrockconnect/listeners/PacketHandler.java | 6 + .../pugmatt/bedrockconnect/sql/Database.java | 40 ++++++ .../pyratron/pugmatt/bedrockconnect/sql/MySQL.java | 137 +++++++++++++++++++++ 6 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/sql/Database.java create mode 100644 serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/sql/MySQL.java (limited to 'serverlist-server') 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 @@ + \ 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 @@ 2.9.8 compile + + mysql + mysql-connector-java + 5.1.17 + jar + compile + 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 -- cgit v1.2.1