Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions mods/beds/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -275,17 +275,25 @@ function beds.on_rightclick(pos, player)
local tod = minetest.get_timeofday()

if tod > beds.day_interval.start and tod < beds.day_interval.finish then
if beds.player[name] then
lay_down(player, nil, nil, false)
end
minetest.chat_send_player(name, S("You can only sleep at night."))
return
end
if beds.player[name] then
lay_down(player, nil, nil, false)
end

-- Only set spawn if not already set to this bed
local current_spawn = beds.spawn[name]
if not current_spawn or vector.distance(current_spawn, pos) > 0.1 then
beds.set_spawns(player, pos)
minetest.chat_send_player(name, S("Spawn point set."))
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about else instead? Have the first click set the spawn point and the 2nd to notify the player that it's yet not time to sleep.


minetest.chat_send_player(name, S("You can only sleep at night."))
return
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please indent the entire new code block so that it makes sense again.


-- move to bed
if not beds.player[name] then
lay_down(player, ppos, pos)
beds.set_spawns() -- save respawn positions when entering bed
beds.set_spawns(player, pos) -- save respawn positions when entering bed
else
lay_down(player, nil, nil, false)
end
Expand Down
16 changes: 6 additions & 10 deletions mods/beds/spawns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,12 @@ function beds.save_spawns()
io.close(output)
end

function beds.set_spawns()
for name,_ in pairs(beds.player) do
local player = minetest.get_player_by_name(name)
local p = player:get_pos()
-- but don't change spawn location if borrowing a bed
if not minetest.is_protected(p, name) then
beds.spawn[name] = p
end
end
beds.save_spawns()
function beds.set_spawns(player, bed_pos)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a documented API function? if so, won't this break compatibility?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thought here. It's not documented, thus no compat break.

Beds API
--------
beds.register_bed(
"beds:bed", -- Bed name
def -- See [#Bed definition]
)
* `beds.can_dig(bed_pos)` Returns a boolean whether the bed at `bed_pos` may be dug
* `beds.read_spawns() ` Returns a table containing players respawn positions
* `beds.kick_players()` Forces all players to leave bed
* `beds.kick_player_at(pos)` Forces player out of bed at pos (returns true on success, false otherwise)
* `beds.skip_night()` Sets world time to morning and saves respawn position of all players currently sleeping
* `beds.day_interval` Is a table with keys "start" and "finish". Allows you
to set the period of the day (timeofday format). Default: `{ start = 0.2, finish = 0.805 }`.
### Bed definition

local name = player:get_player_name()
if not minetest.is_protected(bed_pos, name) then
beds.spawn[name] = bed_pos
beds.save_spawns()
end
end

function beds.remove_spawns_at(pos)
Expand Down
Loading