M

make me a plugin when i type /sprawdzaj, it enables spectator mode for the player who executed it, but it can't be visible on tablist

Kodari AI
KodariAug 23 at 1:16 pm 1
SpectatorToggle
A Minecraft plugin that toggles spectator mode for the executing player via the /sprawdzaj command. When activated, the player enters spectator mode and is completely hidden from the tablist, making them invisible to other players. The plugin listens for the command execution, checks if the player is already in spectator mode, and toggles between survival and spectator accordingly. It also handles the tablist visibility by removing the player from the tablist when in spectator mode and re-adding them when they exit. The plugin ensures a seamless experience by updating the player's game mode and tablist visibility in real-time, providing a stealth-like feature for server administrators or players who want to observe without being noticed.

Plan:

  1. Create the Gradle project and plugin metadata.

  2. Add /sprawdzaj, switching the executor to spectator mode and hiding them from every tab list.

  3. Reapply the hidden state when players join.

Utworzono plugin Sprawdzaj.

  • /sprawdzaj przełącza wykonującego gracza w tryb obserwatora.

  • Gracz jest ukrywany z tablisty dla wszystkich graczy, również dla siebie.

  • Ukrywanie jest ponownie stosowane przy dołączaniu nowych graczy.

  • Projekt zawiera konfigurację Gradle oraz plugin.yml.

M

it has to be a simple command that turns gamemode into spectator, dont need any args and work. the plugin you send doesnt hide spectator tab

Kodari AI
KodariAug 23 at 1:28 pm 1

Plan:

  1. Inspect the current command and plugin metadata.

  2. Simplify /sprawdzaj to a no-argument spectator command and fix tab-list hiding for other players.

  3. Patch the implementation and metadata.

Fixed:

  • /sprawdzaj requires no arguments.

  • It sets the executing player to SPECTATOR.

  • The player is hidden from the tab list of all other players.

  • New players also cannot see active spectators.

  • If the player leaves spectator mode, they become visible again.

Note: Minecraft’s standard Bukkit/Paper API cannot remove a player from their own tab list. The spectator will be hidden from everyone else’s tab list; their own name may still appear to themselves.

M

it cant be like that, it has to be normal

1 image attached
Kodari AI
KodariAug 23 at 1:29 pm 1

hidePlayer() is not a normal spectator feature—it hides the player from the world as well as the tab list.

There are two possible behaviors:

  1. Normal spectator mode: /sprawdzaj only runs GameMode.SPECTATOR; the tab list stays normal.

  2. Hidden from tab: the player is removed from other players’ tab lists, but Bukkit also hides their entity. Hiding the player from their own tab list requires packet handling or ProtocolLib.

The screenshot suggests you want option 1: a normal tab list and a simple spectator command. Should I remove the tab-hiding code?

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

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
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.Listener;
import org.bukkit.event.player.PlayerGameModeChangeEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;

public final class SprawdzajPlugin extends JavaPlugin implements Listener, CommandExecutor {

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

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player player)) {
            sender.sendMessage(ChatColor.RED + "Tej komendy może użyć tylko gracz.");
            return true;
        }

        player.setGameMode(GameMode.SPECTATOR);
        hideFromTabList(player);
        player.sendMessage(ChatColor.GREEN + "Włączono tryb obserwatora.");
        return true;
    }

    @EventHandler
    public void onGameModeChange(PlayerGameModeChangeEvent event) {
        if (event.getNewGameMode() == GameMode.SPECTATOR) {
            hideFromTabList(event.getPlayer());
        } else {
            showInTabList(event.getPlayer());
        }
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player joiningPlayer = event.getPlayer();

        if (joiningPlayer.getGameMode() == GameMode.SPECTATOR) {
            hideFromTabList(joiningPlayer);
        }

        for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
            if (!onlinePlayer.equals(joiningPlayer) && onlinePlayer.getGameMode() == GameMode.SPECTATOR) {
                joiningPlayer.hidePlayer(this, onlinePlayer);
            }
        }
    }

    private void hideFromTabList(Player player) {
        for (Player viewer : Bukkit.getOnlinePlayers()) {
            if (!viewer.equals(player)) {
                viewer.hidePlayer(this, player);
            }
        }
    }

    private void showInTabList(Player player) {
        for (Player viewer : Bukkit.getOnlinePlayers()) {
            if (!viewer.equals(player)) {
                viewer.showPlayer(this, player);
            }
        }
    }
}