tinycodinglm / mc /mc-0043.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame Contribute Delete
1.95 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 use Fabric events like ServerTickEvents, block interaction callbacks, etc.?

Assistant
java
package com.example.mymod;

import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.fabricmc.fabric.api.event.player.AttackBlockCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerChunkEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;

public class MyEvents {
    public static void register() {
        UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> {
            if (!world.isClient && hitResult.getType() == HitResult.Type.BLOCK) {
                player.sendMessage(Text.literal("You clicked " + world.getBlockState(hitResult.getBlockPos())));
            }
            return ActionResult.PASS;
        });

        AttackBlockCallback.EVENT.register((player, world, hand, pos, direction) -> {
            if (!world.isClient) {
                player.sendMessage(Text.literal("Breaking " + world.getBlockState(pos)));
            }
            return ActionResult.PASS;
        });

        ServerTickEvents.END_SERVER_TICK.register(server -> { /* Runs every tick on server */ });
        ServerTickEvents.END_WORLD_TICK.register(world -> { /* Runs every tick for each world */ });
        ServerChunkEvents.CHUNK_LOAD.register((world, chunk) -> { /* Chunk loaded */ });
        ServerWorldEvents.LOAD.register((server, world) -> { /* World loaded */ });
    }
}