add custom on_sleep() function for bed registration.

This commit is contained in:
tenplus1 2024-11-17 15:46:28 +00:00
parent 9b09737d97
commit 6418226c11
4 changed files with 29 additions and 3 deletions

View File

@ -38,6 +38,12 @@ death. Check configuration section for more info.
It features more beds, so along with the Red simple bed we now have White and
Blue, and the fance beds has the original Red and now Pink.
* Custom function:
Beds registered using beds.register_bed() function can also include a custom
on_sleep(pos, player) function that allows certain beds to have their own
characteristics, and by returning True the player will be kicked from their bed.
#### Dependencies
* default
@ -45,7 +51,6 @@ Blue, and the fance beds has the original Red and now Pink.
Optional dependences:
* intllib (only for older engines)
* pova (optional)
The pova mod are not xplicit set as optional depends, due the circular depends bug,

View File

@ -116,6 +116,10 @@ function beds.register_bed(name, def)
can_dig = function(pos, player)
return beds.can_dig(pos)
end,
on_sleep = function(pos, player)
return def.on_sleep and def.on_sleep(pos, player)
end
})

View File

@ -73,7 +73,11 @@ beds.register_bed("beds:bed_blue", {
recipe = {
{"wool:blue", "wool:blue", "wool:white"},
{"group:wood", "group:wood", "group:wood"}
}
},
on_sleep = function(pos, player)
-- print("----", minetest.pos_to_string(pos), player:get_player_name())
minetest.sound_play("default_dig_choppy", {pos = pos, gain = 0.5}, true)
end
})
-- Aliases for PilzAdam's beds mod

View File

@ -137,8 +137,8 @@ local function lay_down(player, pos, bed_pos, state, skip)
return false
end
-- player already in bed, do nothing
if beds.player[name] then
-- player already in bed, do nothing
return false
end
@ -296,6 +296,19 @@ function beds.on_rightclick(pos, player)
lay_down(player, nil, nil, false)
end
-- check for custom on_sleep function
local nod = minetest.get_node(pos).name
local def = minetest.registered_nodes[nod]
local on_sleep = def and def.on_sleep
-- if on_sleep returns True then skip sleeping
if def.on_sleep and def.on_sleep(pos, player) then
lay_down(player, nil, nil, false)
return
end
if not is_sp then
update_formspecs(false)
end