Added ability to reload BentoBox's configuration using /bbox reload

#370
This commit is contained in:
Florian CUNY 2019-02-06 16:46:17 +01:00
parent ca2bb18a56
commit 281d16c175
4 changed files with 47 additions and 15 deletions

View File

@ -101,14 +101,13 @@ public class BentoBox extends JavaPlugin {
// Load Flags // Load Flags
flagsManager = new FlagsManager(this); flagsManager = new FlagsManager(this);
// Load settings from config.yml. This will check if there are any issues with it too. if (!loadSettings()) {
settings = new Config<>(this, Settings.class).loadConfigObject(""); // We're aborting the load.
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);
return; return;
} }
// Saving the config now.
new Config<>(this, Settings.class).saveConfigObject(settings);
// Start Database managers // Start Database managers
playersManager = new PlayersManager(this); playersManager = new PlayersManager(this);
// Check if this plugin is now disabled (due to bad database handling) // Check if this plugin is now disabled (due to bad database handling)
@ -223,10 +222,6 @@ public class BentoBox extends JavaPlugin {
if (islandsManager != null) { if (islandsManager != null) {
islandsManager.shutdown(); 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; 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 * @return the notifier
*/ */
@ -387,6 +401,8 @@ public class BentoBox extends JavaPlugin {
return Optional.ofNullable(metrics); return Optional.ofNullable(metrics);
} }
// Overriding default JavaPlugin methods
/* (non-Javadoc) /* (non-Javadoc)
* @see org.bukkit.plugin.java.JavaPlugin#getDefaultWorldGenerator(java.lang.String, java.lang.String) * @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) { public ChunkGenerator getDefaultWorldGenerator(String worldName, String id) {
return addonsManager.getDefaultWorldGenerator(worldName, id); return addonsManager.getDefaultWorldGenerator(worldName, id);
} }
/* (non-Javadoc)
* @see org.bukkit.plugin.java.JavaPlugin#reloadConfig()
*/
@Override
public void reloadConfig() {
loadSettings();
}
} }

View File

@ -6,6 +6,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.database.AbstractDatabaseHandler; import world.bentobox.bentobox.database.AbstractDatabaseHandler;
@ -55,6 +56,7 @@ public class Config<T> {
* @param uniqueId - unique id of the object * @param uniqueId - unique id of the object
* @return the object or null if it cannot be loaded * @return the object or null if it cannot be loaded
*/ */
@Nullable
public T loadConfigObject(String uniqueId) { public T loadConfigObject(String uniqueId) {
try { try {
return handler.loadObject(uniqueId); return handler.loadObject(uniqueId);
@ -71,6 +73,7 @@ public class Config<T> {
* Loads a config object * Loads a config object
* @return the object or null if it cannot be loaded * @return the object or null if it cannot be loaded
*/ */
@Nullable
public T loadConfigObject() { public T loadConfigObject() {
return loadConfigObject(""); return loadConfigObject("");
} }

View File

@ -7,15 +7,15 @@ import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
/** /**
* Reloads addons and localization. * Reloads settings, addons and localization.
* *
* @author tastybento * @author tastybento
*/ */
public class BentoBoxReloadCommand extends ConfirmableCommand { public class BentoBoxReloadCommand extends ConfirmableCommand {
/** /**
* Reloads locales command * Reloads settings, addons and localization command
* @param parent - command parent * @param parent command parent
*/ */
public BentoBoxReloadCommand(CompositeCommand parent) { public BentoBoxReloadCommand(CompositeCommand parent) {
super(parent, "reload"); super(parent, "reload");
@ -30,6 +30,10 @@ public class BentoBoxReloadCommand extends ConfirmableCommand {
@Override @Override
public boolean execute(User user, String label, List<String> args) { public boolean execute(User user, String label, List<String> args) {
this.askConfirmation(user, () -> { this.askConfirmation(user, () -> {
// Reload settings
getPlugin().loadSettings();
user.sendMessage("commands.bentobox.reload.settings-reloaded");
// Reload addons // Reload addons
getPlugin().getAddonsManager().reloadAddons(); getPlugin().getAddonsManager().reloadAddons();
user.sendMessage("commands.bentobox.reload.addons-reloaded"); user.sendMessage("commands.bentobox.reload.addons-reloaded");
@ -38,6 +42,6 @@ public class BentoBoxReloadCommand extends ConfirmableCommand {
getPlugin().getLocalesManager().reloadLanguages(); getPlugin().getLocalesManager().reloadLanguages();
user.sendMessage("commands.bentobox.reload.locales-reloaded"); user.sendMessage("commands.bentobox.reload.locales-reloaded");
}); });
return false; return true;
} }
} }

View File

@ -222,9 +222,10 @@ commands:
about: about:
description: "display copyright and license info" description: "display copyright and license info"
reload: reload:
description: "reloads addons (if supported) and locale files" description: "reloads settings, addons (if supported) and locales"
locales-reloaded: "&2Languages reloaded." locales-reloaded: "&2Languages reloaded."
addons-reloaded: "&2Addons reloaded." addons-reloaded: "&2Addons reloaded."
settings-reloaded: "&2Settings reloaded."
version: version:
plugin-version: "&2BentoBox version: &3[version]" plugin-version: "&2BentoBox version: &3[version]"
description: "display info" description: "display info"