jamesyeh83-lab

23. Vanish Command Override

AsianStaff's vanish system must also have highest command priority.

The following commands must always execute AsianStaff's vanish system:

* /vanish * /v

If another plugin already registers /vanish or /v, AsianStaff must override the conflicting command instead of allowing the other plugin to handle it.

Vanish Aliases

Also provide:

* /asianvanish * /avanish

These should always execute AsianStaff's vanish system.

Vanish Requirements

When /vanish is executed:

* Toggle the player's vanish state. * Hide the player from normal players. * Remove the player from normal players' tab lists. * Remove the player from normal players' visible player counts. * Keep the vanished player visible to staff with asianstaff.vanish.see. * Prevent normal join/quit messages from revealing the vanished player. * Prevent vanished staff from accidentally revealing themselves. * Allow vanished staff to interact with the server normally. * Store the vanish state while the player remains online. * Cleanly restore visibility when vanish is disabled.

Player Count

If a server has:

30 online

and:

3 vanished staff

normal players should see:

27 online

Staff with asianstaff.vanish.see should see:

30 online

This applies to the tab list and any player-count display controlled by AsianStaff.

Command Override Protection

If another plugin has:

/vanish

AsianStaff must still execute its own vanish system.

If another plugin has:

/v

AsianStaff must still execute its own vanish system.

The command must NOT fall through to the other plugin.

For example:

/vanish

→ AsianStaff vanish system

NOT:

SomeOtherPlugin vanish system

Permission

Require:

asianstaff.vanish

for /vanish and /v.

For seeing vanished players:

asianstaff.vanish.see

Console Logging

When a conflicting vanish command is detected:

[AsianStaff] Overriding conflicting command: /vanish

[AsianStaff] Overriding conflicting command: /v

AsianStaff should remain enabled even if another plugin already owns these commands.

Important

The final command override system should cover BOTH punishment commands and vanish commands.

AsianStaff should have priority over:

/ban /tempban /unban /ipban /tempipban /unbanip /mute /tempmute /unmute /warn /kick /freeze /unfreeze /punish /vanish /v

All of these commands must route through AsianStaff's own systems.

Kodari AI
KodariSep 23 at 5:37 pm 1
VanishOverride
Implements a high-priority vanish command override system for AsianStaff. Registers /vanish, /v, /asianvanish, and /avanish commands that always execute AsianStaff's vanish system, overriding any conflicting commands from other plugins. Toggles player invisibility, removes vanished staff from normal players' tab lists and player counts, while keeping them visible to staff with the asianstaff.vanish.see permission. Stores vanish state online, prevents join/quit message leaks, and restores visibility cleanly when disabled. Logs override detection messages and requires asianstaff.vanish for usage. Integrates with the existing punishment command override system to ensure AsianStaff maintains priority across all staff commands.
  • Scaffold AsianStaff as a Paper plugin.

  • Implement vanish visibility, staff exemptions, and vanish-safe join/quit handling.

  • Override and route the requested vanish and punishment commands through AsianStaff.

Created the AsianStaff Paper plugin with vanish visibility and tab-list hiding, staff visibility via asianstaff.vanish.see, and join/quit message suppression. /vanish, /v, /asianvanish, and /avanish require asianstaff.vanish.

AsianStaff periodically reclaims the requested punishment and vanish command labels from conflicting registrations and logs detected overrides. Those commands route to AsianStaff’s handlers; punishment commands require asianstaff.punish. Mute, freeze, and warning data is in memory only.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
settings.gradle
Code
4 523
src/main/java/com/asianstaff/AsianStaffPlugin.java
package com.asianstaff;

import org.bukkit.BanList;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
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.AsyncPlayerChatEvent;
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 java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.Arrays;

public final class AsianStaffPlugin extends JavaPlugin implements Listener {
    private static final String VANISH_PERMISSION = "asianstaff.vanish";
    private static final String VANISH_SEE_PERMISSION = "asianstaff.vanish.see";
    private static final String PUNISH_PERMISSION = "asianstaff.punish";
    private static final List<String> COMMAND_LABELS = List.of(
            "ban", "tempban", "unban", "ipban", "tempipban", "unbanip",
            "mute", "tempmute", "unmute", "warn", "kick", "freeze", "unfreeze", "punish",
            "vanish", "v", "asianvanish", "avanish"
    );
    private static final Pattern DURATION_PATTERN = Pattern.compile("(?i)^(\\d+)(s|m|h|d|w)$");

    private final Set<UUID> vanishedPlayers = new HashSet<>();
    private final Set<UUID> frozenPlayers = ConcurrentHashMap.newKeySet();
    private final Map<UUID, Long> mutedPlayers = new ConcurrentHashMap<>();
    private final Map<UUID, Integer> warningCounts = new HashMap<>();
    private final Map<String, Command> staffCommands = new HashMap<>();
    private final Set<String> registeredCommands = new HashSet<>();
    private final Map<String, Command> displacedCommands = new HashMap<>();

    private CommandMap commandMap;
    private Map<String, Command> knownCommands;

    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
        for (String label : COMMAND_LABELS) {
            staffCommands.put(label, new StaffCommand(label));
        }
        Bukkit.getScheduler().runTask(this, this::refreshCommandOverrides);
        Bukkit.getScheduler().runTaskTimer(this, this::refreshCommandOverrides, 20L, 20L);
    }

    @Override
    public void onDisable() {
        for (Player observer : Bukkit.getOnlinePlayers()) {
            for (Player target : Bukkit.getOnlinePlayers()) {
                observer.showPlayer(this, target);
            }
        }
        if (knownCommands != null && commandMap != null) {
            for (Command command : staffCommands.values()) {
                command.unregister(commandMap);
            }
            knownCommands.putAll(displacedCommands);
        }
    }

    private void refreshCommandOverrides() {
        try {
            if (commandMap == null) {
                Method getCommandMap = getServer().getClass().getMethod("getCommandMap");
                commandMap = (CommandMap) getCommandMap.invoke(getServer());
                knownCommands = findKnownCommands(commandMap);
            }
            for (String label : COMMAND_LABELS) {
                Command current = knownCommands.get(label);
                Command staffCommand = staffCommands.get(label);
                if (current == staffCommand) {
                    continue;
                }
                if (current != null) {
                    if (!displacedCommands.containsKey(label)) {
                        displacedCommands.put(label, current);
                    }
                    getLogger().info("Overriding conflicting command: /" + label);
                }
                knownCommands.remove(label);
                if (registeredCommands.add(label)) {
                    commandMap.register("asianstaff", staffCommand);
                }
                knownCommands.put(label, staffCommand);
            }
        } catch (ReflectiveOperationException | ClassCastException exception) {
            getLogger().severe("Unable to install command overrides: " + exception.getMessage());
        }
    }

    @SuppressWarnings("unchecked")
    private Map<String, Command> findKnownCommands(CommandMap map) throws ReflectiveOperationException {
        Class<?> type = map.getClass();
        while (type != null) {
            try {
                Field field = type.getDeclaredField("knownCommands");
                field.setAccessible(true);
                return (Map<String, Command>) field.get(map);
            } catch (NoSuchFieldException ignored) {
                type = type.getSuperclass();
            }
        }
        throw new NoSuchFieldException("CommandMap knownCommands");
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onJoin(PlayerJoinEvent event) {
        updateVisibility();
        if (vanishedPlayers.contains(event.getPlayer().getUniqueId())) {
            event.setJoinMessage(null);
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onQuit(PlayerQuitEvent event) {
        UUID playerId = event.getPlayer().getUniqueId();
        if (vanishedPlayers.remove(playerId)) {
            event.setQuitMessage(null);
            updateVisibility();
        }
        frozenPlayers.remove(playerId);
        mutedPlayers.remove(playerId);
    }

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onFrozenMove(PlayerMoveEvent event) {
        if (!frozenPlayers.contains(event.getPlayer().getUniqueId())) {
            return;
        }
        if (event.getTo() != null && (event.getFrom().getBlockX() != event.getTo().getBlockX()
                || event.getFrom().getBlockY() != event.getTo().getBlockY()
                || event.getFrom().getBlockZ() != event.getTo().getBlockZ())) {
            event.setTo(event.getFrom());
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onMutedChat(AsyncPlayerChatEvent event) {
        Long expiresAt = mutedPlayers.get(event.getPlayer().getUniqueId());
        if (expiresAt == null) {
            return;
        }
        if (expiresAt != Long.MAX_VALUE && expiresAt <= System.currentTimeMillis()) {
            mutedPlayers.remove(event.getPlayer().getUniqueId(), expiresAt);
            return;
        }
        event.setCancelled(true);
    }

    private void toggleVanish(Player player) {
        UUID playerId = player.getUniqueId();
        if (vanishedPlayers.remove(playerId)) {
            player.sendMessage(color("&aVanish disabled."));
        } else {
            vanishedPlayers.add(playerId);
            player.sendMessage(color("&aVanish enabled."));
        }
        updateVisibility();
    }

    private void updateVisibility() {
        for (Player observer : Bukkit.getOnlinePlayers()) {
            for (UUID vanishedId : vanishedPlayers) {
                Player target = Bukkit.getPlayer(vanishedId);
                if (target == null || observer.equals(target)) {
                    continue;
                }
                if (observer.hasPermission(VANISH_SEE_PERMISSION)) {
                    observer.showPlayer(this, target);
                } else {
                    observer.hidePlayer(this, target);
                }
            }
            for (Player target : Bukkit.getOnlinePlayers()) {
                if (!vanishedPlayers.contains(target.getUniqueId())) {
                    observer.showPlayer(this, target);