I am trying to add the ability to sleep to my mod.
With “ability to sleep” I mean the behavior known from Minecraft: at night you can “go to sleep” with the help of a certain node, and if all players “go to sleep” the night is skipped and all players “wake up”.
What I have found so far are these mods:
- beds from Minetest Game: Implements this behavior, but has bad API (no documentation, not placed in api.lua).
- mesecraft_beds: Same code as Minetest Game beds, just rebranded.
- mcl_beds: Fork of Minetest Game beds with many modifications, same API under different namespace.
- beds aka Beds Redux: Different code with some API documentation.
- bedz from the Samz game: Independent implementation, though without API.
Beds Redux exposes it directly, Minetest Game hides it behind a on_rightclick function.
But none of all these APIs helps me in any way.
“Going to sleep” is not equivalent to “rightclicking a certain node”, but all these mods assume they are equivalent.
Consider these examples:
- cottages mod: It is possible to sit on a bed, without sleeping.
- advtrains sleeper wagons: If you are in a sleeper wagon, it is reasonable to assume you are willing to sleep. But this is entirely unrelated to any nodes. Additionally, players would not “leave” the sleeper wagon when the night ends.
- Sleeping bags: These allow you to sleep at a different place than in a bed. They would be a tool rather than a node, so it is again entirely unrelated to any nodes.
- Players being AFK or mining deep under the surface: These are not affected by skipping the night, so they should be considered as willing to “sleep”.
I can imagine an API like this:
What do you think?sleep_api.changes_sleeping_state(playername) → nil
Call this when a player changes their sleeping state, regarding player_is_sleeping().
For example, you call this when a player enters or leaves a bed defined by your mod.
sleep_api.register_on_changing_sleeping_state(changing_sleep_state_handler(playername) → nil) → nil
Registers a function that is called when going_to_sleep() is called.
This callback is automatically called when players join or leave the game.
This can be used to notify other players about the new count of sleeping players.
The mod which implements sleeping shall check if enough player are sleeping, and then skip the night.
sleep_api.player_is_sleeping(playername) → bool
Returns true if the player can be considered sleeping.
sleep_api.register_player_is_sleeping(custom_is_sleeping(playername) → string) → nil
Registers a function that can return "sleeping", "awake", or "any".
"sleeping" means that the player is sleeping in any way defined by your mod.
"awake" means that the player is in a state where it is definitely not sleeping.
"any" means that the player is not in any state defined by your mod, and other mods may tell whether it is sleeping.
Note: This callback can even be called when the player is offline.
sleep_api.night_ends(reason) → nil
This shall be called by the mod that implements sleeping logic.
For example, this can be called when a handler for going_to_sleep() has been called, while enough players are sleeping.
The calling mod is responsible to actually make the night end, i. e. set the new time of day.
sleep_api.register_on_night_ends(night_ends_handler(reason) → nil) → nil
Registers a function that is called when the night ends by any mean.
Use this callback to make players wake up when they were sleeping in any way defined by your mod.
For example, players will leave the bed (so they are able to move again), and have their spawnpoint set to the bed position.