2021-05-18 18:11:24 -07:00
|
|
|
|
2021-05-18 20:11:38 -07:00
|
|
|
local misc = dofile(cleaner.modpath .. "/misc_functions.lua")
|
2021-05-18 18:11:24 -07:00
|
|
|
|
2021-05-18 20:11:38 -07:00
|
|
|
-- populate nodes list from file in world path
|
2021-05-18 20:50:14 -07:00
|
|
|
local n_list = {remove={}, replace={}}
|
2021-05-18 20:11:38 -07:00
|
|
|
local n_path = core.get_worldpath() .. "/clean_nodes.json"
|
2021-05-18 18:11:24 -07:00
|
|
|
local n_file = io.open(n_path, "r")
|
|
|
|
|
|
|
|
if n_file then
|
2021-05-18 20:11:38 -07:00
|
|
|
local data_in = core.parse_json(n_file:read("*a"))
|
2021-05-18 18:11:24 -07:00
|
|
|
n_file:close()
|
2021-05-18 20:11:38 -07:00
|
|
|
if data_in then
|
2021-05-18 20:50:14 -07:00
|
|
|
n_list = data_in
|
2021-05-18 18:11:24 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-18 20:11:38 -07:00
|
|
|
-- backward compat
|
|
|
|
local n_path_old = core.get_worldpath() .. "/clean_nodes.txt"
|
|
|
|
n_file = io.open(n_path_old, "r")
|
2021-05-18 18:11:24 -07:00
|
|
|
|
2021-05-18 20:11:38 -07:00
|
|
|
if n_file then
|
|
|
|
cleaner.log("action", "found deprecated clean_nodes.txt, converting to json")
|
|
|
|
|
|
|
|
local data_in = string.split(n_file:read("*a"), "\n")
|
|
|
|
for _, e in ipairs(data_in) do
|
|
|
|
e = e:trim()
|
|
|
|
if e ~= "" and e:sub(1, 1) ~= "#" then
|
|
|
|
table.insert(n_list.remove, e)
|
|
|
|
end
|
2021-05-18 18:11:24 -07:00
|
|
|
end
|
2021-05-18 20:11:38 -07:00
|
|
|
|
|
|
|
n_file:close()
|
|
|
|
os.rename(n_path_old, n_path_old .. ".bak") -- don't read deprecated file again
|
|
|
|
end
|
|
|
|
|
|
|
|
n_list.remove = misc.clean_duplicates(n_list.remove)
|
|
|
|
|
|
|
|
-- update json file with any changes
|
|
|
|
n_file = io.open(n_path, "w")
|
|
|
|
if n_file then
|
2021-05-18 20:50:14 -07:00
|
|
|
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\" : {}")
|
|
|
|
|
2021-05-18 20:11:38 -07:00
|
|
|
n_file:write(data_out)
|
|
|
|
n_file:close()
|
2021-05-18 18:11:24 -07:00
|
|
|
end
|
|
|
|
|
2021-05-18 20:11:38 -07:00
|
|
|
for _, n in ipairs(n_list.remove) do
|
|
|
|
cleaner.log("debug", "Cleaning node: " .. n)
|
2021-05-18 18:11:24 -07:00
|
|
|
|
2021-05-18 20:11:38 -07:00
|
|
|
core.register_node(":" .. n, {
|
2021-05-18 20:50:14 -07:00
|
|
|
groups = {to_remove=1},
|
2021-05-18 18:11:24 -07:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
core.register_abm({
|
2021-05-18 20:50:14 -07:00
|
|
|
nodenames = {"group:to_remove"},
|
2021-05-18 18:11:24 -07:00
|
|
|
interval = 1,
|
|
|
|
chance = 1,
|
|
|
|
action = function(pos, node)
|
2021-05-18 20:11:38 -07:00
|
|
|
core.remove_node(pos)
|
2021-05-18 18:11:24 -07:00
|
|
|
end,
|
|
|
|
})
|
2021-05-18 20:50:14 -07:00
|
|
|
|
|
|
|
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
|