add base project pom

This commit is contained in:
SemvdH
2025-11-09 23:34:42 +01:00
parent 6fa5dde5e2
commit 1c073b1b14
9 changed files with 174 additions and 9 deletions

View File

@@ -6,6 +6,7 @@
<groupId>nl.interestingcorner.coordinates</groupId>
<artifactId>ic_plugin_coordinates</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ic_plugin_coordinates</name>
<!-- FIXME change it to the project's website -->
@@ -30,6 +31,12 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>nl.interestingcorner.core</groupId>
<artifactId>ic_plugin_core</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>

View File

@@ -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;

View File

@@ -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:

View File

@@ -6,6 +6,7 @@
<groupId>nl.interestingcorner.core</groupId>
<artifactId>ic_plugin_core</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ic_plugin_core</name>
<!-- FIXME change it to the project's website -->

View File

@@ -0,0 +1,14 @@
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
<profiles>
<profile>
<id>default</id>
<properties>
<db.username>spigot</db.username>
<db.password>Z7nhqy+spigot-minecraft</db.password>
<db.name>ic_minecraft</db.name>
<db.host>localhost</db.host>
</properties>
</profile>
</profiles>
</settings>

View File

@@ -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!");
}
}

View File

@@ -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);
}

View File

@@ -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<DatabaseInitializeListener> 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());
}
}
}
}

12
pom.xml Normal file
View File

@@ -0,0 +1,12 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>nl.interestingcorner</groupId>
<artifactId>ic_plugins</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
<module>ic_plugin_core</module>
<module>ic_plugin_coordinates</module>
</modules>
</project>