Skip to content

Commit cf1cfae

Browse files
committed
API (hopefully) done
1 parent 9bb11be commit cf1cfae

File tree

8 files changed

+165
-18
lines changed

8 files changed

+165
-18
lines changed

src/main/java/net/minecraftforge/eventbus/api/bus/BusGroup.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
*/
1818
public sealed interface BusGroup permits BusGroupImpl {
1919
/**
20-
* The default BusGroup provided by EventBus. While it is recommended to create your own BusGroup, this can be used
21-
* as a safe default if necessary.
20+
* The default BusGroup provided by EventBus. If when creating an {@link EventBus} the BusGroup is not specified, it
21+
* will be registered in this one.
2222
*
2323
* @see #create(String)
2424
*/

src/main/java/net/minecraftforge/eventbus/api/bus/CancellableEventBus.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ public sealed interface CancellableEventBus<T extends Event & Cancellable>
7070
* breaking changes if the event is no longer cancellable in the future.</p>
7171
* @param listener The listener to add.
7272
* @return A reference that can be used to remove this listener later with {@link #removeListener(EventListener)}.
73-
* @see #addListener(Predicate) For adding a listener that can cancel the event conditionally
74-
* @see #addListener(Consumer) For adding a listener that never cancels the event
73+
* @see #addListener(Predicate)
74+
* @see #addListener(Consumer)
7575
*/
7676
default EventListener addListener(boolean alwaysCancelling, Consumer<T> listener) {
7777
return addListener(Priority.NORMAL, alwaysCancelling, listener);
@@ -85,7 +85,7 @@ default EventListener addListener(boolean alwaysCancelling, Consumer<T> listener
8585
* unnecessary breaking changes if the event is no longer cancellable in the future</p>
8686
* @param listener The listener to add.
8787
* @return A reference that can be used to remove this listener later with {@link #removeListener(EventListener)}.
88-
* @see Priority For common priority values
88+
* @see Priority
8989
*/
9090
EventListener addListener(byte priority, boolean alwaysCancelling, Consumer<T> listener);
9191

@@ -101,18 +101,35 @@ default EventListener addListener(boolean alwaysCancelling, Consumer<T> listener
101101
* @param priority The priority of this listener. Higher numbers are called first.
102102
* @param listener The listener to add.
103103
* @return A reference that can be used to remove this listener later with {@link #removeListener(EventListener)}.
104-
* @see Priority For common priority values
104+
* @see Priority
105105
*/
106106
EventListener addListener(byte priority, Predicate<T> listener);
107107

108-
// MONITORING
108+
/**
109+
* Adds a monitoring listener to this EventBus with an unchanging priority of {@link Priority#MONITOR}.
110+
* @param listener The listener to add.
111+
* @return A reference that can be used to remove this listener later with {@link #removeListener(EventListener)}.
112+
* @see Priority#MONITOR
113+
*/
109114
EventListener addListener(ObjBooleanBiConsumer<T> listener);
110115

116+
/**
117+
* Creates a new CancellableEventBus for the given event type on the {@linkplain BusGroup#DEFAULT default} {@link BusGroup}.
118+
* @param eventType The type of event the bus will have
119+
* @param <T> The type of event
120+
* @return The new CancallableEventBus
121+
*/
111122
@SuppressWarnings("ClassEscapesDefinedScope") // E can be a subtype of Event which is publicly accessible
112123
static <T extends Event & Cancellable> CancellableEventBus<T> create(Class<T> eventType) {
113124
return create(BusGroup.DEFAULT, eventType);
114125
}
115126

127+
/**
128+
* Creates a new CancellableEventBus for the given event type on the given BusGroup.
129+
* @param eventType The type of event the bus will have
130+
* @param <T> The type of event
131+
* @return The new CancallableEventBus
132+
*/
116133
@SuppressWarnings("ClassEscapesDefinedScope") // E can be a subtype of Event which is publicly accessible
117134
static <T extends Event & Cancellable> CancellableEventBus<T> create(BusGroup busGroup, Class<T> eventType) {
118135
return (CancellableEventBus<T>) ((BusGroupImpl) busGroup).getOrCreateEventBus(eventType);

src/main/java/net/minecraftforge/eventbus/api/bus/EventBus.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public sealed interface EventBus<T extends Event> permits CancellableEventBus, A
113113
boolean hasListeners();
114114

115115
/**
116-
* Creates a new EventBus for the given event type on the default {@link BusGroup}.
116+
* Creates a new EventBus for the given event type on the {@linkplain BusGroup#DEFAULT default} {@link BusGroup}.
117117
* <p><strong>Important:</strong> The returned EventBus <i>MUST</i> be stored in a {@code static final} field -
118118
* failing to do so will severely hurt performance.</p>
119119
* @apiNote There can only be one EventBus instance per event type per BusGroup.

src/main/java/net/minecraftforge/eventbus/api/event/characteristic/MonitorAware.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
import net.minecraftforge.eventbus.internal.MutableEventInternals;
99

1010
/**
11-
* Events that are {@link MonitorAware} can provide stronger immutability guarantees to monitor listeners by returning
12-
* unmodifiable views or throwing exceptions on mutation attempts when monitoring.
11+
* Events that are {@link MonitorAware} can provide stronger immutability guarantees to
12+
* {@linkplain net.minecraftforge.eventbus.api.listener.Priority#MONITOR monitor} listeners by returning unmodifiable
13+
* views or throwing exceptions on mutation attempts when monitoring.
1314
* <p>Only supported for {@link MutableEvent} at this time.</p>
1415
*
1516
* @apiNote <strong>This feature is experimental - it may be removed, renamed or otherwise changed without

src/main/java/net/minecraftforge/eventbus/api/listener/EventListener.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
import java.util.function.Consumer;
1313

1414
/**
15-
* Users can retain instances of this interface to remove listeners that were previously added to the same
16-
* {@link EventBus}.You can obtain instances of this interface by calling any of the {@code addListener} methods
17-
* on an EventBus, such as {@link EventBus#addListener(Consumer)}.
18-
* <p>Internally, this acts as a wrapper over lambdas to give them identity, enrich debug info and to allow
19-
* various conversion operations to different lambda types.</p>
15+
* Users can retain instances of this interface to {@linkplain EventBus#removeListener(EventListener) remove listeners}
16+
* that were previously added to the same {@link EventBus}. You can obtain instances of this interface by calling any of
17+
* the {@code addListener} methods on an EventBus, such as {@link EventBus#addListener(Consumer)}.
18+
*
19+
* @implNote Internally, this acts as a wrapper over lambdas to give them identity, enrich debug info and to allow
20+
* various conversion operations to different lambda types.
21+
* @see EventBus
2022
*/
2123
public sealed interface EventListener permits EventListenerImpl {
2224
@SuppressWarnings("ClassEscapesDefinedScope") // ? can be a subtype of Event which is publicly accessible

src/main/java/net/minecraftforge/eventbus/api/listener/ObjBooleanBiConsumer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77
import java.util.function.BiConsumer;
88

99
/**
10-
* A {@link BiConsumer} that takes an object and a primitive boolean, to avoid boxing.
10+
* A {@link BiConsumer} that takes an object and a primitive boolean, to avoid boxing. Used for
11+
* {@linkplain net.minecraftforge.eventbus.api.listener.Priority#MONITOR monitor} listeners.
1112
*/
1213
@FunctionalInterface
1314
public interface ObjBooleanBiConsumer<T> {
15+
/**
16+
* @param obj The object of type {@link T}, usually an event.
17+
* @param bool The boolean value, usually an event's cancelled status.
18+
* @see BiConsumer#accept(Object, Object)
19+
*/
1420
void accept(T obj, boolean bool);
1521
}

src/main/java/net/minecraftforge/eventbus/api/listener/SubscribeEvent.java

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,136 @@
44
*/
55
package net.minecraftforge.eventbus.api.listener;
66

7+
import net.minecraftforge.eventbus.api.bus.BusGroup;
8+
import net.minecraftforge.eventbus.api.bus.CancellableEventBus;
9+
import net.minecraftforge.eventbus.api.bus.EventBus;
710
import net.minecraftforge.eventbus.api.event.characteristic.Cancellable;
811

9-
import java.lang.annotation.*;
12+
import java.lang.annotation.ElementType;
13+
import java.lang.annotation.Inherited;
14+
import java.lang.annotation.Retention;
15+
import java.lang.annotation.RetentionPolicy;
16+
import java.lang.annotation.Target;
17+
import java.util.function.Consumer;
1018

19+
/**
20+
* This annotation is used to mark methods as event handlers. The contract for the method that has this annotation
21+
* applied is the same as if it was treated as a lambda and fed into {@link EventBus#addListener(Consumer)} or one of
22+
* its sister methods from {@link CancellableEventBus} that accept a {@linkplain java.util.function.Predicate predicate}
23+
* or {@linkplain ObjBooleanBiConsumer object-boolean bi-consumer}.
24+
* <p>Classes that contain methods annotated with SubscribeEvent must be registered with the applicable
25+
* {@link BusGroup}.</p>
26+
* {@snippet :
27+
* import net.minecraftforge.eventbus.api.event.InheritableEvent;
28+
* import java.lang.invoke.MethodHandles;
29+
*
30+
* // in MyEvent.java
31+
* public class MyEvent implements InheritableEvent {
32+
* public static final EventBus<MyEvent> BUS = EventBus.create(Main.GROUP, MyEvent.class);
33+
* }
34+
*
35+
* // in MyClass.java
36+
* public class MyClass {
37+
* @SubscribeEvent
38+
* public static void onEvent(MyEvent event) {
39+
* System.out.println("Hello! I exist!");
40+
* }
41+
*
42+
* @SubscribeEvent(priority = Priority.LOW)
43+
* public static void onEventLater(MyEvent event) {
44+
* System.out.println("I also exist! But am called slightly later!");
45+
* }
46+
*
47+
* public static void init() {
48+
* Main.GROUP.register(MethodHandles.lookup(), MyClass.class);
49+
* }
50+
* }
51+
*
52+
* // in Main.java
53+
* public class Main {
54+
* public static final BusGroup GROUP = BusGroup.create("my_group");
55+
*
56+
* public static void main(String[] args) {
57+
* MyClass.init();
58+
* }
59+
* }
60+
*}
61+
*
62+
* <p>The same can also be done for cancellable events. As mentioned earlier, shaping the targeted method in the form
63+
* of a predicate or object-boolean bi-consumer will yield the same results when registered with a BusGroup</p>
64+
* {@snippet :
65+
* import net.minecraftforge.eventbus.api.event.MutableEvent;
66+
* import net.minecraftforge.eventbus.api.event.characteristic.MonitorAware;
67+
* import java.lang.invoke.MethodHandles;
68+
*
69+
* // in MyEvent.java
70+
* public class MyCancellableEvent extends MutableEvent implements MonitorAware {
71+
* public static final CancellableEventBus<MyEvent> BUS = CancellableEventBus.create(Main.GROUP, MyCancellableEvent.class);
72+
*
73+
* private boolean somethingImportant = true;
74+
*
75+
* public void setSomethingImportant(boolean value) {
76+
* if (!this.isMonitoring())
77+
* this.somethingImportant = value;
78+
* }
79+
* }
80+
*
81+
* // in MyClass.java
82+
* public class MyClass {
83+
* @SubscribeEvent
84+
* public static boolean onEvent(MyCancellableEvent event) {
85+
* System.out.println("Hello! I exist!");
86+
* System.out.println("Hello! I will not cancel this event!");
87+
* return false;
88+
* }
89+
*
90+
* @SubscribeEvent(priority = Priority.LOW, alwaysCancelling = true)
91+
* public static void onEventLater(MyCancellableEvent event) {
92+
* System.out.println("I also exist! But I will cancel this event!");
93+
* }
94+
*
95+
* @SubscribeEvent(priority = Priority.LOWEST)
96+
* public static void onEventLatest(MyCancellableEvent event) {
97+
* System.out.println("I don't exist! I can't run because the event's been cancelled!");
98+
* }
99+
*
100+
* @SubscribeEvent // because this is an object-boolean bi-consumer, priority is implicitly MONITOR
101+
* public static void onEventMonitoring(MyCancellableEvent event, boolean cancelled) {
102+
* System.out.println("I exist! I'm a monitoring listener and will always run!");
103+
* // this won't do anything:
104+
* event.setSomethingImportant(false);
105+
* }
106+
*
107+
* public static void init() {
108+
* Main.GROUP.register(MethodHandles.lookup(), MyClass.class);
109+
* }
110+
* }
111+
*
112+
* // in Main.java
113+
* public class Main {
114+
* public static final BusGroup GROUP = BusGroup.create("my_group");
115+
*
116+
* public static void main(String[] args) {
117+
* MyClass.init();
118+
* }
119+
* }
120+
*}
121+
*/
11122
@Inherited
12123
@Target(ElementType.METHOD)
13124
@Retention(RetentionPolicy.RUNTIME)
14125
public @interface SubscribeEvent {
126+
/**
127+
* @return The priority of the listener.
128+
* @see Priority
129+
*/
15130
byte priority() default Priority.NORMAL;
16131

17132
/**
18133
* If the event is cancellable, setting this to true will make the listener always cancel the event.
19134
*
20-
* @implSpec If true, the annotated method must return {@code void} and the event must implement {@link Cancellable}.
135+
* @implSpec If true, the annotated method must return {@code void} and the event must implement
136+
* {@link Cancellable}.
21137
*/
22138
boolean alwaysCancelling() default false;
23139
}

src/main/java/net/minecraftforge/eventbus/api/listener/package-info.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
* Copyright (c) Forge Development LLC
33
* SPDX-License-Identifier: LGPL-2.1-only
44
*/
5+
/**
6+
* This package houses classes related to listener-specific APIs. These interfaces are usually consumed as part of usage
7+
* of the {@linkplain net.minecraftforge.eventbus.api.bus event bus} and
8+
* {@linkplain net.minecraftforge.eventbus.api.event event} APIs.
9+
*/
510
@NullMarked
611
package net.minecraftforge.eventbus.api.listener;
712

0 commit comments

Comments
 (0)