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
{"type": "session", "harness": "opencode", "id": "mc-0009", "name": "Building a tradeable NPC entity with dialogue"}
{"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 tradeable NPC entity with dialogue system and screen-based trading interface?"}}
{"type": "message", "message": {"role": "assistant", "content": "```java\npackage com.example.mymod.entity;\n\nimport net.minecraft.entity.EntityType;\nimport net.minecraft.entity.NpcEntity;\nimport net.minecraft.entity.ai.goal.LookAtEntityGoal;\nimport net.minecraft.entity.ai.goal.LookAroundGoal;\nimport net.minecraft.entity.attribute.DefaultAttributeContainer;\nimport net.minecraft.entity.attribute.EntityAttributes;\nimport net.minecraft.entity.player.PlayerEntity;\nimport net.minecraft.server.network.ServerPlayerEntity;\nimport net.minecraft.text.Text;\nimport net.minecraft.world.World;\n\npublic class TraderNPCEntity extends NpcEntity {\n private String dialogue = \"Hello! Would you like to trade?\";\n private String[] tradeOptions = {\"Emeralds for Rubies\", \"Gold for Enchanted Items\"};\n\n public TraderNPCEntity(EntityType<? extends NpcEntity> type, World world) {\n super(type, world);\n }\n\n @Override\n protected void initGoals() {\n this.goalSelector.add(0, new LookAtEntityGoal(this, PlayerEntity.class, 5.0f));\n this.goalSelector.add(1, new LookAroundGoal(this));\n }\n\n public static DefaultAttributeContainer.Builder createAttributes() {\n return NpcEntity.createMobAttributes()\n .add(EntityAttributes.MAX_HEALTH, 999.0)\n .add(EntityAttributes.MOVEMENT_SPEED, 0.0)\n .add(EntityAttributes.FOLLOW_RANGE, 64.0);\n }\n\n @Override\n public void interactMob(PlayerEntity player, net.minecraft.util.Hand hand) {\n if (!world.isClient) {\n MyNetworking.sendDialogue((ServerPlayerEntity) player, this.getId(), dialogue, tradeOptions);\n }\n }\n\n public String getDialogue() { return dialogue; }\n public String[] getTradeOptions() { return tradeOptions; }\n}\n```\n\nNetworking for dialogue:\n```java\npublic class MyNetworking {\n public static void sendDialogue(ServerPlayerEntity player, int entityId,\n String dialogue, String[] options) {\n // Send packet to client to open dialogue screen\n PacketByteBuf buf = PacketByteBufs.create();\n buf.writeInt(entityId);\n buf.writeString(dialogue);\n buf.writeInt(options.length);\n for (String option : options) buf.writeString(option);\n ServerPlayNetworking.send(player, DIALOGUE_CHANNEL, buf);\n }\n}\n```\n\nClient-side dialogue screen:\n```java\npackage com.example.mymod.screen;\n\nimport net.minecraft.client.gui.screen.Screen;\nimport net.minecraft.text.Text;\n\npublic class DialogueScreen extends Screen {\n private final int entityId;\n private final String dialogue;\n private final String[] options;\n\n public DialogueScreen(int entityId, String dialogue, String[] options) {\n super(Text.literal(\"Trader\"));\n this.entityId = entityId;\n this.dialogue = dialogue;\n this.options = options;\n }\n\n @Override\n protected void init() {\n int y = 60;\n for (int i = 0; i < options.length; i++) {\n final int index = i;\n addDrawableChild(net.minecraft.client.gui.widget.ButtonWidget.builder(\n Text.literal(options[i]), button -> {\n client.setScreen(null);\n }).dimensions(width / 2 - 100, y + i * 25, 200, 20).build());\n }\n addDrawableChild(net.minecraft.client.gui.widget.ButtonWidget.builder(\n Text.literal(\"Close\"), button -> client.setScreen(null)\n ).dimensions(width / 2 - 100, y + options.length * 25, 200, 20).build());\n }\n\n @Override\n public void render(DrawContext context, int mouseX, int mouseY, float delta) {\n renderBackground(context, mouseX, mouseY, delta);\n context.drawCenteredTextWithShadow(textRenderer, dialogue, width / 2, 30);\n super.render(context, mouseX, mouseY, delta);\n }\n\n @Override\n public boolean shouldPause() { return false; }\n}\n```"}}