From b4dcd7f45385ae353b5fdde4c99b714c2c102e6a Mon Sep 17 00:00:00 2001 From: cyklon73 Date: Sun, 20 Oct 2024 14:54:22 +0200 Subject: [PATCH 1/8] updated dependencies --- build.gradle | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index d404234..3e01353 100644 --- a/build.gradle +++ b/build.gradle @@ -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' From 13fab4d100cb2cea36351db20b6708a00dc82f47 Mon Sep 17 00:00:00 2001 From: cyklon73 Date: Sun, 20 Oct 2024 14:56:59 +0200 Subject: [PATCH 2/8] implemented InternalJEvent --- .../jevent/internal/InternalJEvent.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/main/java/de/cyklon/jevent/internal/InternalJEvent.java diff --git a/src/main/java/de/cyklon/jevent/internal/InternalJEvent.java b/src/main/java/de/cyklon/jevent/internal/InternalJEvent.java new file mode 100644 index 0000000..fbfe41b --- /dev/null +++ b/src/main/java/de/cyklon/jevent/internal/InternalJEvent.java @@ -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 boolean isInternal(T event) { + return ReflectClass.getClass(event).getPackage().equals(INTERNAL_PACKAGE); + } +} From 0deebb9a7684294fc90772dd366442c88461bb70 Mon Sep 17 00:00:00 2001 From: cyklon73 Date: Sun, 20 Oct 2024 15:03:20 +0200 Subject: [PATCH 3/8] implemented EventCallJEvent --- .../jevent/internal/EventCallJEvent.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/main/java/de/cyklon/jevent/internal/EventCallJEvent.java diff --git a/src/main/java/de/cyklon/jevent/internal/EventCallJEvent.java b/src/main/java/de/cyklon/jevent/internal/EventCallJEvent.java new file mode 100644 index 0000000..2002ed2 --- /dev/null +++ b/src/main/java/de/cyklon/jevent/internal/EventCallJEvent.java @@ -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); + } +} From bb74438da10e38570649183eeec8c57ee2ca9fa5 Mon Sep 17 00:00:00 2001 From: cyklon73 Date: Sun, 20 Oct 2024 15:07:59 +0200 Subject: [PATCH 4/8] implemented EventManager#internalEventsEnabled --- src/main/java/de/cyklon/jevent/EventManager.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/de/cyklon/jevent/EventManager.java b/src/main/java/de/cyklon/jevent/EventManager.java index 3e53fb5..da64eba 100644 --- a/src/main/java/de/cyklon/jevent/EventManager.java +++ b/src/main/java/de/cyklon/jevent/EventManager.java @@ -12,6 +12,12 @@ */ public sealed interface EventManager permits JEvent { + /** + * Can only be activated when created + * @return true if internal events enabled + */ + boolean internalEventsEnabled(); + /** * registers all {@link MethodHandler EventHandlers} in the listener Class * From 17129d146ede427a520e5435f522f155985b5895 Mon Sep 17 00:00:00 2001 From: cyklon73 Date: Sun, 20 Oct 2024 17:47:45 +0200 Subject: [PATCH 5/8] implemented getId --- .../java/de/cyklon/jevent/EventManager.java | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/cyklon/jevent/EventManager.java b/src/main/java/de/cyklon/jevent/EventManager.java index da64eba..0c3f56a 100644 --- a/src/main/java/de/cyklon/jevent/EventManager.java +++ b/src/main/java/de/cyklon/jevent/EventManager.java @@ -5,6 +5,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.UUID; import java.util.function.Consumer; /** @@ -12,6 +13,12 @@ */ 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 @@ -49,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)); } @@ -60,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 @@ -71,7 +78,7 @@ default void registerListenerPackage(String packageName) { * @param ignoreCancelled if true, the handler is not called for {@link EventHandler#ignoreCancelled() canceled events} * @param the event type */ - void registerHandler(@NotNull Class event, Consumer handler, byte priority, boolean ignoreCancelled); + void registerHandler(@NotNull Class event, @NotNull Consumer handler, byte priority, boolean ignoreCancelled); /** * registers a listener for a specific event, with a consumer instead of a method @@ -81,7 +88,7 @@ default void registerListenerPackage(String packageName) { * @param priority the event {@link EventHandler#priority() priority} * @param the event type */ - default void registerHandler(@NotNull Class event, Consumer handler, byte priority) { + default void registerHandler(@NotNull Class event, @NotNull Consumer handler, byte priority) { registerHandler(event, handler, priority, false); } @@ -93,7 +100,7 @@ default void registerHandler(@NotNull Class event, Consumer * @param ignoreCancelled if true, the handler is not called for {@link EventHandler#ignoreCancelled() canceled events} * @param the event type */ - default void registerHandler(@NotNull Class event, Consumer handler, boolean ignoreCancelled) { + default void registerHandler(@NotNull Class event, @NotNull Consumer handler, boolean ignoreCancelled) { registerHandler(event, handler, EventHandler.NORMAL, ignoreCancelled); } @@ -104,7 +111,7 @@ default void registerHandler(@NotNull Class event, Consumer * @param handler The consumer to be executed when the event is called * @param the event type */ - default void registerHandler(@NotNull Class event, Consumer handler) { + default void registerHandler(@NotNull Class event, @NotNull Consumer handler) { registerHandler(event, handler, EventHandler.NORMAL, false); } @@ -168,7 +175,7 @@ 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 @@ -176,7 +183,7 @@ default void registerParameterInstance(@NotNull Object 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 @@ -200,4 +207,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 logger); + + /** + * @return true if this EventManager is the Default Manager + */ + default boolean isDefaultManager() { + return JEvent.DEFAULT_MANAGER.getId().equals(getId()); + } } From cf29ae8093ac2d3448277fc048d8467a01c983cf Mon Sep 17 00:00:00 2001 From: cyklon73 Date: Sun, 20 Oct 2024 17:51:56 +0200 Subject: [PATCH 6/8] implemented EventManager methods --- src/main/java/de/cyklon/jevent/JEvent.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/de/cyklon/jevent/JEvent.java b/src/main/java/de/cyklon/jevent/JEvent.java index 3583242..84910f5 100644 --- a/src/main/java/de/cyklon/jevent/JEvent.java +++ b/src/main/java/de/cyklon/jevent/JEvent.java @@ -50,6 +50,16 @@ private Collection> getHandlers(@NotNull Class 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> handlers = MethodHandler.getHandlers(obj); From 9e633f889e7645017a89071f6c5f0906a002c55b Mon Sep 17 00:00:00 2001 From: cyklon73 Date: Sun, 20 Oct 2024 17:52:26 +0200 Subject: [PATCH 7/8] implemented EventManager methods --- src/main/java/de/cyklon/jevent/JEvent.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/de/cyklon/jevent/JEvent.java b/src/main/java/de/cyklon/jevent/JEvent.java index 84910f5..49c1cb6 100644 --- a/src/main/java/de/cyklon/jevent/JEvent.java +++ b/src/main/java/de/cyklon/jevent/JEvent.java @@ -37,6 +37,9 @@ public static EventManager createManager() { private final Collection> handlerSet = new HashSet<>(); private final Map parameterInstances = new HashMap<>(); + + private final UUID id; + private final boolean useInternalEvents; private Consumer logger = null; private JEvent() {} From 300ff3bc0c9ccd1d66dacdb4b64c649b4f648ae6 Mon Sep 17 00:00:00 2001 From: cyklon73 Date: Sun, 20 Oct 2024 17:55:08 +0200 Subject: [PATCH 8/8] implemented #createManager(boolean useInternalEvents) --- src/main/java/de/cyklon/jevent/JEvent.java | 45 ++++++++++++++++++---- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/cyklon/jevent/JEvent.java b/src/main/java/de/cyklon/jevent/JEvent.java index 49c1cb6..976cafb 100644 --- a/src/main/java/de/cyklon/jevent/JEvent.java +++ b/src/main/java/de/cyklon/jevent/JEvent.java @@ -17,9 +17,12 @@ * JEvent provides a powerful and lightweight event system based on the syntax of the Spigot event 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. + *

+ * To use internal events, create an EventManager with {@link #createManager(boolean)} * @return The Default EventManager instance */ public static EventManager getDefaultManager() { @@ -28,11 +31,23 @@ public static EventManager getDefaultManager() { /** * create a new EventManager - * @return the new Created Manager + *

+ * 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> handlerSet = new HashSet<>(); @@ -42,7 +57,10 @@ public static EventManager createManager() { private final boolean useInternalEvents; private Consumer logger = null; - private JEvent() {} + private JEvent(boolean useInternalEvents) { + this.id = UUID.randomUUID(); + this.useInternalEvents = useInternalEvents; + } @SuppressWarnings("unchecked") private Collection> getHandlers(@NotNull Class event) { @@ -163,9 +181,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(); }