lefiraaya

Make plugin when player died in pvp set player game mode in spectator for 1.21.11 paper

Kodari AI
KodariJul 18 at 2:34 pm 1
PvPDeathSpectator
A lightweight Paper plugin for 1.21.1 that automatically sets players to spectator mode when defeated in PvP combat. Listens for PlayerDeathEvent, checks if the killer is a player, and transitions the deceased to GameMode.SPECTATOR. Clean, dependency-free, single-purpose design.

Creating the plugin structure — 4 files.

lefiraaya

When player respawn gamemode is survival set it to spectator

Kodari AI
KodariJul 18 at 2:41 pm 1

Added the onPlayerRespawn handler. When a player respawns with gamemode survival, they’ll be set to spectator.

lefiraaya

When players respawn gamemode is survival change it to spectator

Kodari AI
KodariJul 18 at 2:56 pm 1

This is already implemented — onPlayerRespawn at line 22 checks if the player’s gamemode is SURVIVAL on respawn and sets them to SPECTATOR. The plugin is complete.

lefiraaya

Still players spawn with survival add when player died spawn with spectator mode

Kodari AI
KodariJul 18 at 2:57 pm 1

The issue is that after death sets them to spectator, the respawn handler checks getGameMode() == SURVIVAL which never matches. I’ll track PvP-dead players and set them to spectator on respawn regardless.

Now tracks PvP-dead players in a set. On respawn, any player who died in PvP gets set to spectator immediately, regardless of their current gamemode.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
Code
4 79
src/main/java/com/pvpdeathspectator/PvPDeathSpectator.java
package com.pvpdeathspectator;

import com.pvpdeathspectator.listeners.PvPDeathListener;
import org.bukkit.plugin.java.JavaPlugin;

public class PvPDeathSpectator extends JavaPlugin {

    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(new PvPDeathListener(), this);
        getLogger().info("PvPDeathSpectator enabled — PvP deaths will trigger spectator mode.");
    }

    @Override
    public void onDisable() {
        getLogger().info("PvPDeathSpectator disabled.");
    }
}