ayeryuzen

make a command to make it to where every player spawns on the exact block under the player when using the command /playersppawn and also only make them spawn there when they dont have a bed or they spawned in for the first time

Kodari AI
KodariOct 27 at 12:48 am 1
PlayerSpawnPoint
Creates a custom spawn system where players respawn at their exact death location instead of their bed or world spawn. The command /playerspawn sets a player's spawn point to the precise block they are currently standing on. This custom spawn location only applies when the player has no bed assigned or when they are spawning for the first time. If a player has an active bed spawn, the bed takes priority. The plugin tracks each player's custom spawn coordinates and automatically applies them upon respawn, ensuring players return to their chosen location rather than the default world spawn point.
  • Add a Paper plugin scaffold with the /playersppawn command.

  • Save the command user’s position above their current block as the shared spawn.

  • Use it for first-time joins and respawns only when the player has no bed spawn.

Created /playersppawn to set the shared spawn at your current position above the block beneath you. First-time players are sent there on joining, and players without a bed spawn there after death; players with a bed keep their bed spawn. The command is available to operators by default.

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

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
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.PlayerJoinEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.plugin.java.JavaPlugin;

public final class PlayerSpawnPlugin extends JavaPlugin implements Listener, CommandExecutor {
    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
        getCommand("playersppawn").setExecutor(this);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player player)) {
            sender.sendMessage("Only players can set the shared spawn.");
            return true;
        }

        Location spawn = player.getLocation().clone();
        spawn.setX(spawn.getBlockX() + 0.5);
        spawn.setZ(spawn.getBlockZ() + 0.5);

        getConfig().set("spawn.world", spawn.getWorld().getName());
        getConfig().set("spawn.x", spawn.getX());
        getConfig().set("spawn.y", spawn.getY());
        getConfig().set("spawn.z", spawn.getZ());
        getConfig().set("spawn.yaw", spawn.getYaw());
        getConfig().set("spawn.pitch", spawn.getPitch());
        saveConfig();

        player.sendMessage("Shared spawn set to the block beneath you.");
        return true;
    }

    @EventHandler
    public void onFirstJoin(PlayerJoinEvent event) {
        if (!event.getPlayer().hasPlayedBefore()) {
            Location spawn = getSharedSpawn();
            if (spawn != null) {
                event.getPlayer().teleport(spawn);
            }
        }
    }

    @EventHandler
    public void onRespawn(PlayerRespawnEvent event) {
        if (event.getPlayer().getBedSpawnLocation() == null) {
            Location spawn = getSharedSpawn();
            if (spawn != null) {
                event.setRespawnLocation(spawn);
            }
        }
    }

    private Location getSharedSpawn() {
        String worldName = getConfig().getString("spawn.world");
        if (worldName == null) {
            return null;
        }

        World world = Bukkit.getWorld(worldName);
        if (world == null) {
            return null;
        }

        return new Location(
                world,
                getConfig().getDouble("spawn.x"),
                getConfig().getDouble("spawn.y"),
                getConfig().getDouble("spawn.z"),
                (float) getConfig().getDouble("spawn.yaw"),
                (float) getConfig().getDouble("spawn.pitch")
        );
    }
}