feat: Adds convenience overload method SongodaPlugin#initDatabase

Takes DataMigration... instead of an List. Similar how it used to be in the old API.
Reduces visual clutter in plugin code in my opinion
This commit is contained in:
Christian Koop 2023-10-24 02:41:02 +02:00
parent eaf96d51de
commit c79b835e9a
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3

View File

@ -18,6 +18,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.sql.Connection;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
@ -255,7 +256,7 @@ public abstract class SongodaPlugin extends JavaPlugin {
* @return DataManager for this plugin.
*/
public DataManager getDataManager() {
return dataManager;
return this.dataManager;
}
/**
@ -265,6 +266,10 @@ public abstract class SongodaPlugin extends JavaPlugin {
initDatabase(Collections.emptyList());
}
protected void initDatabase(DataMigration... migrations) {
initDatabase(Arrays.asList(migrations));
}
/**
* Initialize the DataManager for this plugin and convert from SQLite to H2 if needed.
*
@ -281,9 +286,9 @@ public abstract class SongodaPlugin extends JavaPlugin {
this.dataManager = new DataManager(this, migrations);
}
if (dataManager.getDatabaseConnector().isInitialized()) {
if (this.dataManager.getDatabaseConnector().isInitialized()) {
//Check if the type is SQLite
if (dataManager.getDatabaseConnector().getType() == DatabaseType.SQLITE) {
if (this.dataManager.getDatabaseConnector().getType() == DatabaseType.SQLITE) {
//Let's convert it to H2
try {
DataManager newDataManager = DataMigration.convert(this, DatabaseType.H2);
@ -291,8 +296,9 @@ public abstract class SongodaPlugin extends JavaPlugin {
//Set the new data manager
setDataManager(newDataManager);
}
} catch (Exception e) {
e.printStackTrace();
} catch (Exception ex) {
// Throwing for keeping backwards compatible Not a fan of just logging a potential critical error here
throw new RuntimeException(ex);
}
}
}
@ -305,10 +311,10 @@ public abstract class SongodaPlugin extends JavaPlugin {
public void setDataManager(DataManager dataManager) {
if (dataManager == null) throw new IllegalArgumentException("DataManager cannot be null!");
if (this.dataManager == dataManager) return;
//Make sure to shut down the old data manager.
// Make sure to shut down the old data manager.
if (this.dataManager != null) {
this.dataManager.shutdown();
this.dataManager = null;
}
this.dataManager = dataManager;
}