Made use of Addon.State

Fixes #281
Replaced Addon#setEnabled(boolean) with Addon#setState(Addon.State).

Now, if an exception gets thrown while enabling an addon, it'll automatically stop enabling it and set its state to "INCOMPATIBLE" or "ERROR". These values could later be get to be displayed in the version command.
This commit is contained in:
Florian CUNY 2018-10-28 11:12:00 +01:00
parent e930510243
commit 6dd02a15b7
3 changed files with 32 additions and 18 deletions

View File

@ -27,14 +27,14 @@ import world.bentobox.bentobox.managers.PlayersManager;
public abstract class Addon implements AddonInterface {
private static final String ADDON_CONFIG_FILENAME = "config.yml";
private boolean enabled;
private State state;
private AddonDescription description;
private FileConfiguration config;
private File dataFolder;
private File file;
public Addon() {
enabled = false;
state = State.DISABLED;
}
public BentoBox getPlugin() {
@ -46,7 +46,7 @@ public abstract class Addon implements AddonInterface {
*
* @author Poslovitch
*/
enum State {
public enum State {
/**
* The addon has been correctly enabled and is now fully working.
*/
@ -121,7 +121,7 @@ public abstract class Addon implements AddonInterface {
}
public boolean isEnabled() {
return enabled;
return state == State.ENABLED;
}
/**
@ -276,19 +276,18 @@ public abstract class Addon implements AddonInterface {
/**
* Set this addons description
*
* @param desc - description
* @param description - description
*/
public void setDescription(AddonDescription desc) {
description = desc;
public void setDescription(AddonDescription description) {
this.description = description;
}
/**
* Set whether this addon is enabled or not
*
* @param enabled - true if enabled
* Sets the addon's state.
* @param state the state to set
*/
public void setEnabled(boolean enabled) {
this.enabled = enabled;
public void setState(State state) {
this.state = state;
}
/**

View File

@ -76,10 +76,25 @@ public class AddonsManager {
public void enableAddons() {
plugin.log("Enabling addons...");
addons.forEach(addon -> {
addon.onEnable();
Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.ENABLE).build());
addon.setEnabled(true);
plugin.log("Enabling " + addon.getDescription().getName() + "...");
try {
addon.onEnable();
Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.ENABLE).build());
addon.setState(Addon.State.ENABLED);
plugin.log("Enabling " + addon.getDescription().getName() + "...");
} catch (NoClassDefFoundError e) {
// Looks like the addon is outdated, because it tries to refer to missing classes.
// Set the AddonState as "INCOMPATIBLE".
addon.setState(Addon.State.INCOMPATIBLE);
plugin.log("Skipping " + addon.getDescription().getName() + " as it is incompatible with the current version of BentoBox or of server software...");
plugin.log("NOTE: The addon is referring to no longer existing classes.");
plugin.log("NOTE: DO NOT report this as a bug from BentoBox.");
} catch (Exception e) {
// Unhandled exception. We'll give a bit of debug here.
// Set the AddonState as "ERROR".
addon.setState(Addon.State.ERROR);
plugin.log("Skipping " + addon.getDescription().getName() + " due to an unhandled exception...");
plugin.log("STACKTRACE: " + e.getMessage() + " - " + e.getCause());
}
});
plugin.log("Addons successfully enabled.");
}

View File

@ -250,9 +250,9 @@ public class AddonTest {
@Test
public void testSetEnabled() {
TestClass test = new TestClass();
test.setEnabled(false);
test.setState(Addon.State.DISABLED);
assertFalse(test.isEnabled());
test.setEnabled(true);
test.setState(Addon.State.ENABLED);
assertTrue(test.isEnabled());
}