Validation & SPI Patterns
Combining multiple targets into one universal JAR introduces two subtle classes of bugs that javac alone cannot catch:
- Silent bytecode divergence — A class compiled from
src/maincompiles cleanly against both Minecraft26.2and26.3, yet emits different bytecode in each target JAR. - Unguarded version-specific mixins — A mixin config written for
26.3gets merged into the universal JAR'sfabric.mod.json/neoforge.mods.tomland crashes the game when loaded on26.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/maincompiles against both26.2and26.3with zero errors. - In
26.2:fabric,javacemitsBIPUSH 66(GLFW_KEY_B). - In
26.3:fabric,javacemits a different SDL3 keycode literal! - If a naive JAR merger picked the
26.2.classfile withDuplicatesStrategy.EXCLUDE, keybinds would silently break on26.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:
- 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.jsonfiles (such asmymod-26_2.mixins.jsonandmymod-26_3.mixins.json). - 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 in26.3but not26.2), it must declare a non-blank"plugin"(IMixinConfigPlugin) to guard itself at runtime so Mixin does not try to apply26.3mixins while running on26.2.
combine("merged") {
fromAllTargets()
mixinConflictCheck = ConflictMode.ERROR // ERROR (default), WARN, or IGNORE
ignoreMixinConfigs("mymod-legacy.mixins.json")
}
Recommended runtime patterns for universal JARs
1. Version-gating mixins with IMixinConfigPlugin
Put each version-specific or shared(...)-layer mixin set in its own *.mixins.json with a lightweight IMixinConfigPlugin:
{
"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.