From ccc45333f8b5957cf03644e6c1305401a8e7fc3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Sat, 22 Oct 2022 16:29:32 +0200 Subject: [PATCH] Implement basic files and database management --- .../wtf/beatrice/nounspicker/NounsPicker.java | 31 ++++++++++- .../nounspicker/utils/DatabaseManager.java | 52 +++++++++++++++++++ .../nounspicker/utils/FileManager.java | 36 +++++++++++++ 3 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java create mode 100644 src/main/java/wtf/beatrice/nounspicker/utils/FileManager.java diff --git a/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java b/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java index c75da8b..19e1385 100644 --- a/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java +++ b/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java @@ -6,6 +6,10 @@ import org.bukkit.plugin.java.JavaPlugin; import wtf.beatrice.nounspicker.commands.NounsCommand; import wtf.beatrice.nounspicker.objects.PAPIManager; import wtf.beatrice.nounspicker.utils.ConsoleLogger; +import wtf.beatrice.nounspicker.utils.DatabaseManager; +import wtf.beatrice.nounspicker.utils.FileManager; + +import java.io.File; public class NounsPicker extends JavaPlugin { @@ -28,7 +32,7 @@ public class NounsPicker extends JavaPlugin logger.log("PlaceholderAPI integration loaded!"); } else { logger.err("Missing PlaceholderAPI! The plugin cannot continue."); - pluginManager.disablePlugin(this); + disable(); return; } @@ -36,6 +40,26 @@ public class NounsPicker extends JavaPlugin getCommand("nouns").setExecutor(new NounsCommand(this)); logger.log("Commands registered!"); + logger.log("Checking files..."); + FileManager fileManager = new FileManager(this); + if (!fileManager.routineCheck()) + { + logger.err("Error during routine file check! The plugin will halt."); + disable(); + return; + } + logger.log("Files checked!"); + + logger.log("Connecting to database..."); + String dbFilePath = getDataFolder().getAbsolutePath() + File.separator + "db.sqlite"; + DatabaseManager dbManager = new DatabaseManager(dbFilePath); + if(dbManager.initialize()) + { + logger.log("Database connection initialized!"); + } else { + logger.err("Error initializing database connection!"); + } + logger.log("Plugin loaded!"); } @@ -43,4 +67,9 @@ public class NounsPicker extends JavaPlugin { return instance; } + + private void disable() + { + pluginManager.disablePlugin(this); + } } \ No newline at end of file diff --git a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java new file mode 100644 index 0000000..55e9415 --- /dev/null +++ b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java @@ -0,0 +1,52 @@ +package wtf.beatrice.nounspicker.utils; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class DatabaseManager +{ + + private final static String sqliteURL = "jdbc:sqlite:%path%"; + private Connection dbConnection = null; + private ConsoleLogger logger; + private final String dbPath; + + public DatabaseManager(String dbPath) + { + this.dbPath = dbPath; + logger = new ConsoleLogger(getClass()); + } + + public boolean initialize() + { + String url = sqliteURL.replace("%path%", dbPath); + + if (dbConnection != null) + { + try { + if(!dbConnection.isClosed()) + { + dbConnection.close(); + } + } catch (SQLException e) { + logger.err(e.getMessage()); + return false; + } + + dbConnection = null; + } + + + try { + dbConnection = DriverManager.getConnection(url); + logger.log("Database connection established!"); + return true; + } catch (SQLException e) { + logger.err(e.getMessage()); + return false; + } + + } + +} diff --git a/src/main/java/wtf/beatrice/nounspicker/utils/FileManager.java b/src/main/java/wtf/beatrice/nounspicker/utils/FileManager.java new file mode 100644 index 0000000..3fda334 --- /dev/null +++ b/src/main/java/wtf/beatrice/nounspicker/utils/FileManager.java @@ -0,0 +1,36 @@ +package wtf.beatrice.nounspicker.utils; + +import wtf.beatrice.nounspicker.NounsPicker; + +import java.io.File; + +public class FileManager +{ + private final NounsPicker plugin; + private final ConsoleLogger logger; + public FileManager(NounsPicker plugin) + { + this.plugin = plugin; + logger = new ConsoleLogger(getClass()); + } + + public boolean routineCheck() + { + File dataDir = plugin.getDataFolder(); + + logger.log("Checking plugin directory..."); + if(!dataDir.exists()) + { + logger.log("Creating plugin directory!"); + if (dataDir.mkdirs()) + { + logger.log("Plugin directory created!"); + } else { + logger.err("Error creating plugin directory!"); + return false; + } + } + + return true; + } +}