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,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();
}
}