Forge Mod Loader has a heavy annotation based system, and when looking to do optional mod dependency, it can be a relatively tricky thing, enabling certain features only when the mod we want to add it for is loaded, for example having support for the great1 Open Source computer mod, OpenComputers!
Notice: OpenComputer’s API will be heavily used throughout this tutorial/reference
Let’s say we have this idea for a nice technology mod and we then want to add the functionality of implementing some Interface only when a mod is loaded, and maybe strip out some of it’s functions within itself if the mod is not loaded.
For this, as you have guessed there is the
@Optional.Method or the
@Optional.InterfaceList to help solve our issues. Though, I have not used it in the case of Scala, I will be able to explain, what I understand of it to you.
Using this, is relatively simple understanding that you know some of it’s constraints which are as follows:
- References to the function will not be stripped if the function is.
- The function and it’s existence will not be true unless the Forge Mod Loader has loaded that mod as well as yours
Understanding these constraints within the system, we can work around the first one using FML’s
Loader.isModLoaded(modname) wrapping those around various function references, tough looking at that type of code can be quite messy.
An example of the annotation used with
@Optional.Method can be as follows2:
Most of this method (the parameters and return type) can be ignored safely due to the factor of the API we are using in this system.
But, to explain how the
@Optional.Method(modid = "OpenComputers") annotation is working, as you can see, we are passing the modid constant3 to the annotation, and what this does, when the class is loading, the method is stripped out via magic™4.
@Optional.Interface and @Optional.InterfaceList
For this, we are combining the explanation of the two Interface bases annotations to explain them simply, since
InterfaceList is just an array of
@Optional.Interface it comes with the same caveats as
@Optional.Method but, in a slightly different way.
- Casting of the instances of the class to that said interface will cause a
ClassCastExceptionat run time (if the class is in the class path) or even a
- The interface and all references (optional) will be stripped from the class.
Yet again, this can be worked around with the
One example, of the annotation can be seen as follows2:
As you can see we are yet passing the modid of the mod that implements the interface we remove, though because of how Java annotations work (We cannot annotate a single implements) we have to specify the full canonical name of the class we want in the
iface parameter. Where this interface supplied (in this case, SimpleComponent) is removed by magic™4.
InterfaceList annotation is simple enough after understanding the
When Using the
InterfaceList we can simply wrap the
@Optional.Interface(...) with this annotation and an array declaration like2:
This can be useful when using external APIs, allowing you to exclude unneeded API files when possible, dropping them if the mod is not there. But due to time constraints on when I was writing this, if there is anything unclear comment below (the comments should work) and I shall clarify more for you.