Skip to content
Snippets Groups Projects
Commit da30bb8e authored by Sofiane Lasri's avatar Sofiane Lasri
Browse files

Déplacement de la fonciton initDatabase dans la classe correspondante, et...

Déplacement de la fonciton initDatabase dans la classe correspondante, et amélioration de la fonction de démarrage du plugin.
parent fffc1522
No related branches found
No related tags found
No related merge requests found
Pipeline #113 passed
...@@ -15,6 +15,7 @@ import net.md_5.bungee.api.ChatMessageType; ...@@ -15,6 +15,7 @@ import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
...@@ -59,10 +60,10 @@ public final class Main extends JavaPlugin implements Listener { ...@@ -59,10 +60,10 @@ public final class Main extends JavaPlugin implements Listener {
@Override @Override
public void onEnable() { public void onEnable() {
pluginName = this.getName(); pluginName = this.getName();
// On s'assure qu'on a placeholder api // On s'assure qu'on a placeholder api
if (getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) { if (getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) {
ConsoleLog.info("PlaceholderAPI chargé"); ConsoleLog.info("PlaceholderAPI chargé");
// On initialise les listeners
getServer().getPluginManager().registerEvents(this, this); getServer().getPluginManager().registerEvents(this, this);
} else { } else {
ConsoleLog.danger("PlaceholderAPI n'est pas accessible!"); ConsoleLog.danger("PlaceholderAPI n'est pas accessible!");
...@@ -88,29 +89,40 @@ public final class Main extends JavaPlugin implements Listener { ...@@ -88,29 +89,40 @@ public final class Main extends JavaPlugin implements Listener {
// Plugin startup logic // Plugin startup logic
try { try {
databaseConnection = Database.bddOpenConn(); databaseConnection = Database.bddOpenConn();
Database.initDatabase();
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
// On charge la config (et on la crée si elle n'existe pas)
saveDefaultConfig(); saveDefaultConfig();
reloadConfig(); reloadConfig();
config = getConfig(); config = getConfig();
updateConfig(); updateConfig();
initDatabase(); // On initialise les handlers
playerDataHandler = new PlayerDataHandler(this); playerDataHandler = new PlayerDataHandler(this);
InternalWebServer.startServer(this);
periodicEvent = new PeriodicEvent(this); periodicEvent = new PeriodicEvent(this);
InternalWebServer.startServer(this);
// On initialise les commandes // On initialise les commandes
wildCommand = new Wild(this); wildCommand = new Wild(this);
getCommand("wild").setExecutor(wildCommand);
WildReset wildReset = new WildReset(this); WildReset wildReset = new WildReset(this);
getCommand("reset-wild").setExecutor(wildReset);
LinkCode linkCodeCommand = new LinkCode(this); LinkCode linkCodeCommand = new LinkCode(this);
getCommand("getLinkCode").setExecutor(linkCodeCommand);
PluginCommand wild = getCommand("wild");
PluginCommand resetWild = getCommand("reset-wild");
PluginCommand getLinkCode = getCommand("getLinkCode");
// On vérifie que les commandes ont bien été initialisées dans plugin.yml
if (wild == null || resetWild == null || getLinkCode == null) {
ConsoleLog.danger("Une commande n'a pas pu être initialisée!");
getServer().getPluginManager().disablePlugin(this);
} else {
wild.setExecutor(wildCommand);
resetWild.setExecutor(wildReset);
getLinkCode.setExecutor(linkCodeCommand);
}
ConsoleLog.success("Plugin démarré"); ConsoleLog.success("Plugin démarré");
} }
...@@ -341,29 +353,4 @@ public final class Main extends JavaPlugin implements Listener { ...@@ -341,29 +353,4 @@ public final class Main extends JavaPlugin implements Listener {
saveConfig(); saveConfig();
reloadConfig(); reloadConfig();
} }
private void initDatabase() {
try {
Connection con = bddOpenConn();
PreparedStatement ps = con.prepareStatement("CREATE TABLE IF NOT EXISTS `site_userSetting` (\n" +
" `uuid` varchar(36) NOT NULL DEFAULT '',\n" +
" `name` varchar(128) NOT NULL,\n" +
" `value` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,\n" +
" PRIMARY KEY (`uuid`,`name`) USING BTREE\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
ps.executeQuery();
ps = con.prepareStatement("CREATE TABLE IF NOT EXISTS `site_linkCode` (\n" +
" `uuid` VARCHAR(36) NOT NULL,\n" +
" `code` VARCHAR(8) NOT NULL,\n" +
" `time` TIMESTAMP NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),\n" +
" `used` BOOLEAN,\n" +
" PRIMARY KEY (`uuid`),\n" +
" UNIQUE INDEX `code` (`code`)\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
ps.executeQuery();
con.close();
} catch (Exception e) {
ConsoleLog.danger("Erreur lors de l'exécution de initDatabase(): " + e);
}
}
} }
...@@ -67,42 +67,6 @@ public class Database { ...@@ -67,42 +67,6 @@ public class Database {
} }
} }
/**
* Ajoute une entrée dans la table site_userSetting
*
* @param uuid UUID du joueur
* @param key Nom de la clé
* @param value Valeur de la clé
*/
private static void insertUserSettingEntry(String uuid, String key, String value) {
Connection con;
try {
con = bddOpenConn();
} catch (SQLException e) {
ConsoleLog.danger("Erreur lors de l'ouverture de la connexion à la bdd.");
throw new RuntimeException(e);
}
try {
PreparedStatement insertEntry = con.prepareStatement("INSERT INTO " + userSettingsTabName + " (uuid, name, value) VALUES (?, ?, ?)");
insertEntry.setString(1, uuid);
insertEntry.setString(2, key);
insertEntry.setString(3, value);
insertEntry.executeQuery();
} catch (SQLException e) {
ConsoleLog.danger("Erreur lors de l'exécution de la requête sql.");
throw new RuntimeException(e);
}
// On ferme la bdd
try {
con.close();
} catch (SQLException e) {
ConsoleLog.danger("Impossible de fermer la connexion à la bdd.");
throw new RuntimeException(e);
}
}
/** /**
* Ouvre une connexion à la base de données * Ouvre une connexion à la base de données
* *
...@@ -122,4 +86,27 @@ public class Database { ...@@ -122,4 +86,27 @@ public class Database {
connection.close(); connection.close();
jLoquentConnector.close(); jLoquentConnector.close();
} }
public static void initDatabase() {
try {
PreparedStatement ps = connection.prepareStatement("CREATE TABLE IF NOT EXISTS `site_userSetting` (\n" +
" `uuid` varchar(36) NOT NULL DEFAULT '',\n" +
" `name` varchar(128) NOT NULL,\n" +
" `value` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,\n" +
" PRIMARY KEY (`uuid`,`name`) USING BTREE\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
ps.executeQuery();
ps = connection.prepareStatement("CREATE TABLE IF NOT EXISTS `site_linkCode` (\n" +
" `uuid` VARCHAR(36) NOT NULL,\n" +
" `code` VARCHAR(8) NOT NULL,\n" +
" `time` TIMESTAMP NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),\n" +
" `used` BOOLEAN,\n" +
" PRIMARY KEY (`uuid`),\n" +
" UNIQUE INDEX `code` (`code`)\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
ps.executeQuery();
} catch (Exception e) {
ConsoleLog.danger("Erreur lors de l'exécution de initDatabase(): " + e);
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment