tinycodinglm / mc /mc-0004.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame Contribute Delete
3.64 kB
opencode trace
System You are an expert Fabric mod developer. You write idiomatic Minecraft Fabric mods using Java, Fabric API, and Yarn mappings.

You are an expert Fabric mod developer. You write idiomatic Minecraft Fabric mods using Java, Fabric API, and Yarn mappings.

User

How do I create a simple ScreenHandler and Screen for a GUI in Fabric?

Assistant
java
package com.example.mymod.screen;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;

public class CustomScreenHandler extends ScreenHandler {
    private final SimpleInventory inventory;

    public CustomScreenHandler(int syncId, PlayerInventory playerInventory) {
        this(syncId, playerInventory, new SimpleInventory(9));
    }

    public CustomScreenHandler(int syncId, PlayerInventory playerInventory, SimpleInventory inventory) {
        super(MyScreenHandlers.CUSTOM, syncId);
        this.inventory = inventory;
        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < 3; col++) {
                this.addSlot(new Slot(inventory, col + row * 3, 8 + col * 18, 18 + row * 18));
            }
        }
        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < 9; col++) {
                this.addSlot(new Slot(playerInventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
            }
        }
        for (int col = 0; col < 9; col++) {
            this.addSlot(new Slot(playerInventory, col, 8 + col * 18, 142));
        }
    }

    @Override
    public boolean canUse(PlayerEntity player) { return this.inventory.canPlayerUse(player); }

    @Override
    public void onClosed(PlayerEntity player) { super.onClosed(player); this.inventory.onClosed(player); }
}

Client-side Screen:

java
package com.example.mymod.screen;

import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

public class CustomScreen extends HandledScreen<CustomScreenHandler> {
    private static final Identifier TEXTURE = Identifier.of("mymod", "textures/gui/custom_gui.png");

    public CustomScreen(CustomScreenHandler handler, PlayerInventory inventory, Text title) {
        super(handler, inventory, title);
        this.backgroundWidth = 176;
        this.backgroundHeight = 166;
    }

    @Override
    protected void init() {
        this.addDrawableChild(new ButtonWidget.Builder(
            Text.literal("Click Me"), button -> { /* Handle click */ }
        ).dimensions(this.x + 50, this.y + 50, 80, 20).build());
    }

    @Override
    protected void drawBackground(DrawContext context, float delta, int mouseX, int mouseY) {
        context.drawTexture(TEXTURE, this.x, this.y, 0, 0, this.backgroundWidth, this.backgroundHeight);
    }
}

Register screen handler:

java
public class MyScreenHandlers {
    public static final ScreenHandlerType<CustomScreenHandler> CUSTOM = 
        new ScreenHandlerType<>(CustomScreenHandler::new);
    public static void register() {
        Registry.register(Registries.SCREEN_HANDLER, MyMod.id("custom"), CUSTOM);
    }
}
// In client initializer:
ClientScreenHandlerRegistry.registerScreenFactory(MyScreenHandlers.CUSTOM, CustomScreen::new);