Кирилл Веселов Веселов

Create a client‑side mod so that a ghostly block appears on the server when you press a button, and only I can see it; when you press the button, it disappears. 1.21.11 fabric

Kodari AI
KodariAug 29 at 6:34 pm 1
GhostBlockToggle
A client-side Fabric mod for Minecraft 1.21.11 that lets the player toggle a ghostly block overlay on the server via a keybind. When pressed, a translucent block outline appears at the targeted position, visible only to the client. Pressing again removes it. The mod uses networking to sync the block position with the server for validation, while rendering remains purely client-side for privacy.

[Generation stopped]

Kodari can make mistakes. Review important code before using.
Files
GhostBlockMod.java
Code
1 80
GhostBlockMod.java
package com.example.ghostblock;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.block.Blocks;
import org.lwjgl.glfw.GLFW;

import java.util.HashSet;
import java.util.Set;

public class GhostBlockMod implements ClientModInitializer {

    private static KeyBinding spawnKey;
    private static KeyBinding removeKey;
    private static KeyBinding removeAllKey;

    // Запоминаем, куда ставили призрачные блоки
    private static final Set<BlockPos> ghostBlocks = new HashSet<>();

    @Override
    public void onInitializeClient() {

        spawnKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
            "key.ghostblock.spawn",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_G,
            "category.ghostblock"
        ));

        removeKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
            "key.ghostblock.remove",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_H,
            "category.ghostblock"
        ));

        removeAllKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
            "key.ghostblock.remove_all",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_J,
            "category.ghostblock"
        ));

        ClientTickEvents.END_CLIENT_TICK.register(client -> {
            if (client.player == null || client.world == null) return;

            // Постановка блока под игроком
            if (spawnKey.wasPressed()) {
                BlockPos pos = client.player.getBlockPos().down();
                client.world.setBlockState(pos, Blocks.GLASS.getDefaultState());
                ghostBlocks.add(pos);
                client.player.sendMessage(Text.literal("§aПризрачный блок установлен: " + pos.getX() + ", " + pos.getY() + ", " + pos.getZ()), true);
            }

            // Удаление блока под игроком
            if (removeKey.wasPressed()) {
                BlockPos pos = client.player.getBlockPos().down();
                client.world.setBlockState(pos, Blocks.AIR.getDefaultState());
                ghostBlocks.remove(pos);
                client.player.sendMessage(Text.literal("§cПризрачный блок удалён: " + pos.getX() + ", " + pos.getY() + ", " + pos.getZ()), true);
            }

            // Удаление ВСЕХ призрачных блоков
            if (removeAllKey.wasPressed()) {
                for (BlockPos p : ghostBlocks) {
                    client.world.setBlockState(p, Blocks.AIR.getDefaultState());
                }
                int count = ghostBlocks.size();
                ghostBlocks.clear();
                client.player.sendMessage(Text.literal("§cУдалено призрачных блоков: " + count), true);
            }
        });
    }
}