tinycodinglm / mc /mc-0009.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame Contribute Delete
4.45 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 tradeable NPC entity with dialogue system and screen-based trading interface?

Assistant
java
package com.example.mymod.entity;

import net.minecraft.entity.EntityType;
import net.minecraft.entity.NpcEntity;
import net.minecraft.entity.ai.goal.LookAtEntityGoal;
import net.minecraft.entity.ai.goal.LookAroundGoal;
import net.minecraft.entity.attribute.DefaultAttributeContainer;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.world.World;

public class TraderNPCEntity extends NpcEntity {
    private String dialogue = "Hello! Would you like to trade?";
    private String[] tradeOptions = {"Emeralds for Rubies", "Gold for Enchanted Items"};

    public TraderNPCEntity(EntityType<? extends NpcEntity> type, World world) {
        super(type, world);
    }

    @Override
    protected void initGoals() {
        this.goalSelector.add(0, new LookAtEntityGoal(this, PlayerEntity.class, 5.0f));
        this.goalSelector.add(1, new LookAroundGoal(this));
    }

    public static DefaultAttributeContainer.Builder createAttributes() {
        return NpcEntity.createMobAttributes()
            .add(EntityAttributes.MAX_HEALTH, 999.0)
            .add(EntityAttributes.MOVEMENT_SPEED, 0.0)
            .add(EntityAttributes.FOLLOW_RANGE, 64.0);
    }

    @Override
    public void interactMob(PlayerEntity player, net.minecraft.util.Hand hand) {
        if (!world.isClient) {
            MyNetworking.sendDialogue((ServerPlayerEntity) player, this.getId(), dialogue, tradeOptions);
        }
    }

    public String getDialogue() { return dialogue; }
    public String[] getTradeOptions() { return tradeOptions; }
}

Networking for dialogue:

java
public class MyNetworking {
    public static void sendDialogue(ServerPlayerEntity player, int entityId,
            String dialogue, String[] options) {
        // Send packet to client to open dialogue screen
        PacketByteBuf buf = PacketByteBufs.create();
        buf.writeInt(entityId);
        buf.writeString(dialogue);
        buf.writeInt(options.length);
        for (String option : options) buf.writeString(option);
        ServerPlayNetworking.send(player, DIALOGUE_CHANNEL, buf);
    }
}

Client-side dialogue screen:

java
package com.example.mymod.screen;

import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;

public class DialogueScreen extends Screen {
    private final int entityId;
    private final String dialogue;
    private final String[] options;

    public DialogueScreen(int entityId, String dialogue, String[] options) {
        super(Text.literal("Trader"));
        this.entityId = entityId;
        this.dialogue = dialogue;
        this.options = options;
    }

    @Override
    protected void init() {
        int y = 60;
        for (int i = 0; i < options.length; i++) {
            final int index = i;
            addDrawableChild(net.minecraft.client.gui.widget.ButtonWidget.builder(
                Text.literal(options[i]), button -> {
                    client.setScreen(null);
                }).dimensions(width / 2 - 100, y + i * 25, 200, 20).build());
        }
        addDrawableChild(net.minecraft.client.gui.widget.ButtonWidget.builder(
            Text.literal("Close"), button -> client.setScreen(null)
        ).dimensions(width / 2 - 100, y + options.length * 25, 200, 20).build());
    }

    @Override
    public void render(DrawContext context, int mouseX, int mouseY, float delta) {
        renderBackground(context, mouseX, mouseY, delta);
        context.drawCenteredTextWithShadow(textRenderer, dialogue, width / 2, 30);
        super.render(context, mouseX, mouseY, delta);
    }

    @Override
    public boolean shouldPause() { return false; }
}