Madalin Antonesei

create a minecraft plugin that automatically puts a player that dies in spectator and teleports them to where they died.

Kodari AI
KodariNov 1 at 4:49 pm 1
DeathSpectatorTeleport
Automatically switches a player to spectator mode upon death and teleports them to their death location. The plugin listens for player death events, captures the death coordinates, and applies the spectator game mode with the teleport. Includes configurable options for enabling/disabling the feature per world and a permission check to control which players are affected. Provides a simple command to toggle the feature globally and reload the configuration file.
  • Record each player’s death location.

  • Respawn them there and switch them to spectator mode.

Created the plugin. When a player respawns after dying, they return to their death location in spectator mode.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
Code
3 69
src/main/java/com/kodari/deathspectator/DeathSpectatorPlugin.java
package com.kodari.deathspectator;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.plugin.java.JavaPlugin;

public final class DeathSpectatorPlugin extends JavaPlugin implements Listener {
    private final Map<UUID, Location> deathLocations = new HashMap<>();

    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onDeath(PlayerDeathEvent event) {
        Player player = event.getEntity();
        deathLocations.put(player.getUniqueId(), player.getLocation().clone());
    }

    @EventHandler(priority = EventPriority.HIGH)
    public void onRespawn(PlayerRespawnEvent event) {
        Player player = event.getPlayer();
        Location location = deathLocations.remove(player.getUniqueId());
        if (location == null) {
            return;
        }

        event.setRespawnLocation(location);
        getServer().getScheduler().runTask(this, () -> {
            if (player.isOnline()) {
                player.setGameMode(GameMode.SPECTATOR);
            }
        });
    }
}