Skip to content

Adding support for Dynamic Crosshair in other mods

Crendgrim edited this page Jan 13, 2023 · 1 revision

Dynamic Crosshair comes with an API that allows other mods to add support for it. This can either be done in the mod itself, or through a compatibility mod (like Dynamic Crosshair Compat).

To achieve this, you need to create an implementation of DynamicCrosshairApi:

package com.example.examplemod;

import mod.crend.dynamiccrosshair.api.DynamicCrosshairApi;

public class ExampleModDynamicCrosshairCompat implements DynamicCrosshairApi {
    @Override
    public String getNamespace() {
        return ExampleMod.MOD_ID;
    }

    // ... implement functions, refer to the DynamicCrosshairApi JavaDoc
}

getNamespace() should return the namespace under which your mod's items and blocks exist. Each API will only get called when one of the following conditions applies:

  • The targeted block or entity is part of the API's namespace
  • One of the held items is part of the API's namespace
  • The API overrides forceCheck() to return true.

Only one API implementation can exist per namespace. If necessary, you can register a second API for the same mod ID by overriding getModId() (defaults to simply return getNamespace()), and returning a unique but fake identifier by getNamespace(). This can be useful to have a small compatibility layer that is always checked if your mod is very big and the normal handler has to do some expensive calculations. APIs are only loaded if the mod their ID is set to is loaded.

This API class must then be registered with DynamicCrosshair. The recommended way depends on your loader.

Fabric

For Fabric, use the entrypoint "dynamiccrosshair" in your mod.fabric.json:

{
    ...
    "entrypoints": {
        ...
        "dynamiccrosshair": [
            "com.example.examplemod.ExampleModDynamicCrosshairCompat"
        ]
    }
}

Forge

For Forge, send an IMC message in your client event handler, pointing to the API constructor:

    @SubscribeEvent
    public static void onModEnqueue(InterModEnqueueEvent event) {
        if (ModList.get().isLoaded(DynamicCrosshair.MOD_ID)) {
            InterModComms.sendTo(DynamicCrosshair.MOD_ID, DynamicCrosshairForgeEvents.REGISTER_API, ExampleModDynamicCrosshairCompat::new);
        }
    }
Clone this wiki locally