Skip to content

Instantly share code, notes, and snippets.

@Mordenkainen
Last active February 12, 2018 12:32
Show Gist options
  • Save Mordenkainen/c5b1f16acb4fdafe8b29f591ae2bf09c to your computer and use it in GitHub Desktop.
Save Mordenkainen/c5b1f16acb4fdafe8b29f591ae2bf09c to your computer and use it in GitHub Desktop.
A description of the "Stuck in bed" bug, it's cause, and the fix that BedPatch uses

The Issue:
The "Stuck In Bed" bug is an issue that has existed in vanilla Minecraft since at least version 1.10.2. The primary symptom of the issue is that after teleporting a player may not be able to leave their bed the next time they sleep. The only vanilla way to deal with the issue when it happens is to exit the game. Some mods, like "Bed Bugs", have given players a better way to deal with the issue by allowing them to kick themselves back to the main menu when the issue happens, saving the player the time it takes to reload Minecraft which is particularly important with larger modpacks that may take a while to load. Unfortunately, due to mod interactions and other factors this sometimes causes the game to crash anyway.

The Cause:
When Minecraft decides a player should wake up it looks them up in the EntityTracker and sends them a signal that tells them to get out of bed. If a player can not be looked up in the EntityTracker, they will never get the signal that tells them to get out of bed.

The EntityTracker is used by Minecraft to keep track of all players and mobs in the world for various reasons. A player is added to it when they log in, and is typically only removed from it when they log off.

Another event that can cause entities to be removed from the EntityTracker is when a Chunk unloads. The Chunk keeps track of all entities in it, and when unloaded instructs Minecraft to remove all those entities from the EntityTracker. Chunks are typicaly only loaded if a player is within a certain range of them, so teleporting out of a Chunk that has no other players nearby will cause the chunk to be unloaded on the next tick.

The player also keeps a record of what Chunk it is in. Each tick, the Chunk a player is actually in is checked against this record. If they do not match, the record is updated, the player is removed from the old Chunks list of entities, and is added to the new Chunks list. This means that when a player teleports, on the next world tick they will be "moved" from the entity list of the old Chunk to the new one.

In 1.7.2, Minecraft runs the check for a players locations before unloading Chunks that no longer need to be loaded. This means that when an old Chunk gets unloaded, any players that have teleported out of it have already been removed from that Chunks list of entities when the Chunk unloads.

In 1.10.2 (and possibly earlier) the Minecraft world object was restructured, and as result the order that events happen was (probably inadvertantly) changed. Now, Minecraft unloads and unneeded chunks before checking the players location. Becasue of this, the player had not been moved to the new Chunks entity list and when the old Chunk unloads the player is removed from the EntityTracker. This may cause other issues related to the player not being in the EntityTracker, for example a player being invisible to other players in multiplayer.

The Bed Patch Fix:
It is unrealistic to reorder the Minecraft tick, so instead Bed Patch changes what happens when a Chunk unloads. When Bed Patch sees a Chunk about to unload, it looks for all players in that Chunks entity list, and forces Minecraft to to run the location check for those players. This gives the player a chance to be "moved" to its new Chunk before the old one unloads. This is a very simple change and has little to no risk of causing unwanted side effects, as Minecraft itself would be running this check a little later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment