From 9b097b7858c20f5917a2b27eadb6acfc498cf36f Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 14 Dec 2018 23:48:46 +0100 Subject: [PATCH] IsEnabled && enabling late components Also finally added the source plugin --- ButtonCore/pom.xml | 12 ++++++++++ .../buttondevteam/core/ComponentManager.java | 24 ++++++++++++++++++- .../lib/architecture/Component.java | 14 ++++++++++- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/ButtonCore/pom.xml b/ButtonCore/pom.xml index 2ae8ac3..2bbce2d 100755 --- a/ButtonCore/pom.xml +++ b/ButtonCore/pom.xml @@ -80,6 +80,18 @@ + + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + + jar + + + + diff --git a/ButtonCore/src/main/java/buttondevteam/core/ComponentManager.java b/ButtonCore/src/main/java/buttondevteam/core/ComponentManager.java index 023dee4..4e7b37d 100644 --- a/ButtonCore/src/main/java/buttondevteam/core/ComponentManager.java +++ b/ButtonCore/src/main/java/buttondevteam/core/ComponentManager.java @@ -2,12 +2,21 @@ package buttondevteam.core; import buttondevteam.lib.architecture.Component; import buttondevteam.lib.architecture.exceptions.UnregisteredComponentException; +import lombok.val; public final class ComponentManager { private ComponentManager() {} + private static boolean componentsEnabled = false; + /** - * Enables components based on a configuration + * This flag is used to enable components registered after the others were enabled. + * @return Whether already registered components have been enabled + */ + public static boolean areComponentsEnabled() { return componentsEnabled; } + + /** + * Enables components based on a configuration - any component registered afterwards will be also enabled */ public static void enableComponents() { //Component.getComponents().values().stream().filter(c->cs.getConfigurationSection(c.getClass().getSimpleName()).getBoolean("enabled")).forEach(c-> { @@ -17,6 +26,7 @@ public final class ComponentManager { } catch (UnregisteredComponentException ignored) { //This *should* never happen } }); + componentsEnabled = true; } /** @@ -29,5 +39,17 @@ public final class ComponentManager { } catch (UnregisteredComponentException ignored) { //This *should* never happen } }); + componentsEnabled = false; + } + + /** + * Will also return false if the component is not registered. + * + * @param cl The component class + * @return Whether the component is registered and enabled + */ + public static boolean isEnabled(Class cl) { + val c = Component.getComponents().get(cl); + return c != null && c.isEnabled(); } } diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java index faeb6a8..4b9219c 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java @@ -1,5 +1,6 @@ package buttondevteam.lib.architecture; +import buttondevteam.core.ComponentManager; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.architecture.exceptions.UnregisteredComponentException; import buttondevteam.lib.chat.TBMCChatAPI; @@ -73,7 +74,8 @@ public abstract class Component { /** * Registers a component checking it's dependencies and calling {@link #register(JavaPlugin)}.
- * Make sure to register the dependencies first. + * Make sure to register the dependencies first.
+ * The component will be enabled automatically, regardless of when it was registered. * * @param component The component to register */ @@ -113,7 +115,17 @@ public abstract class Component { if (component.config == null) component.config = compconf.createSection(component.getClassName()); component.register(plugin); components.put(component.getClass(), component); + if (ComponentManager.areComponentsEnabled() && component.shouldBeEnabled().get()) { + try { //Enable components registered after the previous ones getting enabled + setComponentEnabled(component, true); + } catch (UnregisteredComponentException ignored) { + } + } } else { + if (component.enabled) { + component.disable(); + component.enabled = false; + } component.unregister(plugin); components.remove(component.getClass()); }