architectury / architectury-api

An intermediary api aimed at easing development of multiplatform mods.
https://discord.architectury.dev/
GNU Lesser General Public License v3.0
306 stars 55 forks source link

DeferredRegister fails with custom registries from other mods on Forge #513

Open object-Object opened 4 weeks ago

object-Object commented 4 weeks ago

Description

When using DeferredRegister to register content in a custom registry added by another mod, initializing the register (eg. ACTIONS.register()) fails on Forge with the following error message (full crash report below). The same code works fine on Fabric.

java.lang.IllegalArgumentException: Registry ResourceKey[minecraft:root / hexcasting:action] does not exist!

This particular error message is for registering a value to Hex Casting's action registry. I ran the mod with a debugger, and it seems like my mod's initializer, where ACTIONS.register() is called, runs before Hex Casting's initializer, where the custom registry is added to BuiltInRegistries.REGISTRY. So, when Architectury attempts to look up the registry in ACTIONS.register(), the custom registry hasn't been added yet.

I was able to work around the issue by calling ACTIONS.register() in NewRegistryEvent, which fires after mod initialization but before RegisterEvent (docs). However, I'm not sure this is actually a good idea in general - I think it might fail if the mod adding the registry created it in NewRegistryEvent.

I guess a potential fix could be to defer actually looking up the registry until the corresponding RegisterEvent has been received?

Possibly related: #256

Example repo

I set up a minimal example based on the 1.20.1 Fabric/Forge Architectury template, attempting to register a value to Hex Casting's action registry.

Repository: object-Object/architectury-modded-registry-example (see also the workaround tag)

Relevant files:

Logs

These logs are from running the forge:runClient Gradle task in the above repo, on the failing and workaround tags respectively.

Versions

object-Object commented 4 weeks ago

Update: I was also able to work around the issue by calling ACTIONS.register() in the appropriate RegisterEvent, but it's a bit annoying to implement since DeferredRegister.key doesn't seem to be publicly accessible.

https://github.com/object-Object/architectury-modded-registry-example/commit/4b117e54695fa96d2849fa9031d076be81af4089

Actually, this doesn't seem to be working - the game runs without crashing, but the custom action isn't actually registered. Not sure why.