How do you prevent a player from leaving the bed once he gets in it in Minecraft?

I'm wondering if it's possible to prevent a player from leaving the bed once he gets in it. I want to count how many times a player slept in a bed in a Minecraft map. I made a scoreboard with it:

/scoreboard objectives add sleepingInBed stat.sleepInBed

The problem is that a player can quit the bed when the night isn't over. The score goes up every time the player tries to sleep in it. Is there a solution for this?


Solution 1:

Adjust blocks around the bed in such a way that the player will wake up on 1 particular block every time.

Circuit A :

Now add pressure plate (preferably stone) on that block.

Circuit B:

Now use daylight sensor somewhere outside add repeater to extend the signal.

Circuit Main:

Use "And" gate with input from A and B and output of the and gate will be linked to your counting command-block.