Fabric API

Fabric API

108M Downloads

Wiki - Mixin Misinformation

rshchekotov opened this issue ยท 3 comments

commented

As it's related to the (Official) Fabric Wiki, I think it's appropriate to open the issue here, should that not be the case, please let me know where I should move this issue (thanks):

The mixin introduction article states (in bold), that "mixins must be written in Java, even if you use Kotlin or another language".
That is not true, as I managed to write a perfectly working Mixin in Kotlin.

Feel free to test it out, it should work with the Fabric-Kotlin template updated to the latest version and the following bits of config/code:
modid.mixins.json

{
  "required": true,
  "package": "net.fabricmc.example.mixin",
  "compatibilityLevel": "JAVA_17",
  "injectors": {
    "defaultRequire": 1
  },
  "client": [
    "AuthExSuppressorMixin"
  ],
  "mixins": []
}

and then at net.fabricmc.example.mixin.AuthExSuppressorMixin

@file:Suppress("SpellCheckingInspection")

package net.fabricmc.example.mixin

import net.minecraft.client.MinecraftClient
import org.slf4j.Logger
import org.spongepowered.asm.mixin.Mixin
import org.spongepowered.asm.mixin.injection.At
import org.spongepowered.asm.mixin.injection.Redirect

@Mixin(MinecraftClient::class)
class AuthExSuppressorMixin {
    @Redirect(
        method = ["createUserApiService"],
        at = At(value = "INVOKE", target="Lorg/slf4j/Logger;error(Ljava/lang/String;Ljava/lang/Throwable;)V"))
    private fun errorProxy(logger: Logger, s: String, @Suppress("UNUSED_PARAMETER") ignored: Throwable) {
        logger.error("Failed to authenticate with Mojang servers: $s")
    }
}
commented

Since this is a wiki thing, it doesn't belong on Github at all. Still, the current guideline of the Fabric project is to not recommend Kotlin mixins at all so I don't see a need to change it on the wiki either.

commented

It's possible, but Kotlin's bytecode generation can be wacky and often doesn't match Java's (this is coming from another Kotlin user). Statics require weird workarounds too like file-level mixins.

This is also the wrong repo for this discussion anyway, it's not related to Fabric API.

commented

I see (I'm still rather new to Kotlin - using Fabric as an opportunity to learn and apply the language, so that's some interesting stuff ^^).
@Juuxel - where would the right repo be?
Sorry for the inconvenience ๐Ÿ˜