Disallow digging a bed if in use
authorbell07 <web.alexander@web.de>
Tue, 19 Jun 2018 21:07:01 +0000 (23:07 +0200)
committerParamat <paramat@users.noreply.github.com>
Tue, 19 Jun 2018 21:07:01 +0000 (22:07 +0100)
Use a new table beds.bed_position to check if a bed is in use.

game_api.txt
mods/beds/api.lua
mods/beds/functions.lua
mods/beds/init.lua

index 86938f26efc0c5ed41faf8d36699e2e1fa83caf0..90594e8fdf376c611aa94e0a9964435ffc694b76 100644 (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
index 9349545a972b421bf63a02a4a175c662c9626b6c..ef07fc00740adddc5e1477667767da585595c627 100644 (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")
index 099f41feade9a50d2a96b8a8129dad75088e4858..88ae31e712a81de01b905689336da508753fac7d 100644 (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
index 8b25890177d71804ea74af0e2e78074080760eef..6c4e0819b7742a864ac595ef3b480cfc5764360d 100644 (file)
@@ -1,5 +1,6 @@
 beds = {}
 beds.player = {}
+beds.bed_position = {}
 beds.pos = {}
 beds.spawn = {}