Minecraft Blogs / Tutorial

Detecting Sleeping Players in Minecraft | Command Block Tutorial

  • 4,349 views, 34 today
  • 2
  • 0
Brodie The Nerd avatar Brodie The Nerd
Level 37 : Artisan Nerd
12
When making maps or running vanilla servers, it can be useful to tell when a player is sleeping in a bed. There are data-packs that allow you to have multiplayer sleeping (when only one person needs to be asleep for the night to be skipped) so I won't be covering that (see xisumavoid for information on the datapack). However, there are more uses for detecting sleep than one may think.

This tutorial is super simple and I hope it helps!

For starters, we need two scoreboard objectives. We'll name them "sleeping" and "awake". For the sleeping objective, we want to detect when someone has entered a bed. To do this, we will use the "sleep_in_bed" criteria for the scoreboard objective. Type the following command into chat:

/scoreboard objectives add sleeping minecraft.custom:minecraft.sleep_in_bed

This objective will add to someone's score whenever they sleep in a bed. The "awake" objective needs to detect if someone is awake, so we will use the "time_since_rest" criteria. Type the following command into chat:

/scoreboard objectives add awake minecraft.custom:minecraft.time_since_rest

This objective is a timer that counts every tick (there are 20 ticks per second) since the last time that player was in bed, and resets itself every time that player uses a bed.

Finally, we only need one command block to tell when someone is in bed, so that it will reset every time that player wakes up. Place down a repeating command block and set it to "Always Active". Inside that command block, type the following command:


/scoreboard players set @a[​scores={awake=1..}] sleeping 0

What this command will do is detect if a player's "awake" score is 1 or higher, and set's their "sleeping" score to 0. This way, whenever someone leaves a bed (whether it's because they've slept through the night or they've left their bed before they slept through the night), their "sleeping" score will be reset. So, if you want to detect if that player is in a bed, you will need to check if their "sleeping" score is 1.

Here are a couple ways to use this sleeping detector (as well as tutorials on how to do each one):

Counting How Many Players Are Asleep
If you're on a vanilla server, or you are making a multiplayer map, it can be useful to count how many players are currently asleep.

To start, we will need to do a couple things. For starters, you will need an armor stand to attach the final count to. To do this, follow a compass to the spawn chunks (long story, see ilmango for more information) and type in the following command into chat:

/summon armor_stand ~ ~ ~ {Invisible:1,Invulnerable:1,NoGravity:1,CustomName:"\"reference\""}

This will summon an armor stand at your location named "reference"

Next, you will need to add another scoreboard objective to keep the score of the sleeping players, which we will name "sleepingPlayers". Type the following command into chat:

/scoreboard objectives add sleepingPlayers dummy

This objective will only change the score of a player (or, in this case, the armor stand) when told to by a command, since the criteria is "dummy".

Finally, add two chain command blocks to your original repeating command block. Type the following command into the first chain command block:

/tag @a remove sleeping

In the next chain command block, type the following command:

/execute store result score @e[​type=armor_stand,name=reference] sleepingPlayers run tag @a[​scores={sleeping=1}] add sleeping

What this command will do is give the tag "sleeping" to everyone who is currently asleep. When it does this, it will detect how many players it tagged. This result will be stored in the "sleepingPlayers" objective for the armor stand "reference". Use the following selector argument to detect the number of sleeping players on the server:

@e[type=armor_stand,name=reference,scores={sleepingPlayers=(score here)}]

Detect How Many Times a Player Has Left Their Bed
In some maps or vanilla servers, it can be useful to detect when a player has left a bed. Sometimes it can be for genuine gameplay, other times it can be for funny easter eggs (both are appropriate. No good map doesn't have at least ONE easter egg in it).

To start, you will need to add one more scoreboard objective which will count how many times the player has left the bed. We will name this objective "awoken". Type the following command into chat:

/scoreboard objectives add awoken dummy

This objective will only be changed when told to by a command, since it is a "dummy" criteria.

Next, you will need to add three chain command blocks to the original repeating command block (or, if you've added a chain to it already, just add three more chain command blocks onto the end of that chain). Type the following command to the first chain command block:

/tag @a[​scores={sleeping=1}] remove awake

In the second chain command block, type the following command:

/scoreboard players add @a[​scores={sleeping=0},tag=!awake] awoken 1

This command add 1 to the score of every player who is awake but doesn't have the "awake" tag yet, thus counting when they've woken up.

In the third chain command block, type the following command:

/tag @a[​scores={sleeping=0}] add awake

This will find all the players that are awake (who haven't been tagged already) and give them the awake tag (after their score has been updated, meaning their score won't be constantly updated).

Now, whenever someone leaves a bed, their "awake" score will be updated.

If you have any ideas for other ways we can use the sleeping detector and wish for it to be included (as well as a shoutout for the idea), please leave them in the comments and I will try my hardest to include them!

PS for those who saw my "Counting Players" Tutorial, it was a very overcomplicated solution for it, and it turned out that I had already posted a much, MUCH simpler solution in this tutorial.
Tags

Create an account or sign in to comment.

Planet Minecraft

Website

© 2010 - 2021
www.planetminecraft.com

Welcome