File tree Expand file tree Collapse file tree 8 files changed +94
-10
lines changed
client/java/io/github/jamalam360/jamlib
main/java/io/github/jamalam360/jamlib/events
testmod-common/src/main/java/io/github/jamalam360/testmod Expand file tree Collapse file tree 8 files changed +94
-10
lines changed Original file line number Diff line number Diff line change 11package io .github .jamalam360 .jamlib .api .events .client ;
22
3- import dev .architectury .event .Event ;
4- import dev .architectury .event .EventFactory ;
3+ import io .github .jamalam360 .jamlib .events .Event ;
54import net .minecraft .client .Minecraft ;
65
76/**
@@ -11,11 +10,11 @@ public class ClientPlayLifecycleEvents {
1110 /**
1211 * Called when the local player has joined a logical server.
1312 */
14- public static final Event <Join > JOIN = EventFactory . createLoop ( Join . class );
13+ public static final Event <Join > JOIN = new Event <>( );
1514 /**
1615 * Called when the local player leaves a logical server.
1716 */
18- public static final Event <Leave > DISCONNECT = EventFactory . createLoop ( Leave . class );
17+ public static final Event <Leave > DISCONNECT = new Event <>( );
1918
2019 @ FunctionalInterface
2120 public interface Join {
Original file line number Diff line number Diff line change @@ -19,6 +19,6 @@ public class ClientPacketListenerMixin {
1919 at = @ At ("RETURN" )
2020 )
2121 private void jamlib$joinServer (ClientboundLoginPacket packet , CallbackInfo ci ) {
22- ClientPlayLifecycleEvents .JOIN .invoker () .onJoin (Minecraft .getInstance ());
22+ ClientPlayLifecycleEvents .JOIN .invoke (( listener ) -> listener .onJoin (Minecraft .getInstance () ));
2323 }
2424}
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ public class ConnectionMixin {
1616 at = @ At ("HEAD" )
1717 )
1818 private void jamlib$callDisconnectEvent (ChannelHandlerContext channelHandlerContext , CallbackInfo ci ) {
19- ClientPlayLifecycleEvents .DISCONNECT .invoker () .onLeave (Minecraft .getInstance ());
19+ ClientPlayLifecycleEvents .DISCONNECT .invoke (( listener ) -> listener .onLeave (Minecraft .getInstance () ));
2020 }
2121
2222 @ Inject (
@@ -27,6 +27,6 @@ public class ConnectionMixin {
2727 )
2828 )
2929 private void jamlib$callDisconnectEvent (CallbackInfo ci ) {
30- ClientPlayLifecycleEvents .DISCONNECT .invoker () .onLeave (Minecraft .getInstance ());
30+ ClientPlayLifecycleEvents .DISCONNECT .invoke (( listener ) -> listener .onLeave (Minecraft .getInstance () ));
3131 }
3232}
Original file line number Diff line number Diff line change 1212public class JamLibClient {
1313 @ ApiStatus .Internal
1414 public static void init () {
15- ClientPlayLifecycleEvents .JOIN .register (JamLibClient ::onPlayerJoin );
15+ ClientPlayLifecycleEvents .JOIN .listen (JamLibClient ::onPlayerJoin );
1616 }
1717
1818 private static void onPlayerJoin (Minecraft minecraft ) {
Original file line number Diff line number Diff line change 1+ package io .github .jamalam360 .jamlib .events ;
2+
3+ import java .util .function .Function ;
4+
5+ public class CancellableEvent <L , R > extends Event <L > {
6+ public EventResult <R > invokeCancellable (Function <L , EventResult <R >> invoker ) {
7+ for (int priority : this .getListeners ().keySet ()) {
8+ for (L listener : this .getListeners ().get (priority )) {
9+ EventResult <R > result = invoker .apply (listener );
10+
11+ if (result .wasCancelled ()) {
12+ return result ;
13+ }
14+ }
15+ }
16+
17+ return EventResult .pass ();
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package io .github .jamalam360 .jamlib .events ;
2+
3+ import com .google .common .collect .Ordering ;
4+ import com .google .common .collect .TreeMultimap ;
5+
6+ import java .util .function .Consumer ;
7+
8+ public class Event <L > {
9+ public static final int DEFAULT_PRIORITY = 1000 ;
10+ private final TreeMultimap <Integer , L > listeners ;
11+
12+ public Event () {
13+ this .listeners = TreeMultimap .create (Ordering .natural ().reverse (), Ordering .arbitrary ());
14+ }
15+
16+ public void listen (L listener ) {
17+ this .listen (DEFAULT_PRIORITY , listener );
18+ }
19+
20+ public void listen (int priority , L listener ) {
21+ this .listeners .put (priority , listener );
22+ }
23+
24+ public void invoke (Consumer <L > invoker ) {
25+ for (int priority : this .listeners .keySet ()) {
26+ for (L listener : this .listeners .get (priority )) {
27+ invoker .accept (listener );
28+ }
29+ }
30+ }
31+
32+ protected TreeMultimap <Integer , L > getListeners () {
33+ return this .listeners ;
34+ }
35+ }
Original file line number Diff line number Diff line change 1+ package io .github .jamalam360 .jamlib .events ;
2+
3+ import org .jetbrains .annotations .Nullable ;
4+
5+ public class EventResult <T > {
6+ private final boolean cancelled ;
7+ @ Nullable
8+ private final T result ;
9+
10+ private EventResult (boolean cancelled , @ Nullable T result ) {
11+ this .cancelled = cancelled ;
12+ this .result = result ;
13+ }
14+
15+ public static <T > EventResult <T > pass () {
16+ return new EventResult <>(false , null );
17+ }
18+
19+ public static <T > EventResult <T > cancel (T result ) {
20+ return new EventResult <>(true , result );
21+ }
22+
23+ public boolean wasCancelled () {
24+ return this .cancelled ;
25+ }
26+
27+ @ Nullable
28+ public T getResult () {
29+ return this .result ;
30+ }
31+ }
Original file line number Diff line number Diff line change @@ -33,8 +33,8 @@ public static void init() {
3333 System .out .println (CONFIG_MANAGER .get ());
3434 System .out .println (QCB_CONFIG .get ());
3535
36- ClientPlayLifecycleEvents .JOIN .register (client -> LOGGER .info ("Joined server!" ));
37- ClientPlayLifecycleEvents .DISCONNECT .register (client -> LOGGER .info ("Left server!" ));
36+ ClientPlayLifecycleEvents .JOIN .listen (client -> LOGGER .info ("Joined server!" ));
37+ ClientPlayLifecycleEvents .DISCONNECT .listen (client -> LOGGER .info ("Left server!" ));
3838
3939 Network .registerPayloadType (PotatoPacket .TYPE , PotatoPacket .INSTANCE );
4040
You can’t perform that action at this time.
0 commit comments