iqled

can you make me a 1.21.11 paper plugin that if a player has op on, they are forced to type in a password through the command /login, and if they dont do the correct password after 3 tries they get kicked, and if they take above 30 seconds they get kicked. make this only apply to players with op and have the password be inanegoat, also make it so during the duration of the password not being in yet the player cant move and cant do commands

Kodari AI
KodariOct 22 at 7:34 pm 1
OpLoginGuard
Forces OP players to authenticate via /login with a configured password before gaining full access. Upon joining, OP players are frozen and command-blocked until they enter the correct password. Players get three attempts; failing all three or exceeding a 30-second time limit results in a kick. The password is stored in the plugin config and defaults to "inanegoat". The plugin listens for join, command, and movement events to enforce the login state, providing a secure gate for privileged users on Paper 1.21.11.
  1. Create the Paper 1.21.11 plugin entry point and /login command.

  2. Require operators to log in within 30 seconds; kick after three incorrect attempts.

  3. Freeze unauthenticated operators and block their other commands.

Created the Paper 1.21.11 plugin. Operators must enter /login inanegoat within 30 seconds; three incorrect attempts or a timeout kicks them. While waiting, their movement and other commands are blocked. Non-operators are unaffected.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
settings.gradle
Code
4 189
src/main/java/com/kodari/operatorlogin/OperatorLoginPlugin.java
package com.kodari.operatorlogin;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
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.BukkitTask;

public final class OperatorLoginPlugin extends JavaPlugin implements Listener, CommandExecutor {
    private static final String PASSWORD = "inanegoat";
    private static final int MAX_ATTEMPTS = 3;
    private static final long LOGIN_TIMEOUT_TICKS = 20L * 30L;

    private final Map<UUID, LoginSession> pendingLogins = new HashMap<>();

    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
        if (getCommand("login") == null) {
            throw new IllegalStateException("The /login command is missing from plugin.yml");
        }
        getCommand("login").setExecutor(this);
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        if (!player.isOp()) {
            return;
        }

        UUID playerId = player.getUniqueId();
        LoginSession session = new LoginSession();
        pendingLogins.put(playerId, session);
        player.sendMessage("Operator login required. Enter /login <password> within 30 seconds.");
        session.timeoutTask = Bukkit.getScheduler().runTaskLater(this, () -> {
            LoginSession currentSession = pendingLogins.get(playerId);
            if (currentSession != session) {
                return;
            }
            pendingLogins.remove(playerId);
            Player onlinePlayer = Bukkit.getPlayer(playerId);
            if (onlinePlayer == null) {
                return;
            }
            if (onlinePlayer.isOp()) {
                onlinePlayer.kickPlayer("You did not log in within 30 seconds.");
            }
        }, LOGIN_TIMEOUT_TICKS);
    }

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onPlayerCommand(PlayerCommandPreprocessEvent event) {
        Player player = event.getPlayer();
        UUID playerId = player.getUniqueId();
        if (!pendingLogins.containsKey(playerId)) {
            return;
        }
        if (!player.isOp()) {
            clearPendingLogin(playerId);
            return;
        }

        String commandLine = event.getMessage();
        int firstSpace = commandLine.indexOf(' ');
        String commandName = (firstSpace == -1 ? commandLine : commandLine.substring(0, firstSpace)).toLowerCase();
        if (!commandName.equals("/login")) {
            event.setCancelled(true);
            player.sendMessage("You must log in before using commands.");
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onPlayerMove(PlayerMoveEvent event) {
        UUID playerId = event.getPlayer().getUniqueId();
        if (!pendingLogins.containsKey(playerId)) {
            return;
        }
        if (!event.getPlayer().isOp()) {
            clearPendingLogin(playerId);
            return;
        }

        Location from = event.getFrom();
        Location to = event.getTo();
        if (to != null && (from.getX() != to.getX() || from.getY() != to.getY() || from.getZ() != to.getZ())) {
            event.setCancelled(true);
        }
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event) {
        clearPendingLogin(event.getPlayer().getUniqueId());
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player player)) {
            sender.sendMessage("Only players can use this command.");
            return true;
        }
        if (!player.isOp()) {
            player.sendMessage("Only operators need to use this command.");
            return true;
        }

        UUID playerId = player.getUniqueId();
        LoginSession session = pendingLogins.get(playerId);
        if (session == null) {
            player.sendMessage("You are already logged in.");
            return true;
        }
        if (args.length == 1 && PASSWORD.equals(args[0])) {
            clearPendingLogin(playerId);
            player.sendMessage("Login successful.");
            return true;
        }

        session.attempts++;
        int attemptsRemaining = MAX_ATTEMPTS - session.attempts;
        if (attemptsRemaining <= 0) {
            clearPendingLogin(playerId);
            player.kickPlayer("You entered the wrong password too many times.");
            return true;
        }

        player.sendMessage("Incorrect password. Attempts remaining: " + attemptsRemaining + ".");
        return true;
    }

    private void clearPendingLogin(UUID playerId) {
        LoginSession session = pendingLogins.remove(playerId);
        if (session != null && session.timeoutTask != null) {
            session.timeoutTask.cancel();
        }
    }

    private static final class LoginSession {
        private int attempts;
        private BukkitTask timeoutTask;
    }
}