Added safety checking to config.yml

If there is an enum setting, e.g. FLATFILE instead of YAML in a config,
the config will not load and the resulting object will be null. This
adds a check to the main config.yml load and disables the plugin if
there is a problem.
This commit is contained in:
tastybento 2018-10-30 15:16:13 -07:00
parent 49fa079f13
commit 935704d7dd
3 changed files with 12 additions and 5 deletions

View File

@ -1,5 +1,7 @@
package world.bentobox.bentobox; package world.bentobox.bentobox;
import java.util.Optional;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
@ -30,8 +32,6 @@ import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.managers.SchemsManager; import world.bentobox.bentobox.managers.SchemsManager;
import world.bentobox.bentobox.util.heads.HeadGetter; import world.bentobox.bentobox.util.heads.HeadGetter;
import java.util.Optional;
/** /**
* Main BentoBox class * Main BentoBox class
* @author tastybento, Poslovitch * @author tastybento, Poslovitch
@ -78,6 +78,12 @@ public class BentoBox extends JavaPlugin {
// Load settings from config.yml. This will check if there are any issues with it too. // Load settings from config.yml. This will check if there are any issues with it too.
settings = new Config<>(this, Settings.class).loadConfigObject(""); 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);
return;
}
// 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)

View File

@ -89,7 +89,7 @@ public class Settings implements DataObject {
@ConfigComment("island unnecessarily.") @ConfigComment("island unnecessarily.")
@ConfigEntry(path = "general.allow-obsidian-scooping") @ConfigEntry(path = "general.allow-obsidian-scooping")
private boolean allowObsidianScooping = true; private boolean allowObsidianScooping = true;
@ConfigComment("Rank required to use a command. e.g., use the invite command. Default is owner rank is required.") @ConfigComment("Rank required to use a command. e.g., use the invite command. Default is owner rank is required.")
@ConfigEntry(path = "general.rank-command") @ConfigEntry(path = "general.rank-command")
private Map<String, Integer> rankCommand = new HashMap<>(); private Map<String, Integer> rankCommand = new HashMap<>();

View File

@ -1,10 +1,10 @@
package world.bentobox.bentobox.database; package world.bentobox.bentobox.database;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.database.yaml.YamlDatabase;
import world.bentobox.bentobox.database.json.JSONDatabase; import world.bentobox.bentobox.database.json.JSONDatabase;
import world.bentobox.bentobox.database.mongodb.MongoDBDatabase; import world.bentobox.bentobox.database.mongodb.MongoDBDatabase;
import world.bentobox.bentobox.database.mysql.MySQLDatabase; import world.bentobox.bentobox.database.mysql.MySQLDatabase;
import world.bentobox.bentobox.database.yaml.YamlDatabase;
public interface DatabaseSetup { public interface DatabaseSetup {
@ -15,8 +15,9 @@ public interface DatabaseSetup {
* @return Database type * @return Database type
*/ */
static DatabaseSetup getDatabase() { static DatabaseSetup getDatabase() {
BentoBox plugin = BentoBox.getInstance();
for(DatabaseType type : DatabaseType.values()){ for(DatabaseType type : DatabaseType.values()){
if(type == BentoBox.getInstance().getSettings().getDatabaseType()) { if(type == plugin.getSettings().getDatabaseType()) {
return type.database; return type.database;
} }
} }