Major SQL & Plugin Message refactoring

This commit is contained in:
BuildTools authored and WiIIiam278 committed 2021-05-30 23:13:02 +01:00
1 parent 4b31b31a36
commit b3fd814c0c
18 files changed
+1245 -1452

No files matched your search

@@ -0,0 +1,61 @@
package me.william278.huskhomes2.data.SQL;
import me.william278.huskhomes2.HuskHomes;
import me.william278.huskhomes2.data.Error;
import me.william278.huskhomes2.teleport.points.Home;
import me.william278.huskhomes2.teleport.points.TeleportationPoint;
import me.william278.huskhomes2.teleport.points.Warp;
import org.bukkit.entity.Player;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.UUID;
import java.util.logging.Level;
/*
TODO Should not relay on SQL
Instead should contain abstract getters and setters etc, so some alternative way of storing data may exist
Current method should possibly go into the SQL class
*/
public abstract class Database {
protected HuskHomes plugin;
private Connection connection;
public Database(HuskHomes instance) {
plugin = instance;
}
public abstract Connection getConnection();
public abstract void load();
public void initialize() {
connection = getConnection();
// Test the retrieved connection; throw an error if it fails
try {
PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + ";");
ResultSet rs = ps.executeQuery();
close(ps, rs);
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, "Failed to retrieve Database connection: ", ex);
}
}
// Close the mySQL connection
public void close(PreparedStatement ps, ResultSet rs) {
try {
if (ps != null)
ps.close();
if (rs != null)
rs.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, "Failed to close the Database connection: ", ex);
}
}
}
@@ -0,0 +1,112 @@
package me.william278.huskhomes2.data.SQL;
import me.william278.huskhomes2.HuskHomes;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
public class MySQL extends Database {
final static String[] SQL_SETUP_STATEMENTS = {
"CREATE TABLE IF NOT EXISTS " + HuskHomes.getSettings().getPlayerDataTable() + " (" +
"`player_id` integer AUTO_INCREMENT," +
"`user_uuid` char(36) NOT NULL UNIQUE," +
"`username` varchar(16) NOT NULL," +
"`home_slots` integer NOT NULL," +
"`rtp_cooldown` integer NOT NULL DEFAULT 0," +
"`is_teleporting` boolean NOT NULL DEFAULT 0," +
"`dest_location_id` integer NULL," +
"`last_location_id` integer NULL," +
"PRIMARY KEY (`player_id`)," +
"FOREIGN KEY (`dest_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION," +
"FOREIGN KEY (`last_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION" +
");",
"CREATE TABLE IF NOT EXISTS " + HuskHomes.getSettings().getLocationsDataTable() + " (" +
"`location_id` integer AUTO_INCREMENT," +
"`server` text NOT NULL," +
"`world` text NOT NULL," +
"`x` double NOT NULL," +
"`y` double NOT NULL," +
"`z` double NOT NULL," +
"`yaw` float NOT NULL," +
"`pitch` float NOT NULL," +
"PRIMARY KEY (`location_id`)" +
");",
"CREATE TABLE IF NOT EXISTS " + HuskHomes.getSettings().getHomesDataTable() + " (" +
"`player_id` integer NOT NULL," +
"`location_id` integer NOT NULL," +
"`name` varchar(16) NOT NULL," +
"`description` varchar(255) NOT NULL," +
"`public` boolean NOT NULL," +
"PRIMARY KEY (`player_id`, `name`)," +
"FOREIGN KEY (`player_id`) REFERENCES " + HuskHomes.getSettings().getPlayerDataTable() + " (`player_id`) ON DELETE CASCADE ON UPDATE NO ACTION," +
"FOREIGN KEY (`location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" +
");",
"CREATE TABLE IF NOT EXISTS " + HuskHomes.getSettings().getWarpsDataTable() + " (" +
"`location_id` integer NOT NULL," +
"`name` varchar(16) NOT NULL UNIQUE," +
"`description` varchar(255) NOT NULL," +
"PRIMARY KEY (`location_id`)," +
"FOREIGN KEY (`location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" +
");"
};
final String host = HuskHomes.getSettings().getMySQLhost();
final int port = HuskHomes.getSettings().getMySQLport();
final String database = HuskHomes.getSettings().getMySQLdatabase();
final String username = HuskHomes.getSettings().getMySQLusername();
final String password = HuskHomes.getSettings().getMySQLpassword();
final String params = HuskHomes.getSettings().getMySQLparams();
private Connection connection;
public MySQL(HuskHomes instance) {
super(instance);
}
@Override
public Connection getConnection() {
try {
if (connection == null || connection.isClosed()) {
try {
synchronized (HuskHomes.getInstance()) {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = (DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + params, username, password));
}
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, "An exception occurred initialising the mySQL database: ", ex);
} catch (ClassNotFoundException ex) {
plugin.getLogger().log(Level.SEVERE, "The mySQL JBDC library is missing! Please download and place this in the /lib folder.");
}
}
} catch (SQLException exception) {
plugin.getLogger().log(Level.WARNING, "An error occurred checking the status of the SQL connection: ", exception);
}
return connection;
}
@Override
public void load() {
connection = getConnection();
try {
Statement statement = connection.createStatement();
for (String tableCreationStatement : SQL_SETUP_STATEMENTS) {
statement.execute(tableCreationStatement);
}
statement.close();
} catch (SQLException exception) {
plugin.getLogger().log(Level.SEVERE, "An error occurred creating tables: ", exception);
exception.printStackTrace();
}
initialize();
}
}
@@ -0,0 +1,116 @@
package me.william278.huskhomes2.data.SQL;
import me.william278.huskhomes2.HuskHomes;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
public class SQLite extends Database {
final static String[] SQL_SETUP_STATEMENTS = {
"PRAGMA foreign_keys = ON;",
"CREATE TABLE IF NOT EXISTS " + HuskHomes.getSettings().getPlayerDataTable() + " (" +
"`player_id` integer NOT NULL," +
"`user_uuid` char(36) NOT NULL UNIQUE," +
"`username` varchar(16) NOT NULL," +
"`home_slots` integer NOT NULL," +
"`rtp_cooldown` integer NOT NULL DEFAULT 0," +
"`is_teleporting` boolean NOT NULL DEFAULT 0," +
"`dest_location_id` integer NULL," +
"`last_location_id` integer NULL," +
"PRIMARY KEY (`player_id`)," +
"FOREIGN KEY (`dest_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION," +
"FOREIGN KEY (`last_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION" +
");",
"CREATE TABLE IF NOT EXISTS " + HuskHomes.getSettings().getLocationsDataTable() + " (" +
"`location_id` integer PRIMARY KEY," +
"`server` text NOT NULL," +
"`world` text NOT NULL," +
"`x` double NOT NULL," +
"`y` double NOT NULL," +
"`z` double NOT NULL," +
"`yaw` float NOT NULL," +
"`pitch` float NOT NULL" +
");",
"CREATE TABLE IF NOT EXISTS " + HuskHomes.getSettings().getHomesDataTable() + " (" +
"`player_id` integer NOT NULL," +
"`location_id` integer NOT NULL," +
"`name` varchar(16) NOT NULL," +
"`description` varchar(255) NOT NULL," +
"`public` boolean NOT NULL," +
"PRIMARY KEY (`player_id`, `name`)," +
"FOREIGN KEY (`player_id`) REFERENCES " + HuskHomes.getSettings().getPlayerDataTable() + " (`player_id`) ON DELETE CASCADE ON UPDATE NO ACTION," +
"FOREIGN KEY (`location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" +
");",
"CREATE TABLE IF NOT EXISTS " + HuskHomes.getSettings().getWarpsDataTable() + " (" +
"`location_id` integer NOT NULL," +
"`name` varchar(16) NOT NULL UNIQUE," +
"`description` varchar(255) NOT NULL," +
"PRIMARY KEY (`location_id`)," +
"FOREIGN KEY (`location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" +
");"
};
private static final String DATABASE_NAME = "HuskHomesData";
private Connection connection;
public SQLite(HuskHomes instance) {
super(instance);
}
@Override
public Connection getConnection() {
try {
if (connection == null || connection.isClosed()) {
File databaseFile = new File(plugin.getDataFolder(), DATABASE_NAME + ".db");
if (!databaseFile.exists()) {
try {
if (!databaseFile.createNewFile()) {
plugin.getLogger().log(Level.SEVERE, "Failed to write new file: " + DATABASE_NAME + ".db (file already exists)");
}
} catch (IOException e) {
plugin.getLogger().log(Level.SEVERE, "An error occurred writing a file: " + DATABASE_NAME + ".db (" + e.getCause() + ")");
}
}
try {
Class.forName("org.sqlite.JDBC");
connection = (DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/" + DATABASE_NAME + ".db"));
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, "An exception occurred initialising the SQLite database", ex);
} catch (ClassNotFoundException ex) {
plugin.getLogger().log(Level.SEVERE, "The SQLite JBDC library is missing! Please download and place this in the /lib folder.");
}
}
} catch (SQLException exception) {
plugin.getLogger().log(Level.WARNING, "An error occurred checking the status of the SQL connection: ", exception);
}
return connection;
}
@Override
public void load() {
connection = getConnection();
try {
Statement statement = connection.createStatement();
for (String tableCreationStatement : SQL_SETUP_STATEMENTS) {
statement.execute(tableCreationStatement);
}
statement.close();
} catch (SQLException exception) {
plugin.getLogger().log(Level.SEVERE, "An error occurred creating tables: ", exception);
exception.printStackTrace();
}
initialize();
}
}