99import net .minecraftforge .eventbus .api .listener .SubscribeEvent ;
1010import net .minecraftforge .eventbus .internal .BusGroupImpl ;
1111
12+ import java .lang .invoke .LambdaMetafactory ;
1213import java .lang .invoke .MethodHandles ;
1314import java .util .Collection ;
1415
1516/**
16- * A collection of {@link EventBus} instances that are grouped together for easier management.
17+ * A collection of {@link EventBus} instances that are grouped together for easier management, allowing for bulk
18+ * operations.
1719 */
1820public sealed interface BusGroup permits BusGroupImpl {
21+ /**
22+ * The default BusGroup, which is used when an {@link EventBus} is created without specifying a BusGroup.
23+ */
1924 BusGroup DEFAULT = create ("default" );
2025
26+ /**
27+ * Creates a new BusGroup with the given name.
28+ *
29+ * @param name The unique name of the BusGroup
30+ * @return A new BusGroup with the given name
31+ * @throws IllegalArgumentException if the name is already in use by another BusGroup
32+ * @apiNote To enforce a base type for all events in this BusGroup, use {@link #create(String, Class)}.
33+ */
2134 static BusGroup create (String name ) {
2235 return new BusGroupImpl (name , Event .class );
2336 }
2437
38+ /**
39+ * Creates a new BusGroup with the given name and base type.
40+ *
41+ * @param name The unique name of the BusGroup
42+ * @param baseType The base type that all events in this BusGroup must extend or implement
43+ * @return A new BusGroup with the given name and base type
44+ * @throws IllegalArgumentException if the name is already in use by another BusGroup
45+ */
2546 static BusGroup create (String name , Class <?> baseType ) {
2647 return new BusGroupImpl (name , baseType );
2748 }
2849
2950 /**
3051 * The unique name of this BusGroup.
52+ * <p>The uniqueness of this name is enforced when the bus group is {@linkplain #create(String) created}.</p>
3153 */
3254 String name ();
3355
@@ -40,21 +62,30 @@ static BusGroup create(String name, Class<?> baseType) {
4062 /**
4163 * Shuts down all EventBus instances associated with this BusGroup, preventing any further events from being posted
4264 * until {@link #startup()} is called.
65+ *
66+ * @apiNote If you don't intend on using this BusGroup again, prefer {@link #dispose()} instead as that will also
67+ * free up resources.
4368 */
4469 void shutdown ();
4570
4671 /**
47- * Shuts down all EventBus instances associated with this BusGroup, unregisters all listeners and frees resources
48- * no longer needed.
49- * <p>Warning: This is a destructive operation - this BusGroup should not be used again after calling this method.</p>
72+ * {@linkplain #shutdown() Shuts down} all EventBus instances associated with this BusGroup,
73+ * {@linkplain #unregister(Collection) unregisters} all listeners and frees no longer needed resources.
74+ *
75+ * <p>Warning: This is a destructive operation - this BusGroup should not be used again after calling this method -
76+ * attempting to do so may throw exceptions or act as a no-op.</p>
77+ *
78+ * @apiNote If you plan on using this BusGroup again, prefer {@link #shutdown()} instead.
5079 */
5180 void dispose ();
5281
5382 /**
54- * Experimental feature - may be removed, renamed or otherwise changed without notice .
55- * <p>Trims the backing lists of all EventBus instances associated with this BusGroup to free up resources.</p>
83+ * Trims the backing lists of all EventBus instances associated with this BusGroup to free up resources .
84+ *
5685 * <p>Warning: This is only intended to be called <b>once</b> after all listeners are registered - calling this
5786 * repeatedly may hurt performance.</p>
87+ *
88+ * @apiNote This is an experimental feature that may be removed, renamed or otherwise changed without notice.
5889 */
5990 void trim ();
6091
@@ -68,6 +99,11 @@ static BusGroup create(String name, Class<?> baseType) {
6899 * @apiNote This method only registers static listeners.
69100 * <p>If you want to register both instance and static methods, use
70101 * {@link BusGroup#register(MethodHandles.Lookup, Object)} instead.</p>
102+ * @implNote Internally, bulk registration uses {@link LambdaMetafactory} to create method references to the
103+ * annotated methods using the provided {@code callerLookup} - said lookup must have
104+ * {@linkplain MethodHandles.Lookup#hasFullPrivilegeAccess() full privilege access} as
105+ * {@linkplain LambdaMetafactory LMF} may need to spin an inner class for implementing the lambda, which
106+ * inherently allows access to private fields and methods.
71107 */
72108 Collection <EventListener > register (MethodHandles .Lookup callerLookup , Class <?> utilityClassWithStaticListeners );
73109
@@ -80,6 +116,11 @@ static BusGroup create(String name, Class<?> baseType) {
80116 *
81117 * @apiNote If you know all the listeners are static methods, use
82118 * {@link BusGroup#register(MethodHandles.Lookup, Class)} instead for better registration performance.
119+ * @implNote Internally, bulk registration uses {@link LambdaMetafactory} to create method references to the
120+ * annotated methods using the provided {@code callerLookup} - said lookup must have
121+ * {@linkplain MethodHandles.Lookup#hasFullPrivilegeAccess() full privilege access} as
122+ * {@linkplain LambdaMetafactory LMF} may need to spin an inner class for implementing the lambda, which
123+ * inherently allows access to private fields and methods.
83124 */
84125 Collection <EventListener > register (MethodHandles .Lookup callerLookup , Object listener );
85126
0 commit comments