Sodium Documentation

repository·dev·Indexed 26 days ago

https://github.com/caffeinemc/sodium

A high-performance rendering engine and optimization mod for Minecraft designed to improve frame rates and reduce micro-stutter. Documentation covers installation for Fabric and NeoForge, hardware requirements (OpenGL 4.5+), building from source using Gradle and OpenJDK 21, and the Sodium Config API for developers to register custom mod settings, pages, and options.

Tokens
3.5K
Snippets
2
Records
16
Agent score
89%

What's inside Sodium

  1. Overview of Sodium block model optimizations

    dev

    Sodium uses optimized block models that are based on the original Minecraft game assets. These models have been modified to reduce the total number of vertices, improving performance.

    Compatibility Note for Resource Pack Authors: The texture mapping of these optimized models is identical to the original game's models. Resource packs that replace block textures should remain compatible and should not look different. If you encounter issues where models or textures no longer work correctly with Sodium, please report the issue.

  2. Design icons and theme colors for Sodium Config

    dev

    When adding icons and colors to your config pages, follow these UI guidelines:

    • Icons: The UI is optimized for monochrome, binary-alpha icons. These are automatically tinted with your mod's theme color. While full-color icons can be used, monochrome is recommended for visual consistency.
    • Theme Colors: You do not need to set a color manually; the system selects one automatically. If you provide a custom color, ensure it has sufficient saturation (to act as an accent) and appropriate brightness (not too dark, so the highlight color remains visible, and not too bright, so the highlight has contrast).
  3. Create a Sodium Config Entrypoint

    dev

    To register options, you must implement the net.caffeinemc.mods.sodium.api.config.ConfigEntryPoint interface. You can register this entrypoint using metadata (Fabric) or annotations (NeoForge).

    Fabric Registration

    Add the entrypoint to your fabric.mod.json under the sodium:config_api_user key.

    {
        "entrypoints": {
            "sodium:config_api_user": [
                "com.example.examplemod.ExampleModConfigBuilder"
            ]
        }
    }

    NeoForge Registration

    Use the @ConfigEntryPointForge("your_mod_id") annotation on your implementation class. The annotation requires your mod ID to correctly associate the config with your mod when using ConfigBuilder.registerOwnModOptions().

    import net.caffeinemc.mods.sodium.api.config.ConfigEntryPoint;
    import net.caffeinemc.mods.sodium.api.config.ConfigEntryPointForge;
    
    @ConfigEntryPointForge("examplemod")
    public class ExampleConfigUser implements ConfigEntryPoint {
        // Implementation of registerConfigLate
    }
  4. Download Sodium stable or nightly builds

    dev

    Stable builds

    The latest stable releases are available on Modrinth and CurseForge.

    Nightly builds

    Bleeding-edge builds for testing latest changes are available via the Nightly Builds wiki page. These are intended for developers and expert users and come without warranty.

    Maven Repository

    Developers can include Sodium in their workspace using our Maven repository. Documentation for this is available on the CaffeineMC Maven & Config API wiki.

  5. Register options using ConfigBuilder

    dev

    Implement registerConfigLate(ConfigBuilder builder) to define your mod's settings pages. The API uses a declarative builder pattern to create pages, groups, and options.

    Key Concepts:

    • Pages: Top-level containers for a mod's settings. Can have an ID, name, version, theme color, and an icon.
    • Groups: Collections of options within a page.
    • Options: Individual settings (Boolean, Integer slider, Enum, or External Screen).

    Important Note on Data Storage: The Sodium Config API is a presentation API only. It does not handle saving or loading files. You must provide a storageHandler (a Runnable to flush changes) and use setBinding to connect the UI to your own configuration storage logic.

    package com.example.examplemod;
    
    import net.caffeinemc.mods.sodium.api.config.ConfigEntryPoint;
    import net.caffeinemc.mods.sodium.api.config.structure.ConfigBuilder;
    import net.minecraft.network.chat.Component;
    import net.minecraft.resources.Identifier;
    
    public class ExampleConfigUser implements ConfigEntryPoint {
        private final OptionStorage storage = new OptionStorage();
        private final Runnable handler = this.storage::flush;
        
        @Override
        public void registerConfigLate(ConfigBuilder builder) {
            builder.registerOwnModOptions()
                    .setIcon(Identifier.parse("examplemod:textures/gui/icon.png"))
                    .addPage(builder.createOptionPage()
                            .setName(Component.literal("Example Page"))
                            .addOptionGroup(builder.createOptionGroup()
                                    .setName(Component.literal("Example Group"))
                                    .addOption(builder.createBooleanOption(Identifier.parse("examplemod:example_option"))
                                            .setName(Component.literal("Example Option"))
                                            .setTooltip(Component.literal("Example tooltip"))
                                            .setStorageHandler(this.handler)
                                            .setBinding(this.storage::setExampleOption, this.storage::getExampleOption)
                                            .setDefaultValue(true)
                                    )
                            )
                    );
        }
    }
    
    class OptionStorage {
        private boolean exampleOption = true;
        public boolean getExampleOption() { return this.exampleOption; }
        public void setExampleOption(boolean value) { this.exampleOption = value; }
        public void flush() { /* Save to file here */ }
    }
  6. Build Sodium from source

    dev

    Sodium uses the Gradle build tool. Use the provided Gradle wrapper to ensure the correct version is used.

    Build Commands

    • macOS/Linux: ./gradlew build
    • Windows: ./gradlew.bat build

    Build artifacts (production binaries and source bundles) are located in the build/mods directory.

  7. Add Sodium Config API dependency to your project

    dev

    To use the Sodium Config API, you must declare the CaffeineMC Maven repository and add the appropriate API dependency based on your platform and Minecraft version.

    Maven Repository Configuration:

    // Groovy
    maven {
        name "CaffeineMC"
        url "https://maven.caffeinemc.net/releases" // or /snapshots
    }
    // Kotlin
    maven {
        name = "CaffeineMC"
        url = uri("https://maven.caffeinemc.net/releases") // or /snapshots
    }

    Dependency Configuration:

    • Fabric 1.21.11: Use modImplementation.
    • Fabric 1.21.12+ and NeoForge 1.21.11+: Use implementation.
  8. Configure individual options with `OptionBuilder`

    dev

    The OptionBuilder provides methods to define the behavior, appearance, and side effects of a configuration option:

    • Storage: OptionBuilder.setStorageHandler allows you to flush changes to the config file once after all bindings are updated, rather than on every single change.
    • UI/UX:
      • OptionBuilder.setTooltip(Function): Sets a tooltip, which can be a function that generates text based on the current value (useful for long enum descriptions).
      • OptionBuilder.setImpact(Impact): Specifies performance impact (e.g., low, high, or "varies").
      • OptionBuilder.setEnabled(boolean): Disables the option (shows as strikethrough and non-interactive).
    • Values & Constraints:
      • OptionBuilder.setDefaultValue(T) or OptionBuilder.setDefaultProvider(Provider): Sets the fallback value if constraints are not met.
      • OptionBuilder.setBinding(Binding): Configures the binding used to load the initial value and handle updates/resets.
    • Side Effects:
      • OptionBuilder.setFlags(Set<OptionFlag>): Controls what is reset when the option is applied (e.g., reloading chunks or resource packs).
      • OptionBuilder.setApplyHook(Consumer<ConfigState>): Runs a hook after the option has been saved if its value changed.
  9. Replace or Overlay existing options

    dev

    You can modify options belonging to Sodium or other mods using two patterns:

    Replacement Use ModOptionsBuilder.registerOptionReplacement(Identifier targetId, OptionBuilder newOption) to completely swap an existing option with a new one. You can optionally choose to adopt the old ID so that other overlays targeting the original ID still work.

    Overlay Use ModOptionsBuilder.registerOptionOverlay(Identifier targetId, OptionBuilder partialOption) to modify specific properties of an existing option without replacing the entire thing. The properties provided in the partialOption will overwrite the target's properties.

  10. Configure Mod Metadata and Themes with `ConfigBuilder` and `ModOptionsBuilder`

    dev

    Use ConfigBuilder.registerOwnModOptions to register options for the mod that owns the entrypoint (or the mod ID specified in @ConfigEntryPointForge). This returns a ModOptionsBuilder to configure the mod's identity and appearance in the video settings page list.

    Key Configuration Options:

    • Identity: Set the mod's ID, name, version, or a version formatter.
    • Color Themes: Use ModOptionsBuilder.setColorTheme to set a custom color. You can provide three (A)RGB colors or a single base color (lighter and darker colors are derived automatically).
    • Icons:
      • ModOptionsBuilder.setIcon(Identifier): Sets an icon that is tinted with the theme color and rendered as a square.
      • ModOptionsBuilder.setNonTintedIcon(Identifier): Sets an icon that is not tinted.
    • External Pages: Use ConfigBuilder.createExternalPage(String name, Consumer<Screen> screenSwitcher) to create a link that switches to a custom Screen when clicked.