How to add ImGuiMC to your Stonecutter Project

This guide will show you how to add ImGuiMC from Foundry to your Stonecutter Project

Tags:

java

gradle

stonecutter

sc

froundry

imguimc

1. Add the ImGui Version settings to your versions in the stonecutter.properties.toml

mod.imgui_version = "<the prefererd imgui version>"
mod.imgui_mc_version = "<the minecraft version of the imgui version>"

You need to also set the Minecraft Version because some older versions also support newer versions, so some minecraft versions wont exist on the Maven Repo of Rayanh Code.

To see what imgui minecraft version supports what version in the Repo check here If the Version that you want to support is grayed out, use the Version above it. (Like for example if you want to support 1.21.10 you need to use 1.21.9)

2. Add the Maven Repository to the repositories Section in the build.gradle.kts File

maven("https://maven.ryanhcode.dev/releases") {
        name = "RyanHCode Maven"
}

3. Add the ImGuiMC Dependency in the dependencies Section in the same File

include("foundry.imguimc:imguimc-fabric-${sc.properties.rawOrNull("mod", "imgui_mc_version")}:${sc.properties.rawOrNull("mod", "imgui_version")}")
modImplementation("foundry.imguimc:imguimc-fabric-${sc.properties.rawOrNull("mod", "imgui_mc_version")}:${sc.properties.rawOrNull("mod", "imgui_version")}")

4. Add a Screen that will use ImGuiMC

Create a new File called e.g. ExampleScreen.java in a folder called screens that is located at com.example.examplemod.client.screens with this code

package com.example.examplemod.client.screens;

import foundry.imgui.api.ImGuiMC;
import imgui.ImGui;
import imgui.type.ImBoolean;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;

public final class ExampleScreen extends Screen {
    private static final ImBoolean showDemoWindow = new ImBoolean(false);

    public ExampleScreen() {
        super(Component.literal("Example Screen"));
    }

    @Override
    public void extractRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) {
        try(ImGuiMC.ActiveContext ctx = ImGuiMC.withImGui()) {
            if (ctx != null) {
                if (ImGui.begin("Hello, World!")) {
                    ImGui.setWindowSize(800, 600);
                    ImGui.checkbox("Show Demo Window", showDemoWindow);
                    ImGui.end();
                }

                ImGui.showDemoWindow(showDemoWindow);;
            }
        }
        super.extractRenderState(graphics, mouseX, mouseY, a);
    }
}

5. Make the user be able to open the Screen

Now to make the user be able to open the Screen I used for testing pourpuses a Button in the Title Screen If you want to add a Button to the Title Screen you need to create a new Mixin that will mixin into the TitleScreen.class

To add a Mixin that will mixin into the TitleScreen.class File you will need to create a new file called TitleScreenMixin.java that is located at com.example.examplemod.client.mixins The File needs to have this code

package com.example.examplemod.client.mixins;

import com.example.examplemod.client.screens..ExampleScreen;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.TitleScreen;
import net.minecraft.network.chat.Component;
import org.slf4j.Logger;
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;

@Mixin(TitleScreen.class)
public class TitleScreenMixin extends Screen {

    @Shadow
    @Final
    private static Logger LOGGER;

    protected TitleScreenMixin(Component title) {
        super(title);
    }

    @Inject(method = "init", at = @At("RETURN"))
    public void render(CallbackInfo ci) {
        this.addRenderableWidget(Button.builder(Component.literal("ImGui Demo"), (button) -> {
            Minecraft.getInstance().setScreenAndShow(new ExampleScreen());
        }).bounds(this.width - 75 - 3, 3, 70, 20).build());
    }
}

If you want to open the Screen you need to just call this function

Minecraft.getInstance().setScreenAndShow(new ExampleScreen());

This will add a new Buttom on the Right Top of the Title Screen with the Text ImGui Demo on it.

To make the Mixin work you now need to add it into your examplemod.mixins.json The File will look something like this but its important to add the Name of the File in the client Section.

{
  "required": true,
  "package": "com.example.examplemod.client.mixins",
  "compatibilityLevel": "${java}",
  "injectors": {
    "defaultRequire": 1
  },
  "client": [
    "TitleScreenMixin"
  ]
}

The package Field will set where all Mixins are found so set it to the Folder where you have / will create all Mixin Files.

6. Final words

Now you can start your Mod via either compiling it and then launching it with Minecraft Fabric.

How to install the Fabric Launcher?

How to compile Fabric mods?

How to launch Fabric mods?

To then open the ImGuiMC Screen press on the button at the Right Top in the Title Screen It will look something like this ImGui Demo Button on the Top Right in the Title Screen

Info

The code is programmed for the Minecraft Version 26. The function and class names may vary for you, if you use a diffrent version or diffrent mappings.