Build a Paper server plugin for Minecraft 1.21.11 that lets operators lock arbitrary player commands. The project is a new generic Minecraft plugin; inspect its existing scaffold and adapt it for Paper 1.21.11 using the scaffold's supported approach.

Requirements:

  • Register /lockcommand <command>. It is operator-only and accepts a command label with or without a leading slash (for example, /lockcommand spawn). Store the normalized command label so all players other than operators are prevented from using it. Operators remain allowed to use locked commands.
  • Register /unlockcommand [command]. It is operator-only. Calling /unlockcommand with no argument must automatically display all currently locked command labels (clearly say when there are none). Calling /unlockcommand <command> removes that command from the locked set and reports success or that it was not locked.
  • Persist locked commands across restarts using the plugin's config/data file. Normalize labels case-insensitively; when players use command arguments, compare only the first command token, ignoring any arguments and an optional leading slash. Do not block operators.
  • Provide clear usage, permission/error, and result messages. Do not add tests.

Implement the complete plugin, including plugin metadata and default configuration if needed. Do not build or claim build verification; compilation is performed separately after your work.

Kodari AI
KodariOct 19 at 11:58 pm 1
CommandLock
An operator-only Paper plugin for Minecraft 1.21.11 that locks arbitrary player commands. Admins use /lockcommand &amp;lt;command&amp;gt; to restrict any command from non-operators, with case-insensitive normalization and optional leading slash support. The /unlockcommand command lists all locked commands when called without arguments, or removes a specific lock when given a command label. Locked commands persist across server restarts via the plugin's config filece. Only the first command token is compared against the locked set, ignoring any arguments players may pass. Operators remain unaffected and can always execute locked commands.
  1. Create a Java 21 Paper 1.21.11 Gradle scaffold with plugin metadata and default config.

  2. Implement operator-only lock/unlock commands, normalized persistence, and command interception.

Created the Paper 1.21.11 CommandLocker plugin with operator-only /lockcommand and /unlockcommand, case-insensitive command matching, and config persistence. Build verification was not run.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
settings.gradle
Code
5 180
src/main/java/com/kodari/commandlocker/CommandLockerPlugin.java
package com.kodari.commandlocker;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

public final class CommandLockerPlugin extends JavaPlugin implements Listener, CommandExecutor {
    private final Set<String> lockedCommands = new LinkedHashSet<>();

    @Override
    public void onEnable() {
        saveDefaultConfig();
        for (String command : getConfig().getStringList("locked-commands")) {
            String normalized = normalizeLabel(command);
            if (!normalized.isEmpty()) {
                lockedCommands.add(normalized);
            }
        }
        saveLockedCommands();

        getCommand("lockcommand").setExecutor(this);
        getCommand("unlockcommand").setExecutor(this);
        getServer().getPluginManager().registerEvents(this, this);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!sender.isOp()) {
            sender.sendMessage(color("&cYou must be an operator to use this command."));
            return true;
        }

        if (command.getName().equalsIgnoreCase("lockcommand")) {
            return lockCommand(sender, args);
        }
        if (command.getName().equalsIgnoreCase("unlockcommand")) {
            return unlockCommand(sender, args);
        }
        return true;
    }

    private boolean lockCommand(CommandSender sender, String[] args) {
        if (args.length != 1) {
            sender.sendMessage(color("&cUsage: /lockcommand <command>"));
            return true;
        }

        String normalized = normalizeLabel(args[0]);
        if (normalized.isEmpty() || normalized.chars().anyMatch(Character::isWhitespace)) {
            sender.sendMessage(color("&cUsage: /lockcommand <command>"));
            return true;
        }

        if (!lockedCommands.add(normalized)) {
            sender.sendMessage(color("&eThat command is already locked: &f" + normalized));
            return true;
        }

        saveLockedCommands();
        sender.sendMessage(color("&aLocked command: &f" + normalized));
        return true;
    }

    private boolean unlockCommand(CommandSender sender, String[] args) {
        if (args.length == 0) {
            if (lockedCommands.isEmpty()) {
                sender.sendMessage(color("&eThere are no locked commands."));
            } else {
                sender.sendMessage(color("&aLocked commands: &f" + String.join(", ", lockedCommands)));
            }
            return true;
        }
        if (args.length != 1) {
            sender.sendMessage(color("&cUsage: /unlockcommand [command]"));
            return true;
        }

        String normalized = normalizeLabel(args[0]);
        if (normalized.isEmpty() || normalized.chars().anyMatch(Character::isWhitespace)) {
            sender.sendMessage(color("&cUsage: /unlockcommand [command]"));
            return true;
        }

        if (!lockedCommands.remove(normalized)) {
            sender.sendMessage(color("&eThat command was not locked: &f" + normalized));
            return true;
        }

        saveLockedCommands();
        sender.sendMessage(color("&aUnlocked command: &f" + normalized));
        return true;
    }

    @EventHandler
    public void onPlayerCommand(PlayerCommandPreprocessEvent event) {
        if (event.getPlayer().isOp()) {
            return;
        }

        String commandLabel = firstCommandToken(event.getMessage());
        if (lockedCommands.contains(commandLabel)) {
            event.setCancelled(true);
            event.getPlayer().sendMessage(color("&cThat command is currently locked."));
        }
    }

    private String firstCommandToken(String commandLine) {
        String input = commandLine.trim();
        if (input.startsWith("/")) {
            input = input.substring(1);
        }
        int separator = input.indexOf(' ');
        String token = separator >= 0 ? input.substring(0, separator) : input;
        return normalizeLabel(token);
    }

    private String normalizeLabel(String command) {
        String normalized = command.trim();
        if (normalized.startsWith("/")) {
            normalized = normalized.substring(1);
        }
        return normalized.toLowerCase(Locale.ROOT);
    }

    private void saveLockedCommands() {
        getConfig().set("locked-commands", List.copyOf(lockedCommands));
        saveConfig();
    }

    private String color(String message) {
        return ChatColor.translateAlternateColorCodes('&', message);
    }
}