Permalink
Browse files

Disallow digging a bed if in use

Use a new table beds.bed_position to check if a bed is in use.
  • Loading branch information...
bell07 authored and paramat committed Jun 19, 2018
1 parent e9fbd3d commit 5692c15b4d19eab371a0daba2efa9f04c00b81b9
Showing with 22 additions and 0 deletions.
  1. +1 −0 game_api.txt
  2. +9 −0 mods/beds/api.lua
  3. +11 −0 mods/beds/functions.lua
  4. +1 −0 mods/beds/init.lua
@@ -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
@@ -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")
@@ -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
@@ -1,5 +1,6 @@
beds = {}
beds.player = {}
beds.bed_position = {}
beds.pos = {}
beds.spawn = {}

0 comments on commit 5692c15

Please sign in to comment.