Skip to content

Support loading Fabric mods through Connector on Mohist - #2343

Open
TansuoTro wants to merge 4 commits into
Sinytra:1.20.1from
TansuoTro:1.20.1
Open

TansuoTro wants to merge 4 commits into
Sinytra:1.20.1from
TansuoTro:1.20.1

Conversation

@TansuoTro

Copy link
Copy Markdown

The Issue

Mohist implements the Bukkit API on top of Forge and, in
doing so, forks ModLauncher and securejarhandler. A few assumptions Connector makes do not hold
there, so loading a Fabric mod through Connector on a Mohist server fails during boot — and it
fails in a way that is very hard to trace.

The server dies inside Bootstrap, naming a vanilla class the mod never touches:

  java.lang.NoClassDefFoundError: net/minecraft/world/item/DyeableArmorItem
      at net.minecraft.world.level.block.ComposterBlock.m_51988_(ComposterBlock.java:70)
      at net.minecraft.server.Bootstrap.m_135870_(Bootstrap.java:47)

The class exists in the runtime jar and ComposterBlock does not reference it. What actually
happens is that a mixin failed to apply, Mixin aborted the transformation of its target class, and
ModuleClassLoader.loadClass turned the resulting MixinTransformerError into a
ClassNotFoundException. Because ComposterBlock.bootStrap() resolves Items first, the error is
reported at that frame instead of at its cause. Anyone chasing the class name ends up in the wrong
place; the real signal is an earlier Mixin apply failed line.

Three independent gaps produce those failed mixins:

  1. Mixin 0.12-only APIs. Connector's transformation calls
    Locals.getLocalsAt(ClassNode, MethodNode, AbstractInsnNode, int) and reads
    ClassInfo.nonExpandedCache. Neither exists in Mixin 0.8.x, where the four-argument
    getLocalsAt takes a Locals$Settings. The resulting NoSuchMethodError /
    NoSuchFieldException aborts the transformation of the whole jar.
  2. Interface mixins. MixinPreProcessorInterface rejects private/protected methods and
    MixinApplicatorInterface rejects injectors declared by an interface mixin. Mods that use Java 9
    private interface methods or interface-mixin injectors — valid on Fabric's Mixin 0.12 — fail here.
  3. Rewritten vanilla. Mohist rewrites large parts of the vanilla classes Fabric mods target, so
    injection points frequently no longer match. Mixin treats a missing injection point as fatal for
    the entire target class, and the module class loader surfaces the abort as a missing class.

None of these are recoverable by the mod author, and together they make Connector effectively
unusable on Mohist.

The Proposal

Detect Mohist at runtime and degrade gracefully only where its forked loader/Mixin cannot honour
Connector's assumptions. The changes are split across four commits:

  • Add Mohist runtime detection (MohistCompat, ConnectorLoaderService, ConnectorBootstrap)

    • MohistCompat.isMohistRuntime() (cached) looks for the mohist_implementer launch-plugin id by
      reflecting into ModLauncher's private launchPlugins field, with a classpath probe
      (com.mohistmc.MohistMCStart / com.mohistmc.MohistMC on the ModLauncher, system and platform
      class loaders) as a fallback. Detection cannot use Class.forName alone: the Mohist server jar
      sits on the application class loader while Connector runs inside the ModLauncher module layer.
    • ConnectorLoaderService logs the active mode and wraps the authlib module move in a try/catch —
      Mohist's forked securejarhandler does not always accept it, and losing it only affects Fabric
      API internals, not startup.
    • ConnectorBootstrap.shouldApplyMixin skips IForgeItemMixin on Mohist. It targets an interface,
      which the interface applicator rejects, and that failure aborts the launch during
      BuiltInRegistries init; skipping it only loses the piglin-bartering redirect.
  • Handle interface mixins and the Locals API on Mixin 0.8.x (MixinPatchTransformer,
    JarTransformer, InterfaceMixinAccessWidener, InterfaceMixinInjectorStripper)

    • DynamicLVTPatch is added only when the 4-arg Locals.getLocalsAt(...) overload is present
      (hasExtendedLocalsApi()), so 0.8.x no longer throws NoSuchMethodError mid-transformation.
    • InterfaceMixinAccessWidener promotes non-public methods declared by interface mixins to public,
      turning Java 9 private interface methods into default methods with the same body so
      MixinPreProcessorInterface accepts them.
    • InterfaceMixinInjectorStripper removes injector handlers from interface mixins (both Mixin and
      MixinExtras injectors). MixinApplicatorInterface cannot apply them and a class mixin may not
      target an interface, so dropping the handler is the only option; every removed handler is logged
      at WARN.
    • JarTransformer.cleanupEnvironment treats ClassInfo.nonExpandedCache as optional. On 0.8.x the
      lookup used to throw and skip the rest of cleanup, including restoring java/lang/Object in the
      class cache.
  • Downgrade unmatched injections to warnings on Mohist (MixinPatchTransformer)

    • On Mohist, finalize() writes injectors.defaultRequire = 0 into each transformed mod's mixin
      config (idempotent, pretty-printed). Missing injection points then become warnings instead of a
      fatal class-transformation abort, so the mod loads with the affected hook inert.
  • Document running Connector on Mohist (MOHIST.md)

    • Covers the misleading NoClassDefFoundError, the Mixin 0.8.x gaps, what Mohist does
      differently, the known limitations, and how to run it.

Possible Side Effects

  • No effect outside Mohist. Every change is gated behind MohistCompat.isMohistRuntime() except
    the Mixin-version guards (hasExtendedLocalsApi, optional nonExpandedCache), which are pure
    no-ops on 0.12+ and simply prevent a crash on 0.8.x. Regular Forge / Fabric-runtime behaviour is
    unchanged.
  • Hooks can be silently inert. With defaultRequire = 0 and interface-mixin injector stripping,
    a mod will load even when some injection points no longer match on Mohist. The affected hooks do
    nothing. This is logged (WARN per removed handler, warnings per unmatched injection), but it is a
    correctness trade-off: a mod may appear to work while one of its features is quietly inactive.
  • IForgeItemMixin is skipped on Mohist, so the piglin-bartering redirect is not applied there.
  • Detection is reflective. MohistCompat reads a private ModLauncher field (launchPlugins) and
    probes the system/platform class loaders. If ModLauncher's internals change, detection falls back
    to the classpath probe and, if that also fails, simply reports "not Mohist" (safe default).
  • DynamicLVTPatch is not applied where the extended Locals API is absent, so LVT-dependent patches
    are not rewritten in that environment — but they previously crashed the jar transformation there,
    so this only changes a hard failure into a successful, partially-patched load.

Alternatives

  • Detect Mohist with Class.forName("com.mohistmc.*"). Rejected — it never succeeds, because the
    Mohist server jar is on the application class loader while Connector runs inside the ModLauncher
    module layer. That is why detection goes through the launch-plugin list and explicit class loaders.
  • Fail fast with a clear diagnostic instead of degrading. Rejected — the goal is to actually run
    Fabric mods on Mohist, not merely to explain why they cannot. A clear error message is still
    emitted for the cases that remain unsupported.
  • Convert interface mixins into class mixins to keep their injectors. Not possible — a class mixin
    may not target an interface, and the interface applicator rejects injectors outright. Removing the
    handler (and logging it) is the only path that lets the mod load.
  • Leave the 0.12-only API calls unconditional and require Mixin 0.12 on Mohist. Not viable —
    Mohist ships 0.8.5 and forks the loader; we cannot change the Mixin version it runs.

Additional Notes

  • Tested with Middle-earth 1.4.2 (2033 classes) on Mohist 1.20.1-ecf8d5ad / Forge 47.4.13.
    -Xmx2G is not enough (world generation OOMs while preparing the nether); -Xmx4G works.
  • This PR targets the 1.20.1 branch (Forge 47.x / Mixin 0.8.5). The changes are scoped to the
    transformation/loader path and do not touch the hot path at runtime.
  • MOHIST.md documents the symptoms, root causes, limitations (@At("STORE") with @Inject is
    rejected by 0.8.5; mixins Mohist applies itself are outside Connector's transformation), and how to
    run it (including deleting mods/.connector after replacing the Connector jar, or the cached
    transformation is reused).

Mohist forks ModLauncher and securejarhandler, so a few Connector assumptions do
not hold there. Detection goes through the "mohist_implementer" launch plugin id:
Class.forName on com.mohistmc.* never succeeds, because the server jar sits on the
application class loader while Connector runs inside the ModLauncher module layer.
The plugin list is read reflectively for that reason, and probing the class path
is kept as a fallback.

Two places use the flag. IForgeItemMixin targets an interface, which the interface
applicator rejects, and the resulting failure aborts the launch while
BuiltInRegistries initialises; skipping the mixin on Mohist only loses the piglin
bartering redirect. The authlib module move also fails against Mohist's forked
securejarhandler, and is now allowed to fail without taking the launch with it.
Fabric ships Mixin 0.12, Forge ships 0.8.5, and three things that work on Fabric
do not exist there.

DynamicLVTPatch calls Locals.getLocalsAt(ClassNode, MethodNode, AbstractInsnNode,
int). That overload arrived in 0.12; on 0.8.x the four argument method takes
Locals$Settings, so the call throws NoSuchMethodError and aborts the
transformation of the whole jar. The patch is now added only when the overload is
present.

Interface mixins are checked by MixinPreProcessorInterface, which rejects private
and protected methods. Mods use Java 9 private interface methods for injector
handlers, so InterfaceMixinAccessWidener promotes them to public, which turns them
into default methods with the same body.

Injectors declared by an interface mixin are rejected by MixinApplicatorInterface,
and a class mixin cannot target an interface either, so the handler cannot be kept
at all. InterfaceMixinInjectorStripper removes it and logs what was lost.

JarTransformer.cleanupEnvironment also read ClassInfo.nonExpandedCache, a
0.12-only field. The failed lookup skipped the rest of the cleanup, including
restoring java/lang/Object in the class cache. The field is optional now.
Mohist rewrites large parts of the vanilla classes that Fabric mods target, so
injection points often no longer match. Mixin treats a missing injection point as
fatal for the whole target class, and the module class loader then reports the
aborted transformation as a missing class, which is hard to trace back to the
mixin that caused it.

Setting injectors.defaultRequire to 0 in the mixin configs of the transformed mod
keeps the mod loading and leaves the affected hook inert. The failures are still
logged.
Covers the misleading NoClassDefFoundError, the Mixin 0.8.x gaps, what Mohist
does differently, and the known limitations of this approach.
@TansuoTro

Copy link
Copy Markdown
Author

I’ve tested it with many Fabric mods (including many that use Mixin; I can provide the mod list if needed). It is normally compatible with Mohist and in fact integrates very well with the Bukkit API ecosystem, which is within expectations.

@jonafanho2 jonafanho2 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 🆕 New

Development

Successfully merging this pull request may close these issues.

2 participants