Bunch of fixes to the AddonsManager

Renamed loader list to loaders.
Removed #loadAddonsFromFile().
Fixed the error handling when enabling an addon.
Fixed #disableAddons() disabling not-enabled addons.
Renamed #setLoader() into #setLoaders()
This commit is contained in:
Florian CUNY 2018-10-28 13:42:13 +01:00
parent b87416d030
commit 12d1909a22

View File

@ -35,26 +35,14 @@ public class AddonsManager {
private static final String LOCALE_FOLDER = "locales"; private static final String LOCALE_FOLDER = "locales";
private List<Addon> addons; private List<Addon> addons;
private List<AddonClassLoader> loader; private List<AddonClassLoader> loaders;
private final Map<String, Class<?>> classes = new HashMap<>(); private final Map<String, Class<?>> classes = new HashMap<>();
private BentoBox plugin; private BentoBox plugin;
public AddonsManager(BentoBox plugin) { public AddonsManager(BentoBox plugin) {
this.plugin = plugin; this.plugin = plugin;
addons = new ArrayList<>(); addons = new ArrayList<>();
loader = new ArrayList<>(); loaders = new ArrayList<>();
loadAddonsFromFile();
}
public void loadAddonsFromFile() {
plugin.log("Loading addons...");
File f = new File(plugin.getDataFolder(), "addons");
if (!f.exists() && !f.mkdirs()) {
plugin.logError("Cannot make addons folder!");
return;
}
Arrays.stream(Objects.requireNonNull(f.listFiles())).filter(x -> !x.isDirectory() && x.getName().endsWith(".jar")).forEach(this::loadAddon);
sortAddons();
} }
/** /**
@ -62,13 +50,14 @@ public class AddonsManager {
*/ */
public void loadAddons() { public void loadAddons() {
plugin.log("Loading addons..."); plugin.log("Loading addons...");
// Run each onLoad File f = new File(plugin.getDataFolder(), "addons");
addons.forEach(addon -> { if (!f.exists() && !f.mkdirs()) {
addon.onLoad(); plugin.logError("Cannot create addons folder!");
Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.LOAD).build()); return;
plugin.log("Loading " + addon.getDescription().getName() + "..."); }
}); Arrays.stream(Objects.requireNonNull(f.listFiles())).filter(x -> !x.isDirectory() && x.getName().endsWith(".jar")).forEach(this::loadAddon);
plugin.log("Loaded " + addons.size() + " addons."); plugin.log("Loaded " + addons.size() + " addons.");
sortAddons();
} }
/** /**
@ -82,19 +71,19 @@ public class AddonsManager {
Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.ENABLE).build()); Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.ENABLE).build());
addon.setState(Addon.State.ENABLED); addon.setState(Addon.State.ENABLED);
plugin.log("Enabling " + addon.getDescription().getName() + "..."); plugin.log("Enabling " + addon.getDescription().getName() + "...");
} catch (NoClassDefFoundError e) { } catch (NoClassDefFoundError | NoSuchMethodError e) {
// Looks like the addon is outdated, because it tries to refer to missing classes. // Looks like the addon is outdated, because it tries to refer to missing classes.
// Set the AddonState as "INCOMPATIBLE". // Set the AddonState as "INCOMPATIBLE".
addon.setState(Addon.State.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("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: The addon is referring to no longer existing classes.");
plugin.log("NOTE: DO NOT report this as a bug from BentoBox."); plugin.log("NOTE: DO NOT report this as a bug from BentoBox.");
} catch (Exception e) { } catch (Exception | Error e) {
// Unhandled exception. We'll give a bit of debug here. // Unhandled exception. We'll give a bit of debug here.
// Set the AddonState as "ERROR". // Set the AddonState as "ERROR".
addon.setState(Addon.State.ERROR); addon.setState(Addon.State.ERROR);
plugin.log("Skipping " + addon.getDescription().getName() + " due to an unhandled exception..."); plugin.log("Skipping " + addon.getDescription().getName() + " due to an unhandled exception...");
plugin.log("STACKTRACE: " + e.getMessage() + " - " + e.getCause()); plugin.log("STACKTRACE: " + e.getClass().getSimpleName() + " - " + e.getMessage() + " - " + e.getCause());
} }
}); });
plugin.log("Addons successfully enabled."); plugin.log("Addons successfully enabled.");
@ -131,7 +120,7 @@ public class AddonsManager {
// Load the addon // Load the addon
AddonClassLoader addonClassLoader = new AddonClassLoader(this, data, f, this.getClass().getClassLoader()); AddonClassLoader addonClassLoader = new AddonClassLoader(this, data, f, this.getClass().getClassLoader());
// Add to the list of loaders // Add to the list of loaders
loader.add(addonClassLoader); loaders.add(addonClassLoader);
// Get the addon itself // Get the addon itself
addon = addonClassLoader.getAddon(); addon = addonClassLoader.getAddon();
@ -149,6 +138,8 @@ public class AddonsManager {
Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.LOAD).build()); Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.LOAD).build());
// Add it to the list of addons // Add it to the list of addons
addons.add(addon); addons.add(addon);
// Run the onLoad.
addon.onLoad();
} catch (Exception e) { } catch (Exception e) {
plugin.log(e.getMessage()); plugin.log(e.getMessage());
} }
@ -161,12 +152,14 @@ public class AddonsManager {
plugin.log("Disabling addons..."); plugin.log("Disabling addons...");
// Unload addons // Unload addons
addons.forEach(addon -> { addons.forEach(addon -> {
addon.onDisable(); if (addon.isEnabled()) {
Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.DISABLE).build()); addon.onDisable();
plugin.log("Disabling " + addon.getDescription().getName() + "..."); Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.DISABLE).build());
plugin.log("Disabling " + addon.getDescription().getName() + "...");
}
}); });
loader.forEach(l -> { loaders.forEach(l -> {
try { try {
l.close(); l.close();
} catch (IOException ignore) { } catch (IOException ignore) {
@ -180,12 +173,12 @@ public class AddonsManager {
return addons; return addons;
} }
public List<AddonClassLoader> getLoader() { public List<AddonClassLoader> getLoaders() {
return loader; return loaders;
} }
public void setLoader(List<AddonClassLoader> loader) { public void setLoaders(List<AddonClassLoader> loaders) {
this.loader = loader; this.loaders = loaders;
} }
/** /**
@ -194,7 +187,7 @@ public class AddonsManager {
* @return Class - the class * @return Class - the class
*/ */
public Class<?> getClassByName(final String name) { public Class<?> getClassByName(final String name) {
return classes.getOrDefault(name, loader.stream().map(l -> l.findClass(name, false)).filter(Objects::nonNull).findFirst().orElse(null)); return classes.getOrDefault(name, loaders.stream().map(l -> l.findClass(name, false)).filter(Objects::nonNull).findFirst().orElse(null));
} }
/** /**