Przejdź do treści
SoulFire LogoSoulFire

Mixins and access wideners

Use Fabric Mixins and access wideners when public events are not enough.

Ten poradnik jest dostępny tylko po angielsku.

Use Mixins and access wideners only when events, settings, and the control APIs are not enough. Mixins inject code into Minecraft or SoulFire methods. Access wideners make private members accessible.

External documentation

Use these references for low-level integration work:

TopicLink
Fabric documentationdocs.fabricmc.net
Fabric wiki: Mixin injection referencetutorial:mixin_injects
Fabric wiki: Access widenerstutorial:accesswideners
SpongePowered Mixin wikigithub.com/SpongePowered/Mixin/wiki

When to use a Mixin

Use a Mixin when:

  • there is no event at the right timing point
  • you need to change or observe control flow inside Minecraft
  • you need access to data that SoulFire does not expose yet
  • you need a hook inside one specific Minecraft method

SoulFire itself uses Mixins heavily. Its own event hooks for chat, packets, ticks, client settings, and movement input are all implemented through Mixins. Use the SoulFire source tree as an example for plugin Mixins.

Required resource files

Your plugin normally needs these resources:

  • src/main/resources/fabric.mod.json
  • src/main/resources/<plugin-id>.mixins.json
  • src/main/resources/<plugin-id>.accesswidener if you need widened access

fabric.mod.json

Make sure the Fabric metadata points to the Mixin config and optional access widener:

{
  "schemaVersion": 1,
  "id": "my-plugin",
  "version": "${version}",
  "entrypoints": {
    "main": ["com.example.myplugin.Main"]
  },
  "mixins": ["my-plugin.mixins.json"],
  "accessWidener": "my-plugin.accesswidener",
  "depends": {
    "fabricloader": "*",
    "minecraft": "*",
    "soulfire": "*"
  }
}

Mixin config

A minimal Mixin config looks like this:

{
  "required": true,
  "minVersion": "0.8",
  "package": "com.example.myplugin.mixin",
  "compatibilityLevel": "JAVA_21",
  "mixins": [],
  "client": ["MyMixin"],
  "injectors": {
    "defaultRequire": 1
  }
}

SoulFire and the template both use compatibilityLevel: "JAVA_21", even though they build with Java 25. Keep JAVA_21 unless the SoulFire release you target changes it.

Loom wiring

The template wires the access widener like this:

loom {
  accessWidenerPath = file("src/main/resources/my-plugin.accesswidener")
}

If you do not need an access widener yet, keep the empty file so the build setup stays valid.

Find the bot inside a Mixin

Use BotConnection.currentOptional() to get the bot that owns the current thread:

@Mixin(LocalPlayer.class)
public class MyMixin {
  @Inject(method = "tick", at = @At("HEAD"))
  private void onTick(CallbackInfo ci) {
    var bot = BotConnection.currentOptional().orElse(null);
    if (bot == null) {
      return;
    }

    if (bot.settingsSource().get(MySettings.ENABLED)) {
      // your low-level logic here
    }
  }
}

currentOptional() is empty when the code runs outside a bot thread, so the Mixin does nothing there.

Access widener versus Mixin

Use an access widener when:

  • you only need visibility changes
  • you want to read or call an otherwise inaccessible field or method
  • you do not need to alter control flow

Use a Mixin when:

  • you need to inject before or after a method call
  • you need to wrap or replace logic
  • you need access to locals or method parameters at a precise point

Many plugins need both.

Best practices

  • Keep Mixins thin. Put the real logic in ordinary classes.
  • Prefer stable high-level targets over fragile local-variable-heavy injections.
  • Null-check runtime objects like player, level, and gameMode.
  • Test against the exact SoulFire version you ship for.
  • Recheck the current built-in mixins before copying old examples.

Source trees worth reading

Next steps

Ostatnia aktualizacja:

Czy ta strona jest pomocna?