Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

How to add compatibility with other mods


Insane96MCP
 Share

Recommended Posts

You can write a @SidedProxy-like system:

interface ModInteropProxy {
    void doModCall(args here)
}

class DummyModInteropProxy implements ModInteropProxy {
// implementation when mod is not present
}

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

 

Then in your main mod class:
 

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();
    }
}

 

  • Like 2
Link to comment
Share on other sites

7 minutes ago, Insane96MCP said:

So, here: ActiveModInteropProxy i need to use reflection to get the other mod's method and call it?

No, inside ActiveModInteropProxy you can directly reference the other mod's classes, since it is only loaded when the other mod is active (that's why Class.forName needs to be used, to avoid a strong reference).

 

8 minutes ago, Insane96MCP said:

And here: your.package.ActiveModInteropProxy the mod I'm trying to call?

Wat?

Link to comment
Share on other sites

  • 2 weeks later...
9 hours ago, Insane96MCP said:

What if there's a method that returns an object of other mod's class type?

I don't think you're really thinking this through. You're trying to avoid a dependency but then want to do things that depend on the other mod.

 

But if you insiste, what you need to do is to encapsulate the proxy interface such that the method prototypes work even when the other mod isn't loaded. That means the methods cannot return a value that is specifically of the other mod's type, and cannot take parameters that are specifically of the other mod's type.

 

However, the other mod's classes should have parents that may be common. Like if they have an EntityVampire that extends EntityMob, then you can just type the return value as EntityMob. But note that if you want to further act on it as an EntityVampire then you'll need proxied methods for that too like have a method called setVampireCoffin(EntityMob mobIn, BlockPos posIn) and then in your proxy you'd check if it is an instanceof EntityVampire and act accordingly.

 

In other words, you'll need to encapsulate everything you want to do with the other mods stuff with generic method prototypes that re-cast back to specific types according to whether the other mod is loaded.

Edited by jabelar

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

  • 2 weeks later...
44 minutes ago, Insane96MCP said:

And how I can add compatbility then?

You need to encapsulate everything. If there is an action that you want to happen if mod X is installed, you also must decide what should happen if it is not installed. That is how the proxy system I mentioned above works. You need to write two versions of your code: One when the mod is present and one if it's not. The "mod not installed" version can do nothing, but it needs to be there.

Link to comment
Share on other sites

Here's an example of what would work and would not work. Assume that the optional mod has an EntityVampire and in your mod if the other mod is loaded you want to spawn vampires, but if the other mod is not loaded then you want to spawn EntityWitch.

 

Proper Encapsulation Example

 

For proper encapsulation, the interface is designed so that the dummy implementation doesn't see any classes it won't have. For example, in your proxy interface you could have a method called spawnEvilEntity() that is prototyped like this:

 

     void spawnEvilEntity(World worldIn, BlockPos posIn);

 

Then in your dummy proxy implementation class you implement the method as:

 

     public void spawnEvilEntity(World worldIn, BlockPos posIn)

     {

          EntityWitch entity = new EntityWitch(worldIn);

          entity.setLocationAndAngles(posIn);

          worldIn.spawnEntity(entity);

     }

 

And in your active mod proxy implementation class you implement the method as:

 

     public void spawnEvilEntity(World worldIn, BlockPos posIn)

     {

          EntityVampire entity = new EntityVampire(worldIn);

          entity.setLocationAndAngles(posIn);

          worldIn.spawnEntity(entity);

     }

 

Then you could use this in your main mod code whenever you want to spawn one of these you just call:

 

     proxy.spawnEvilEntity(theWorld, thePos);

 

Wrong Way

 

If you expose any classes from the optional  mod to the interface it will cause trouble. For example, if instead of actually spawning the mob, if you made a method called createVampire() in your proxy interface like:

 

     EntityVampire createVampire(World worldIn, BlockPos posIn);

 

And in your dummy proxy you had:

 

     public EntityVampire createVampire(World worldIn, BlockPos posIn)

     {

          return null;

     }

And in your active mod proxy you had:

 

     public EntityVampire createVampire(World worldIn, BlockPos posIn)

     {

          EntityVampire entity = new EntityVampire(worldIn);

          entity.setLocationAndAngles(posIn);

          return entity;

     }

 

In which case you might try to use it this way in your main mod:

 

     EntityVampire vampire = proxy.createVampire(theWorld, thePos);

     if (vampire != null)

     {

          theWorld.spawn(vampire);

     }

     else

     {

          EntityWitch witch = new EntityWitch(worldIn);

          witch.setLocationAndAngles(posIn);

          theWorld.spawn(witch);

     }

 

This will fail because you have your main mod trying to access EntityVampire which may not be available.

 

Not sure if my example is clear but the point is that you need to organize your proxy code so that all the code that depends on the classes from the optional mod are ONLY accessed within the active mod proxy implementation. You cannot expose those classes to the dummy proxy or the rest of your mod.

 

If you get more specific on what you're trying to do, we can give more specific examples. 

Edited by jabelar
  • Like 1
  • Thanks 1

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

1 hour ago, loordgek said:

it is better to use json

You guys aren't answering the topic the original poster asked about. He actually changed his original post a bit, but basically he wants to call a method from a mod but doesn't want to force the other mod as a dependency. That means the code cannot be sure to be available, so diesieben07 has provided a very clever approach to make it optional.

 

It has nothing to do with recipes, and also both of your suggestions don't help with case where he wants to optionally access code from another mod from his mod.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

  • 8 months later...
On 4/27/2018 at 4:29 PM, jabelar said:

Here's an example of what would work and would not work. Assume that the optional mod has an EntityVampire and in your mod if the other mod is loaded you want to spawn vampires, but if the other mod is not loaded then you want to spawn EntityWitch.

 

I want to do something really similar to this. Can I?

I want that my mod only use it's Ore Generation only when there is no other mods that do this present.

(Obviously I already have created all the Ores, in case this happens.)

And I want it to have some other materials - like rubber, or sludge and some others - that are only present if certain mods are present (in some cases), or if its not present (in other cases). This kind of thing will enable/disable some recipes and fetures from my mod.

 

Can you send me to a tutorial about this? Or there isn't any out there?

Link to comment
Share on other sites

The actual implementation depends on the mod you are being compatible with, for example in one of my mods I only register a special event subscriber if I am able to detect that BetterFoliage is installed. This topic provides a pretty comprehensive overview

 

 

 

 

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

  • 2 months later...
On 4/27/2018 at 9:29 PM, jabelar said:

public void spawnEvilEntity(World worldIn, BlockPos posIn)

     {

          EntityVampire entity = new EntityVampire(worldIn);

          entity.setLocationAndAngles(posIn);

          worldIn.spawnEntity(entity);

     }

Wouldn't the compiler throw out an error because the class doesn't exist?

Link to comment
Share on other sites

Just now, diesieben07 said:

You need to include the dependency-mod in your compilation process of course.

And when I build the mod and run it on forge with the code that I wrote while having the dependency is not going to cause problems?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.