[SPIGOT-4666] Force sleep not working during daytime Created: 13/Mar/19  Updated: 13/Mar/19  Resolved: 13/Mar/19

Status: Resolved
Project: Spigot
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor
Reporter: Shane Bee Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: 1.13
Environment:

Fresh build of Spigot.
/ver:

[18:54:36 INFO]: This server is running CraftBukkit version git-Spigot-3cb9dcb-8b1588e (MC: 1.13.2) (Implementing API version 1.13.2-R0.1-SNAPSHOT)
[18:54:36 INFO]: Checking version, please wait...
[18:54:37 INFO]: You are running the latest version

macOS 10.14.3 (test server)
Java = Java(TM) SE Runtime Environment (build 1.8.0_201-b09)

Only one plugin that I am working on, no other plugins


Version: CraftBukkit version git-Spigot-3cb9dcb-8b1588e
Guidelines Read: Yes

 Description   

I just come to find out that the force player to sleep method does not seem to be doing its job.
https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/HumanEntity.html#sleep-org.bukkit.Location-boolean-

According to the docs, it should be able to force the player to sleep if conditions are not met.
" The game may also enforce other requirements such as proximity to bed, monsters, and dimension type if force is not set. " and "force - whether to try and sleep at the location even if not normally possible"

Here is the code I tested

@EventHandler
public void enterBedTest(PlayerInteractEvent e) {
	if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {

		e.setCancelled(true);
		Bukkit.broadcastMessage("Clicked block: " + e.getClickedBlock().getType() + " Player: " + e.getPlayer().getName());

		if (e.getPlayer().sleep(e.getClickedBlock().getLocation(), true))
			e.getPlayer().sendMessage("SLEEPY TIME");
		else
			e.getPlayer().sendMessage("NO SLEEPY TIME");
	}
}

This code was just used for testing. I added the cancel to prevent the "You can only sleep at night message" If I remove the event cancel, I get the same outcome.

If it is night time, the player will sleep and get the message "SLEEPY TIME"
When it is daytime, the player is not forced to sleep and they get the message "NO SLEEPY TIME"

I included a short little video so you can see this in action (this video is taking forever to publish)
https://youtu.be/sIQyC_jv-U8



 Comments   
Comment by Shane Bee [ 13/Mar/19 ]

Well, that sounds good. Thank you very much.

The plugin I am working on the player just needs to hop into bed to "refresh" so they don't actually need to "fall asleep"
So I am sure that will work just swell.

Comment by blablubbabc [ 13/Mar/19 ]

It will allow you to enter the bed during the day (set the bed spawn location, etc.) but the player will get thrown out of the bed immediately again. So this is mainly to fix the issue of the force parameter not able to bypass the other sleeping restrictions (nether, monsters nearby, etc.).

Comment by Shane Bee [ 13/Mar/19 ]

OOO ee.... thank you!

Im super bad at reading some of the spigot/bukkit code, but would that PR enable one to force a player to sleep during the day?
Or just bypass checks during the night time?

Comment by blablubbabc [ 13/Mar/19 ]

https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/pull-requests/542/

Comment by Shane Bee [ 13/Mar/19 ]

fair enough.
Thanks for letting me know anyways

Comment by blablubbabc [ 13/Mar/19 ]

I don't think that will be possible without further changes (besides the fixes pointed out above), which basically turns this ticket into a feature request for a cancelable PlayerLeaveBedEvent.

Comment by Shane Bee [ 13/Mar/19 ]

Ah gotcha gotcha!
Thank you for an extremely detailed response.

Personally, I wouldn't mind it so much if the player could get into the bed, "fall asleep" and then when they get to the point where they're almost fully sleeping, boot them out.
I'd be happy with that IF that is possible to do.

Comment by blablubbabc [ 13/Mar/19 ]

The 'force' parameter doesn't get used currently, I think. So maybe this is already solved by actually forwarding this force parameter here, as probably originally intended: https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/browse/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java#171

 

Another potential issue: There are certain conditions which might not actually be safe for plugins to bypass. I originally skipped those (https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/browse/nms-patches/EntityHuman.patch#350) from even calling the PlayerBedEnterEvent. This later added force parameter however seems to allow bypassing those conditions (https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/browse/nms-patches/EntityHuman.patch#347).

 

Another note, as pointed out in the PlayerBedEnterEvent (https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/browse/src/main/java/org/bukkit/event/player/PlayerBedEnterEvent.java#34): If the player is forced to enter the bed during the day, he will actually get kicked out of the bed shortly afterwards again since there is some check somewhere that gets run every tick. So there is no way to actually force the player to continually sleep during the day with the currently available methods / events. The PlayerLeaveBedEvent cannot be cancelled currently.

Comment by md_5 [ 13/Mar/19 ]

@blablubbabc

Generated at Thu Dec 18 20:43:29 UTC 2025 using Jira 10.3.15#10030015-sha1:909cc43d292ca09e774c1a243d9819be3f2628cd.