From 281d16c175da24e0427bc4c2c69a82a38a6a3504 Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Wed, 6 Feb 2019 16:46:17 +0100 Subject: [PATCH] Added ability to reload BentoBox's configuration using /bbox reload #370 --- .../world/bentobox/bentobox/BentoBox.java | 44 ++++++++++++++----- .../bentobox/api/configuration/Config.java | 3 ++ .../commands/BentoBoxReloadCommand.java | 12 +++-- src/main/resources/locales/en-US.yml | 3 +- 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/BentoBox.java b/src/main/java/world/bentobox/bentobox/BentoBox.java index 484d32bcd..7765900ac 100644 --- a/src/main/java/world/bentobox/bentobox/BentoBox.java +++ b/src/main/java/world/bentobox/bentobox/BentoBox.java @@ -101,14 +101,13 @@ public class BentoBox extends JavaPlugin { // Load Flags flagsManager = new FlagsManager(this); - // Load settings from config.yml. This will check if there are any issues with it too. - settings = new Config<>(this, Settings.class).loadConfigObject(""); - if (settings == null) { - // Settings did no load correctly. Disable plugin. - logError("Settings did not load correctly - disabling plugin - please check config.yml"); - getPluginLoader().disablePlugin(this); + if (!loadSettings()) { + // We're aborting the load. return; } + // Saving the config now. + new Config<>(this, Settings.class).saveConfigObject(settings); + // Start Database managers playersManager = new PlayersManager(this); // Check if this plugin is now disabled (due to bad database handling) @@ -223,10 +222,6 @@ public class BentoBox extends JavaPlugin { if (islandsManager != null) { islandsManager.shutdown(); } - // Save settings - ensures admins always have the latest config file - if (settings != null) { - new Config<>(this, Settings.class).saveConfigObject(settings); - } } /** @@ -302,6 +297,25 @@ public class BentoBox extends JavaPlugin { return settings; } + /** + * Loads the settings from the config file. + * If it fails, it can shut the plugin down. + * @return {@code true} if it loaded successfully. + * @since 1.3.0 + */ + public boolean loadSettings() { + log("Loading Settings from config.yml..."); + // Load settings from config.yml. This will check if there are any issues with it too. + settings = new Config<>(this, Settings.class).loadConfigObject(); + if (settings == null) { + // Settings did not load correctly. Disable plugin. + logError("Settings did not load correctly - disabling plugin - please check config.yml"); + getPluginLoader().disablePlugin(this); + return false; + } + return true; + } + /** * @return the notifier */ @@ -387,6 +401,8 @@ public class BentoBox extends JavaPlugin { return Optional.ofNullable(metrics); } + // Overriding default JavaPlugin methods + /* (non-Javadoc) * @see org.bukkit.plugin.java.JavaPlugin#getDefaultWorldGenerator(java.lang.String, java.lang.String) */ @@ -394,4 +410,12 @@ public class BentoBox extends JavaPlugin { public ChunkGenerator getDefaultWorldGenerator(String worldName, String id) { return addonsManager.getDefaultWorldGenerator(worldName, id); } + + /* (non-Javadoc) + * @see org.bukkit.plugin.java.JavaPlugin#reloadConfig() + */ + @Override + public void reloadConfig() { + loadSettings(); + } } diff --git a/src/main/java/world/bentobox/bentobox/api/configuration/Config.java b/src/main/java/world/bentobox/bentobox/api/configuration/Config.java index f09a0e469..a769fa4a6 100644 --- a/src/main/java/world/bentobox/bentobox/api/configuration/Config.java +++ b/src/main/java/world/bentobox/bentobox/api/configuration/Config.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.List; import java.util.logging.Logger; +import org.eclipse.jdt.annotation.Nullable; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.database.AbstractDatabaseHandler; @@ -55,6 +56,7 @@ public class Config { * @param uniqueId - unique id of the object * @return the object or null if it cannot be loaded */ + @Nullable public T loadConfigObject(String uniqueId) { try { return handler.loadObject(uniqueId); @@ -71,6 +73,7 @@ public class Config { * Loads a config object * @return the object or null if it cannot be loaded */ + @Nullable public T loadConfigObject() { return loadConfigObject(""); } diff --git a/src/main/java/world/bentobox/bentobox/commands/BentoBoxReloadCommand.java b/src/main/java/world/bentobox/bentobox/commands/BentoBoxReloadCommand.java index 092476f7c..29b68a4ec 100644 --- a/src/main/java/world/bentobox/bentobox/commands/BentoBoxReloadCommand.java +++ b/src/main/java/world/bentobox/bentobox/commands/BentoBoxReloadCommand.java @@ -7,15 +7,15 @@ import world.bentobox.bentobox.api.commands.ConfirmableCommand; import world.bentobox.bentobox.api.user.User; /** - * Reloads addons and localization. + * Reloads settings, addons and localization. * * @author tastybento */ public class BentoBoxReloadCommand extends ConfirmableCommand { /** - * Reloads locales command - * @param parent - command parent + * Reloads settings, addons and localization command + * @param parent command parent */ public BentoBoxReloadCommand(CompositeCommand parent) { super(parent, "reload"); @@ -30,6 +30,10 @@ public class BentoBoxReloadCommand extends ConfirmableCommand { @Override public boolean execute(User user, String label, List args) { this.askConfirmation(user, () -> { + // Reload settings + getPlugin().loadSettings(); + user.sendMessage("commands.bentobox.reload.settings-reloaded"); + // Reload addons getPlugin().getAddonsManager().reloadAddons(); user.sendMessage("commands.bentobox.reload.addons-reloaded"); @@ -38,6 +42,6 @@ public class BentoBoxReloadCommand extends ConfirmableCommand { getPlugin().getLocalesManager().reloadLanguages(); user.sendMessage("commands.bentobox.reload.locales-reloaded"); }); - return false; + return true; } } diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index 02eff2190..8771b73aa 100644 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -222,9 +222,10 @@ commands: about: description: "display copyright and license info" reload: - description: "reloads addons (if supported) and locale files" + description: "reloads settings, addons (if supported) and locales" locales-reloaded: "&2Languages reloaded." addons-reloaded: "&2Addons reloaded." + settings-reloaded: "&2Settings reloaded." version: plugin-version: "&2BentoBox version: &3[version]" description: "display info"