9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-23 16:49:19 +00:00

Improve initialization logic

This commit is contained in:
William
2022-07-11 13:18:36 +01:00
parent ff1c8cddb5
commit 723c79b3a9
3 changed files with 105 additions and 123 deletions

View File

@@ -41,6 +41,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -75,147 +76,116 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync {
@Override @Override
public void onEnable() { public void onEnable() {
// Process initialization stages // Initialize HuskSync
CompletableFuture.supplyAsync(() -> { final AtomicBoolean initialized = new AtomicBoolean(true);
try {
// Set the logging adapter and resource reader // Set the logging adapter and resource reader
this.logger = new BukkitLogger(this.getLogger()); this.logger = new BukkitLogger(this.getLogger());
this.resourceReader = new BukkitResourceReader(this); this.resourceReader = new BukkitResourceReader(this);
// Load settings and locales // Load settings and locales
getLoggingAdapter().log(Level.INFO, "Loading plugin configuration settings & locales..."); getLoggingAdapter().log(Level.INFO, "Loading plugin configuration settings & locales...");
return reload().thenApply(loadedSettings -> { initialized.set(reload().join());
if (loadedSettings) { if (initialized.get()) {
logger.showDebugLogs(settings.getBooleanValue(Settings.ConfigOption.DEBUG_LOGGING)); logger.showDebugLogs(settings.getBooleanValue(Settings.ConfigOption.DEBUG_LOGGING));
getLoggingAdapter().log(Level.INFO, "Successfully loaded plugin configuration settings & locales"); getLoggingAdapter().log(Level.INFO, "Successfully loaded plugin configuration settings & locales");
} else { } else {
getLoggingAdapter().log(Level.SEVERE, "Failed to load plugin configuration settings and/or locales"); throw new HuskSyncInitializationException("Failed to load plugin configuration settings and/or locales");
}
return loadedSettings;
}).join();
}).thenApply(succeeded -> {
// Prepare data adapter
if (succeeded) {
if (settings.getBooleanValue(Settings.ConfigOption.SYNCHRONIZATION_COMPRESS_DATA)) {
dataAdapter = new CompressedDataAdapter();
} else {
dataAdapter = new JsonDataAdapter();
}
} }
return succeeded;
}).thenApply(succeeded -> {
// Prepare event cannon
if (succeeded) {
eventCannon = new BukkitEventCannon();
}
return succeeded;
}).thenApply(succeeded -> {
// Prepare data editor
if (succeeded) {
dataEditor = new DataEditor(locales);
}
return succeeded;
}).thenApply(succeeded -> {
// Prepare migrators
if (succeeded) {
availableMigrators = new ArrayList<>();
availableMigrators.add(new LegacyMigrator(this));
final Plugin mySqlPlayerDataBridge = Bukkit.getPluginManager().getPlugin("MySqlPlayerDataBridge");
if (mySqlPlayerDataBridge != null) {
availableMigrators.add(new MpdbMigrator(this, mySqlPlayerDataBridge));
}
}
return succeeded;
}).thenApply(succeeded -> {
// Establish connection to the database
if (succeeded) {
this.database = new MySqlDatabase(settings, resourceReader, logger, dataAdapter, eventCannon);
getLoggingAdapter().log(Level.INFO, "Attempting to establish connection to the database...");
final CompletableFuture<Boolean> databaseConnectFuture = new CompletableFuture<>();
Bukkit.getScheduler().runTask(this, () -> {
final boolean initialized = this.database.initialize();
if (!initialized) {
getLoggingAdapter().log(Level.SEVERE, "Failed to establish a connection to the database. " + "Please check the supplied database credentials in the config file");
databaseConnectFuture.completeAsync(() -> false);
return;
}
getLoggingAdapter().log(Level.INFO, "Successfully established a connection to the database");
databaseConnectFuture.completeAsync(() -> true);
});
return databaseConnectFuture.join();
}
return false;
}).thenApply(succeeded -> {
// Establish connection to the Redis server
if (succeeded) {
this.redisManager = new RedisManager(this);
getLoggingAdapter().log(Level.INFO, "Attempting to establish connection to the Redis server...");
return this.redisManager.initialize().thenApply(initialized -> {
if (!initialized) {
getLoggingAdapter().log(Level.SEVERE, "Failed to establish a connection to the Redis server. " + "Please check the supplied Redis credentials in the config file");
return false;
}
getLoggingAdapter().log(Level.INFO, "Successfully established a connection to the Redis server");
return true;
}).join();
}
return false;
}).thenApply(succeeded -> {
// Register events
if (succeeded) {
getLoggingAdapter().log(Level.INFO, "Registering events...");
this.eventListener = new BukkitEventListener(this);
getLoggingAdapter().log(Level.INFO, "Successfully registered events listener");
}
return succeeded;
}).thenApply(succeeded -> {
// Register permissions
if (succeeded) {
getLoggingAdapter().log(Level.INFO, "Registering permissions & commands...");
Arrays.stream(Permission.values()).forEach(permission -> getServer().getPluginManager().addPermission(new org.bukkit.permissions.Permission(permission.node, switch (permission.defaultAccess) {
case EVERYONE -> PermissionDefault.TRUE;
case NOBODY -> PermissionDefault.FALSE;
case OPERATORS -> PermissionDefault.OP;
})));
// Register commands // Prepare data adapter
for (final BukkitCommandType bukkitCommandType : BukkitCommandType.values()) { if (settings.getBooleanValue(Settings.ConfigOption.SYNCHRONIZATION_COMPRESS_DATA)) {
final PluginCommand pluginCommand = getCommand(bukkitCommandType.commandBase.command); dataAdapter = new CompressedDataAdapter();
if (pluginCommand != null) { } else {
new BukkitCommand(bukkitCommandType.commandBase, this).register(pluginCommand); dataAdapter = new JsonDataAdapter();
}
}
getLoggingAdapter().log(Level.INFO, "Successfully registered permissions & commands");
} }
return succeeded;
}).thenApply(succeeded -> { // Prepare event cannon
if (succeeded && Bukkit.getPluginManager().getPlugin("Plan") != null) { eventCannon = new BukkitEventCannon();
// Prepare data editor
dataEditor = new DataEditor(locales);
// Prepare migrators
availableMigrators = new ArrayList<>();
availableMigrators.add(new LegacyMigrator(this));
final Plugin mySqlPlayerDataBridge = Bukkit.getPluginManager().getPlugin("MySqlPlayerDataBridge");
if (mySqlPlayerDataBridge != null) {
availableMigrators.add(new MpdbMigrator(this, mySqlPlayerDataBridge));
}
// Prepare database connection
this.database = new MySqlDatabase(settings, resourceReader, logger, dataAdapter, eventCannon);
getLoggingAdapter().log(Level.INFO, "Attempting to establish connection to the database...");
initialized.set(this.database.initialize());
if (initialized.get()) {
getLoggingAdapter().log(Level.INFO, "Successfully established a connection to the database");
} else {
throw new HuskSyncInitializationException("Failed to establish a connection to the database. " +
"Please check the supplied database credentials in the config file");
}
// Prepare redis connection
this.redisManager = new RedisManager(this);
getLoggingAdapter().log(Level.INFO, "Attempting to establish connection to the Redis server...");
initialized.set(this.redisManager.initialize().join());
if (initialized.get()) {
getLoggingAdapter().log(Level.INFO, "Successfully established a connection to the Redis server");
} else {
throw new HuskSyncInitializationException("Failed to establish a connection to the Redis server. " +
"Please check the supplied Redis credentials in the config file");
}
// Register events
getLoggingAdapter().log(Level.INFO, "Registering events...");
this.eventListener = new BukkitEventListener(this);
getLoggingAdapter().log(Level.INFO, "Successfully registered events listener");
// Register permissions
getLoggingAdapter().log(Level.INFO, "Registering permissions & commands...");
Arrays.stream(Permission.values()).forEach(permission -> getServer().getPluginManager()
.addPermission(new org.bukkit.permissions.Permission(permission.node, switch (permission.defaultAccess) {
case EVERYONE -> PermissionDefault.TRUE;
case NOBODY -> PermissionDefault.FALSE;
case OPERATORS -> PermissionDefault.OP;
})));
// Register commands
for (final BukkitCommandType bukkitCommandType : BukkitCommandType.values()) {
final PluginCommand pluginCommand = getCommand(bukkitCommandType.commandBase.command);
if (pluginCommand != null) {
new BukkitCommand(bukkitCommandType.commandBase, this).register(pluginCommand);
}
}
getLoggingAdapter().log(Level.INFO, "Successfully registered permissions & commands");
// Hook into plan
if (Bukkit.getPluginManager().getPlugin("Plan") != null) {
getLoggingAdapter().log(Level.INFO, "Enabling Plan integration..."); getLoggingAdapter().log(Level.INFO, "Enabling Plan integration...");
new PlanHook(database, logger).hookIntoPlan(); new PlanHook(database, logger).hookIntoPlan();
getLoggingAdapter().log(Level.INFO, "Plan integration enabled!"); getLoggingAdapter().log(Level.INFO, "Plan integration enabled!");
} }
return succeeded;
}).thenApply(succeeded -> {
// Check for updates // Check for updates
if (succeeded && settings.getBooleanValue(Settings.ConfigOption.CHECK_FOR_UPDATES)) { if (settings.getBooleanValue(Settings.ConfigOption.CHECK_FOR_UPDATES)) {
getLoggingAdapter().log(Level.INFO, "Checking for updates..."); getLoggingAdapter().log(Level.INFO, "Checking for updates...");
new UpdateChecker(getPluginVersion(), getLoggingAdapter()).logToConsole(); CompletableFuture.runAsync(() -> new UpdateChecker(getPluginVersion(), getLoggingAdapter()).logToConsole());
} }
return succeeded; } catch (HuskSyncInitializationException exception) {
}).thenAccept(succeeded -> { getLoggingAdapter().log(Level.SEVERE, exception.getMessage());
// Handle failed initialization initialized.set(false);
if (!succeeded) { } catch (Exception exception) {
getLoggingAdapter().log(Level.SEVERE, "An unhandled exception occurred initializing HuskSync!", exception);
initialized.set(false);
} finally {
// Validate initialization
if (initialized.get()) {
getLoggingAdapter().log(Level.INFO, "Successfully enabled HuskSync v" + getPluginVersion());
} else {
getLoggingAdapter().log(Level.SEVERE, "Failed to initialize HuskSync. The plugin will now be disabled"); getLoggingAdapter().log(Level.SEVERE, "Failed to initialize HuskSync. The plugin will now be disabled");
getServer().getPluginManager().disablePlugin(this); getServer().getPluginManager().disablePlugin(this);
} else {
getLoggingAdapter().log(Level.INFO, "Successfully enabled HuskSync v" + getPluginVersion());
} }
}).exceptionally(exception -> { }
getLoggingAdapter().log(Level.SEVERE, "An exception occurred initializing HuskSync. (" + exception.getMessage() + ") The plugin will now be disabled.");
exception.printStackTrace();
getServer().getPluginManager().disablePlugin(this);
return null;
});
} }
@Override @Override

View File

@@ -0,0 +1,12 @@
package net.william278.husksync;
import org.jetbrains.annotations.NotNull;
/**
* Indicates an exception occurred while initialising the HuskSync plugin
*/
public class HuskSyncInitializationException extends RuntimeException {
public HuskSyncInitializationException(@NotNull String message) {
super(message);
}
}

View File

@@ -114,7 +114,7 @@ public class MySqlDatabase extends Database {
getLogger().log(Level.SEVERE, "Failed to perform database setup: " + e.getMessage()); getLogger().log(Level.SEVERE, "Failed to perform database setup: " + e.getMessage());
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); getLogger().log(Level.SEVERE, "An unhandled exception occurred during database setup!", e);
} }
return false; return false;
} }