Skip to content

Commit

Permalink
initial mysql setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzech committed Jan 9, 2022
1 parent 5cb85f5 commit eebf45d
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 1 deletion.
4 changes: 4 additions & 0 deletions smpcore-bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
}

}
Expand Down
92 changes: 92 additions & 0 deletions smpcore-bukkit/src/main/java/net/ryzech/smpcore/util/MySQL.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
9 changes: 8 additions & 1 deletion smpcore-bukkit/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ Stuck:
pitch: 0
Bot:
Token: ""
LogChannel: ""
LogChannel: ""
db:
host: 127.0.0.1
port: 3306
database: database
username: root
password: password
connections: 10

0 comments on commit eebf45d

Please sign in to comment.