From 5692c15b4d19eab371a0daba2efa9f04c00b81b9 Mon Sep 17 00:00:00 2001 From: bell07 Date: Tue, 19 Jun 2018 23:07:01 +0200 Subject: [PATCH] Disallow digging a bed if in use Use a new table beds.bed_position to check if a bed is in use. --- game_api.txt | 1 + mods/beds/api.lua | 9 +++++++++ mods/beds/functions.lua | 11 +++++++++++ mods/beds/init.lua | 1 + 4 files changed, 22 insertions(+) diff --git a/game_api.txt b/game_api.txt index 86938f26..90594e8f 100644 --- a/game_api.txt +++ b/game_api.txt @@ -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 diff --git a/mods/beds/api.lua b/mods/beds/api.lua index 9349545a..ef07fc00 100644 --- a/mods/beds/api.lua +++ b/mods/beds/api.lua @@ -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") diff --git a/mods/beds/functions.lua b/mods/beds/functions.lua index 099f41fe..88ae31e7 100644 --- a/mods/beds/functions.lua +++ b/mods/beds/functions.lua @@ -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 diff --git a/mods/beds/init.lua b/mods/beds/init.lua index 8b258901..6c4e0819 100644 --- a/mods/beds/init.lua +++ b/mods/beds/init.lua @@ -1,5 +1,6 @@ beds = {} beds.player = {} +beds.bed_position = {} beds.pos = {} beds.spawn = {} -- 2.25.1