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