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

View File

@ -76,10 +76,25 @@ public class AddonsManager {
public void enableAddons() { public void enableAddons() {
plugin.log("Enabling addons..."); plugin.log("Enabling addons...");
addons.forEach(addon -> { addons.forEach(addon -> {
addon.onEnable(); try {
Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.ENABLE).build()); addon.onEnable();
addon.setEnabled(true); Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.ENABLE).build());
plugin.log("Enabling " + addon.getDescription().getName() + "..."); 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."); plugin.log("Addons successfully enabled.");
} }

View File

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