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);
}
});
}
}