2019-03-07 22:35:02 +02:00
|
|
|
local S = minetest.get_translator("mcl_beds")
|
|
|
|
|
2017-02-13 00:43:30 +02:00
|
|
|
local pi = math.pi
|
|
|
|
local player_in_bed = 0
|
|
|
|
local is_sp = minetest.is_singleplayer()
|
2017-11-21 02:35:31 +02:00
|
|
|
local weather_mod = minetest.get_modpath("mcl_weather") ~= nil
|
2017-02-13 00:43:30 +02:00
|
|
|
|
|
|
|
-- Helper functions
|
|
|
|
|
|
|
|
local function get_look_yaw(pos)
|
|
|
|
local n = minetest.get_node(pos)
|
|
|
|
if n.param2 == 1 then
|
|
|
|
return pi / 2, n.param2
|
|
|
|
elseif n.param2 == 3 then
|
|
|
|
return -pi / 2, n.param2
|
|
|
|
elseif n.param2 == 0 then
|
|
|
|
return pi, n.param2
|
|
|
|
else
|
|
|
|
return 0, n.param2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function is_night_skip_enabled()
|
2017-08-09 17:17:00 +03:00
|
|
|
local enable_night_skip = minetest.settings:get_bool("enable_bed_night_skip")
|
2017-02-13 00:43:30 +02:00
|
|
|
if enable_night_skip == nil then
|
|
|
|
enable_night_skip = true
|
|
|
|
end
|
|
|
|
return enable_night_skip
|
|
|
|
end
|
|
|
|
|
|
|
|
local function check_in_beds(players)
|
2017-05-07 21:21:37 +03:00
|
|
|
local in_bed = mcl_beds.player
|
2017-02-13 00:43:30 +02:00
|
|
|
if not players then
|
|
|
|
players = minetest.get_connected_players()
|
|
|
|
end
|
|
|
|
|
|
|
|
for n, player in ipairs(players) do
|
|
|
|
local name = player:get_player_name()
|
|
|
|
if not in_bed[name] then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return #players > 0
|
|
|
|
end
|
|
|
|
|
2018-01-08 04:16:27 +02:00
|
|
|
-- These monsters do not prevent sleep
|
|
|
|
local monster_exceptions = {
|
|
|
|
["mobs_mc:ghast"] = true,
|
|
|
|
["mobs_mc:enderdragon"] = true,
|
|
|
|
["mobs_mc:killer_bunny"] = true,
|
|
|
|
["mobs_mc:slime_big"] = true,
|
|
|
|
["mobs_mc:slime_small"] = true,
|
|
|
|
["mobs_mc:slime_tiny"] = true,
|
|
|
|
["mobs_mc:magma_cube_big"] = true,
|
|
|
|
["mobs_mc:magma_cube_small"] = true,
|
|
|
|
["mobs_mc:magma_cube_tiny"] = true,
|
|
|
|
["mobs_mc:shulker"] = true,
|
|
|
|
}
|
|
|
|
|
2017-02-13 00:43:30 +02:00
|
|
|
local function lay_down(player, pos, bed_pos, state, skip)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
local hud_flags = player:hud_get_flags()
|
|
|
|
|
|
|
|
if not player or not name then
|
2019-02-20 20:22:24 +02:00
|
|
|
return false
|
2017-02-13 00:43:30 +02:00
|
|
|
end
|
|
|
|
|
2018-01-08 03:49:41 +02:00
|
|
|
if bed_pos then
|
2018-01-08 04:19:19 +02:00
|
|
|
-- No sleeping if too far away
|
|
|
|
if vector.distance(bed_pos, pos) > 2 then
|
2019-03-07 22:35:02 +02:00
|
|
|
minetest.chat_send_player(name, S("You can't sleep, the bed's too far away!"))
|
2019-02-20 20:22:24 +02:00
|
|
|
return false
|
2018-01-08 04:19:19 +02:00
|
|
|
end
|
|
|
|
|
2019-02-20 21:40:06 +02:00
|
|
|
for _, other_pos in pairs(mcl_beds.bed_pos) do
|
|
|
|
if vector.distance(bed_pos, other_pos) < 0.1 then
|
2019-03-07 22:35:02 +02:00
|
|
|
minetest.chat_send_player(name, S("This bed is already occupied!"))
|
2019-02-20 21:40:06 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-20 21:45:55 +02:00
|
|
|
-- No sleeping while moving. Slightly different behaviour than in MC.
|
2019-02-11 17:14:08 +02:00
|
|
|
if vector.length(player:get_player_velocity()) > 0.001 then
|
2019-03-07 22:35:02 +02:00
|
|
|
minetest.chat_send_player(name, S("You have to stop moving before going to bed!"))
|
2019-02-20 20:22:24 +02:00
|
|
|
return false
|
2019-02-11 17:14:08 +02:00
|
|
|
end
|
|
|
|
|
2018-01-08 04:16:27 +02:00
|
|
|
-- No sleeping if monsters nearby.
|
|
|
|
-- The exceptions above apply.
|
|
|
|
-- Zombie pigmen only prevent sleep while they are hostle.
|
2018-01-08 03:49:41 +02:00
|
|
|
local objs = minetest.get_objects_inside_radius(bed_pos, 8)
|
|
|
|
for _, obj in ipairs(objs) do
|
|
|
|
if obj ~= nil and not obj:is_player() then
|
2018-01-08 04:16:27 +02:00
|
|
|
local ent = obj:get_luaentity()
|
|
|
|
local mobname = ent.name
|
|
|
|
local def = minetest.registered_entities[mobname]
|
2018-01-08 03:49:41 +02:00
|
|
|
-- Approximation of monster detection range
|
2018-01-08 04:16:27 +02:00
|
|
|
if def._cmi_is_mob and ((mobname ~= "mobs_mc:pigman" and def.type == "monster" and not monster_exceptions[mobname]) or (mobname == "mobs_mc:pigman" and ent.state == "attack")) then
|
|
|
|
if math.abs(bed_pos.y - obj:get_pos().y) <= 5 then
|
2019-03-07 22:35:02 +02:00
|
|
|
minetest.chat_send_player(name, S("You can't sleep now, monsters are nearby!"))
|
2018-01-08 04:16:27 +02:00
|
|
|
end
|
2019-02-20 20:22:24 +02:00
|
|
|
return false
|
2018-01-08 03:49:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-13 00:43:30 +02:00
|
|
|
-- stand up
|
|
|
|
if state ~= nil and not state then
|
2017-05-07 21:21:37 +03:00
|
|
|
local p = mcl_beds.pos[name] or nil
|
|
|
|
if mcl_beds.player[name] ~= nil then
|
|
|
|
mcl_beds.player[name] = nil
|
2017-02-13 00:43:30 +02:00
|
|
|
player_in_bed = player_in_bed - 1
|
|
|
|
end
|
|
|
|
-- skip here to prevent sending player specific changes (used for leaving players)
|
|
|
|
if skip then
|
2019-02-20 20:22:24 +02:00
|
|
|
return false
|
2017-02-13 00:43:30 +02:00
|
|
|
end
|
|
|
|
if p then
|
2019-02-20 21:40:06 +02:00
|
|
|
player:set_pos(p)
|
2017-02-13 00:43:30 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- physics, eye_offset, etc
|
|
|
|
player:set_eye_offset({x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
|
2019-02-20 20:22:24 +02:00
|
|
|
if player:get_look_vertical() > 0 then
|
|
|
|
player:set_look_vertical(0)
|
|
|
|
end
|
2017-02-13 00:43:30 +02:00
|
|
|
mcl_player.player_attached[name] = false
|
2018-10-23 19:51:19 +03:00
|
|
|
playerphysics.remove_physics_factor(player, "speed", "mcl_beds:sleeping")
|
|
|
|
playerphysics.remove_physics_factor(player, "jump", "mcl_beds:sleeping")
|
2019-03-06 06:45:16 +02:00
|
|
|
player:get_meta():set_string("mcl_beds:sleeping", "false")
|
2017-02-13 00:43:30 +02:00
|
|
|
hud_flags.wielditem = true
|
|
|
|
mcl_player.player_set_animation(player, "stand" , 30)
|
2019-02-20 21:40:06 +02:00
|
|
|
mcl_beds.pos[name] = nil
|
|
|
|
mcl_beds.bed_pos[name] = nil
|
2017-02-13 00:43:30 +02:00
|
|
|
|
|
|
|
-- lay down
|
|
|
|
else
|
2019-02-04 18:53:30 +02:00
|
|
|
local yaw, param2 = get_look_yaw(bed_pos)
|
|
|
|
local dir = minetest.facedir_to_dir(param2)
|
2019-02-20 21:59:01 +02:00
|
|
|
local p = {x = bed_pos.x - dir.x/2, y = bed_pos.y, z = bed_pos.z - dir.z/2}
|
2019-02-04 18:53:30 +02:00
|
|
|
local n1 = minetest.get_node({x=bed_pos.x, y=bed_pos.y+1, z=bed_pos.z})
|
|
|
|
local n2 = minetest.get_node({x=bed_pos.x, y=bed_pos.y+2, z=bed_pos.z})
|
|
|
|
local def1 = minetest.registered_nodes[n1.name]
|
|
|
|
local def2 = minetest.registered_nodes[n2.name]
|
|
|
|
if def1.walkable or def2.walkable then
|
2019-03-07 22:35:02 +02:00
|
|
|
minetest.chat_send_player(name, S("You can't sleep, the bed is obstructed!"))
|
2019-02-20 20:22:24 +02:00
|
|
|
return false
|
2019-02-04 18:53:30 +02:00
|
|
|
elseif (def1.damage_per_second ~= nil and def1.damage_per_second > 0) or (def2.damage_per_second ~= nil and def2.damage_per_second > 0) then
|
2019-03-07 22:35:02 +02:00
|
|
|
minetest.chat_send_player(name, S("It's too dangerous to sleep here!"))
|
2019-02-20 20:22:24 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2019-02-20 20:39:12 +02:00
|
|
|
local spawn_changed = false
|
2019-02-20 20:22:24 +02:00
|
|
|
if minetest.get_modpath("mcl_spawn") then
|
|
|
|
local spos = table.copy(bed_pos)
|
|
|
|
spos.y = spos.y + 0.1
|
2019-02-20 20:39:12 +02:00
|
|
|
spawn_changed = mcl_spawn.set_spawn_pos(player, spos) -- save respawn position when entering bed
|
2019-02-20 20:22:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Check day of time and weather
|
|
|
|
local tod = minetest.get_timeofday() * 24000
|
|
|
|
-- Values taken from Minecraft Wiki with offset of +6000
|
|
|
|
if tod < 18541 and tod > 5458 and (not weather_mod or (mcl_weather.get_weather() ~= "thunder")) then
|
2019-02-20 20:39:12 +02:00
|
|
|
if spawn_changed then
|
2019-03-07 22:35:02 +02:00
|
|
|
minetest.chat_send_player(name, S("New respawn position set! But you can only sleep at night or during a thunderstorm."))
|
2019-02-20 20:39:12 +02:00
|
|
|
else
|
2019-03-07 22:35:02 +02:00
|
|
|
minetest.chat_send_player(name, S("You can only sleep at night or during a thunderstorm."))
|
2019-02-20 20:39:12 +02:00
|
|
|
end
|
2019-02-20 20:22:24 +02:00
|
|
|
return false
|
2019-02-04 18:53:30 +02:00
|
|
|
end
|
2019-02-20 20:39:12 +02:00
|
|
|
if spawn_changed then
|
2019-03-07 22:35:02 +02:00
|
|
|
minetest.chat_send_player(name, S("New respawn position set!"))
|
2019-02-20 20:39:12 +02:00
|
|
|
end
|
2019-02-04 18:53:30 +02:00
|
|
|
|
2017-05-07 21:21:37 +03:00
|
|
|
mcl_beds.player[name] = 1
|
|
|
|
mcl_beds.pos[name] = pos
|
2019-02-20 21:40:06 +02:00
|
|
|
mcl_beds.bed_pos[name] = bed_pos
|
2017-02-13 00:43:30 +02:00
|
|
|
player_in_bed = player_in_bed + 1
|
|
|
|
-- physics, eye_offset, etc
|
|
|
|
player:set_eye_offset({x = 0, y = -13, z = 0}, {x = 0, y = 0, z = 0})
|
|
|
|
player:set_look_horizontal(yaw)
|
2019-02-20 20:22:24 +02:00
|
|
|
player:set_look_vertical(0)
|
2019-02-04 18:53:30 +02:00
|
|
|
|
2019-03-06 06:45:16 +02:00
|
|
|
player:get_meta():set_string("mcl_beds:sleeping", "true")
|
2018-10-23 19:51:19 +03:00
|
|
|
playerphysics.add_physics_factor(player, "speed", "mcl_beds:sleeping", 0)
|
|
|
|
playerphysics.add_physics_factor(player, "jump", "mcl_beds:sleeping", 0)
|
2019-02-20 21:40:06 +02:00
|
|
|
player:set_pos(p)
|
2017-02-13 00:43:30 +02:00
|
|
|
mcl_player.player_attached[name] = true
|
|
|
|
hud_flags.wielditem = false
|
|
|
|
mcl_player.player_set_animation(player, "lay" , 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
player:hud_set_flags(hud_flags)
|
2019-02-20 20:22:24 +02:00
|
|
|
return true
|
2017-02-13 00:43:30 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function update_formspecs(finished)
|
|
|
|
local ges = #minetest.get_connected_players()
|
2019-03-07 13:42:08 +02:00
|
|
|
local form_n = "size[8,15;true]"
|
2017-02-13 01:03:34 +02:00
|
|
|
local all_in_bed = ges == player_in_bed
|
2019-02-20 18:32:22 +02:00
|
|
|
local night_skip = is_night_skip_enabled()
|
|
|
|
local button_leave = "button_exit[2,12;4,0.75;leave;Leave bed]"
|
|
|
|
local button_abort = "button_exit[2,12;4,0.75;leave;Abort sleep]"
|
|
|
|
local bg_presleep = "bgcolor[#00000080;true]"
|
|
|
|
local bg_sleep = "bgcolor[#000000FF;true]"
|
2017-02-13 00:43:30 +02:00
|
|
|
|
|
|
|
if finished then
|
2019-02-20 18:04:38 +02:00
|
|
|
for name,_ in pairs(mcl_beds.player) do
|
|
|
|
minetest.close_formspec(name, "mcl_beds_form")
|
|
|
|
end
|
|
|
|
return
|
|
|
|
elseif not is_sp then
|
2019-02-20 22:09:04 +02:00
|
|
|
local text = string.format("Players in bed: %d/%d", player_in_bed, ges)
|
2019-02-20 18:32:22 +02:00
|
|
|
if not night_skip then
|
2019-02-20 21:45:55 +02:00
|
|
|
text = text .. "\n" .. "Note: Night skip is disabled."
|
2019-02-20 18:32:22 +02:00
|
|
|
form_n = form_n .. bg_presleep
|
|
|
|
form_n = form_n .. button_leave
|
|
|
|
elseif all_in_bed then
|
2019-02-20 18:04:38 +02:00
|
|
|
text = text .. "\n" .. "You're sleeping."
|
2019-02-20 18:32:22 +02:00
|
|
|
form_n = form_n .. bg_sleep
|
|
|
|
form_n = form_n .. button_abort
|
2019-02-20 18:04:38 +02:00
|
|
|
else
|
2019-02-20 21:45:55 +02:00
|
|
|
text = text .. "\n" .. "You will fall asleep when all players are in bed."
|
2019-02-20 18:32:22 +02:00
|
|
|
form_n = form_n .. bg_presleep
|
|
|
|
form_n = form_n .. button_leave
|
2019-02-20 18:04:38 +02:00
|
|
|
end
|
|
|
|
form_n = form_n .. "label[2.2,7.5;"..minetest.formspec_escape(text).."]"
|
2017-02-13 00:43:30 +02:00
|
|
|
else
|
2019-02-20 18:32:22 +02:00
|
|
|
local text
|
|
|
|
if night_skip then
|
|
|
|
text = "You're sleeping."
|
|
|
|
form_n = form_n .. bg_sleep
|
|
|
|
form_n = form_n .. button_abort
|
|
|
|
else
|
|
|
|
text = "You're in bed." .. "\n" .. "Note: Night skip is disabled."
|
|
|
|
form_n = form_n .. bg_presleep
|
|
|
|
form_n = form_n .. button_leave
|
|
|
|
end
|
|
|
|
form_n = form_n .. "label[2.2,7.5;"..minetest.formspec_escape(text).."]"
|
2017-02-13 00:43:30 +02:00
|
|
|
end
|
|
|
|
|
2017-05-07 21:21:37 +03:00
|
|
|
for name,_ in pairs(mcl_beds.player) do
|
|
|
|
minetest.show_formspec(name, "mcl_beds_form", form_n)
|
2017-02-13 00:43:30 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Public functions
|
|
|
|
|
2017-11-20 06:06:58 +02:00
|
|
|
-- Handle environment stuff related to sleeping: skip night and thunderstorm
|
|
|
|
function mcl_beds.sleep()
|
|
|
|
local storm_skipped = mcl_beds.skip_thunderstorm()
|
2018-01-08 04:42:51 +02:00
|
|
|
-- Always clear weather
|
|
|
|
if weather_mod then
|
|
|
|
mcl_weather.change_weather("none")
|
|
|
|
end
|
2017-11-20 06:06:58 +02:00
|
|
|
if is_night_skip_enabled() then
|
|
|
|
if not storm_skipped then
|
|
|
|
mcl_beds.skip_night()
|
|
|
|
end
|
|
|
|
mcl_beds.kick_players()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-20 17:09:39 +02:00
|
|
|
-- Throw all players out of bed
|
2017-05-07 21:21:37 +03:00
|
|
|
function mcl_beds.kick_players()
|
|
|
|
for name, _ in pairs(mcl_beds.player) do
|
2017-02-13 00:43:30 +02:00
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
lay_down(player, nil, nil, false)
|
|
|
|
end
|
2019-02-20 18:04:38 +02:00
|
|
|
update_formspecs(false)
|
2017-02-13 00:43:30 +02:00
|
|
|
end
|
|
|
|
|
2019-02-20 17:09:39 +02:00
|
|
|
-- Throw a player out of bed
|
|
|
|
function mcl_beds.kick_player(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
if mcl_beds.player[name] ~= nil then
|
|
|
|
lay_down(player, nil, nil, false)
|
2019-02-20 18:04:38 +02:00
|
|
|
update_formspecs(false)
|
|
|
|
minetest.close_formspec(name, "mcl_beds_form")
|
2019-02-20 17:09:39 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-07 21:21:37 +03:00
|
|
|
function mcl_beds.skip_night()
|
2017-02-21 03:53:32 +02:00
|
|
|
minetest.set_timeofday(0.25) -- tod = 6000
|
2017-02-13 00:43:30 +02:00
|
|
|
end
|
|
|
|
|
2017-11-20 06:06:58 +02:00
|
|
|
function mcl_beds.skip_thunderstorm()
|
|
|
|
-- Skip thunderstorm
|
2017-11-21 02:35:31 +02:00
|
|
|
if weather_mod and mcl_weather.get_weather() == "thunder" then
|
2017-11-20 06:06:58 +02:00
|
|
|
-- Sleep for a half day (=minimum thunderstorm duration)
|
|
|
|
minetest.set_timeofday((minetest.get_timeofday() + 0.5) % 1)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2019-02-20 20:22:24 +02:00
|
|
|
function mcl_beds.on_rightclick(pos, player, is_top)
|
2017-11-21 21:23:34 +02:00
|
|
|
-- Anti-Inception: Don't allow to sleep while you're sleeping
|
2019-03-06 06:45:16 +02:00
|
|
|
if player:get_meta():get_string("mcl_beds:sleeping") == "true" then
|
2017-11-21 21:23:34 +02:00
|
|
|
return
|
|
|
|
end
|
2017-11-24 04:10:02 +02:00
|
|
|
if minetest.get_modpath("mcl_worlds") then
|
|
|
|
local dim = mcl_worlds.pos_to_dimension(pos)
|
2017-08-17 21:22:30 +03:00
|
|
|
if dim == "nether" or dim == "end" then
|
|
|
|
-- Bed goes BOOM in the Nether or End.
|
|
|
|
minetest.remove_node(pos)
|
|
|
|
if minetest.get_modpath("mcl_tnt") then
|
|
|
|
tnt.boom(pos, {radius = 4, damage_radius = 4})
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2017-02-13 00:43:30 +02:00
|
|
|
local name = player:get_player_name()
|
2019-02-01 07:33:07 +02:00
|
|
|
local ppos = player:get_pos()
|
2017-02-13 00:43:30 +02:00
|
|
|
|
|
|
|
-- move to bed
|
2017-05-07 21:21:37 +03:00
|
|
|
if not mcl_beds.player[name] then
|
2019-02-20 20:22:24 +02:00
|
|
|
if is_top then
|
|
|
|
lay_down(player, ppos, pos)
|
|
|
|
else
|
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
local dir = minetest.facedir_to_dir(node.param2)
|
|
|
|
local other = vector.add(pos, dir)
|
|
|
|
lay_down(player, ppos, other)
|
2017-11-24 08:04:03 +02:00
|
|
|
end
|
2017-02-13 00:43:30 +02:00
|
|
|
else
|
|
|
|
lay_down(player, nil, nil, false)
|
|
|
|
end
|
|
|
|
|
2019-02-20 18:04:38 +02:00
|
|
|
update_formspecs(false)
|
2017-02-13 00:43:30 +02:00
|
|
|
|
|
|
|
-- skip the night and let all players stand up
|
|
|
|
if check_in_beds() then
|
2018-01-08 04:44:06 +02:00
|
|
|
minetest.after(5, function()
|
2019-02-20 17:18:13 +02:00
|
|
|
if check_in_beds() then
|
2017-02-13 00:43:30 +02:00
|
|
|
update_formspecs(is_night_skip_enabled())
|
2019-02-20 17:18:13 +02:00
|
|
|
mcl_beds.sleep()
|
2017-02-13 00:43:30 +02:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Callbacks
|
2017-11-24 08:24:13 +02:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
2019-03-06 06:45:16 +02:00
|
|
|
local meta = player:get_meta()
|
|
|
|
if meta:get_string("mcl_beds:sleeping") == "true" then
|
2019-02-20 19:17:44 +02:00
|
|
|
-- Make player awake on joining server
|
2019-03-06 06:45:16 +02:00
|
|
|
meta:set_string("mcl_beds:sleeping", "false")
|
2017-11-24 08:24:13 +02:00
|
|
|
end
|
2019-02-20 19:17:44 +02:00
|
|
|
playerphysics.remove_physics_factor(player, "speed", "mcl_beds:sleeping")
|
|
|
|
playerphysics.remove_physics_factor(player, "jump", "mcl_beds:sleeping")
|
2019-02-20 22:03:25 +02:00
|
|
|
update_formspecs(false)
|
2017-11-24 08:24:13 +02:00
|
|
|
end)
|
2017-02-13 00:43:30 +02:00
|
|
|
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
lay_down(player, nil, nil, false, true)
|
2017-05-07 21:21:37 +03:00
|
|
|
mcl_beds.player[name] = nil
|
2017-02-13 00:43:30 +02:00
|
|
|
if check_in_beds() then
|
2018-01-08 04:53:15 +02:00
|
|
|
minetest.after(5, function()
|
2019-02-20 17:18:13 +02:00
|
|
|
if check_in_beds() then
|
|
|
|
update_formspecs(is_night_skip_enabled())
|
|
|
|
mcl_beds.sleep()
|
|
|
|
end
|
2017-02-13 00:43:30 +02:00
|
|
|
end)
|
|
|
|
end
|
2019-02-20 22:03:25 +02:00
|
|
|
update_formspecs(false)
|
2017-02-13 00:43:30 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
2017-05-07 21:21:37 +03:00
|
|
|
if formname ~= "mcl_beds_form" then
|
2017-02-13 00:43:30 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
if fields.quit or fields.leave then
|
|
|
|
lay_down(player, nil, nil, false)
|
|
|
|
update_formspecs(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
if fields.force then
|
|
|
|
update_formspecs(is_night_skip_enabled())
|
2017-11-20 06:06:58 +02:00
|
|
|
mcl_beds.sleep()
|
2017-02-13 00:43:30 +02:00
|
|
|
end
|
|
|
|
end)
|
2019-02-20 17:09:39 +02:00
|
|
|
|
|
|
|
minetest.register_on_player_hpchange(function(player, hp_change)
|
|
|
|
if hp_change < 0 then
|
|
|
|
mcl_beds.kick_player(player)
|
|
|
|
end
|
|
|
|
end)
|