From 920d691a8a1b1d0864b11a3d5815c421e276a9ed Mon Sep 17 00:00:00 2001 From: SemvdH Date: Wed, 15 Oct 2025 00:22:11 +0200 Subject: [PATCH] Add creating database --- .gitignore | 1 + default.code-workspace | 4 +- ic_plugin/pom.xml | 11 ++++ .../main/java/nl/interestingcorner/App.java | 17 ++++++- .../interestingcorner/db/DatabaseManager.java | 50 +++++++++++++++++++ 5 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 ic_plugin/src/main/java/nl/interestingcorner/db/DatabaseManager.java diff --git a/.gitignore b/.gitignore index 883c300..5fc79b5 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,7 @@ buildNumber.properties .mvn/timing.properties # https://github.com/takari/maven-wrapper#usage-without-binary-jar .mvn/wrapper/maven-wrapper.jar +ic_plugin/settings.xml # Eclipse m2e generated files # Eclipse Core diff --git a/default.code-workspace b/default.code-workspace index a0ed526..6aaf09d 100644 --- a/default.code-workspace +++ b/default.code-workspace @@ -9,6 +9,8 @@ "files.encoding": "utf8", "java.home": "/usr/lib/jvm/jdk-25", "java.jdt.ls.vmargs": "-Dfile.encoding=UTF-8", - "javac-linter.javac": "javac -Dfile.encoding=UTF-8" + "javac-linter.javac": "javac -Dfile.encoding=UTF-8", + "java.configuration.updateBuildConfiguration": "automatic", + "java.format.settings.url": "eclipse-formatter.xml" } } \ No newline at end of file diff --git a/ic_plugin/pom.xml b/ic_plugin/pom.xml index 74da306..ca489ab 100644 --- a/ic_plugin/pom.xml +++ b/ic_plugin/pom.xml @@ -57,6 +57,11 @@ 1.21.8-R0.1-SNAPSHOT provided + + org.xerial + sqlite-jdbc + 3.50.3.0 + @@ -92,6 +97,12 @@ maven-jar-plugin 3.4.2 + + + ${db.username} + ${db.password} + + maven-install-plugin diff --git a/ic_plugin/src/main/java/nl/interestingcorner/App.java b/ic_plugin/src/main/java/nl/interestingcorner/App.java index 67554c7..bde12fb 100644 --- a/ic_plugin/src/main/java/nl/interestingcorner/App.java +++ b/ic_plugin/src/main/java/nl/interestingcorner/App.java @@ -1,14 +1,27 @@ package nl.interestingcorner; +import java.util.logging.Level; + +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; +import nl.interestingcorner.db.DatabaseManager; + public class App extends JavaPlugin { @Override public void onEnable() { - getLogger().info("Hello, SpigotMC!"); + getLogger().info("Eyooo we boutta get lit!"); + + // saveDefaultConfig(); // create config.yml if not exists + if (!DatabaseManager.INSTANCE.initialize(this)) + { + getLogger().severe("Could not initialize database. Exiting!"); + System.exit(1); + } } @Override public void onDisable() { - getLogger().info("See you again, SpigotMC!"); + getLogger().info("Eyooo we outta here!"); } } diff --git a/ic_plugin/src/main/java/nl/interestingcorner/db/DatabaseManager.java b/ic_plugin/src/main/java/nl/interestingcorner/db/DatabaseManager.java new file mode 100644 index 0000000..68015d0 --- /dev/null +++ b/ic_plugin/src/main/java/nl/interestingcorner/db/DatabaseManager.java @@ -0,0 +1,50 @@ +package nl.interestingcorner.db; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Properties; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.bukkit.plugin.java.JavaPlugin; + +public enum DatabaseManager { + + INSTANCE; + + private Connection connection; + private JavaPlugin app; + + public boolean initialize(JavaPlugin app) { + this.app = app; + + if (!app.getDataFolder().exists()) { + if (!app.getDataFolder().mkdirs()) { + app.getLogger().severe("Could not create data folder"); + return false; + } + } + + File dbFile = new File(app.getDataFolder(), "database.db"); + String url = "jdbc:sqlite:" + dbFile.getAbsolutePath(); + + try { + this.connection = DriverManager.getConnection(url); + } + catch (SQLException e) { + app.getLogger().log(Level.SEVERE, "Could not connect to database file {0}: {1}", + new Object[] { dbFile, e.getMessage() }); + } + + app.getLogger().log(Level.INFO, "Connected to SQLite database: {0}", dbFile.getName()); + + return true; + + } + +}