Disallow digging a bed if in use

Use a new table beds.bed_position to check if a bed is in use.
This commit is contained in:
bell07 2018-06-19 23:07:01 +02:00 committed by Paramat
parent e9fbd3d75d
commit 5692c15b4d
4 changed files with 22 additions and 0 deletions

View File

@ -42,6 +42,7 @@ Beds API
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.skip_night()` Sets world time to morning and saves respawn position of all players currently sleeping

View File

@ -141,6 +141,9 @@ function beds.register_bed(name, def)
minetest.set_node(newp, {name = name .. "_top", param2 = new_param2})
return true
end,
can_dig = function(pos, player)
return beds.can_dig(pos)
end,
})
minetest.register_node(name .. "_top", {
@ -160,6 +163,12 @@ function beds.register_bed(name, def)
on_destruct = function(pos)
destruct_bed(pos, 2)
end,
can_dig = function(pos, player)
local node = minetest.get_node(pos)
local dir = minetest.facedir_to_dir(node.param2)
local p = vector.add(pos, dir)
return beds.can_dig(p)
end,
})
minetest.register_alias(name, name .. "_bottom")

View File

@ -61,6 +61,7 @@ local function lay_down(player, pos, bed_pos, state, skip)
local p = beds.pos[name] or nil
if beds.player[name] ~= nil then
beds.player[name] = nil
beds.bed_position[name] = nil
player_in_bed = player_in_bed - 1
end
-- skip here to prevent sending player specific changes (used for leaving players)
@ -83,6 +84,7 @@ local function lay_down(player, pos, bed_pos, state, skip)
else
beds.player[name] = 1
beds.pos[name] = pos
beds.bed_position[name] = bed_pos
player_in_bed = player_in_bed + 1
-- physics, eye_offset, etc
@ -174,6 +176,15 @@ function beds.on_rightclick(pos, player)
end
end
function beds.can_dig(bed_pos)
-- Check all players in bed which one is at the expected position
for _, player_bed_pos in pairs(beds.bed_position) do
if vector.equals(bed_pos, player_bed_pos) then
return false
end
end
return true
end
-- Callbacks
-- Only register respawn callback if respawn enabled

View File

@ -1,5 +1,6 @@
beds = {}
beds.player = {}
beds.bed_position = {}
beds.pos = {}
beds.spawn = {}