Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Add bed interaction / sleeping #619

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

Johni0702
Copy link
Contributor

This PR adds interaction with beds, sleeping and bed spawn points.

Interacting and sleeping

When the player right-clicks on the bed and the following conditions are met, the player enters the bed:

  • Biome is neither HELL nor SKY aka. End (bed will explode in these cases)
  • It is night time or thundering
  • The bed must not be occupied
  • Player must be within reach (smaller than normal)
  • No hostile mob must be within certain distance

This PR calls PlayerBedLeaveEvent and PlayerBedEnterEvent where necessary.
If every player in a world is sleeping for at least 100 ticks, the night is skipped and weather is reset.

Bed spawn

If a player leaves the bed by either clicking 'Leave Bed' or completing sleep, their spawn is set to the head of the bed.
When the player respawns and they have a bed spawn set, this looks for the bed. If it is still there (or was destroyed and replaced) and a valid spawn location can be found next to the bed (in the same order as vanilla does), the player will spawn at that location instead of the world spawn.
If their bed was missing or obstructed, their spawn is reset and they'll spawn at the world spawn.

Additional notes

This does not include removing beds in any way.
When the client leaves their bed, they determine their own position. Only respawning after death must be handled by the server. However this PR also handles the first case as the new location must be available to plugins when the PlayerBedLeaveEvent is called.
I could not find a pattern in the blocks the client tolerates when leaving their bed, therefore I've created a helper method BlockBed.isValidSpawn(Material). If there is a more simple way to determine these block, which I failed to find, it should be used instead.
The world I used to create above screenshot and to test distances, exit positions, etc. can be downloaded here. It was created in Vanilla 1.8 and might be useful for testing.

Relevant links

This adds the UseBed packet: #75
Glowkit: GlowstoneMC/Glowkit#66

Bed spawn is set and used where necessary
Sleeping in beds will skip time and stop weather
Sleeping in the end or nether will cause the bed to explode
@turt2live
Copy link
Contributor

This does not include removing beds in any way.
Why not?

@Johni0702
Copy link
Contributor Author

@turt2live I figured that this PR is already big enough and removing beds could be done in a separate PR without any conflicts. But if you wish, I'll add bed removal as well.

@turt2live
Copy link
Contributor

What would be involved in removing a bed? Should it not be one method override?

@Johni0702
Copy link
Contributor Author

Yes, it should be rather straight forward. Override the method, remove the other half of the bed, eject any player and make sure all events fire as expected, etc.
I'll probably implement it anyways. It's up to you whether you want it in this PR or in a separate one.

@turt2live
Copy link
Contributor

In this one is preferred

@Johni0702
Copy link
Contributor Author

I finally got to have a look at bed breaking and it turns out that it's more complicated than I thought.
The main reason being that it sometimes drops an item and sometime doesn't (especially when setting the head/foot programmatically via setType(Material) etc.).
I haven't had the time to look at it closer but I'd rather have that done another time than blocking this PR.

Updated the Glowkit PR as well as this PR to include the required changes discussed in the Glowkit PR.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants