2

Situation: Java vanilla server. Three players. Full-size map. One of the players sets their spawn to a bed. The bed breaks (for X reason), the player dies before they can set a new spawn point, and is sent to world spawn.

Twist! World spawn is a chamber under the world where the player appears above, landing on a pressure plate activating a command block to teleport them back to their PREVIOUSLY recorded spawn location. In this case, the noted bed that was destroyed.

Twist 2!! I'm trying to get the command to recognize WHICH player activates the plate and teleport them to their specific previously recorded spawn point.

Does MC keep a bed, which was been set as a spawn point, location, even after the bed has been broken?

If yes, where might a person find this data?

And more importantly, is there a way for a command to recognize WHICH specific player activates the command.

1
  • Pretty sure the coords saved are of the bed. What happens if it is all solid blocks there? Will he just be teleported there forever and dying sufocated?
    – BunnyMerz
    Commented Feb 5, 2022 at 17:57

1 Answer 1

1

Dead player detection

Detecting a player that died is pretty easy with commands. Create a scoreboard for this:

/scoreboard objectives add Deaths deathCount

Then constantly check if the value in the scoreboard is greater than 0:

# Every tick
execute as @a[scores={Death=1..}] run <some command>

Don't forget to set the score back to 0 at the end. You can either run this command using repeating command blocks or using a datapack. I recommend the second option. This requires a bit more preparation, but has better performance and other benefits.

Spawn points

To your second point: This one is quiet a bit harder. Each player stores the coordinates of the head of the last bed, they slept in, in their NBT data in SpawnX, SpawnY and SpawnZ. Storing these in a scoreboard isn't the problem. Create three scoreboards:

/scoreboard objectives add Spawn_X dummy
/scoreboard objectives add Spawn_Y dummy
/scoreboard objectives add Spawn_Z dummy

and store the values for each player:

# Every tick
execute as @a store result score @s Spawn_X run data get entity @s SpawnX
execute as @a store result score @s Spawn_Y run data get entity @s SpawnY
execute as @a store result score @s Spawn_Z run data get entity @s SpawnZ

But now comes the problem: There is no direct way to insert the scoreboard values into a /teleport command. You can teleport players only to a hard-coded set of coordinates or to another entity.

Possible solution

It follows a idea from myself, how you may be able to achieve this and what problems may arise. I assume from your post, that you aren't extremely proficient with commands, so this might be too complicated for you.

What you could do, is to create one marker entity for each player, which marks their spawnpoint. You would need to create a link between the player and their marker, e.g. using an ID system. It's possible to detect when a player changes their spawnpoint using a second set of three scoreboards (Spawn_X_new, Spawn_Y_new, Spawn_Z_new) and checking if at least one of them is unequal to old spawnpoint. Whenever this happens, we kill the old marker and summon a new marker at the position of the player.

# Every tick
# Update scoreboard
execute as @a store result score @s Spawn_X_new run data get entity @s SpawnX
execute as @a store result score @s Spawn_Y_new run data get entity @s SpawnY
execute as @a store result score @s Spawn_Z_new run data get entity @s SpawnZ

# Check if anything changed
execute as @a unless score @s Spawn_X = @s Spawn_X_new at @s run function update_spawn
execute as @a if score @s Spawn_X = @s Spawn_X_new unless score @s Spawn_Y = @s Spawn_Y_new at @s run function update_spawn
execute as @a if score @s Spawn_X = @s Spawn_X_new if score @s Spawn_Y = @s Spawn_Y_new unless score @s Spawn_Z = @s Spawn_Z_new at @s run function update_spawn

# Update the other set of coordinates
execute as @a store result score @s Spawn_X run scoreboard players get @s Spawn_X_new
execute as @a store result score @s Spawn_Y run scoreboard players get @s Spawn_Y_new
execute as @a store result score @s Spawn_Z run scoreboard players get @s Spawn_Z_new
# update_spawn.mcfunction
execute as @e[type=marker,tag=Respawn] if score @s ID = @p ID run kill @s
summon marker ~ ~ ~ {Tags:["Respawn"]}
execute store result score @e[type=marker,tag=Respawn,sort=nearest,limit=1] ID run scoreboard players get @s ID

Then whenever the player dies, we teleport them to their corresponding marker:

# Every tick
execute at @a[scores={Deaths=1..}] as @e[type=minecraft:marker,tag=Respawn] if score @s ID = @p ID run tp @p @s
scoreboard players set @a[scores={Deaths=1..}] Deaths 0

The problem we encounter now, is that the corresponding marker entity might not be loaded if no player is nearby. We can cirumvent this, by using /forceload when summoning markers.

# update_spawn.mcfunction
execute as @e[type=marker,tag=Respawn] if score @s ID = @p ID at @s run function <kill_marker_and_remove_forceload>
summon marker ~ ~ ~ {Tags:["Respawn"]}
execute store result score @e[type=marker,tag=Respawn,sort=nearest,limit=1] ID run scoreboard players get @s ID
forceload add ~ ~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.