File size: 3,644 Bytes
d517dc9
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "mc-0004", "name": "Screens and GUIs"}
{"type": "message", "message": {"role": "system", "content": "You are an expert Fabric mod developer. You write idiomatic Minecraft Fabric mods using Java, Fabric API, and Yarn mappings."}}
{"type": "message", "message": {"role": "user", "content": "How do I create a simple ScreenHandler and Screen for a GUI in Fabric?"}}
{"type": "message", "message": {"role": "assistant", "content": "```java\npackage com.example.mymod.screen;\n\nimport net.minecraft.entity.player.PlayerEntity;\nimport net.minecraft.entity.player.PlayerInventory;\nimport net.minecraft.inventory.SimpleInventory;\nimport net.minecraft.screen.ScreenHandler;\nimport net.minecraft.screen.slot.Slot;\n\npublic class CustomScreenHandler extends ScreenHandler {\n    private final SimpleInventory inventory;\n\n    public CustomScreenHandler(int syncId, PlayerInventory playerInventory) {\n        this(syncId, playerInventory, new SimpleInventory(9));\n    }\n\n    public CustomScreenHandler(int syncId, PlayerInventory playerInventory, SimpleInventory inventory) {\n        super(MyScreenHandlers.CUSTOM, syncId);\n        this.inventory = inventory;\n        for (int row = 0; row < 3; row++) {\n            for (int col = 0; col < 3; col++) {\n                this.addSlot(new Slot(inventory, col + row * 3, 8 + col * 18, 18 + row * 18));\n            }\n        }\n        for (int row = 0; row < 3; row++) {\n            for (int col = 0; col < 9; col++) {\n                this.addSlot(new Slot(playerInventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));\n            }\n        }\n        for (int col = 0; col < 9; col++) {\n            this.addSlot(new Slot(playerInventory, col, 8 + col * 18, 142));\n        }\n    }\n\n    @Override\n    public boolean canUse(PlayerEntity player) { return this.inventory.canPlayerUse(player); }\n\n    @Override\n    public void onClosed(PlayerEntity player) { super.onClosed(player); this.inventory.onClosed(player); }\n}\n```\n\nClient-side Screen:\n```java\npackage com.example.mymod.screen;\n\nimport net.minecraft.client.gui.DrawContext;\nimport net.minecraft.client.gui.screen.ingame.HandledScreen;\nimport net.minecraft.text.Text;\nimport net.minecraft.util.Identifier;\n\npublic class CustomScreen extends HandledScreen<CustomScreenHandler> {\n    private static final Identifier TEXTURE = Identifier.of(\"mymod\", \"textures/gui/custom_gui.png\");\n\n    public CustomScreen(CustomScreenHandler handler, PlayerInventory inventory, Text title) {\n        super(handler, inventory, title);\n        this.backgroundWidth = 176;\n        this.backgroundHeight = 166;\n    }\n\n    @Override\n    protected void init() {\n        this.addDrawableChild(new ButtonWidget.Builder(\n            Text.literal(\"Click Me\"), button -> { /* Handle click */ }\n        ).dimensions(this.x + 50, this.y + 50, 80, 20).build());\n    }\n\n    @Override\n    protected void drawBackground(DrawContext context, float delta, int mouseX, int mouseY) {\n        context.drawTexture(TEXTURE, this.x, this.y, 0, 0, this.backgroundWidth, this.backgroundHeight);\n    }\n}\n```\n\nRegister screen handler:\n```java\npublic class MyScreenHandlers {\n    public static final ScreenHandlerType<CustomScreenHandler> CUSTOM = \n        new ScreenHandlerType<>(CustomScreenHandler::new);\n    public static void register() {\n        Registry.register(Registries.SCREEN_HANDLER, MyMod.id(\"custom\"), CUSTOM);\n    }\n}\n// In client initializer:\nClientScreenHandlerRegistry.registerScreenFactory(MyScreenHandlers.CUSTOM, CustomScreen::new);\n```"}}