Skip to content

Commit

Permalink
Only enable/disable addons if there is at least one addon loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
Poslovitch committed Dec 22, 2018
1 parent bfa19dc commit 5854183
Showing 1 changed file with 50 additions and 41 deletions.
91 changes: 50 additions & 41 deletions src/main/java/world/bentobox/bentobox/managers/AddonsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public AddonsManager(BentoBox plugin) {
loaders = new HashMap<>();
}

//TODO: add addon reload

/**
* Loads all the addons from the addons folder
*/
Expand All @@ -57,36 +59,41 @@ public void loadAddons() {
}
Arrays.stream(Objects.requireNonNull(f.listFiles())).filter(x -> !x.isDirectory() && x.getName().endsWith(".jar")).forEach(this::loadAddon);
plugin.log("Loaded " + addons.size() + " addons.");
sortAddons();

if (!addons.isEmpty()) {
sortAddons();
}
}

/**
* Enables all the addons
*/
public void enableAddons() {
plugin.log("Enabling addons...");
addons.forEach(addon -> {
try {
addon.onEnable();
Bukkit.getPluginManager().callEvent(new AddonEvent().builder().addon(addon).reason(AddonEvent.Reason.ENABLE).build());
addon.setState(Addon.State.ENABLED);
plugin.log("Enabling " + addon.getDescription().getName() + "...");
} catch (NoClassDefFoundError | NoSuchMethodError 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.getClass().getSimpleName() + " - " + e.getMessage() + " - " + e.getCause());
}
});
plugin.log("Addons successfully enabled.");
if (!addons.isEmpty()) {
plugin.log("Enabling addons...");
addons.forEach(addon -> {
try {
addon.onEnable();
Bukkit.getPluginManager().callEvent(new AddonEvent().builder().addon(addon).reason(AddonEvent.Reason.ENABLE).build());
addon.setState(Addon.State.ENABLED);
plugin.log("Enabling " + addon.getDescription().getName() + "...");
} catch (NoClassDefFoundError | NoSuchMethodError 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.getClass().getSimpleName() + " - " + e.getMessage() + " - " + e.getCause());
}
});
plugin.log("Addons successfully enabled.");
}
}

/**
Expand Down Expand Up @@ -149,24 +156,26 @@ private void loadAddon(File f) {
* Disable all the enabled addons
*/
public void disableAddons() {
plugin.log("Disabling addons...");
// Unload addons
addons.forEach(addon -> {
if (addon.isEnabled()) {
addon.onDisable();
Bukkit.getPluginManager().callEvent(new AddonEvent().builder().addon(addon).reason(AddonEvent.Reason.DISABLE).build());
plugin.log("Disabling " + addon.getDescription().getName() + "...");
}
});
if (!addons.isEmpty()) {
plugin.log("Disabling addons...");
// Disable addons
addons.forEach(addon -> {
if (addon.isEnabled()) {
addon.onDisable();
Bukkit.getPluginManager().callEvent(new AddonEvent().builder().addon(addon).reason(AddonEvent.Reason.DISABLE).build());
plugin.log("Disabling " + addon.getDescription().getName() + "...");
}
});

loaders.values().forEach(l -> {
try {
l.close();
} catch (IOException ignore) {
// Ignore
}
});
plugin.log("Addons successfully disabled.");
loaders.values().forEach(l -> {
try {
l.close();
} catch (IOException ignore) {
// Ignore
}
});
plugin.log("Addons successfully disabled.");
}
}

public List<Addon> getAddons() {
Expand Down

0 comments on commit 5854183

Please sign in to comment.