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

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now


  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I am trying to override or disable some vanilla recipes, as part of my mod. All of the methods that I have seen online, so far, do not work. So far, I have tried: Creating a JSON file in the "data/minecraft/recipes" folder, that has air as the ingredient and a barrier as the result This only works for crafting table recipes adding a smelting recipe this way works as intended Editing the ordering to AFTER in the forge dependency inside the "mods.toml" file Found an older post about using the FurnaceRecipe class, that no longer exists From: here So, what is required to disable a vanilla smelting recipe? I know some may say to not do this, but part of the progression of my mod kind of requires disabling some of the furnace recipes. Also, it seems that smelting recipes don't technically have to have the same filename as the block used, r as the result. When overriding crafting recipes, just adding the edited/removed recipe file in the minecraft data folder, under the same name as the original recipe.  Edit: the crafting recipes seem to be cached between worlds? I created a new world and the edited crafting recipes works, but it doesn't work in an already made world?
    • Using a modified version of "Fabulously Optimized" installed a few more mods and now my Hot Bar is invisible... Latest log : https://paste.ee/p/72TSs (No DeBug log txt file.)
    • I know that with this you can return the game folder   FMLPaths.GAMEDIR.get() I want the player to be created in the world where a txt file was created in the folder of the same world. How can I return the path of the world folder that the player is in?
    • [09Jun2023 15:38:56.969] [main/ERROR] [mixin/]: Mixin config dynamiclightsreforged.mixins.json does not specify "minVersion" property [09Jun2023 15:38:57.512] [main/ERROR] [mixin/]: Mixin config itshallnottick.mixins.json does not specify "minVersion" property how do i fix those 2
    • Whoops sorry uploaded wrong logs, here are the correct ones: 09:28:31.876 at java.base/java.lang.Thread.run(Thread.java:833) 09:28:31.876 Caused by: java.lang.UnsupportedOperationException: A mod tried to access the state neighbor table directly. Please report this at https://github.com/malte0811/FerriteCore/issues. As a temporary workaround you can enable "populateNeighborTable" in the FerriteCore config 09:28:31.876 at TRANSFORMER/ferritecore@4.2.2/malte0811.ferritecore.fastmap.table.CrashNeighborTable.crashOnAccess(CrashNeighborTable.java:101) 09:28:31.876 at TRANSFORMER/ferritecore@4.2.2/malte0811.ferritecore.fastmap.table.CrashNeighborTable.rowKeySet(CrashNeighborTable.java:77) 09:28:31.876 at TRANSFORMER/canary@0.2.1/com.abdelaziz.canary.common.state.FastImmutableTable.<init>(FastImmutableTable.java:36) 09:28:31.876 at TRANSFORMER/minecraft@1.18.2/net.minecraft.world.level.block.state.StateHolder.handler$bbj000$postCreateWithTable(StateHolder.java:531) 09:28:31.876 at TRANSFORMER/minecraft@1.18.2/net.minecraft.world.level.block.state.StateHolder.m_61133_(StateHolder.java:1052) 09:28:31.876 at TRANSFORMER/minecraft@1.18.2/net.minecraft.world.level.block.state.StateDefinition.<init>(StateDefinition.java:68) 09:28:31.876 at TRANSFORMER/minecraft@1.18.2/net.minecraft.world.level.block.state.StateDefinition$Builder.m_61101_(StateDefinition.java:157) 09:28:31.877 at TRANSFORMER/minecraft@1.18.2/net.minecraft.world.level.block.Block.<init>(Block.java:174) 09:28:31.877 at TRANSFORMER/minecraft@1.18.2/net.minecraft.world.level.block.AirBlock.<init>(AirBlock.java:12) 09:28:31.877 at TRANSFORMER/minecraft@1.18.2/net.minecraft.world.level.block.Blocks.<clinit>(Blocks.java:40) 09:28:31.877 ... 18 more 09:28:32.435 Process crashed with exit code 1
  • Topics

×
×
  • Create New...

Important Information

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