How can I stop players from spawning at their beds in Minecraft 1.14?
I have been making a custom UHC map for a while now. It wouldn't be PVP-based, just try to last the longest, but the catch is that after dying, that's it: you are back to spawn, then teleported to a new random location in the world, from -1000000 to 1000000.
If the players were to sleep, their spawn point would be their bed, which would mean that if they'd die, they could respawn at their bed, collect items, and go on with their day. This is not how I want it to work (UHC is where you only regenerate with golden apples or potions, so hearts are valuable).
Solution 1:
You can track people who sleep with the sleep_in_bed
scoreboard:
/scoreboard objectives add sleeping minecraft.custom:minecraft.sleep_in_bed
Then you can remove beds around people who are sleeping:
/execute at @e[scores={sleeping=1..}] run fill ~-5 ~-5 ~-5 ~5 ~5 ~5 air replace #beds
And finally reset the score:
/scoreboard players reset @e sleeping
Solution 2:
If you just want to a player out of bed after a small delay, without destroying the bed (that it drops instead of being overwritten by air in my other solution is caused by a bug), you can explode him out of it! But carefully, of course…
You still need the sleeping scoreboard:
/scoreboard players add sleeping custom:sleep_in_bed
These are the looped commands, they should be in a repeating command block chain or function:
execute at @a[scores={sleeping=1..}] run summon fireball ~ ~1 ~ {ExplosionPower:0,direction:[0.0,-1.0,0.0]}
effect give @a[scores={sleeping=1..}] resistance 1 5 true
scoreboard players reset @a[scores={sleeping=1..}] sleeping
The first command creates a fireball above the player's head that flies downwards very quickly. It has an explosion power of 0, so it won't damage the environment, even the bed stays where it is.
But you still get damage from it (probably because it's still a burning ball of stone hitting your head), so you are protected from that with a resistance effect that is so high that it absorbs all damage. This would in theory interfere slightly with effects like poison, but you can't sleep while poisoned anyway, so it's not too bad.
And finally, the last command resets the scoreboard so that you don't summon an army of fireballs on top of you every tick.