diff --git a/README.md b/README.md index f67988e..2783fad 100644 --- a/README.md +++ b/README.md @@ -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, diff --git a/api.lua b/api.lua index f627240..bb8e048 100644 --- a/api.lua +++ b/api.lua @@ -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 }) diff --git a/beds.lua b/beds.lua index 323e068..eb7c37f 100644 --- a/beds.lua +++ b/beds.lua @@ -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 diff --git a/functions.lua b/functions.lua index ae9a733..1631ffc 100644 --- a/functions.lua +++ b/functions.lua @@ -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