Is there anything like a “sleep_api” mod?

Post Reply
doxygen_spammer
Member
Posts: 70
Joined: Wed Dec 16, 2020 16:52
GitHub: doxygen-spammer

Is there anything like a “sleep_api” mod?

by doxygen_spammer » Post

Hi,

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.
Most these mods have one thing in common: A lay_down(playername, node_pos) function.
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”.
What I am looking for is some kind of API that allows me to mark a player as “willing to sleep” or “going to sleep”.
I can imagine an API like this:
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.
What do you think?
Last edited by doxygen_spammer on Thu Dec 01, 2022 13:09, edited 2 times in total.

User avatar
runs
Member
Posts: 3225
Joined: Sat Oct 27, 2018 08:32

Re: Is there anything like a “sleep_api” mod?

by runs » Post

Samzs Bedz mod already implements that.

doxygen_spammer
Member
Posts: 70
Joined: Wed Dec 16, 2020 16:52
GitHub: doxygen-spammer

Re: Is there anything like a “sleep_api” mod?

by doxygen_spammer » Post

Do you mean this? https://github.com/runsy/samz/tree/main/mods/bedz (From here: https://content.minetest.net/packages/runs/samz/)

I do not see which of the mentioned aspects are implemented there.
I see only two API functions: bedz.check_bed(pos) and bedz.register_bed(name, def).

Sorry, but I think your answer is not related to my question. :(

doxygen_spammer
Member
Posts: 70
Joined: Wed Dec 16, 2020 16:52
GitHub: doxygen-spammer

Re: Is there anything like a “sleep_api” mod?

by doxygen_spammer » Post

To be clear, my question is:
Is there anything like a “sleep_api” mod?
In the text of my post I didn’t make an explicit answer, but I provided context and explained what I mean with “anything like sleep_api”.

User avatar
runs
Member
Posts: 3225
Joined: Sat Oct 27, 2018 08:32

Re: Is there anything like a “sleep_api” mod?

by runs » Post

doxygen_spammer wrote:
Wed Nov 30, 2022 18:25
Do you mean this? https://github.com/runsy/samz/tree/main/mods/bedz (From here: https://content.minetest.net/packages/runs/samz/)

I do not see which of the mentioned aspects are implemented there.
I see only two API functions: bedz.check_bed(pos) and bedz.register_bed(name, def).

Sorry, but I think your answer is not related to my question. :(
Yes, related. Bedz do this precisely:

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”.

doxygen_spammer
Member
Posts: 70
Joined: Wed Dec 16, 2020 16:52
GitHub: doxygen-spammer

Re: Is there anything like a “sleep_api” mod?

by doxygen_spammer » Post

Fine. bedz implements sleeping in a similar way as Minecraft. I will add that to my compact list in the first post.

Sokomine
Member
Posts: 4276
Joined: Sun Sep 09, 2012 17:31
GitHub: Sokomine
IRC: Sokomine
In-game: Sokomine

Re: Is there anything like a “sleep_api” mod?

by Sokomine » Post

doxygen_spammer wrote: layers 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”.
That's probably the most important factor. Right now beds have no meanings on servers and are not beeing used. Players that are mining don't notice that it's night and that those on the surface have need for skipping it. Perhaps your idea can be included in the skip-night-feature of beds from MTG?
A list of my mods can be found here.

User avatar
Festus1965
Member
Posts: 4181
Joined: Sun Jan 03, 2016 11:58
GitHub: Festus1965
In-game: Festus1965 Thomas Thailand Explorer
Location: Thailand ChiangMai
Contact:

Re: Is there anything like a “sleep_api” mod?

by Festus1965 » Post

just change the setting about the +50% of player should sleep rule to a lower factor, that might fix your servers needs,

on the other side, I learned to play soon without the need of daylight:
* first to adapt : day farming, outside, night housework (sorting, crafting, ...)
* farming under mese or glowglass
* in the old times when people WAS asking in chat about 'sleep ?' I also had a bed at most used spots, even near mining, or in the bag
Human has no future (climate change)
If urgend, you find me in Roblox (as CNXThomas)

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests