Skip to content

Commit

Permalink
Go back to silently returning the event if the bus is not started (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Technici4n authored Oct 20, 2023
1 parent 4b82c3f commit fbce02c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/main/java/net/neoforged/bus/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ public void unregister(Object object)

@Override
public <T extends Event> T post(T event) {
if (shutdown) {
return event;
}
doPostChecks(event);

return post(event, getListenerList(event.getClass()).getListeners());
Expand All @@ -354,17 +357,15 @@ public <T extends Event> 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 {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/net/neoforged/bus/api/IEventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,24 +194,26 @@ public interface IEventBus {

/**
* Submit the event for dispatch to appropriate listeners
* <p>
* 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 extends Event> T post(T event);

/**
* Submit the event for dispatch to listeners registered with a specific {@link EventPriority}.
* <p>
* If this bus was not started yet, the event is returned without being dispatched.
* <p>
* 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.
*
* @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 extends Event> T post(EventPriority phase, T event);
Expand Down

0 comments on commit fbce02c

Please sign in to comment.