From fbce02c314050780819e8acfa37ad9ef0ff4ca76 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Fri, 20 Oct 2023 15:34:13 +0200 Subject: [PATCH] Go back to silently returning the event if the bus is not started (#26) --- src/main/java/net/neoforged/bus/EventBus.java | 11 ++++++----- src/main/java/net/neoforged/bus/api/IEventBus.java | 6 ++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/neoforged/bus/EventBus.java b/src/main/java/net/neoforged/bus/EventBus.java index 3d4959f..6084b17 100644 --- a/src/main/java/net/neoforged/bus/EventBus.java +++ b/src/main/java/net/neoforged/bus/EventBus.java @@ -343,6 +343,9 @@ public void unregister(Object object) @Override public T post(T event) { + if (shutdown) { + return event; + } doPostChecks(event); return post(event, getListenerList(event.getClass()).getListeners()); @@ -354,17 +357,15 @@ public T post(EventPriority phase, T event) { throw new IllegalStateException("This bus does not allow calling phase-specific post."); } + if (shutdown) { + return event; + } doPostChecks(event); return post(event, getListenerList(event.getClass()).getPhaseListeners(phase)); } private void doPostChecks(Event event) { - if (shutdown) - { - throw new IllegalStateException("Attempted to post event of type " + - event.getClass().getSimpleName() + " on a bus that was not started yet!"); - } if (checkTypesOnDispatch) { try { diff --git a/src/main/java/net/neoforged/bus/api/IEventBus.java b/src/main/java/net/neoforged/bus/api/IEventBus.java index 5a97a9b..9d63ca3 100644 --- a/src/main/java/net/neoforged/bus/api/IEventBus.java +++ b/src/main/java/net/neoforged/bus/api/IEventBus.java @@ -194,16 +194,19 @@ public interface IEventBus { /** * Submit the event for dispatch to appropriate listeners + *

+ * If this bus was not started yet, the event is returned without being dispatched. * * @param event The event to dispatch to listeners * @return the event that was passed in - * @throws IllegalStateException if the bus is not started yet */ T post(T event); /** * Submit the event for dispatch to listeners registered with a specific {@link EventPriority}. *

+ * If this bus was not started yet, the event is returned without being dispatched. + *

* Manually posting events phase-by-phase through this method is less performant * than dispatching to all phases through a {@link #post(Event)} call. * Prefer that method when per-phase dispatching is not needed. @@ -211,7 +214,6 @@ public interface IEventBus { * @param event The event to dispatch to listeners * @return the event that was passed in * @throws IllegalStateException if the bus does not allow per-phase post - * @throws IllegalStateException if the bus is not started yet * @see BusBuilder#allowPerPhasePost() */ T post(EventPriority phase, T event);