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 @@
+
+
- * 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
+ *
+ * @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