Skip to content

Instantly share code, notes, and snippets.

@BoogieMonster1O1
Last active May 22, 2024 02:07
Show Gist options
  • Save BoogieMonster1O1/3a4f427aa553b8751ca4e349cc160d96 to your computer and use it in GitHub Desktop.
Save BoogieMonster1O1/3a4f427aa553b8751ca4e349cc160d96 to your computer and use it in GitHub Desktop.

Adding Dependencies

Introduction

Adding Dependencies allows you to access hooks, tools and apis provided by other mods. Depending on other's code allows you to spend lesser time on writing your code, and more time on refining it. A commonly used dependency in fabric is Fabric API.

Adding the repository

Open your build.gradle file and add the required maven repositories

repositories {
    maven {
        name = "Example"
        url = "https://maven.fabricmc.net"
    }
    maven{
        name = "Tutorial"
        url = "https://jitpack.io"
    }
    [...]
}

The repositories block must be a top level block. If it does not exist, create it. Do not place the repositories in the repositories block thats under publishing

Adding the dependency

To add a dependency, you must know the maven group id, the maven artifact id and the version. These three are the coordinates that will tell gradle to add that dependency. Its also good to know whether the dependency is mod, or not and whether the dependency is an api, or not.

Adding a mod as a dependency

Hard dependencies

Hard dependencies are usually mods that the core behavior of your mod depends on. They should be included whenever someone adds your mod as a dependency. Open build.gradle and under dependencies, add the following.

[...]
modImplementation "[groupid]:[artifactid]:[version]"

Next, add it to your fabric.mod.json as so, under depends

"modid":"version"

Soft dependencies

Soft dependencies are usually mods that your mod adds special behavior to, but are not necessary for main functionaility. These will not be included when your mod is added as a dependency. This can be done by using modCompileOnly in combination with modRuntime instead of modImplementation Open build.gradle and under dependencies, add the following.

[...]
modCompileOnly "[groupid]:[artifactid]:[version]"
modRuntime "[groupid]:[artifactid]:[version]"

If the mod is a library, add it to your fabric.mod.json as so, under suggests.

"modid":"version"

Adding something that isn't a mod as a dependency

Sometimes, you need to use a dependency that isn't a mod, like a database connector.
For these, do it the exact same way as adding a mod, but use implementation, instead of modImplementation. For soft dependencies of this sort, use compileOnly and runtime.

implementation "[groupid]:[artifactid]:[version]"

You should not add these to your fabric.mod.json as they do not have a mod id and they are not a mod.

Including dependencies with your mod

Including dependencies within your mod allows you to run your mod as is. The dependency will be stored inside your mod's jar file. To include it, add another line to build.gradle, under dependencies.

include "[groupid]:[artifactid]:[version]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment