2022-05-01 19:48:52 +00:00
|
|
|
package com.alttd.altitudequests.database;
|
|
|
|
|
|
2022-05-29 15:46:50 +00:00
|
|
|
import com.alttd.altitudequests.AQuest;
|
2022-05-01 19:48:52 +00:00
|
|
|
import com.alttd.altitudequests.config.DatabaseConfig;
|
|
|
|
|
import com.alttd.altitudequests.util.Logger;
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
|
|
2022-06-01 17:33:02 +00:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.lang.reflect.Modifier;
|
2022-05-01 19:48:52 +00:00
|
|
|
import java.sql.Connection;
|
|
|
|
|
import java.sql.DriverManager;
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
|
|
|
|
|
public class Database {
|
|
|
|
|
|
|
|
|
|
private static Database instance = null;
|
2022-06-01 17:33:02 +00:00
|
|
|
private Connection connection = null;
|
2022-05-01 19:48:52 +00:00
|
|
|
|
2022-06-01 17:33:02 +00:00
|
|
|
private Database() {}
|
2022-05-01 19:48:52 +00:00
|
|
|
|
|
|
|
|
public static Database getDatabase(){
|
|
|
|
|
if (instance == null)
|
2022-06-01 21:17:45 +00:00
|
|
|
{
|
2022-05-01 19:48:52 +00:00
|
|
|
instance = new Database();
|
2022-06-01 21:17:45 +00:00
|
|
|
instance.init();
|
|
|
|
|
}
|
2022-05-01 19:48:52 +00:00
|
|
|
return (instance);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-01 21:17:45 +00:00
|
|
|
protected void init() {
|
2022-05-01 19:48:52 +00:00
|
|
|
try {
|
|
|
|
|
openConnection();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-01 17:33:02 +00:00
|
|
|
//Run all create table functions
|
|
|
|
|
for (Method method : Database.class.getDeclaredMethods()) {
|
|
|
|
|
if (Modifier.isPrivate(method.getModifiers())) {
|
|
|
|
|
if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) {
|
|
|
|
|
try {
|
|
|
|
|
method.setAccessible(true);
|
|
|
|
|
method.invoke(instance);
|
|
|
|
|
} catch (InvocationTargetException ex) {
|
|
|
|
|
throw new RuntimeException(ex.getCause());
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
Logger.severe("Error invoking %.", method.toString());
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-01 19:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Opens the connection if it's not already open.
|
|
|
|
|
* @throws SQLException If it can't create the connection.
|
|
|
|
|
*/
|
|
|
|
|
private void openConnection() throws SQLException {
|
|
|
|
|
if (connection != null && !connection.isClosed()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
synchronized (this) {
|
|
|
|
|
if (connection != null && !connection.isClosed()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
Class.forName("com.mysql.cj.jdbc.Driver");
|
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connection = DriverManager.getConnection(
|
|
|
|
|
"jdbc:mysql://" + DatabaseConfig.IP + ":" + DatabaseConfig.PORT + "/" + DatabaseConfig.DATABASE_NAME +
|
|
|
|
|
"?autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true",
|
|
|
|
|
DatabaseConfig.USERNAME, DatabaseConfig.PASSWORD);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-01 17:33:02 +00:00
|
|
|
public Connection getConnection() {
|
|
|
|
|
try {
|
|
|
|
|
openConnection();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return connection;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 19:48:52 +00:00
|
|
|
private static void createUserPointsTable() {
|
|
|
|
|
try {
|
2022-06-01 17:33:02 +00:00
|
|
|
String sql = "CREATE TABLE IF NOT EXISTS generic_quest_progress(" +
|
2022-06-02 20:12:29 +00:00
|
|
|
"year_day INT NOT NULL, " +
|
2022-05-01 19:48:52 +00:00
|
|
|
"uuid VARCHAR(36) NOT NULL, " +
|
|
|
|
|
"quest VARCHAR(36) NOT NULL, " +
|
2022-06-01 17:33:02 +00:00
|
|
|
"quest_variant VARCHAR(36) NOT NULL, " +
|
|
|
|
|
"step_1_progress INT NOT NULL, " +
|
|
|
|
|
"step_2_progress INT NOT NULL, " +
|
2022-05-01 19:48:52 +00:00
|
|
|
"PRIMARY KEY (UUID, quest)" +
|
|
|
|
|
")";
|
2022-06-01 17:33:02 +00:00
|
|
|
getDatabase().getConnection().prepareStatement(sql).executeUpdate();
|
2022-05-01 19:48:52 +00:00
|
|
|
} catch (SQLException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
Logger.severe("Error while trying to create user point table");
|
|
|
|
|
Logger.severe("Shutting down AltitudeQuests");
|
2022-05-29 15:46:50 +00:00
|
|
|
Bukkit.getPluginManager().disablePlugin(AQuest.getInstance());
|
2022-05-01 19:48:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|