IsEnabled && enabling late components

Also finally added the source plugin
This commit is contained in:
Norbi Peti 2018-12-14 23:48:46 +01:00
parent f8a850df76
commit 9b097b7858
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 48 additions and 2 deletions

View file

@ -80,6 +80,18 @@
</useSystemClassLoader> <!-- https://stackoverflow.com/a/53012553/2703239 -->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>

View file

@ -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<? extends Component> cl) {
val c = Component.getComponents().get(cl);
return c != null && c.isEnabled();
}
}

View file

@ -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)}.<br>
* Make sure to register the dependencies first.
* Make sure to register the dependencies first.<br>
* 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());
}