Minecraft Modding: A System For Optional Mod Dependencies

Background


In modding it is possible that you want to enable extra functionality if certain other mods are installed, but you don't want to require that the other mods be installed. Here is an idea suggested by diesieben07 for managing this with a "proxy-ike" system.

A Proxy-Like System

The concept is that during mod loading you instantiate different versions of a class depending on a condition (in this case whether certain other mods are loaded).

Step 1: Create A Proxy Interface


Something like this:

interface ModInteropProxy 
{
    void doModCall(args here)
}

Step 2: Create A "Dummy" Implementation


This version of the implementation will be used when the other mod is NOT loaded.

class DummyModInteropProxy implements ModInteropProxy 
{
     // implementation when mod is not present, cannot reference classes from the mod
}

Step 3: Create An "Active" Implementation


This version of the implementation will be used with the other mod is loaded.

class ActiveModInteropProxy implements ModInteropProxy 
{
     //implementation when mod is present, can reference classes from the mod
}

Step 4: Instantiate The Appropriate Proxy Class During Mod Loading


In your main mod class, you check for the other mod and instantiate during your pre-init handling method.

static ModInteropProxy modInterop;

preInit() 
{
    if (Loader.isModLoader("bla")) 
    {
        // reflection to avoid hard dependency
        modInterop = Class.forName("your.package.ActiveModInteropProxy").asSubclass(ModInteropProxy.class).newInstance();
    } 
    else 
    {
        modInterop = new DummyModInteropProxy();
    }
}

Step 5: Populate Your Implementations With Appropriate Methods


Then for everything you might want to do, you create a method of the same name and parameters in both implementations. In the active implementation you can use classes from the other mod, and in the dumy you do not.

Conclusion


I think this is a fairly clever approach. Hope it helps and happy modding!

No comments:

Post a Comment