mirror of
https://codeberg.org/AntumLuanti/mod-cleaner.git
synced 2025-03-21 15:41:22 +00:00
Support replacing nodes
This commit is contained in:
parent
8a7fc3ca04
commit
35f409c5c0
3 changed files with 26 additions and 10 deletions
|
@ -2,6 +2,7 @@
|
||||||
0.5
|
0.5
|
||||||
- changed license to MIT
|
- changed license to MIT
|
||||||
- "clean_entities" & "clean_nodes" files now use json format
|
- "clean_entities" & "clean_nodes" files now use json format
|
||||||
|
- nodes can be replaced with other nodes
|
||||||
|
|
||||||
|
|
||||||
0.4
|
0.4
|
||||||
|
|
|
@ -10,9 +10,7 @@ if e_file then
|
||||||
local data_in = core.parse_json(e_file:read("*a"))
|
local data_in = core.parse_json(e_file:read("*a"))
|
||||||
e_file:close()
|
e_file:close()
|
||||||
if data_in then
|
if data_in then
|
||||||
for _, e in ipairs(data_in.remove) do
|
e_list = data_in
|
||||||
table.insert(e_list.remove, e)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
31
nodes.lua
31
nodes.lua
|
@ -2,7 +2,7 @@
|
||||||
local misc = dofile(cleaner.modpath .. "/misc_functions.lua")
|
local misc = dofile(cleaner.modpath .. "/misc_functions.lua")
|
||||||
|
|
||||||
-- populate nodes list from file in world path
|
-- populate nodes list from file in world path
|
||||||
local n_list = {remove={}}
|
local n_list = {remove={}, replace={}}
|
||||||
local n_path = core.get_worldpath() .. "/clean_nodes.json"
|
local n_path = core.get_worldpath() .. "/clean_nodes.json"
|
||||||
local n_file = io.open(n_path, "r")
|
local n_file = io.open(n_path, "r")
|
||||||
|
|
||||||
|
@ -10,9 +10,7 @@ if n_file then
|
||||||
local data_in = core.parse_json(n_file:read("*a"))
|
local data_in = core.parse_json(n_file:read("*a"))
|
||||||
n_file:close()
|
n_file:close()
|
||||||
if data_in then
|
if data_in then
|
||||||
for _, n in ipairs(data_in.remove) do
|
n_list = data_in
|
||||||
table.insert(n_list.remove, n)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -40,7 +38,12 @@ n_list.remove = misc.clean_duplicates(n_list.remove)
|
||||||
-- update json file with any changes
|
-- update json file with any changes
|
||||||
n_file = io.open(n_path, "w")
|
n_file = io.open(n_path, "w")
|
||||||
if n_file then
|
if n_file then
|
||||||
local data_out = core.write_json(n_list, true):gsub("\"remove\" : null", "\"remove\" : []")
|
local data_out = core.write_json(n_list, true)
|
||||||
|
|
||||||
|
-- FIXME: how to do this with a single regex?
|
||||||
|
data_out = data_out:gsub("\"remove\" : null", "\"remove\" : []")
|
||||||
|
data_out = data_out:gsub("\"replace\" : null", "\"replace\" : {}")
|
||||||
|
|
||||||
n_file:write(data_out)
|
n_file:write(data_out)
|
||||||
n_file:close()
|
n_file:close()
|
||||||
end
|
end
|
||||||
|
@ -49,15 +52,29 @@ for _, n in ipairs(n_list.remove) do
|
||||||
cleaner.log("debug", "Cleaning node: " .. n)
|
cleaner.log("debug", "Cleaning node: " .. n)
|
||||||
|
|
||||||
core.register_node(":" .. n, {
|
core.register_node(":" .. n, {
|
||||||
groups = {old=1},
|
groups = {to_remove=1},
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
core.register_abm({
|
core.register_abm({
|
||||||
nodenames = {"group:old"},
|
nodenames = {"group:to_remove"},
|
||||||
interval = 1,
|
interval = 1,
|
||||||
chance = 1,
|
chance = 1,
|
||||||
action = function(pos, node)
|
action = function(pos, node)
|
||||||
core.remove_node(pos)
|
core.remove_node(pos)
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
for n_old, n_new in pairs(n_list.replace) do
|
||||||
|
cleaner.log("debug", "Replacing node \"" .. n_old .. "\" with \"" .. n_new .. "\"")
|
||||||
|
|
||||||
|
core.register_abm({
|
||||||
|
nodenames = {n_old},
|
||||||
|
interval = 1,
|
||||||
|
chance = 1,
|
||||||
|
action = function(pos, node)
|
||||||
|
core.remove_node(pos)
|
||||||
|
core.place_node(pos, n_new)
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue