Skip to content

Commit

Permalink
Merge pull request #6 from cyklon73/internal
Browse files Browse the repository at this point in the history
InternalEvents
  • Loading branch information
cyklon73 authored Oct 20, 2024
2 parents 7c98c03 + 300ff3b commit 020b687
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 19 deletions.
7 changes: 3 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ var commit = getCommit()
var release = System.getenv("RELEASE") == "true"

repositories {
mavenCentral()
maven { url "https://maven.mineking.dev/releases" }
maven { url "https://maven.mineking.dev/snapshots" }
mavenCentral()
}

dependencies {
implementation 'org.jetbrains:annotations:22.0.0'
implementation "de.cyklon:ReflectionUtils:fa134db"
implementation 'org.jetbrains:annotations:26.0.1'
implementation "de.cyklon:ReflectionUtils:1.2.1"

testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
Expand Down
36 changes: 28 additions & 8 deletions src/main/java/de/cyklon/jevent/EventManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,26 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;
import java.util.function.Consumer;

/**
* The EventManager to manage and execute events
*/
public sealed interface EventManager permits JEvent {

/**
* Each EventManager has a unique identifier so that it can be uniquely identified for internal events, for example
* @return The unique id of the EventManager
*/
UUID getId();

/**
* Can only be activated when created
* @return true if internal events enabled
*/
boolean internalEventsEnabled();

/**
* registers all {@link MethodHandler EventHandlers} in the listener Class
*
Expand Down Expand Up @@ -43,7 +56,7 @@ public sealed interface EventManager permits JEvent {
*
* @param packageName the name of the package to be registered as a listener package
*/
default void registerListenerPackage(String packageName) {
default void registerListenerPackage(@NotNull String packageName) {
registerListenerPackage(OfflinePackage.get(packageName));
}

Expand All @@ -54,7 +67,7 @@ default void registerListenerPackage(String packageName) {
*
* @param pkg the package
*/
void registerListenerPackage(OfflinePackage pkg);
void registerListenerPackage(@NotNull OfflinePackage pkg);

/**
* registers a listener for a specific event, with a consumer instead of a method
Expand All @@ -65,7 +78,7 @@ default void registerListenerPackage(String packageName) {
* @param ignoreCancelled if true, the handler is not called for {@link EventHandler#ignoreCancelled() canceled events}
* @param <T> the event type
*/
<T extends Event> void registerHandler(@NotNull Class<T> event, Consumer<T> handler, byte priority, boolean ignoreCancelled);
<T extends Event> void registerHandler(@NotNull Class<T> event, @NotNull Consumer<T> handler, byte priority, boolean ignoreCancelled);

/**
* registers a listener for a specific event, with a consumer instead of a method
Expand All @@ -75,7 +88,7 @@ default void registerListenerPackage(String packageName) {
* @param priority the event {@link EventHandler#priority() priority}
* @param <T> the event type
*/
default <T extends Event> void registerHandler(@NotNull Class<T> event, Consumer<T> handler, byte priority) {
default <T extends Event> void registerHandler(@NotNull Class<T> event, @NotNull Consumer<T> handler, byte priority) {
registerHandler(event, handler, priority, false);
}

Expand All @@ -87,7 +100,7 @@ default <T extends Event> void registerHandler(@NotNull Class<T> event, Consumer
* @param ignoreCancelled if true, the handler is not called for {@link EventHandler#ignoreCancelled() canceled events}
* @param <T> the event type
*/
default <T extends Event> void registerHandler(@NotNull Class<T> event, Consumer<T> handler, boolean ignoreCancelled) {
default <T extends Event> void registerHandler(@NotNull Class<T> event, @NotNull Consumer<T> handler, boolean ignoreCancelled) {
registerHandler(event, handler, EventHandler.NORMAL, ignoreCancelled);
}

Expand All @@ -98,7 +111,7 @@ default <T extends Event> void registerHandler(@NotNull Class<T> event, Consumer
* @param handler The consumer to be executed when the event is called
* @param <T> the event type
*/
default <T extends Event> void registerHandler(@NotNull Class<T> event, Consumer<T> handler) {
default <T extends Event> void registerHandler(@NotNull Class<T> event, @NotNull Consumer<T> handler) {
registerHandler(event, handler, EventHandler.NORMAL, false);
}

Expand Down Expand Up @@ -208,15 +221,15 @@ default void registerParameterInstance(@NotNull Object instance) {
* @return the instance registered to this key, or null if no instance is registered to this key
*/
@Nullable
Object removeParameterInstance(String key);
Object removeParameterInstance(@NotNull String key);

/**
* returns the parameter instance registered to the given key
* @param key the key of the instance
* @return the value or null if there is no instance for this key, or the value is set to null
*/
@Nullable
Object getParameterInstance(String key);
Object getParameterInstance(@NotNull String key);

/**
* Sets a logger for debug prints
Expand All @@ -240,4 +253,11 @@ default void registerParameterInstance(@NotNull Object instance) {
* @param logger the consumer with the corresponding log method or null to disable debug messages
*/
void setDebugLogger(@Nullable Consumer<String> logger);

/**
* @return true if this EventManager is the Default Manager
*/
default boolean isDefaultManager() {
return JEvent.DEFAULT_MANAGER.getId().equals(getId());
}
}
58 changes: 51 additions & 7 deletions src/main/java/de/cyklon/jevent/JEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
* JEvent provides a powerful and lightweight event system based on the syntax of the <a href="https://www.spigotmc.org/wiki/using-the-event-api/">Spigot event</a> system
*/
public final class JEvent implements EventManager {
static final EventManager DEFAULT_MANAGER = new JEvent();
static final EventManager DEFAULT_MANAGER = new JEvent(false);

/**
* Internal events are deactivated for this EventManager.
* <p>
* To use internal events, create an EventManager with {@link #createManager(boolean)}
* @return The Default EventManager instance
*/
public static EventManager getDefaultManager() {
Expand All @@ -28,18 +31,36 @@ public static EventManager getDefaultManager() {

/**
* create a new EventManager
* @return the new Created Manager
* <p>
* internal events are deactivated. To enable internal events, use {@link #createManager(boolean)}
* @return the new Created EventManager
*/
@NotNull
public static EventManager createManager() {
return new JEvent();
return createManager(false);
}

/**
* create a new EventManager
* @param useInternalEvents determines whether internal events are activated for this EventManager
* @return the new Created EventManager
*/
@NotNull
public static EventManager createManager(boolean useInternalEvents) {
return new JEvent(useInternalEvents);
}

private final Collection<Handler<?>> handlerSet = new HashSet<>();
private final Map<String, Object> parameterInstances = new HashMap<>();

private final UUID id;
private final boolean useInternalEvents;
private Consumer<String> logger = null;

private JEvent() {}
private JEvent(boolean useInternalEvents) {
this.id = UUID.randomUUID();
this.useInternalEvents = useInternalEvents;
}

@SuppressWarnings("unchecked")
private <T extends Event> Collection<Handler<T>> getHandlers(@NotNull Class<T> event) {
Expand All @@ -50,6 +71,16 @@ private <T extends Event> Collection<Handler<T>> getHandlers(@NotNull Class<T> e
.toList();
}

@Override
public UUID getId() {
return id;
}

@Override
public boolean internalEventsEnabled() {
return useInternalEvents;
}

public void registerListener(@NotNull Object obj) {
debug("register listener " + obj.getClass());
Collection<MethodHandler<?>> handlers = MethodHandler.getHandlers(obj);
Expand Down Expand Up @@ -161,9 +192,22 @@ public void unregisterAll() {
handlerSet.clear();
}

public boolean callEvent(Event event) {
debug("call event " + event.getClass());
getHandlers(event.getClass()).forEach(h -> h.invoke(this, event));
public boolean callEvent(@NotNull Event event) {
if (InternalJEvent.isInternal(event)) {
if (!internalEventsEnabled()) return true;
} else {
EventCallJEvent ec = new EventCallJEvent(this, event);
callEvent(ec);
if (ec.isCancelled()) {
debug("Event call of " + event.getClass() + " cancelled by internal event");
return false;
}
if (ec.isEventModified()) debug("Event changed by internal event from " + event.getClass() + " to " + ec.getEvent().getClass());
event = ec.getEvent();
debug("call event " + event.getClass());
}
Event finalEvent = event;
getHandlers(event.getClass()).forEach(h -> h.invoke(this, finalEvent));
return event instanceof Cancellable e && e.isCancelled();
}

Expand Down
32 changes: 32 additions & 0 deletions src/main/java/de/cyklon/jevent/internal/EventCallJEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package de.cyklon.jevent.internal;

import de.cyklon.jevent.Event;
import de.cyklon.jevent.EventManager;

public class EventCallJEvent extends InternalJEvent {

private final Event initialEvent;
private Event event;

public EventCallJEvent(EventManager manager, Event event) {
super(manager);
this.event = event;
this.initialEvent = event;
}

public Event getEvent() {
return event;
}

public void setEvent(Event event) {
this.event = event;
}

public Event getInitialEvent() {
return initialEvent;
}

public boolean isEventModified() {
return !initialEvent.equals(event);
}
}
26 changes: 26 additions & 0 deletions src/main/java/de/cyklon/jevent/internal/InternalJEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package de.cyklon.jevent.internal;

import de.cyklon.jevent.CancellableEvent;
import de.cyklon.jevent.Event;
import de.cyklon.jevent.EventManager;
import de.cyklon.reflection.entities.ReflectClass;
import de.cyklon.reflection.entities.ReflectPackage;

public abstract class InternalJEvent extends CancellableEvent {

private static final ReflectPackage INTERNAL_PACKAGE = ReflectClass.wrap(InternalJEvent.class).getPackage();

private final EventManager manager;

protected InternalJEvent(EventManager manager) {
this.manager = manager;
}

public EventManager getManager() {
return manager;
}

public static <T extends Event> boolean isInternal(T event) {
return ReflectClass.getClass(event).getPackage().equals(INTERNAL_PACKAGE);
}
}

0 comments on commit 020b687

Please sign in to comment.