Posted November 7, 201311 yr I don't personally play with mods, so I haven't run across this myself, but while coding some event stuff a question occurred to me, and I apologize if it isn't very clear. If multiple mods or even one mod with multiple event handlers handling the same event, what happens? I assume that's where the priority level comes in, but what if they are all the same priority? What happens if one of the handlers cancels the event? Would it follow that if your handler has the possibility of canceling the event, you should give it high priority, whereas if your handler needs to be sure the event is going to happen it should have the lowest possible priority? An example: Two hypothetical mods both use LivingDeathEvent at default priority. Mod 1 wants to prevent the player from dying due to a death ward ability and cancels the event, while mod 2 uses the event to transform the player into a vampire and subsequently respawn normally (as a vampire). How is order determined in such a case, and is there anything we can do as modders to minimize potential impacts from this sort of scenario (if there are indeed any impacts)? Thanks for reading, coolAlias http://i.imgur.com/NdrFdld.png[/img]
November 7, 201311 yr public boolean post(Event event) { IEventListener[] listeners = event.getListenerList().getListeners(busID); for (IEventListener listener : listeners) { listener.invoke(event); } return (event.isCancelable() ? event.isCanceled() : false); } This is what happens in EventBus. Simply, all listeners will be #invoke in registration order, for each priority list. (see ListenerList) By "default", IEventListener is implemented by @Override public void invoke(Event event) { if (handler != null) { if (!event.isCancelable() || !event.isCanceled() || subInfo.receiveCanceled()) { handler.invoke(event); } } } in ASMEventHandler. Note that you can receive a canceled event with @ForgeSubscribe(receiveCanceled=true). Then you can uncancel it. Only the final state of the event will be accounted for. You can also register your own IEventListener with ListenerList#register(int, EventPriority, IEventListener). Fast answer to each of your question: -All will receive the event if (!event.isCancelable() || !event.isCanceled() || subInfo.receiveCanceled()) if they use the "default" IEventListener. -Same priority method will receive the event according to the registration order. -See EventPriority enum to check the priority order. To be sure the event is not canceled, use @ForgeSubscribe(priority=LOWEST, receiveCanceled=true) and uncancel it. -To minimize logical conflicts, you should always have special conditions, and only "rarely" cancel an event.
November 7, 201311 yr Author Wow, that was an awesome explanation! Thank you for the insights GotoLink. That makes a lot of sense and only increases my appreciation for this fine piece of code. Events make modding so much easier http://i.imgur.com/NdrFdld.png[/img]
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.