Handle errors for the components

This commit is contained in:
Norbi Peti 2019-01-01 03:26:43 +01:00
parent ff246b8c8b
commit 7f385df3b1
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 56 additions and 37 deletions

View file

@ -1,7 +1,7 @@
package buttondevteam.core;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.architecture.Component;
import buttondevteam.lib.architecture.exceptions.UnregisteredComponentException;
import lombok.val;
public final class ComponentManager {
@ -23,7 +23,8 @@ public final class ComponentManager {
Component.getComponents().values().stream().filter(c -> c.shouldBeEnabled().get()).forEach(c -> {
try {
Component.setComponentEnabled(c, true);
} catch (UnregisteredComponentException ignored) { //This *should* never happen
} catch (Exception e) {
TBMCCoreAPI.SendException("Failed to enable one of the components: " + c.getClass().getSimpleName(), e);
}
});
componentsEnabled = true;
@ -36,7 +37,8 @@ public final class ComponentManager {
Component.getComponents().values().stream().filter(Component::isEnabled).forEach(c -> {
try {
Component.setComponentEnabled(c, false);
} catch (UnregisteredComponentException ignored) { //This *should* never happen
} catch (Exception e) {
TBMCCoreAPI.SendException("Failed to disable one of the components: " + c.getClass().getSimpleName(), e);
}
});
componentsEnabled = false;

View file

@ -59,9 +59,10 @@ public abstract class Component {
* The component will be enabled automatically, regardless of when it was registered.
*
* @param component The component to register
* @return Whether the component is registered successfully (it may have failed to enable)
*/
public static void registerComponent(JavaPlugin plugin, Component component) {
registerUnregisterComponent(plugin, component, true);
public static boolean registerComponent(JavaPlugin plugin, Component component) {
return registerUnregisterComponent(plugin, component, true);
}
/**
@ -69,22 +70,24 @@ public abstract class Component {
* Make sure to unregister the dependencies last.
*
* @param componentClass The component class to unregister
* @return Whether the component is unregistered successfully (it also got disabled)
*/
public static void unregisterComponent(JavaPlugin plugin, Class<? extends Component> componentClass) {
public static boolean unregisterComponent(JavaPlugin plugin, Class<? extends Component> componentClass) {
val component = components.get(componentClass);
if (component == null)
return; //Failed to load
registerUnregisterComponent(plugin, component, false);
return false; //Failed to load
return registerUnregisterComponent(plugin, component, false);
}
public static void registerUnregisterComponent(JavaPlugin plugin, Component component, boolean register) {
public static boolean registerUnregisterComponent(JavaPlugin plugin, Component component, boolean register) {
try {
val metaAnn = component.getClass().getAnnotation(ComponentMetadata.class);
if (metaAnn != null) {
Class<? extends Component>[] dependencies = metaAnn.depends();
for (val dep : dependencies) {
if (!components.containsKey(dep)) {
plugin.getLogger().warning("Failed to " + (register ? "" : "un") + "register component " + component.getClassName() + " as a required dependency is missing/disabled: " + dep.getSimpleName());
return;
return false;
}
}
}
@ -99,16 +102,30 @@ public abstract class Component {
if (ComponentManager.areComponentsEnabled() && component.shouldBeEnabled().get()) {
try { //Enable components registered after the previous ones getting enabled
setComponentEnabled(component, true);
} catch (UnregisteredComponentException ignored) {
return true;
} catch (Exception | NoClassDefFoundError e) {
TBMCCoreAPI.SendException("Failed to enable component " + component.getClassName() + "!", e);
return true;
}
}
return true;
} else {
if (component.enabled) {
try {
component.disable();
component.enabled = false;
} catch (Exception | NoClassDefFoundError e) {
TBMCCoreAPI.SendException("Failed to disable component " + component.getClassName() + "!", e);
return false; //If failed to disable, won't unregister either
}
}
component.unregister(plugin);
components.remove(component.getClass());
return true;
}
} catch (Exception e) {
TBMCCoreAPI.SendException("Failed to " + (register ? "" : "un") + "register component " + component.getClassName() + "!", e);
return false;
}
}

View file

@ -2,7 +2,7 @@ package buttondevteam.lib.architecture.exceptions;
import buttondevteam.lib.architecture.Component;
public class UnregisteredComponentException extends Throwable {
public class UnregisteredComponentException extends Exception {
public UnregisteredComponentException(Component component) {
super("The component '" + component.getClass().getSimpleName() + "' isn't registered!");