diff --git a/ic_plugin_coordinates/pom.xml b/ic_plugin_coordinates/pom.xml index 29579c3..650a271 100644 --- a/ic_plugin_coordinates/pom.xml +++ b/ic_plugin_coordinates/pom.xml @@ -6,6 +6,7 @@ nl.interestingcorner.coordinates ic_plugin_coordinates 1.0-SNAPSHOT + jar ic_plugin_coordinates @@ -30,6 +31,12 @@ pom import + + nl.interestingcorner.core + ic_plugin_core + 1.0-SNAPSHOT + provided + diff --git a/ic_plugin_coordinates/src/main/java/nl/interestingcorner/coordinates/db/DatabaseManager.java b/ic_plugin_coordinates/src/main/java/nl/interestingcorner/coordinates/db/DatabaseManager.java index d4784ce..13132c4 100644 --- a/ic_plugin_coordinates/src/main/java/nl/interestingcorner/coordinates/db/DatabaseManager.java +++ b/ic_plugin_coordinates/src/main/java/nl/interestingcorner/coordinates/db/DatabaseManager.java @@ -1,9 +1,6 @@ package nl.interestingcorner.coordinates.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.PreparedStatement; @@ -11,9 +8,9 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import java.util.Properties; import java.util.logging.Level; -import java.util.logging.Logger; + +import nl.interestingcorner.core.db.*; import org.bukkit.plugin.java.JavaPlugin; diff --git a/ic_plugin_coordinates/src/main/resources/plugin.yml b/ic_plugin_coordinates/src/main/resources/plugin.yml index a6cb5f3..a3146ca 100644 --- a/ic_plugin_coordinates/src/main/resources/plugin.yml +++ b/ic_plugin_coordinates/src/main/resources/plugin.yml @@ -2,6 +2,7 @@ main: nl.interestingcorner.coordinates.App name: IC-coords version: 0.1 api-version: 1.21 +depend: [IC-core] commands: ic-coords: diff --git a/ic_plugin_core/pom.xml b/ic_plugin_core/pom.xml index a14bf82..57b70b8 100644 --- a/ic_plugin_core/pom.xml +++ b/ic_plugin_core/pom.xml @@ -6,6 +6,7 @@ nl.interestingcorner.core ic_plugin_core 1.0-SNAPSHOT + jar ic_plugin_core diff --git a/ic_plugin_core/settings.xml b/ic_plugin_core/settings.xml new file mode 100644 index 0000000..1e175f7 --- /dev/null +++ b/ic_plugin_core/settings.xml @@ -0,0 +1,14 @@ + + + + default + + spigot + Z7nhqy+spigot-minecraft + ic_minecraft + localhost + + + + \ No newline at end of file diff --git a/ic_plugin_core/src/main/java/nl/interestingcorner/core/App.java b/ic_plugin_core/src/main/java/nl/interestingcorner/core/App.java index 43ef657..1def67b 100644 --- a/ic_plugin_core/src/main/java/nl/interestingcorner/core/App.java +++ b/ic_plugin_core/src/main/java/nl/interestingcorner/core/App.java @@ -2,16 +2,21 @@ package nl.interestingcorner.core; import org.bukkit.plugin.java.JavaPlugin; +import nl.interestingcorner.core.db.DatabaseManager; + /** - * Hello world! + * Core plugin class for the Interesting Corner plugin */ -public class App { +public class App extends JavaPlugin { @Override public void onEnable() { - getLogger().info("Hello, SpigotMC!"); + DatabaseManager.INSTANCE.initialize(this); + getLogger().info("Core plugin loaded!"); } + @Override public void onDisable() { - getLogger().info("See you again, SpigotMC!"); + DatabaseManager.INSTANCE.close(); + getLogger().info("Core plugin exited!"); } } diff --git a/ic_plugin_core/src/main/java/nl/interestingcorner/core/db/DatabaseInitializeListener.java b/ic_plugin_core/src/main/java/nl/interestingcorner/core/db/DatabaseInitializeListener.java new file mode 100644 index 0000000..d6bcdfd --- /dev/null +++ b/ic_plugin_core/src/main/java/nl/interestingcorner/core/db/DatabaseInitializeListener.java @@ -0,0 +1,11 @@ +package nl.interestingcorner.core.db; + +import java.sql.Connection; + +public interface DatabaseInitializeListener { + /** + * Gets called when the databse has been setup and tables can be initialized. + * @param connection the connection to the database + */ + void initializeDatabaseTables(Connection connection); +} diff --git a/ic_plugin_core/src/main/java/nl/interestingcorner/core/db/DatabaseManager.java b/ic_plugin_core/src/main/java/nl/interestingcorner/core/db/DatabaseManager.java new file mode 100644 index 0000000..76ce6c8 --- /dev/null +++ b/ic_plugin_core/src/main/java/nl/interestingcorner/core/db/DatabaseManager.java @@ -0,0 +1,117 @@ +package nl.interestingcorner.core.db; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; + +import org.bukkit.plugin.java.JavaPlugin; + +public enum DatabaseManager { + INSTANCE; + + private Connection connection; + private JavaPlugin app; + private boolean initialized = false; + private final List databaseInitializeListeners = new ArrayList<>(); + + /** + * Initializes the database. Creates the file and sets up the db structure. + * + * @param app the app to use for logging + * @return true if the database was initialized successfully. False if not. + */ + public boolean initialize(JavaPlugin app) { + if (this.initialized) { + return true; + } + + this.app = app; + + if (!setupDatabase()) { + return false; + } + + initializeTables(); + return true; + } + + /** + * Closes the connectino to the database + * + * @return true if the connection closed succesfully, false if not. + */ + public boolean close() { + try { + if (this.connection != null && !this.connection.isClosed()) { + this.connection.close(); + this.initialized = false; + } + } catch (SQLException ex) { + this.app.getLogger().log(Level.SEVERE, "Error while closing the databse connection: {0}", ex.getMessage()); + return false; + } + + return true; + } + + public void registerDatabaseInitializeListener(DatabaseInitializeListener listener) { + this.databaseInitializeListeners.add(listener); + + // immediately initialize if the database is already initialized + if (this.initialized) { + try { + listener.initializeDatabaseTables(this.connection); + } catch (Exception e) { + this.app.getLogger().log(Level.SEVERE, "Could not initialize database tables: {0}", e.getMessage()); + } + } + } + + /** + * Sets up the connectino to the database. Creates a new .db file if it + * doesn't exist yet. + * + * @return true if the connection was set up successfully. False if not. + */ + private boolean setupDatabase() { + if (!this.app.getDataFolder().exists()) { + if (!this.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) { + this.app.getLogger().log(Level.SEVERE, "Could not connect to database file {0}: {1}", + new Object[] { dbFile, e.getMessage() }); + } + + this.app.getLogger().log(Level.INFO, "Connected to SQLite database: {0}", dbFile.getName()); + this.initialized = true; + + return true; + } + + /** + * initializes the tables for the database. + */ + private void initializeTables() { + for (DatabaseInitializeListener listener : this.databaseInitializeListeners) { + try { + listener.initializeDatabaseTables(this.connection); + } catch (Exception e) { + this.app.getLogger().log(Level.SEVERE, "Could not initialize database tables: {0}", e.getMessage()); + } + } + } + +} diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e5823d9 --- /dev/null +++ b/pom.xml @@ -0,0 +1,12 @@ + + 4.0.0 + nl.interestingcorner + ic_plugins + 1.0.0 + pom + + + ic_plugin_core + ic_plugin_coordinates + + \ No newline at end of file