diff --git a/.gitignore b/.gitignore index 25cfc14..9a2c893 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /.idea/ /target/ /*.iml +/dependency-reduced-pom.xml diff --git a/pom.xml b/pom.xml index 504e1ce..c4991fe 100644 --- a/pom.xml +++ b/pom.xml @@ -52,6 +52,20 @@ + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + @@ -93,5 +107,10 @@ 5.0.1 compile + + mysql + mysql-connector-java + 8.0.29 + \ No newline at end of file diff --git a/src/main/java/com/grubnest/game/core/DatabaseHandler/ConnectionPoolManager.java b/src/main/java/com/grubnest/game/core/DatabaseHandler/ConnectionPoolManager.java deleted file mode 100644 index cfe689a..0000000 --- a/src/main/java/com/grubnest/game/core/DatabaseHandler/ConnectionPoolManager.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.grubnest.game.core.DatabaseHandler; - -import com.grubnest.game.core.DatabaseHandler.Utils.Deactivated; -import com.grubnest.game.core.DatabaseHandler.Utils.Disabler; -import com.zaxxer.hikari.HikariConfig; -import com.zaxxer.hikari.HikariDataSource; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; - -/** Handles connection from mysql and uses HikariCP - * @author tamilpp25 - * @version 1.0 at 15-5-2022 - */ -public class ConnectionPoolManager implements Deactivated { - private final MySQLData data; - private HikariDataSource dataSource; - - - /** - * Construct mysql HikariCP - * @param data MysqlDATA - */ - public ConnectionPoolManager(MySQLData data) { - this.data = data; - setupPool(); - Disabler.getInstance().registerDeactivated(this); - } - - /** - * Setup mysql connection Pool with HikariCP to get multiple connections. - */ - private void setupPool() { - HikariConfig config = new HikariConfig(); - config.setJdbcUrl( - "jdbc:mysql://" + - data.HOST + - ":" + - data.PORT+ - "/" + - data.DATABASE - ); - config.setDriverClassName("com.mysql.jdbc.Driver"); - config.setUsername(data.USERNAME); - config.setPassword(data.PASSWORD); - config.setMinimumIdle(data.minimumConnections); - config.setMaximumPoolSize(data.maximumConnections); - config.setConnectionTimeout(data.connectionTimeout); - config.setConnectionTestQuery("SELECT 1"); - dataSource = new HikariDataSource(config); - } - - /** - * Get MySQL Pool connection - * @return Sql Connection - * @throws SQLException if some error.. - */ - public Connection getConnection() throws SQLException { - return dataSource.getConnection(); - } - - /** - * Temp close method can close using try-with statement too - * Recommending using try-with statement to close automatically! - * @param conn Connection - * @param ps PreparedStatement - * @param res ResultSet - */ - public void close(Connection conn, PreparedStatement ps, ResultSet res) { - if (conn != null) try { conn.close(); } catch (SQLException ignored) {} - if (ps != null) try { ps.close(); } catch (SQLException ignored) {} - if (res != null) try { res.close(); } catch (SQLException ignored) {} - } - - /** - * Close pool when everything done... - */ - public void closePool() { - if (dataSource != null && !dataSource.isClosed()) { - dataSource.close(); - } - } - - /** - * Auto disable classes on disable if multiple instance are there too - */ - @Override - public void onDisable() { - closePool(); - } -} diff --git a/src/main/java/com/grubnest/game/core/DatabaseHandler/MySQL.java b/src/main/java/com/grubnest/game/core/DatabaseHandler/MySQL.java deleted file mode 100644 index 5212c0e..0000000 --- a/src/main/java/com/grubnest/game/core/DatabaseHandler/MySQL.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.grubnest.game.core.DatabaseHandler; - -import com.grubnest.game.core.GrubnestCorePlugin; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; - -/** - * Main class for handling mysql data - * make your methods for getting / setting here in async! - * new MySQL class can be initialized outside main class too! - * - * Recommend using getMySQL method from main class as it properly does the work and closes connection on disable! - * make a new instance of this if you really know what you are doing! - * - * @author tamilpp25 - * @version 1.0 at 15-5-2022 - */ -public class MySQL extends ConnectionPoolManager { - public MySQL(MySQLData data) { - super(data); - } - - /** - * This is an example table query for creating queries make a similar method like this! - * MAKE SURE that all queries are run async so that it doesn't freeze the main thread - * you don't have to worry about closing a connection since its auto closed by - */ - public void testTableQuery() { - GrubnestCorePlugin.getInstance().getServer().getScheduler().runTaskAsynchronously(GrubnestCorePlugin.getInstance(),()->{ - try (Connection conn = getConnection()){ - PreparedStatement statement = conn.prepareStatement( - "CREATE TABLE IF NOT EXISTS `Test` " + - "(" + - "UUID varchar(30)" + - ")" - ); - statement.executeUpdate(); - } catch (SQLException e) { - e.printStackTrace(); - } - }); - } - - //You make method to fetch / add data in this class - - /** - * Close pool on plugin Disable - */ - public void onDisable(){ - closePool(); - } - -} diff --git a/src/main/java/com/grubnest/game/core/DatabaseHandler/MySQLData.java b/src/main/java/com/grubnest/game/core/DatabaseHandler/MySQLData.java deleted file mode 100644 index 93eead0..0000000 --- a/src/main/java/com/grubnest/game/core/DatabaseHandler/MySQLData.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.grubnest.game.core.DatabaseHandler; - -/** MysqlData object to store credentials etc - * @author tamilpp25 - * @version 1.0 at 15-5-2022 - */ -public class MySQLData { - public final String HOST; - public final String USERNAME; - public final String PASSWORD; - public final String PORT; - public final String DATABASE; - - public final int minimumConnections; - public final int maximumConnections; - public final long connectionTimeout; - - public MySQLData(String host, String username, String password, String port, String database, int minimumConnections, int maximumConnections, long connectionTimeout) { - HOST = host; - USERNAME = username; - PASSWORD = password; - PORT = port; - DATABASE = database; - this.minimumConnections = minimumConnections; - this.maximumConnections = maximumConnections; - this.connectionTimeout = connectionTimeout; - } -} diff --git a/src/main/java/com/grubnest/game/core/GrubnestCorePlugin.java b/src/main/java/com/grubnest/game/core/GrubnestCorePlugin.java index bfa8b42..6f140c3 100644 --- a/src/main/java/com/grubnest/game/core/GrubnestCorePlugin.java +++ b/src/main/java/com/grubnest/game/core/GrubnestCorePlugin.java @@ -1,8 +1,9 @@ package com.grubnest.game.core; -import com.grubnest.game.core.DatabaseHandler.MySQL; -import com.grubnest.game.core.DatabaseHandler.MySQLData; -import com.grubnest.game.core.DatabaseHandler.Utils.Disabler; +import com.grubnest.game.core.databasehandler.MySQL; +import com.grubnest.game.core.databasehandler.MySQLData; +import com.grubnest.game.core.databasehandler.utils.Disabler; +import org.bukkit.ChatColor; import org.bukkit.plugin.java.JavaPlugin; public class GrubnestCorePlugin extends JavaPlugin { @@ -21,9 +22,18 @@ public void onEnable() { this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord"); this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new PluginMessage()); + getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "GrubnestCore is Enabled"); + + loadConfig(); + this.sql = new MySQL(dataInitializer()); + } + + /** + * Loads the config and enables copying defaults + */ + public void loadConfig() { getConfig().options().copyDefaults(true); saveConfig(); - sql = new MySQL(dataInitializer()); } /** @@ -44,7 +54,7 @@ public void onDisable() { */ private MySQLData dataInitializer() { String host = getConfig().getString("Database.hostname"); - String port = getConfig().getString("Database.port"); + int port = getConfig().getInt("Database.port"); String database = getConfig().getString("Database.database"); String username = getConfig().getString("Database.username"); String password = getConfig().getString("Database.password"); @@ -72,4 +82,5 @@ public MySQL getMySQL() { public static GrubnestCorePlugin getInstance() { return instance; } + } diff --git a/src/main/java/com/grubnest/game/core/Velocity/VelocityPlugin.java b/src/main/java/com/grubnest/game/core/Velocity/VelocityPlugin.java deleted file mode 100644 index 026cfc0..0000000 --- a/src/main/java/com/grubnest/game/core/Velocity/VelocityPlugin.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.grubnest.game.core.Velocity; - -import com.google.inject.Inject; -import com.velocitypowered.api.plugin.Plugin; -import com.velocitypowered.api.proxy.ProxyServer; -import net.kyori.adventure.text.Component; -import org.slf4j.Logger; - -/** - * The VelocityPlugin class is an implementation of the Velocity API. - * It provides communication to and from the Velocity Proxy server - * running on the Grubnest network - *

- * Date: 5/20/2022 - * Authors: Theeef - */ -@Plugin(id = "grubnestcore", name = "Grubnest Core Plugin", version = "0.1.0-SNAPSHOT", - url = "htts://grubnest.com", description = "Grubnest Core running on Velocity", authors = {"Theeef"}) -public class VelocityPlugin { - - private final ProxyServer server; - private final Logger logger; - - - /** - * Creates an instance of the Velocity Plugin and injects it - * - * @param server The velocity proxy server - * @param logger The proxy server's logger - */ - @Inject - public VelocityPlugin(ProxyServer server, Logger logger) { - this.server = server; - this.logger = logger; - - this.server.sendMessage(Component.text("GrubnestCore is enabled on Velocity!")); - } - -} diff --git a/src/main/java/com/grubnest/game/core/databasehandler/ConnectionPoolManager.java b/src/main/java/com/grubnest/game/core/databasehandler/ConnectionPoolManager.java new file mode 100644 index 0000000..befe4f1 --- /dev/null +++ b/src/main/java/com/grubnest/game/core/databasehandler/ConnectionPoolManager.java @@ -0,0 +1,108 @@ +package com.grubnest.game.core.databasehandler; + +import com.grubnest.game.core.databasehandler.utils.Deactivated; +import com.grubnest.game.core.databasehandler.utils.Disabler; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * Handles connection from mysql and uses HikariCP + * + * @author tamilpp25 + * @version 1.0 at 15-5-2022 + */ +public class ConnectionPoolManager implements Deactivated { + + private final MySQLData data; + private HikariDataSource dataSource; + + /** + * Construct mysql HikariCP + * + * @param data MysqlDATA + */ + public ConnectionPoolManager(MySQLData data) { + this.data = data; + setupPool(); + Disabler.getInstance().registerDeactivated(this); + } + + /** + * Setup mysql connection Pool with HikariCP to get multiple connections. + */ + private void setupPool() { + HikariConfig config = new HikariConfig(); + config.setJdbcUrl( + "jdbc:mysql://" + + data.HOST + + ":" + + data.PORT + + "/" + + data.DATABASE + ); + + config.setDriverClassName("com.mysql.cj.jdbc.Driver"); + config.setUsername(data.USERNAME); + config.setPassword(data.PASSWORD); + config.setMinimumIdle(data.minimumConnections); + config.setMaximumPoolSize(data.maximumConnections); + config.setConnectionTimeout(data.connectionTimeout); + config.setConnectionTestQuery("SELECT 1"); + dataSource = new HikariDataSource(config); + } + + /** + * Get MySQL Pool connection + * + * @return Sql Connection + * @throws SQLException if some error.. + */ + public Connection getConnection() throws SQLException { + return dataSource.getConnection(); + } + + /** + * Temp close method can close using try-with statement too + * Recommending using try-with statement to close automatically! + * + * @param conn Connection + * @param ps PreparedStatement + * @param res ResultSet + */ + public void close(Connection conn, PreparedStatement ps, ResultSet res) { + if (conn != null) try { + conn.close(); + } catch (SQLException ignored) { + } + if (ps != null) try { + ps.close(); + } catch (SQLException ignored) { + } + if (res != null) try { + res.close(); + } catch (SQLException ignored) { + } + } + + /** + * Close pool when everything done... + */ + public void closePool() { + if (dataSource != null && !dataSource.isClosed()) { + dataSource.close(); + } + } + + /** + * Auto disable classes on disable if multiple instance are there too + */ + @Override + public void onDisable() { + closePool(); + } +} diff --git a/src/main/java/com/grubnest/game/core/databasehandler/MySQL.java b/src/main/java/com/grubnest/game/core/databasehandler/MySQL.java new file mode 100644 index 0000000..f946403 --- /dev/null +++ b/src/main/java/com/grubnest/game/core/databasehandler/MySQL.java @@ -0,0 +1,45 @@ +package com.grubnest.game.core.databasehandler; + +import java.sql.PreparedStatement; +import java.sql.SQLException; + +/** + * Main class for handling mysql data + * make your methods for getting / setting here in async! + * new MySQL class can be initialized outside main class too! + *

+ * Recommend using getMySQL method from main class as it properly does the work and closes connection on disable! + * make a new instance of this if you really know what you are doing! + * + * @author tamilpp25 + * @version 1.0 at 15-5-2022 + */ +public class MySQL extends ConnectionPoolManager { + public MySQL(MySQLData data) { + super(data); + } + + //You make method to fetch / add data in this class + + /** + * Close pool on plugin Disable + */ + public void onDisable() { + closePool(); + } + + public void createTables() { + try { + PreparedStatement statement = getConnection().prepareStatement(""" + CREATE TABLE IF NOT EXISTS `player` ( + uuid varchar(36) PRIMARY KEY, + username varchar(16) + ) + """); + statement.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + +} diff --git a/src/main/java/com/grubnest/game/core/databasehandler/MySQLData.java b/src/main/java/com/grubnest/game/core/databasehandler/MySQLData.java new file mode 100644 index 0000000..ed66154 --- /dev/null +++ b/src/main/java/com/grubnest/game/core/databasehandler/MySQLData.java @@ -0,0 +1,30 @@ +package com.grubnest.game.core.databasehandler; + +/** + * MysqlData object to store credentials etc + * + * @author tamilpp25 + * @version 1.0 at 15-5-2022 + */ +public class MySQLData { + public final String HOST; + public final String USERNAME; + public final String PASSWORD; + public final int PORT; + public final String DATABASE; + + public final int minimumConnections; + public final int maximumConnections; + public final long connectionTimeout; + + public MySQLData(String host, String username, String password, int port, String database, int minimumConnections, int maximumConnections, long connectionTimeout) { + HOST = host; + USERNAME = username; + PASSWORD = password; + PORT = port; + DATABASE = database; + this.minimumConnections = minimumConnections; + this.maximumConnections = maximumConnections; + this.connectionTimeout = connectionTimeout; + } +} diff --git a/src/main/java/com/grubnest/game/core/DatabaseHandler/Utils/Deactivated.java b/src/main/java/com/grubnest/game/core/databasehandler/utils/Deactivated.java similarity index 74% rename from src/main/java/com/grubnest/game/core/DatabaseHandler/Utils/Deactivated.java rename to src/main/java/com/grubnest/game/core/databasehandler/utils/Deactivated.java index 007acdb..332901c 100644 --- a/src/main/java/com/grubnest/game/core/DatabaseHandler/Utils/Deactivated.java +++ b/src/main/java/com/grubnest/game/core/databasehandler/utils/Deactivated.java @@ -1,4 +1,4 @@ -package com.grubnest.game.core.DatabaseHandler.Utils; +package com.grubnest.game.core.databasehandler.utils; /** * Utility class to prevent Memory leak. diff --git a/src/main/java/com/grubnest/game/core/DatabaseHandler/Utils/Disabler.java b/src/main/java/com/grubnest/game/core/databasehandler/utils/Disabler.java similarity index 94% rename from src/main/java/com/grubnest/game/core/DatabaseHandler/Utils/Disabler.java rename to src/main/java/com/grubnest/game/core/databasehandler/utils/Disabler.java index c15c82a..e248f40 100644 --- a/src/main/java/com/grubnest/game/core/DatabaseHandler/Utils/Disabler.java +++ b/src/main/java/com/grubnest/game/core/databasehandler/utils/Disabler.java @@ -1,4 +1,4 @@ -package com.grubnest.game.core.DatabaseHandler.Utils; +package com.grubnest.game.core.databasehandler.utils; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/grubnest/game/core/velocity/VelocityPlugin.java b/src/main/java/com/grubnest/game/core/velocity/VelocityPlugin.java new file mode 100644 index 0000000..1cec789 --- /dev/null +++ b/src/main/java/com/grubnest/game/core/velocity/VelocityPlugin.java @@ -0,0 +1,108 @@ +package com.grubnest.game.core.velocity; + +import com.google.inject.Inject; +import com.grubnest.game.core.databasehandler.MySQL; +import com.grubnest.game.core.databasehandler.MySQLData; +import com.grubnest.game.core.velocity.events.CoreEventListener; +import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; +import com.velocitypowered.api.plugin.Plugin; +import com.velocitypowered.api.proxy.ProxyServer; +import net.kyori.adventure.text.Component; +import org.slf4j.Logger; +import org.yaml.snakeyaml.Yaml; + +import java.io.InputStream; +import java.util.Map; + +/** + * The VelocityPlugin class is an implementation of the Velocity API. + * It provides communication to and from the Velocity Proxy server + * running on the Grubnest network + * + * @author Theeef + * @version 1.1 at 5/23/2022 + */ +@Plugin(id = "grubnestcore", name = "Grubnest Core Plugin", version = "0.1.0-SNAPSHOT", + url = "htts://grubnest.com", description = "Grubnest Core running on Velocity", authors = {"Theeef"}) +public class VelocityPlugin { + + private final ProxyServer server; + private final MySQL sql; + private static VelocityPlugin instance; + + + /** + * Creates an instance of the Velocity Plugin and injects it + * + * @param server The velocity proxy server + * @param logger The proxy server's logger + */ + @Inject + public VelocityPlugin(ProxyServer server, Logger logger) { + this.server = server; + + this.server.sendMessage(Component.text("GrubnestCore is enabled on Velocity!")); + this.sql = new MySQL(dataInitializer()); + + instance = this; + } + + @Subscribe + public void onInitialize(ProxyInitializeEvent event) { + this.server.getEventManager().register(this, new CoreEventListener()); + + getMySQL().createTables(); + } + + /** + * Initialize data from config.yml + * + * @return MySQLData + */ + private MySQLData dataInitializer() { + Yaml yaml = new Yaml(); + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config.yml"); + Map config; + config = (Map) ((Map) yaml.load(inputStream)).get("Database"); + + String host = (String) config.get("hostname"); + int port = (int) config.get("port"); + String database = (String) config.get("database"); + String username = (String) config.get("username"); + String password = (String) config.get("password"); + + int minimumConnections = (int) config.get("minimumConnections"); + int maximumConnections = (int) config.get("maximumConnections"); + long connectionTimeout = (long) (int) config.get("connectionTimeout"); + + return new MySQLData(host, username, password, port, database, minimumConnections, maximumConnections, connectionTimeout); + } + + /** + * Get SQL Object + * + * @return SQL object + */ + public MySQL getMySQL() { + return this.sql; + } + + /** + * Get the ProxyServer object + * + * @return ProxyServer object + */ + public ProxyServer getServer() { + return this.server; + } + + /** + * Get Plugin Instance + * + * @return Plugin Instance + */ + public static VelocityPlugin getInstance() { + return instance; + } +} diff --git a/src/main/java/com/grubnest/game/core/velocity/events/CoreEventListener.java b/src/main/java/com/grubnest/game/core/velocity/events/CoreEventListener.java new file mode 100644 index 0000000..cf14e79 --- /dev/null +++ b/src/main/java/com/grubnest/game/core/velocity/events/CoreEventListener.java @@ -0,0 +1,49 @@ +package com.grubnest.game.core.velocity.events; + +import com.grubnest.game.core.velocity.VelocityPlugin; +import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.player.ServerConnectedEvent; +import net.kyori.adventure.text.Component; + +import java.sql.PreparedStatement; +import java.sql.SQLException; + +/** + * Listens for core events, like Server Connection for Velocity + *

+ * + * @author Theeef + * @version 1.0 at 5/23/2022 + */ +public class CoreEventListener { + + /** + * Logs basic user data to the server database when a user connects + * to a server on our network + * + * @param event The connection event that occurs + */ + @Subscribe + public void onServerConnect(ServerConnectedEvent event) { + VelocityPlugin.getInstance().getServer().sendMessage(Component.text("SERVER CONNECTION EVENT FIRED FOR: " + event.getPlayer().getUsername())); + String query = """ + INSERT INTO player + (uuid, username) + VALUES + ("%uuid%", "%username%") + ON DUPLICATE KEY UPDATE + username = "%username%"; + """; + query = query.replaceAll("%username%", event.getPlayer().getUsername()).replaceAll("%uuid%", event.getPlayer().getUniqueId().toString()); + + try { + PreparedStatement statement = VelocityPlugin.getInstance().getMySQL().getConnection().prepareStatement(query); + statement.executeUpdate(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + + + } + +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 0ca59d5..866e8db 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,10 +1,10 @@ # DEFAULT VALUES FOR MYSQL DATABASE update later!! Database: - hostname: localhost + hostname: grubnest.com port: 3306 - database: grubnest - username: root - password: 1234 + database: s4_grubnest + username: u4_xb6o263UWu + password: Sf^R4vzWq6nKl!av!u+JTo81 minimumConnections: 5 maximumConnections: 10 connectionTimeout: 5000 \ No newline at end of file