summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xlua/api.lua12
-rwxr-xr-xlua/helpers.lua14
-rwxr-xr-xlua/visual.lua13
3 files changed, 25 insertions, 14 deletions
diff --git a/lua/api.lua b/lua/api.lua
index dafb934..71af49b 100755
--- a/lua/api.lua
+++ b/lua/api.lua
@@ -59,20 +59,12 @@ function drawers.drawer_on_construct(pos)
i = i + 1
end
- drawers.spawn_visual(pos)
+ drawers.spawn_visuals(pos)
end
-- destruct drawer
function drawers.drawer_on_destruct(pos)
- local objs = core.get_objects_inside_radius(pos, 0.537)
- if not objs then return end
-
- for _, obj in pairs(objs) do
- if obj and obj:get_luaentity() and
- obj:get_luaentity().name == "drawers:visual" then
- obj:remove()
- end
- end
+ drawers.remove_visuals(pos)
end
-- drop all items
diff --git a/lua/helpers.lua b/lua/helpers.lua
index 3662da2..374cd95 100755
--- a/lua/helpers.lua
+++ b/lua/helpers.lua
@@ -66,7 +66,7 @@ function drawers.get_inv_image(name)
return texture
end
-function drawers.spawn_visual(pos)
+function drawers.spawn_visuals(pos)
local node = core.get_node(pos)
local ndef = core.registered_nodes[node.name]
local drawerType = ndef.groups.drawer
@@ -150,6 +150,18 @@ function drawers.spawn_visual(pos)
end
end
+function drawers.remove_visuals(pos)
+ local objs = core.get_objects_inside_radius(pos, 0.537)
+ if not objs then return end
+
+ for _, obj in pairs(objs) do
+ if obj and obj:get_luaentity() and
+ obj:get_luaentity().name == "drawers:visual" then
+ obj:remove()
+ end
+ end
+end
+
function drawers.randomize_pos(pos)
local rndpos = table.copy(pos)
local x = math.random(-50, 50) * 0.01
diff --git a/lua/visual.lua b/lua/visual.lua
index d2a0c36..3ddd20a 100755
--- a/lua/visual.lua
+++ b/lua/visual.lua
@@ -260,17 +260,24 @@ core.register_lbm({
nodenames = {"group:drawer"},
run_at_every_load = true,
action = function(pos, node)
+ local drawerType = core.registered_nodes[node.name].groups.drawer
+ local foundVisuals = 0
local objs = core.get_objects_inside_radius(pos, 0.537)
if objs then
for _, obj in pairs(objs) do
if obj and obj:get_luaentity() and
obj:get_luaentity().name == "drawers:visual" then
- return
+ foundVisuals = foundVisuals + 1
end
end
end
+ -- if all drawer visuals were found, return
+ if foundVisuals == drawerType then
+ return
+ end
- -- no visual found, create a new one
- drawers.spawn_visual(pos)
+ -- not enough visuals found, remove existing and create new ones
+ drawers.remove_visuals(pos)
+ drawers.spawn_visuals(pos)
end
})