GlowstoneMC / Glowstone-Legacy

An open-source server for the Bukkit Minecraft modding interface
Other
363 stars 121 forks source link

Add bed interaction / sleeping #619

Open Johni0702 opened 9 years ago

Johni0702 commented 9 years ago

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:

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

turt2live commented 9 years ago

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

Johni0702 commented 9 years ago

@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 commented 9 years ago

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

Johni0702 commented 9 years ago

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 commented 9 years ago

In this one is preferred

Johni0702 commented 9 years ago

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.