Jump to content

[solved] Question about Forge Events


coolAlias

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.