Warp player back to bed if they get shifted away too far, closes #45
This commit is contained in:
parent
fd24eca9ea
commit
fcee665c27
@ -11,9 +11,14 @@ bed = {}
|
|||||||
|
|
||||||
bed.userdata = {}
|
bed.userdata = {}
|
||||||
|
|
||||||
|
-- Savefile
|
||||||
|
|
||||||
local bed_file = minetest.get_worldpath() .. "/bed.dat"
|
local bed_file = minetest.get_worldpath() .. "/bed.dat"
|
||||||
local saving = false
|
local saving = false
|
||||||
|
|
||||||
|
-- Timer
|
||||||
|
|
||||||
|
local timer_interval = 1
|
||||||
local timer = 0
|
local timer = 0
|
||||||
|
|
||||||
local delay_daytime = false
|
local delay_daytime = false
|
||||||
@ -57,7 +62,7 @@ local function put_player_in_bed(player)
|
|||||||
{x=162, y=168},
|
{x=162, y=168},
|
||||||
default.player_animation_speed)
|
default.player_animation_speed)
|
||||||
|
|
||||||
default.player_set_animation(player, "lay", player_animation_speed)
|
default.player_set_animation(player, "lay", default.player_animation_speed)
|
||||||
|
|
||||||
default.player_attached[name] = true
|
default.player_attached[name] = true
|
||||||
|
|
||||||
@ -117,14 +122,20 @@ local function load_bed()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Server start
|
||||||
|
|
||||||
local function on_load()
|
local function on_load()
|
||||||
load_bed()
|
load_bed()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Server shutdown
|
||||||
|
|
||||||
local function on_shutdown()
|
local function on_shutdown()
|
||||||
save_bed()
|
save_bed()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Joining player
|
||||||
|
|
||||||
local function on_joinplayer(player)
|
local function on_joinplayer(player)
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
|
|
||||||
@ -152,6 +163,8 @@ local function on_joinplayer(player)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Respawning player
|
||||||
|
|
||||||
local function on_respawnplayer(player)
|
local function on_respawnplayer(player)
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
|
|
||||||
@ -180,10 +193,12 @@ local function on_respawnplayer(player)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Update function
|
||||||
|
|
||||||
local function on_globalstep(dtime)
|
local function on_globalstep(dtime)
|
||||||
timer = timer + dtime
|
timer = timer + dtime
|
||||||
|
|
||||||
if timer < 2 then
|
if timer < timer_interval then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -191,15 +206,22 @@ local function on_globalstep(dtime)
|
|||||||
|
|
||||||
local sleeping_players = 0
|
local sleeping_players = 0
|
||||||
|
|
||||||
for _, data in pairs(bed.userdata) do
|
for name, data in pairs(bed.userdata) do
|
||||||
if data.in_bed then
|
if data.in_bed then
|
||||||
|
local player = minetest.get_player_by_name(name)
|
||||||
|
|
||||||
sleeping_players = sleeping_players + 1
|
sleeping_players = sleeping_players + 1
|
||||||
|
|
||||||
|
if vector.distance(player:getpos(), data.spawn_pos) > 2 then
|
||||||
|
player:moveto(data.spawn_pos)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local players = #minetest.get_connected_players()
|
local players = minetest.get_connected_players()
|
||||||
|
local player_count = #players
|
||||||
|
|
||||||
if players > 0 and (players / 2.0) < sleeping_players then
|
if player_count > 0 and (player_count / 2.0) < sleeping_players then
|
||||||
if minetest.get_timeofday() < 0.2 or minetest.get_timeofday() > 0.8 then
|
if minetest.get_timeofday() < 0.2 or minetest.get_timeofday() > 0.8 then
|
||||||
if not delay_daytime then
|
if not delay_daytime then
|
||||||
delay_daytime = true
|
delay_daytime = true
|
||||||
@ -210,13 +232,13 @@ local function on_globalstep(dtime)
|
|||||||
minetest.chat_send_all(
|
minetest.chat_send_all(
|
||||||
minetest.colorize(
|
minetest.colorize(
|
||||||
"#0ff",
|
"#0ff",
|
||||||
"*** " .. sleeping_players .. " of " .. players
|
"*** " .. sleeping_players .. " of " .. player_count
|
||||||
.. " players slept, rise and shine!"))
|
.. " players slept, rise and shine!"))
|
||||||
|
|
||||||
minetest.set_timeofday(0.23)
|
minetest.set_timeofday(0.23)
|
||||||
delay_daytime = false
|
delay_daytime = false
|
||||||
|
|
||||||
for _, player in ipairs(minetest.get_connected_players()) do
|
for _, player in ipairs(players) do
|
||||||
if bed.userdata[player:get_player_name()].in_bed then
|
if bed.userdata[player:get_player_name()].in_bed then
|
||||||
bed.userdata[player:get_player_name()].slept = true
|
bed.userdata[player:get_player_name()].slept = true
|
||||||
end
|
end
|
||||||
@ -239,6 +261,8 @@ minetest.register_on_respawnplayer(on_respawnplayer)
|
|||||||
|
|
||||||
minetest.register_globalstep(on_globalstep)
|
minetest.register_globalstep(on_globalstep)
|
||||||
|
|
||||||
|
-- Nodes
|
||||||
|
|
||||||
minetest.register_node(
|
minetest.register_node(
|
||||||
"bed:bed_foot",
|
"bed:bed_foot",
|
||||||
{
|
{
|
||||||
@ -350,6 +374,8 @@ minetest.register_node(
|
|||||||
|
|
||||||
minetest.register_alias("bed:bed", "bed:bed_foot")
|
minetest.register_alias("bed:bed", "bed:bed_foot")
|
||||||
|
|
||||||
|
-- Crafting
|
||||||
|
|
||||||
crafting.register_craft(
|
crafting.register_craft(
|
||||||
{
|
{
|
||||||
output = "bed:bed",
|
output = "bed:bed",
|
||||||
@ -359,6 +385,8 @@ crafting.register_craft(
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
-- Player effects
|
||||||
|
|
||||||
player_effects.register_effect(
|
player_effects.register_effect(
|
||||||
"inbed",
|
"inbed",
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user