package foleon.mmso2;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerGameModeChangeEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

public class Mmso2 extends JavaPlugin implements Listener {

    private Map<UUID, String> captchaCodes = new HashMap<>();
    private Map<UUID, Integer> captchaTimers = new HashMap<>();
    private Map<UUID, Boolean> captchaInProgress = new HashMap<>();

    @Override
    public void onEnable() {
        Bukkit.getPluginManager().registerEvents(this, this);
    }

    @Override
    public void onDisable() {
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        generateCaptchaCode(player);
        startCaptchaTimer(player);

        new BukkitRunnable() {
            @Override
            public void run() {
                player.sendMessage(ChatColor.GREEN + "Введите код из центра экрана в чат: " + ChatColor.YELLOW + captchaCodes.get(player.getUniqueId()));
            }
        }.runTaskLater(this, 20);
    }

    @EventHandler
    public void onPlayerChat(AsyncPlayerChatEvent event) {
        Player player = event.getPlayer();
        UUID playerUUID = player.getUniqueId();
        String message = event.getMessage();

        if (captchaInProgress.containsKey(playerUUID)) {
            event.setCancelled(true);
            if (message.equalsIgnoreCase(captchaCodes.get(playerUUID))) {
                stopCaptchaTimer(playerUUID);
                player.sendMessage(ChatColor.GREEN + "Проверка пройдена!");
                captchaInProgress.remove(playerUUID);
                player.setGameMode(GameMode.SURVIVAL);
            } else {
                player.kickPlayer(ChatColor.RED + "Проверка не пройдена!");
                cleanup(playerUUID);
            }
        }
    }

    @EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
        Player player = event.getPlayer();
        UUID playerUUID = player.getUniqueId();

        if (captchaInProgress.containsKey(playerUUID)) {
            event.setCancelled(true);
        }
    }

    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
        Player player = event.getPlayer();
        UUID playerUUID = player.getUniqueId();

        if (captchaInProgress.containsKey(playerUUID)) {
            event.setCancelled(true);
        }
    }

    @EventHandler
    public void onPlayerGameModeChange(PlayerGameModeChangeEvent event) {
        Player player = event.getPlayer();
        UUID playerUUID = player.getUniqueId();

        if (captchaInProgress.containsKey(playerUUID)) {
            event.setCancelled(true);
        }
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event) {
        Player player = event.getPlayer();
        UUID playerUUID = player.getUniqueId();
        cleanup(playerUUID);
    }

    private void generateCaptchaCode(Player player) {
        UUID playerUUID = player.getUniqueId();
        int code = new Random().nextInt(9000) + 1000;
        String captchaCode = String.valueOf(code);
        captchaCodes.put(playerUUID, captchaCode);
        captchaInProgress.put(playerUUID, true);
        player.setGameMode(GameMode.ADVENTURE);
    }

    private void startCaptchaTimer(Player player) {
        UUID playerUUID = player.getUniqueId();
        captchaTimers.put(playerUUID, 30);
        new BukkitRunnable() {
            @Override
            public void run() {
                int timeLeft = captchaTimers.get(playerUUID);
                if (timeLeft > 0) {
                    if (!player.isOnline()) {
                        stopCaptchaTimer(playerUUID);
                        cleanup(playerUUID);
                    } else {
                        player.sendTitle(ChatColor.RED + "Капча: " + timeLeft + " секунд", "");
                                captchaTimers.put(playerUUID, timeLeft - 1);
                    }
                } else {
                    player.kickPlayer(ChatColor.RED + "Время на капчу истекло!");
                    stopCaptchaTimer(playerUUID);
                    cleanup(playerUUID);
                }
            }
        }.runTaskTimer(this, 20, 20);
    }

    private void stopCaptchaTimer(UUID playerUUID) {
        captchaCodes.remove(playerUUID);
        captchaTimers.remove(playerUUID);
    }

    private void cleanup(UUID playerUUID) {
        captchaInProgress.remove(playerUUID);
        captchaCodes.remove(playerUUID);
        captchaTimers.remove(playerUUID);
    }
} заблокируй возможность вводить команды пока ты не пройдешь капчу и пофикси спам в консоли. скинь итоговый код