/shared-resources

A Minecraft mod to easily share resources between multiple game instances.

Primary LanguageJavaGNU Lesser General Public License v3.0LGPL-3.0

Shared Resources

Mod Loader Enviroment Discord

Modrinth CurseForge

A mod for easily sharing game files between separate Minecraft instances. Best used with a multi-instance launcher like MultiMC or Prism Launcher.

It works by selecting a global directory to hold your common game files, and modifying Minecraft to use that directory instead of the default one. Where possible, the mod tries to load files from the global directory together with the local ones, merging the two and allowing you to select from both. This is currently only available for resource packs, data packs and shaders.

Currently supported game files/directories:

  • Resource packs
  • Data packs
  • Saves
  • Vanilla options (video settings, controls, etc.)
  • Servers
  • Screenshots
  • Saved Hotbars
  • The fabric config folder (should work with the configs of any properly coded mods)
  • Shader packs (via Iris)
  • Schematics (via Litematica)
  • Replay Recordings (via ReplayMod)
  • Skin Presets (via SkinShuffle)
  • World Map Data (via Xaero's World Map)

Incorporates code from RememberMyTxt (with permission from DuncanRuns) to ensure game options aren't lost when sharing the options.txt file between different game versions and mod sets.

API

A robust API is available for other mods to use, allowing them to easily add support for their own game files and directories.

To make use of the API, first add it as a dependency in gradle. It's recommended to include the API as a JiJ dependency as well, as the file size is small, and it doesn't do anything without the main mod present. You can also use it without JiJ'ing, but you'll have to make sure you only interact with API classes when the main mod is present.

repositories {
    maven {
        url "https://maven.enjarai.nl/releases"
    }
}

dependencies {
    modImplementation include("nl.enjarai:shared-resources-api::[VERSION]")
}

As the version, use the latest version from the releases page

Then, you'll want to create a Shared Resources entrypoint in your mod's fabric.mod.json:

{
  "entrypoints": {
    "shared-resources": [
      "com.example.modid.GameResources"
    ]
  }
}

Make sure to implement the SharedResourcesEntryPoint interface in your entrypoint class, and use that to create and register your GameResource instances, each corresponding to a game file or directory. This entrypoint is run during fabric's preLaunch phase, limit your interaction with Minecraft classes to a minimum.

public class GameResources implements SharedResourcesEntrypoint {
    // You can manually implement these interfaces, but a builder is provided for convenience
    public static final ResourceDirectory MY_CUSTOM_DIRECTORY = new ResourceDirectoryBuilder("custom_directory")
            .setDisplayName(Text.translatable("modid.directory.custom_directory"))
            .setDescription(
                    Text.translatable("modid.directory.custom_directory.description[0]"),
                    Text.translatable("modid.directory.custom_directory.description[1]")
            )
            .requiresRestart() // Set this if the directory requires a restart to take effect
            .overridesDefaultDirectory() // Set this if the directory completely overrides the default one
            .defaultEnabled(false) // You can probably guess what this does
            .isExperimental() // Set this to warn the user that issues may arise
            .build(); // For a comprehensive list of the available options, see the javadocs
    public static final ResourceFile A_CUSTOM_FILE = new ResourceFileBuilder("custom_file")
            .setDisplayName(Text.translatable("modid.file.custom_file"))
            .build();
    
    @Override
    public void registerResources(GameResourceRegistry registry) {
        // Don't forget to register your resources
        registry.register(new Identifier("modid", "custom_directory"), MY_CUSTOM_DIRECTORY);
        registry.register(new Identifier("modid", "custom_file"), A_CUSTOM_FILE);
    }
}

After registering your resources, they will automatically show up in the main mod's config screen, if it is loaded.

Finally, you can use GameDirectoryHelper to get the path to the location of your resource when loading it:

// If a global directory is selected, this will return the path to the global directory
Path dirLocation = GameDirectoryHelper.getPathFor(GameResources.MY_CUSTOM_DIRECTORY);

// Make sure to check if its null before using it
if (dirLocation != null) {
    loadYourStuffFunction(dirLocation);
}
    

// Try to load from your default directory as well when possible
loadYourStuffFunction(GameResources.MY_CUSTOM_DIRECTORY.getDefaultPath());

License

This mod is under the GNU Lesser General Public License v3.0.