mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 06:28:49 +00:00
123 lines
5.8 KiB
Java
123 lines
5.8 KiB
Java
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().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().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," +
|
|
"`offline_location_id` integer NULL," +
|
|
"`is_ignoring_requests` boolean NOT NULL DEFAULT 0," +
|
|
|
|
"PRIMARY KEY (`player_id`)," +
|
|
"FOREIGN KEY (`offline_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION," +
|
|
"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().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," +
|
|
"`creation_time` timestamp 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," +
|
|
"`creation_time` timestamp 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();
|
|
}
|
|
|
|
@Override
|
|
public void backup() {
|
|
plugin.getLogger().info("Remember to make backups of your HuskHomes Database before updating HuskHomes!");
|
|
}
|
|
}
|