|
4 | 4 | */ |
5 | 5 | package net.minecraftforge.eventbus.api.listener; |
6 | 6 |
|
| 7 | +import net.minecraftforge.eventbus.api.bus.BusGroup; |
| 8 | +import net.minecraftforge.eventbus.api.bus.CancellableEventBus; |
| 9 | +import net.minecraftforge.eventbus.api.bus.EventBus; |
7 | 10 | import net.minecraftforge.eventbus.api.event.characteristic.Cancellable; |
8 | 11 |
|
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; |
10 | 18 |
|
| 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 | + */ |
11 | 122 | @Inherited |
12 | 123 | @Target(ElementType.METHOD) |
13 | 124 | @Retention(RetentionPolicy.RUNTIME) |
14 | 125 | public @interface SubscribeEvent { |
| 126 | + /** |
| 127 | + * @return The priority of the listener. |
| 128 | + * @see Priority |
| 129 | + */ |
15 | 130 | byte priority() default Priority.NORMAL; |
16 | 131 |
|
17 | 132 | /** |
18 | 133 | * If the event is cancellable, setting this to true will make the listener always cancel the event. |
19 | 134 | * |
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}. |
21 | 137 | */ |
22 | 138 | boolean alwaysCancelling() default false; |
23 | 139 | } |
0 commit comments