diff --git a/src/main/java/com/grubnest/game/core/PluginMessage.java b/src/main/java/com/grubnest/game/core/PluginMessage.java index 157b392..e1f76fa 100644 --- a/src/main/java/com/grubnest/game/core/PluginMessage.java +++ b/src/main/java/com/grubnest/game/core/PluginMessage.java @@ -3,6 +3,7 @@ import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; +import com.grubnest.game.core.paper.GrubnestCorePlugin; import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.plugin.messaging.PluginMessageListener; diff --git a/src/main/java/com/grubnest/game/core/bridge/entities/GrubnestPlayer.java b/src/main/java/com/grubnest/game/core/bridge/entities/GrubnestPlayer.java new file mode 100644 index 0000000..8c19c5c --- /dev/null +++ b/src/main/java/com/grubnest/game/core/bridge/entities/GrubnestPlayer.java @@ -0,0 +1,7 @@ +package com.grubnest.game.core.bridge.entities; + +public interface GrubnestPlayer { + + void updateUsername(); + +} diff --git a/src/main/java/com/grubnest/game/core/databasehandler/MySQL.java b/src/main/java/com/grubnest/game/core/databasehandler/MySQL.java index d6a526f..068df00 100644 --- a/src/main/java/com/grubnest/game/core/databasehandler/MySQL.java +++ b/src/main/java/com/grubnest/game/core/databasehandler/MySQL.java @@ -1,6 +1,7 @@ package com.grubnest.game.core.databasehandler; -import java.sql.Connection; +import com.velocitypowered.api.proxy.Player; + import java.sql.PreparedStatement; import java.sql.SQLException; @@ -20,8 +21,6 @@ public MySQL(MySQLData data) { super(data); } - //You make method to fetch / add data in this class - /** * Close pool on plugin Disable */ @@ -29,10 +28,12 @@ public void onDisable() { closePool(); } + /** + * Creates any tables required for the database. Runs at proxy server initialization + */ public void createTables() { try { - Connection connection = getConnection(); - PreparedStatement statement = connection.prepareStatement(""" + PreparedStatement statement = getConnection().prepareStatement(""" CREATE TABLE IF NOT EXISTS `player` ( uuid varchar(36) PRIMARY KEY, username varchar(16) @@ -40,10 +41,33 @@ username varchar(16) """); statement.executeUpdate(); statement.close(); - connection.close(); } catch (SQLException e) { e.printStackTrace(); } } + /** + * Updates a players username stored in the database + * + * @param player the player to store + */ + public void updatePlayerUsername(Player player) { + try { + PreparedStatement statement = getConnection().prepareStatement(""" + INSERT INTO player + (uuid, username) + VALUES + (?, ?) + ON DUPLICATE KEY UPDATE + username = ?; + """); + statement.setString(1, player.getUniqueId().toString()); + statement.setString(2, player.getUsername()); + statement.setString(3, player.getUsername()); + statement.executeUpdate(); + statement.close(); + } 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 index ed66154..beadfb5 100644 --- a/src/main/java/com/grubnest/game/core/databasehandler/MySQLData.java +++ b/src/main/java/com/grubnest/game/core/databasehandler/MySQLData.java @@ -1,10 +1,15 @@ package com.grubnest.game.core.databasehandler; +import org.yaml.snakeyaml.Yaml; + +import java.io.InputStream; +import java.util.Map; + /** * MysqlData object to store credentials etc * - * @author tamilpp25 - * @version 1.0 at 15-5-2022 + * @author tamilpp25, Theeef + * @version 1.0 at 5/26/2022 */ public class MySQLData { public final String HOST; @@ -27,4 +32,28 @@ public MySQLData(String host, String username, String password, int port, String this.maximumConnections = maximumConnections; this.connectionTimeout = connectionTimeout; } + + /** + * Initialize data from config.yml + * + * @return MySQLData + */ + public static MySQLData dataInitializer() { + Yaml yaml = new Yaml(); + InputStream inputStream = MySQLData.class.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); + } } diff --git a/src/main/java/com/grubnest/game/core/GrubnestCorePlugin.java b/src/main/java/com/grubnest/game/core/paper/GrubnestCorePlugin.java similarity index 64% rename from src/main/java/com/grubnest/game/core/GrubnestCorePlugin.java rename to src/main/java/com/grubnest/game/core/paper/GrubnestCorePlugin.java index 6f140c3..de7eaa8 100644 --- a/src/main/java/com/grubnest/game/core/GrubnestCorePlugin.java +++ b/src/main/java/com/grubnest/game/core/paper/GrubnestCorePlugin.java @@ -1,5 +1,6 @@ -package com.grubnest.game.core; +package com.grubnest.game.core.paper; +import com.grubnest.game.core.PluginMessage; import com.grubnest.game.core.databasehandler.MySQL; import com.grubnest.game.core.databasehandler.MySQLData; import com.grubnest.game.core.databasehandler.utils.Disabler; @@ -25,7 +26,7 @@ public void onEnable() { getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "GrubnestCore is Enabled"); loadConfig(); - this.sql = new MySQL(dataInitializer()); + this.sql = new MySQL(MySQLData.dataInitializer()); } /** @@ -47,24 +48,6 @@ public void onDisable() { this.getServer().getMessenger().unregisterIncomingPluginChannel(this); } - /** - * Initialize data from config.yml - * - * @return MySQLData - */ - private MySQLData dataInitializer() { - String host = getConfig().getString("Database.hostname"); - int port = getConfig().getInt("Database.port"); - String database = getConfig().getString("Database.database"); - String username = getConfig().getString("Database.username"); - String password = getConfig().getString("Database.password"); - - int minimumConnections = getConfig().getInt("Database.minimumConnections"); - int maximumConnections = getConfig().getInt("Database.maximumConnections"); - long connectionTimeout = getConfig().getLong("Database.connectionTimeout"); - return new MySQLData(host, username, password, port, database, minimumConnections, maximumConnections, connectionTimeout); - } - /** * Get SQL Object * diff --git a/src/main/java/com/grubnest/game/core/velocity/VelocityPlugin.java b/src/main/java/com/grubnest/game/core/velocity/VelocityPlugin.java index 1cec789..c7dd5a0 100644 --- a/src/main/java/com/grubnest/game/core/velocity/VelocityPlugin.java +++ b/src/main/java/com/grubnest/game/core/velocity/VelocityPlugin.java @@ -21,7 +21,7 @@ * running on the Grubnest network * * @author Theeef - * @version 1.1 at 5/23/2022 + * @version 1.2 at 5/26/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"}) @@ -43,11 +43,16 @@ public VelocityPlugin(ProxyServer server, Logger logger) { this.server = server; this.server.sendMessage(Component.text("GrubnestCore is enabled on Velocity!")); - this.sql = new MySQL(dataInitializer()); + this.sql = new MySQL(MySQLData.dataInitializer()); instance = this; } + /** + * Runs when the Proxy server initializes + * + * @param event event used in callback + */ @Subscribe public void onInitialize(ProxyInitializeEvent event) { this.server.getEventManager().register(this, new CoreEventListener()); @@ -55,30 +60,6 @@ public void onInitialize(ProxyInitializeEvent event) { 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 * diff --git a/src/main/java/com/grubnest/game/core/velocity/entities/VPlayer.java b/src/main/java/com/grubnest/game/core/velocity/entities/VPlayer.java new file mode 100644 index 0000000..0371918 --- /dev/null +++ b/src/main/java/com/grubnest/game/core/velocity/entities/VPlayer.java @@ -0,0 +1,33 @@ +package com.grubnest.game.core.velocity.entities; + +import com.grubnest.game.core.bridge.entities.GrubnestPlayer; +import com.grubnest.game.core.velocity.VelocityPlugin; +import com.velocitypowered.api.proxy.Player; + +/** + * Represents a custom player entity on the Velocity server + * + * @author Theeef + * @version 1.0 at 5/26/2022 + */ +public class VPlayer implements GrubnestPlayer { + + private Player player; + + /** + * Creates a custom player instance for Velocity + * + * @param player the player this represents + */ + public VPlayer(Player player) { + this.player = player; + } + + /** + * Updates the cached username of this player in the database + */ + public void updateUsername() { + VelocityPlugin.getInstance().getMySQL().updatePlayerUsername(this.player); + } + +} 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 index a8ab741..a82996a 100644 --- a/src/main/java/com/grubnest/game/core/velocity/events/CoreEventListener.java +++ b/src/main/java/com/grubnest/game/core/velocity/events/CoreEventListener.java @@ -1,13 +1,8 @@ package com.grubnest.game.core.velocity.events; -import com.grubnest.game.core.velocity.VelocityPlugin; +import com.grubnest.game.core.velocity.entities.VPlayer; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.player.ServerConnectedEvent; -import net.kyori.adventure.text.Component; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; /** * Listens for core events, like Server Connection for Velocity @@ -26,27 +21,8 @@ public class CoreEventListener { */ @Subscribe public void onServerConnect(ServerConnectedEvent event) { - try { - Connection connection = VelocityPlugin.getInstance().getMySQL().getConnection(); - PreparedStatement statement = connection.prepareStatement(""" - INSERT INTO player - (uuid, username) - VALUES - (?, ?) - ON DUPLICATE KEY UPDATE - username = ?; - """); - statement.setString(1, event.getPlayer().getUniqueId().toString()); - statement.setString(2, event.getPlayer().getUsername()); - statement.setString(3, event.getPlayer().getUsername()); - statement.executeUpdate(); - statement.close(); - connection.close(); - } catch (SQLException throwables) { - throwables.printStackTrace(); - } - - + VPlayer player = new VPlayer(event.getPlayer()); + player.updateUsername(); } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 9ed2e37..ac3f33d 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: GrubnestCore -main: com.grubnest.game.core.GrubnestCorePlugin +main: com.grubnest.game.core.paper.GrubnestCorePlugin version: 1.0 authors: - Theeef