Loading the DH Api

The example repo the DH Api is simply added through a implementation directive in the build.grade file. However, I am facing the issue that the game will obviously crash when my mod is loaded, but not DH, and the class loaded can't find DH's classes. For example: java.lang.NoClassDefFoundError: com/seibel/distanthorizons/api/methods/events/interfaces/IDhApiEvent Now I am not sure I am supposed to include the DH Api jar or what? I'm also confused because the API version on Modrinth seems really old. Is it assured to be stable?
7 Replies
BackSun
BackSun5mo ago
The API is stable, the reason the API jar is so old is because the API hasn't changed and as such hasn't needed a new version (this is good because it means the API hasn't had any breaking changes). The API is a subset of the full DH jar and is MC version agnostic so you can develop for all DH versions at the same time. When working with an optional dependency you generally put that code in a separate class that's only loaded if the dependency is present. In this case check if DH is loaded using your mod loader's API (for example: FabricLoader.getInstance().isModLoaded("distanthorizons")) and then load the class.
Wendelin
Wendelin5mo ago
Right, that makes sense. I was quite confused because I had already added such a check, but it was still throwing a NoClassDefFoundError. I think that is because the event callbacks were loaded by the class loaded despite the code using them not being executed. I would be nice if you could change the implementation to a compileOnly in the buld.gradle of the example project. Also, do you think it's better to check via isModLoaded("distanthorizons") or Class.forName("com.seibel.distanthorizons.api.DhApi"); with a try-catch?
BackSun
BackSun5mo ago
Personally I'd do Class.forName("com.seibel.distanthorizons.api.DhApi"); because it's mod loader agnostic, but either should work just fine.
Wendelin
Wendelin5mo ago
I guess checking for both is actually best, since another mod might JIJ the api.
BackSun
BackSun5mo ago
Unlikely, but it can't hurt.
Wendelin
Wendelin5mo ago
Thanks for the quick responses!
BackSun
BackSun5mo ago
👍