From 36751d5573ab2af32b0cfa4ff7d124a0cccd8f60 Mon Sep 17 00:00:00 2001 From: BONNe Date: Fri, 1 Apr 2022 00:12:27 +0300 Subject: [PATCH] Solve crashes with Addon#allLoaded call (#1959) If some addon has code in Addon#allLoaded that crashes the call, then it did not disable addon as well as did not call allLoaded for every other addon that was left in the list. This should be solved by adding an extra try-catch. --- .../bentobox/managers/AddonsManager.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java b/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java index 0026d94c3..0d47ed93e 100644 --- a/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java @@ -666,7 +666,28 @@ public class AddonsManager { * @since 1.8.0 */ public void allLoaded() { - addons.forEach(Addon::allLoaded); + this.getEnabledAddons().forEach(this::allLoaded); } + + /** + * This method calls Addon#allLoaded in safe manner. If for some reason addon crashes on Addon#allLoaded, then + * it will disable itself without harming other addons. + * @param addon Addon that should trigger Addon#allLoaded method. + */ + private void allLoaded(@NonNull Addon addon) { + try { + addon.allLoaded(); + } catch (NoClassDefFoundError | NoSuchMethodError | NoSuchFieldError e) { + // Looks like the addon is incompatible, because it tries to refer to missing classes... + this.handleAddonIncompatibility(addon, e); + // Disable addon. + this.disable(addon); + } catch (Exception e) { + // Unhandled exception. We'll give a bit of debug here. + this.handleAddonError(addon, e); + // Disable addon. + this.disable(addon); + } + } }