From eebf45d010eb35d274dc5a2906b9e190f399561d Mon Sep 17 00:00:00 2001 From: ryzech Date: Sat, 8 Jan 2022 23:00:39 -0600 Subject: [PATCH] initial mysql setup --- smpcore-bukkit/build.gradle.kts | 4 + .../net/ryzech/smpcore/SmpCorePlugin.java | 9 ++ .../smpcore/commands/everyone/report.java | 13 +++ .../java/net/ryzech/smpcore/util/MySQL.java | 92 +++++++++++++++++++ smpcore-bukkit/src/main/resources/config.yml | 9 +- 5 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 smpcore-bukkit/src/main/java/net/ryzech/smpcore/util/MySQL.java diff --git a/smpcore-bukkit/build.gradle.kts b/smpcore-bukkit/build.gradle.kts index f875636..9fbdbf4 100644 --- a/smpcore-bukkit/build.gradle.kts +++ b/smpcore-bukkit/build.gradle.kts @@ -25,7 +25,11 @@ dependencies { implementation("org.yaml:snakeyaml:1.30") implementation("org.bstats:bstats-bukkit:2.2.1") implementation("com.github.DV8FromTheWorld:JDA:v5.0.0-alpha.3") +<<<<<<< HEAD implementation("net.kyori:adventure-text-minimessage:4.1.0-SNAPSHOT") +======= + implementation("com.zaxxer:HikariCP:5.0.0") +>>>>>>> 24e0f72 (initial mysql setup) compileOnly("io.papermc.paper:paper-api:1.18.1-R0.1-SNAPSHOT") compileOnly("com.gmail.filoghost.holographicdisplays:holographicdisplays-api:2.4.9") compileOnly("me.clip:placeholderapi:2.11.1") diff --git a/smpcore-bukkit/src/main/java/net/ryzech/smpcore/SmpCorePlugin.java b/smpcore-bukkit/src/main/java/net/ryzech/smpcore/SmpCorePlugin.java index 9d19e2b..7e3ccf3 100644 --- a/smpcore-bukkit/src/main/java/net/ryzech/smpcore/SmpCorePlugin.java +++ b/smpcore-bukkit/src/main/java/net/ryzech/smpcore/SmpCorePlugin.java @@ -13,6 +13,7 @@ import net.ryzech.smpcore.managers.HologramManager; import net.ryzech.smpcore.managers.barManager; import net.ryzech.smpcore.util.FileUtils; +import net.ryzech.smpcore.util.MySQL; import net.ryzech.smpcore.util.SmpCoreApi; import net.milkbowl.vault.chat.Chat; import org.bukkit.*; @@ -38,6 +39,7 @@ import org.bukkit.potion.PotionEffectType; import java.io.File; +import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.logging.Logger; @@ -88,6 +90,13 @@ public void onEnable() { PluginDescriptionFile pdf = this.getDescription(); // gets the plugin.yml file log = this.getLogger(); new report(this); + new MySQL(this); + try { + MySQL.connect(); + if(MySQL.update("CREATE TABLE `smpcore_reports` ( `uuid` CHAR(36) NOT NULL, `report_id` INT NOT NULL AUTO_INCREMENT primary key, `report_message` TINYTEXT, `timestamp` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP );")); + } catch (SQLException e) { + e.printStackTrace(); + } // commandExecutor SmpCoreCommandExecutor commandExecutor = new SmpCoreCommandExecutor(this); diff --git a/smpcore-bukkit/src/main/java/net/ryzech/smpcore/commands/everyone/report.java b/smpcore-bukkit/src/main/java/net/ryzech/smpcore/commands/everyone/report.java index c5bcb0a..8865819 100644 --- a/smpcore-bukkit/src/main/java/net/ryzech/smpcore/commands/everyone/report.java +++ b/smpcore-bukkit/src/main/java/net/ryzech/smpcore/commands/everyone/report.java @@ -8,6 +8,7 @@ import net.kyori.adventure.text.format.NamedTextColor; import net.ryzech.smpcore.SmpCorePlugin; import net.ryzech.smpcore.util.FileUtils; +import net.ryzech.smpcore.util.MySQL; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -17,6 +18,9 @@ import javax.security.auth.login.LoginException; import java.lang.reflect.Array; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; import java.util.Arrays; import java.util.List; import java.util.Objects; @@ -66,6 +70,15 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command true); modLog.sendMessageEmbeds(eb.build()).queue(); player.sendMessage(Component.text("You reported " + reported.getName() + " for: " + msgContent, NamedTextColor.DARK_AQUA)); + try (Connection conn = MySQL.getConnection(); PreparedStatement stmt = conn.prepareStatement( + "INSERT INTO smpcore_reports(uuid, report_message) VALUES(?, ?);")) { + stmt.setString(1, player.getUniqueId().toString()); + stmt.setString(2, msgContent); + stmt.execute(); + return true; + } catch (SQLException e) { + e.printStackTrace(); + } } } diff --git a/smpcore-bukkit/src/main/java/net/ryzech/smpcore/util/MySQL.java b/smpcore-bukkit/src/main/java/net/ryzech/smpcore/util/MySQL.java new file mode 100644 index 0000000..70acca7 --- /dev/null +++ b/smpcore-bukkit/src/main/java/net/ryzech/smpcore/util/MySQL.java @@ -0,0 +1,92 @@ +package net.ryzech.smpcore.util; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import net.ryzech.smpcore.SmpCorePlugin; +import org.bukkit.Bukkit; + +public class MySQL { + + public static SmpCorePlugin plugin; + public FileUtils fileUtils; + + public MySQL(SmpCorePlugin plugin) {this.plugin = plugin;} + private static HikariConfig config = new HikariConfig(); + private static HikariDataSource ds; + + // connect + public static void connect() { + FileUtils fileUtils; + fileUtils = new FileUtils(plugin); + String host = fileUtils.getMain().getString("db.host"); + String port = fileUtils.getMain().getString("db.port"); + String database = fileUtils.getMain().getString("db.database"); + String username = fileUtils.getMain().getString("db.username"); + String password = fileUtils.getMain().getString("db.password"); + + config.setDriverClassName("com.mysql.cj.jdbc.Driver"); + config.setJdbcUrl("jdbc:mysql://" + host + ":" + port + "/" + database + "?serverTimezone=UTC"); + config.setUsername(username); + config.setPassword(password); + config.setMaximumPoolSize(fileUtils.getMain().getInt("db.connections")); + ds = new HikariDataSource( config ); + } + + // isConnected + public static boolean isConnected() { + return (!ds.isRunning() ? false : true); + } + + // getConnection + public static synchronized ResultSet query(String query) throws SQLException { + Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { + @Override + public void run() { + try { + ds.getConnection().prepareStatement(query).executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + }); + return null; + } + + public static synchronized boolean update(String query) throws SQLException { + Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { + @Override + public void run() { + try { + ds.getConnection().prepareStatement(query).execute(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + }); + return false; + } + + public static synchronized void close() throws SQLException { + Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { + @Override + public void run() { + if (!ds.isClosed()) { + try { + ds.getConnection().close(); + Bukkit.getLogger().info("SmpCore disconnected from the database."); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + }); + } + + public static synchronized Connection getConnection() throws SQLException { + return ds.getConnection(); + } +} \ No newline at end of file diff --git a/smpcore-bukkit/src/main/resources/config.yml b/smpcore-bukkit/src/main/resources/config.yml index 2b9aa27..f9c29e3 100644 --- a/smpcore-bukkit/src/main/resources/config.yml +++ b/smpcore-bukkit/src/main/resources/config.yml @@ -15,4 +15,11 @@ Stuck: pitch: 0 Bot: Token: "" - LogChannel: "" \ No newline at end of file + LogChannel: "" +db: + host: 127.0.0.1 + port: 3306 + database: database + username: root + password: password + connections: 10 \ No newline at end of file