Add event support
This commit is contained in:
parent
2144063a9a
commit
a19aad7038
6 changed files with 149 additions and 0 deletions
|
@ -15,6 +15,9 @@ dependencies {
|
|||
// ANSI support
|
||||
implementation 'org.fusesource.jansi:jansi:' + project.dependencyJansi
|
||||
|
||||
// Reflections
|
||||
implementation 'org.reflections:reflections:' + project.dependencyReflections
|
||||
|
||||
// -> Testing <-
|
||||
// Jetbrains Annotations
|
||||
testCompileOnly 'org.jetbrains:annotations:' + project.dependencyJetbrainsAnnotations
|
||||
|
|
|
@ -91,6 +91,9 @@ public class Engine {
|
|||
// Initialize classes
|
||||
initializeClasses();
|
||||
|
||||
// Initialize events
|
||||
initializeEvents();
|
||||
|
||||
// Populate crash content
|
||||
populateCrashContent();
|
||||
|
||||
|
@ -116,6 +119,14 @@ public class Engine {
|
|||
new ShortcodeConverter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes all events.
|
||||
*
|
||||
* @since 1-alpha0
|
||||
*/
|
||||
protected void initializeEvents() {
|
||||
}
|
||||
|
||||
/**
|
||||
* This method populates the Crash Handler's content with the default set of content.
|
||||
*
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
package de.staropensource.sosengine.base.annotations;
|
||||
|
||||
import de.staropensource.sosengine.base.classes.Event;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* Annotation for registering events on methods.
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
@Documented
|
||||
public @interface EventListener {
|
||||
Class<? extends Event> event();
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
/**
|
||||
* Contains annotations.
|
||||
*
|
||||
* @since 1-alpha0
|
||||
*/
|
||||
package de.staropensource.sosengine.base.annotations;
|
|
@ -0,0 +1,113 @@
|
|||
package de.staropensource.sosengine.base.classes;
|
||||
|
||||
import de.staropensource.sosengine.base.annotations.EventListener;
|
||||
import de.staropensource.sosengine.base.logging.Logger;
|
||||
import de.staropensource.sosengine.base.types.CodePart;
|
||||
import de.staropensource.sosengine.base.types.LogIssuer;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.reflections.Reflections;
|
||||
import org.reflections.scanners.Scanners;
|
||||
import org.reflections.util.ClasspathHelper;
|
||||
import org.reflections.util.ConfigurationBuilder;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Represents an event.
|
||||
*
|
||||
* @since 1-alpha0
|
||||
*/
|
||||
@Getter
|
||||
@SuppressWarnings({ "unused", "JavadocDeclaration", "JavadocBlankLines" })
|
||||
public abstract class Event {
|
||||
/**
|
||||
* Instance.
|
||||
*
|
||||
* @since 1-alpha0
|
||||
*
|
||||
* -- GETTER --
|
||||
* Returns the extending {@link Event} instance.
|
||||
*
|
||||
* @return extending {@link Event} instance
|
||||
* @since 1-alpha0
|
||||
*/
|
||||
@Getter()
|
||||
protected static Event instance;
|
||||
|
||||
/**
|
||||
* The name of the event.
|
||||
*
|
||||
* @since 1-alpha0
|
||||
*
|
||||
* -- GETTER --
|
||||
* Returns the name of the event.
|
||||
*
|
||||
* @return the event name
|
||||
* @since 1-alpha0
|
||||
*/
|
||||
@NotNull
|
||||
private final String eventName;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1-alpha0
|
||||
*/
|
||||
public Event() {
|
||||
this.eventName = this.getClass().getName().replace(this.getClass().getPackage() + ".", "");
|
||||
|
||||
// Only allow one instance
|
||||
if (instance == null)
|
||||
instance = this;
|
||||
else
|
||||
Logger.crash(new LogIssuer(getClass(), CodePart.ENGINE), "Tried reinitializing " + getClass().getName() + " twice");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all annotated methods.
|
||||
*
|
||||
* @return list of annotated methods
|
||||
* @since 1-alpha0
|
||||
*/
|
||||
@NotNull
|
||||
protected List<Method> getAnnotatedMethods() {
|
||||
List<Method> methods = new ArrayList<>();
|
||||
|
||||
Reflections reflections = new Reflections(
|
||||
new ConfigurationBuilder()
|
||||
.setUrls(ClasspathHelper.forJavaClassPath())
|
||||
.setScanners(Scanners.MethodsAnnotated)
|
||||
);
|
||||
Set<Method> annotatedMethods = reflections.getMethodsAnnotatedWith(EventListener.class);
|
||||
|
||||
for (Method method : annotatedMethods)
|
||||
if (method.getAnnotation(EventListener.class).event() == this.getClass())
|
||||
methods.add(method);
|
||||
|
||||
return methods;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes all annotated methods without any arguments.
|
||||
*/
|
||||
protected void invokeAnnotatedMethods() {
|
||||
for (Method method : getAnnotatedMethods()) {
|
||||
try {
|
||||
method.invoke(null);
|
||||
} catch (IllegalAccessException | InvocationTargetException ignored) {}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the event and notifies all annotated methods about it.
|
||||
*
|
||||
* @since 1-alpha0
|
||||
*/
|
||||
public abstract void callEvent();
|
||||
}
|
|
@ -14,6 +14,7 @@ dependencyJoor=0.9.14
|
|||
|
||||
# Subsystem dependencies
|
||||
dependencyJansi=2.4.1
|
||||
dependencyReflections=0.10.2
|
||||
|
||||
# Plugins
|
||||
pluginShadow=8.1.1
|
||||
|
|
Loading…
Reference in a new issue