FlorianMichael / fabric-imgui-example-mod

Example Fabric mod which includes Dear ImGui
Creative Commons Zero v1.0 Universal
21 stars 1 forks source link

Mixin Errors in MinecraftClientMixin #9

Closed tobiazsh closed 1 month ago

tobiazsh commented 1 month ago

Hi! I just tried the full version of the example mod implemented in my project. I came across multiple Mixin errors and I don't know what any of them mean since I honestly have never even touched Mixins and I just want to get ImGui working so I can move on. The error it's throwing me, is: (FabricLoader/Mixin) Mixin apply for mod myworld_traffic_addition failed myworld_traffic_addition.client.mixins.json:ImGui.MinecraftClientMixin from mod myworld_traffic_addition -> com.mojang.authlib.minecraft.client.MinecraftClient: org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException @Shadow field window was not located in the target class com.mojang.authlib.minecraft.client.MinecraftClient. No refMap loaded.

And in the MinecraftClientMixin file, my IDE (IntelliJ) shows the the following error messages:

Here's my MinecraftClientMixin:

import at.tobiazsh.myworld.traffic_addition.ImGui.ImGuiImpl;
import com.mojang.authlib.minecraft.client.MinecraftClient;
import net.minecraft.client.util.Window;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.io.IOException;

@Mixin(MinecraftClient.class)
public class MinecraftClientMixin {

    @Shadow
    @Final
    private Window window;

    @Inject(method = "<init>", at = @At("RETURN"))
    public void initImGui(CallbackInfo ci) throws IOException {
        ImGuiImpl.create(window.getHandle());
    }

    @Inject(method = "close", at = @At("RETURN"))
    public void closeImGui(CallbackInfo ci) {
        ImGuiImpl.dispose();
    }
}

and here's my ExampleClientMixin:

import at.tobiazsh.myworld.traffic_addition.ImGui.ImGuiImpl;
import at.tobiazsh.myworld.traffic_addition.MyWorldTrafficAddition;
import imgui.ImGui;
import net.minecraft.client.MinecraftClient;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(MinecraftClient.class)
public class ExampleClientMixin {
    @Inject(at = @At("HEAD"), method = "run")
    private void init(CallbackInfo info) {
        MyWorldTrafficAddition.LOGGER.info("Initialized Mixins!");
    }

    @Inject(method = "render", at = @At("RETURN"))
    private void render(boolean tick, CallbackInfo ci) {
        ImGuiImpl.draw(io -> {
            ImGui.begin("Hello World");

            ImGui.end();

            ImGui.showDemoWindow();
        });
    }
}

Thanks or any help in advance!

FlorianMichael commented 1 month ago

Still looks like you didn't setup Fabric correctly.

tobiazsh commented 1 month ago

First of all, thanks for the reply! I mean, I think I did set it up correctly. I copied all the in my opinion necessary things from build.gradle and fabric.mod.json into mine. I must've missed a few things then...

That's my build.gradle:

plugins {
    id 'fabric-loom' version '1.7-SNAPSHOT'
    id 'maven-publish'
}

version = project.mod_version
group = project.maven_group

project.ext.imgui_version = "1.87.5";

base {
    archivesName = project.archives_base_name
}

repositories {
    // Add repositories to retrieve artifacts from in here.
    // You should only use this when depending on other mods because
    // Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
    // See https://docs.gradle.org/current/userguide/declaring_repositories.html
    // for more information about repositories.

    maven {
        name = "deftuRepoSnapshots"
        url = uri("https://maven.deftu.dev/snapshots")
    }

    mavenCentral()
}

loom {
    splitEnvironmentSourceSets()

    mods {
        "myworld_traffic_addition" {
            sourceSet sourceSets.main
            sourceSet sourceSets.client
        }
    }

    accessWidenerPath = file("src/main/resources/myworld_traffic_addition.accesswidener")
}

dependencies {
    // To change the versions see the gradle.properties file
    minecraft "com.mojang:minecraft:${project.minecraft_version}"
    mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
    modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

    // Fabric API. This is technically optional, but you probably want it anyway.
    modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

    // Dear ImGui Implementation
    include implementation("io.github.spair:imgui-java-binding:${project.imgui_version}")
    include implementation("io.github.spair:imgui-java-lwjgl3:${project.imgui_version}")

    include implementation("io.github.spair:imgui-java-natives-windows:${project.imgui_version}")
    include implementation("io.github.spair:imgui-java-natives-linux:${project.imgui_version}")
    include implementation("io.github.spair:imgui-java-natives-macos:${project.imgui_version}")
}

processResources {
    inputs.property "version", project.version

    filesMatching("fabric.mod.json") {
        expand "version": project.version
    }
}

tasks.withType(JavaCompile).configureEach {
    it.options.release = 21
}

java {
    // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
    // if it is present.
    // If you remove this line, sources will not be generated.
    withSourcesJar()

    sourceCompatibility = JavaVersion.VERSION_21
    targetCompatibility = JavaVersion.VERSION_21
}

jar {
    from("LICENSE") {
        rename { "${it}_${project.base.archivesName.get()}"}
    }
}

// configure the maven publication
publishing {
    publications {
        create("mavenJava", MavenPublication) {
            artifactId = project.archives_base_name
            from components.java
        }
    }

    // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing.
    repositories {
        // Add repositories to publish to here.
        // Notice: This block does NOT have the same function as the block in the top level.
        // The repositories here will be used for publishing your artifact, not for
        // retrieving dependencies.
    }
}

and that's my fabric.mod.json:

{
    "schemaVersion": 1,
    "id": "myworld_traffic_addition",
    "version": "${version}",
    "name": "MyWorld Traffic Addition",
    "description": "A Minecraft Mod that adds so much more than just stupid, lousy, pre-defined traffic signs! Create your own signs, make directional signs, turn them at any angle to create your perfect roads and highways!",
    "authors": [
        "Tobiazsh"
    ],
    "contact": {
        "homepage": "https://fabricmc.net/",
        "sources": "https://github.com/FabricMC/fabric-example-mod"
    },
    "license": "CC0-1.0",
    "icon": "assets/myworld_traffic_addition/icon.png",
    "environment": "*",
    "entrypoints": {
        "main": [
            "at.tobiazsh.myworld.traffic_addition.MyWorldTrafficAddition"
        ],
        "client": [
            "at.tobiazsh.myworld.traffic_addition.MyWorldTrafficAdditionClient"
        ]
    },
    "mixins": [
        "myworld_traffic_addition.mixins.json",
        {
            "config": "myworld_traffic_addition.client.mixins.json",
            "environment": "client"
        }
    ],
    "depends": {
        "fabricloader": ">=0.15.11",
        "minecraft": "~1.21",
        "java": ">=21",
        "fabric-api": "*"
    },
    "suggests": {
        "another-mod": "*"
    },
    "accessWidener" : "myworld_traffic_addition.accesswidener"
}
FlorianMichael commented 1 month ago

That's not really what I meant, the errors you provided are indicating that you haven't setup the environment in your IDE correctly. I at least can't see any issues in my example mod / the code you provided.

tobiazsh commented 1 month ago

Hmmm... I followed the Fabric Docs 1:1. If I run it from the command line, I get these errors:

    @Inject(method = "<init>", at = @At("RETURN"))
    ^
D:\Development\Fabric\MyWorld Traffic Addition\src\client\java\at\tobiazsh\myworld\traffic_addition\mixin\client\ImGui\MinecraftClientMixin.java:34: Warnung: Unable to determine descriptor for @Inject target method
    @Inject(method = "close", at = @At("RETURN"))
    ^
D:\Development\Fabric\MyWorld Traffic Addition\src\client\java\at\tobiazsh\myworld\traffic_addition\mixin\client\ImGui\MinecraftClientMixin.java:25: Warnung: Cannot find target for @Shadow field in com.mojang.authlib.minecraft.client.MinecraftClient
    @Shadow
    ^

I really don't know what's wrong here...

FlorianMichael commented 1 month ago

Are you sure you used the 1.21 branch and not the 1.21.1 branch?

tobiazsh commented 1 month ago

Okay, yeah. That was one error. But the errors unfortunately still stay the same:

Warnung: Unable to locate obfuscation mapping for @Inject target <init>
    @Inject(method = "<init>", at = @At("RETURN"))

@Shadow field window was not located in the target class com.mojang.authlib.minecraft.client.MinecraftClient. No refMap loaded.
FlorianMichael commented 1 month ago

I think you imported the wrong MinecraftClient class, it should be in net.minecraft package

tobiazsh commented 1 month ago

YES Thank you so much. That fixed the mixin errors.... And there are new one's again. java.lang.NoSuchMethodError: 'boolean imgui.gl3.ImGuiImplGl3.init()'


    public static void create(final long handle) {
        ImGui.createContext();
        ImPlot.createContext();

        final ImGuiIO data = ImGui.getIO();
        data.setIniFilename(MyWorldTrafficAddition.MOD_ID + ".ini");
        data.setFontGlobalScale(1F);

        /*final ImFontAtlas fonts = data.getFonts();
        final ImFontGlyphRangesBuilder rangesBuilder = new ImFontGlyphRangesBuilder();

        rangesBuilder.addRanges(data.getFonts().getGlyphRangesDefault());
        rangesBuilder.addRanges(data.getFonts().getGlyphRangesCyrillic());
        rangesBuilder.addRanges(data.getFonts().getGlyphRangesJapanese());

        final short[] glyphRanges = rangesBuilder.buildRanges();

        final ImFontConfig basicConfig = new ImFontConfig();
        basicConfig.setGlyphRanges(glyphRanges);

        final List<ImFont> generatedFonts = new ArrayList<>();
        for (int i = 5; i < 50; i++) {
            basicConfig.setName("DejaVuSans " + i + "px");
            generatedFonts.add(fonts.addFontFromMemoryTTF(IOUtils.toByteArray(ImGuiImpl.class.getResourceAsStream("DejaVuSans.ttf")), i, basicConfig, glyphRanges));
        }
        fonts.build();
        basicConfig.destroy();*/

        data.setConfigFlags(ImGuiConfigFlags.DockingEnable);

        imGuiImplGlfw.init(handle, true);
        imGuiImplGl3.init();
    }```
FlorianMichael commented 1 month ago

Removed imGuiImplGl3.init()

tobiazsh commented 1 month ago

But it's in your example. Won't that mess up things if I remove it?

FlorianMichael commented 1 month ago

The 1.21.0 branch doesn't have this line

tobiazsh commented 1 month ago

grafik I'm on 1.21 but I see it

FlorianMichael commented 1 month ago

Change the ImGui version to 1.86.4

tobiazsh commented 1 month ago

Yes that fixed. Everything. Thank you so much!

FlorianMichael commented 1 month ago

👍