Skip to main content

Validation & SPI Patterns

Combining multiple targets into one universal JAR introduces two subtle classes of bugs that javac alone cannot catch:

  1. Silent bytecode divergence — A class compiled from src/main compiles cleanly against both Minecraft 26.2 and 26.3, yet emits different bytecode in each target JAR.
  2. Unguarded version-specific mixins — A mixin config written for 26.3 gets merged into the universal JAR's fabric.mod.json / neoforge.mods.toml and crashes the game when loaded on 26.2.

Before assembling any combined JAR, GraftValidateClassesTask (validate<Name>Classes) checks for both and fails the build by default (ConflictMode.ERROR).

ASM bytecode conflict checking​

Real-world example: constant inlining across Minecraft versions​

In Minecraft 26.3, Mojang migrated input handling from GLFW to SDL3, changing the numeric values of public static final int constants on com.mojang.blaze3d.platform.InputConstants (such as KEY_B, MOUSE_BUTTON_LEFT, MOUSE_BUTTON_RIGHT).

Because Java compilers inline static final primitive constants at compile time:

  • Your common class in src/main compiles against both 26.2 and 26.3 with zero errors.
  • In 26.2:fabric, javac emits BIPUSH 66 (GLFW_KEY_B).
  • In 26.3:fabric, javac emits a different SDL3 keycode literal!
  • If a naive JAR merger picked the 26.2 .class file with DuplicatesStrategy.EXCLUDE, keybinds would silently break on 26.3.

GraftValidateClassesTask catches this immediately. Using ASM (ClassReader + ClassNode with SKIP_DEBUG | SKIP_FRAMES), it fingerprints every .class file across all combined targets — comparing class headers (access, supername, interfaces), fields (name, descriptor, access, constant value), and every instruction of every method — and reports the exact class, targets, method, and instruction index where bytecode diverged:

Detected 1 incompatible duplicate class(es) across combined targets:
- io/github/kosmx/emotes/mc/Keybinds.class (26.2:fabric vs 26.3:fabric):
* method <clinit>()V differs (at insn #4: [26.2:fabric] INT:16:66 vs [26.3:fabric] INT:16:98)

Configuring class validation​

combine("merged") {
fromAllTargets()
classConflictCheck = ConflictMode.ERROR // ERROR (default), WARN, or IGNORE
ignoreClassConflicts(
"com/example/generated/**",
"com.example.KnownDiff",
)
}

Both / and . separators and Ant-style * / ** wildcards are supported. A full report is always written to build/graft/<name>/class-validation-report.txt.

Mixin configuration validation​

When combining multiple Minecraft versions for the same loader, GraftValidateClassesTask also inspects fabric.mod.json and META-INF/neoforge.mods.toml along with every referenced *.mixins.json file and enforces two rules:

  1. Same filename, different contents — If two targets ship a mixin config with the same filename (e.g. mymod.mixins.json) whose JSON differs between targets, validation fails and tells you to split version- or loader-specific mixins into separate *.mixins.json files (such as mymod-26_2.mixins.json and mymod-26_3.mixins.json).
  2. Version-specific mixin config without "plugin" — If a mixin config is only present in a subset of Minecraft versions for a given loader (for example present in 26.3 but not 26.2), it must declare a non-blank "plugin" (IMixinConfigPlugin) to guard itself at runtime so Mixin does not try to apply 26.3 mixins while running on 26.2.
combine("merged") {
fromAllTargets()
mixinConflictCheck = ConflictMode.ERROR // ERROR (default), WARN, or IGNORE
ignoreMixinConfigs("mymod-legacy.mixins.json")
}

1. Version-gating mixins with IMixinConfigPlugin​

Put each version-specific or shared(...)-layer mixin set in its own *.mixins.json with a lightweight IMixinConfigPlugin:

src/26.3/common/resources/mymod-26_3.mixins.json
{
"required": true,
"package": "com.example.mymod.mc263.mixin",
"compatibilityLevel": "JAVA_25",
"plugin": "com.example.mymod.mc263.MyMod263MixinPlugin",
"client": [
"SomeChangedClassMixin"
]
}

To detect which Minecraft version is running before Minecraft classes are loaded (important inside IMixinConfigPlugin#shouldApplyMixin, where referencing Minecraft classes triggers premature classloading!), probe for a class or method that changed between versions via MixinService.getService().getBytecodeProvider() or getResourceAsStream(...):

public abstract class AbstractVersionMixinPlugin implements IMixinConfigPlugin {
protected static boolean hasClass(String internalName) {
return MixinService.getService()
.getResourceAsStream(internalName + ".class") != null;
}

protected abstract boolean isVersionSupported();

@Override
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
return isVersionSupported();
}
// ... empty defaults for the other IMixinConfigPlugin methods ...
}

For example, in Minecraft 26.3, net/minecraft/server/packs/resources/ResourceManager$Selector exists whereas it does not exist in 26.1 or 26.2 — so hasClass("net/minecraft/server/packs/resources/ResourceManager$Selector") cleanly distinguishes 26.3+ from <26.3 on both Fabric and NeoForge without ever loading a Minecraft class!

2. Version & loader dispatch via Java SPI (ServiceLoader)​

For regular Java code (and constants like InputConstants), define an interface in src/main and implement it once per version (or loader) with distinct class names registered in META-INF/services/<fqcn>:

src/main/java/.../MyVersionCompat.java # interface + ServiceLoader lookup
src/pre263/java/.../AbstractPre263Compat.java # shared base for 26.1 & 26.2
src/26.1/common/java/.../Compat261.java # concrete 26.1 impl
src/26.1/common/resources/META-INF/services/...MyVersionCompat
src/26.2/common/java/.../Compat262.java # concrete 26.2 impl
src/26.2/common/resources/META-INF/services/...MyVersionCompat
src/26.3/common/java/.../Compat263.java # concrete 26.3 impl
src/26.3/common/resources/META-INF/services/...MyVersionCompat

Each provider exposes an isActive() / getPriority() check (for instance using org.redlance.common.services.AdvancedService or a simple ServiceLoader.load(...) filter). When graft.combine(...) merges all targets, GraftMergeMetadataTask automatically combines every target's META-INF/services/...MyVersionCompat into a single deduplicated service file listing all active providers.