Mixins and access wideners
Use Fabric Mixins and access wideners when public events are not enough.
Diese Anleitung gibt es nur auf Englisch.
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:
| Topic | Link |
|---|---|
| Fabric documentation | docs.fabricmc.net |
| Fabric wiki: Mixin injection reference | tutorial:mixin_injects |
| Fabric wiki: Access wideners | tutorial:accesswideners |
| SpongePowered Mixin wiki | github.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.jsonsrc/main/resources/<plugin-id>.mixins.jsonsrc/main/resources/<plugin-id>.accesswidenerif 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, andgameMode. - Test against the exact SoulFire version you ship for.
- Recheck the current built-in mixins before copying old examples.
Source trees worth reading
- SoulFire Fabric metadata
- SoulFire mixin config
- SoulFire event-emitting mixins
- Plugin example mixin
- SoulFire access widener
Next steps
Zuletzt aktualisiert am
War diese Seite hilfreich?
