2021-05-18 18:11:24 -07:00
|
|
|
|
2021-05-18 19:57:56 -07:00
|
|
|
local function clean_duplicates(t)
|
|
|
|
local tmp = {}
|
|
|
|
for _, v in ipairs(t) do
|
|
|
|
tmp[v] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
t = {}
|
|
|
|
for k in pairs(tmp) do
|
|
|
|
table.insert(t, k)
|
|
|
|
end
|
|
|
|
|
|
|
|
return t
|
|
|
|
end
|
2021-05-18 18:11:24 -07:00
|
|
|
|
|
|
|
-- Populate entities list from file in world path
|
2021-05-18 19:57:56 -07:00
|
|
|
local e_list = {remove={}}
|
|
|
|
local e_path = core.get_worldpath() .. "/clean_entities.json"
|
2021-05-18 18:11:24 -07:00
|
|
|
local e_file = io.open(e_path, "r")
|
|
|
|
|
|
|
|
if e_file then
|
2021-05-18 19:57:56 -07:00
|
|
|
local data_in = core.parse_json(e_file:read("*a"))
|
2021-05-18 18:11:24 -07:00
|
|
|
e_file:close()
|
2021-05-18 19:57:56 -07:00
|
|
|
if data_in then
|
|
|
|
for _, e in ipairs(data_in.remove) do
|
|
|
|
table.insert(e_list.remove, e)
|
|
|
|
end
|
2021-05-18 18:11:24 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-18 19:57:56 -07:00
|
|
|
-- backward compat
|
|
|
|
local e_path_old = core.get_worldpath() .. "/clean_entities.txt"
|
|
|
|
e_file = io.open(e_path_old, "r")
|
|
|
|
|
|
|
|
if e_file then
|
|
|
|
cleaner.log("action", "found deprecated clean_entities.txt, converting to json")
|
2021-05-18 18:11:24 -07:00
|
|
|
|
2021-05-18 19:57:56 -07:00
|
|
|
local data = string.split(e_file:read("*a"), "\n")
|
|
|
|
for _, e in ipairs(data) do
|
|
|
|
e = e:trim()
|
|
|
|
if e ~= "" and e:sub(1, 1) ~= "#" then
|
|
|
|
table.insert(e_list.remove, e)
|
|
|
|
end
|
2021-05-18 18:11:24 -07:00
|
|
|
end
|
2021-05-18 19:57:56 -07:00
|
|
|
|
|
|
|
e_file:close()
|
|
|
|
os.rename(e_path_old, e_path_old .. ".bak") -- don't read deprecated file again
|
2021-05-18 18:11:24 -07:00
|
|
|
end
|
|
|
|
|
2021-05-18 19:57:56 -07:00
|
|
|
e_list.remove = clean_duplicates(e_list.remove)
|
|
|
|
|
|
|
|
-- update json file with any changes
|
|
|
|
e_file = io.open(e_path, "w")
|
|
|
|
if e_file then
|
|
|
|
local data = core.write_json(e_list, true):gsub("\"remove\" : null", "\"remove\" : []")
|
|
|
|
e_file:write(data)
|
|
|
|
e_file:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
for _, e in ipairs(e_list.remove) do
|
|
|
|
cleaner.log("debug", "Cleaning entity: " .. e)
|
2021-05-18 18:11:24 -07:00
|
|
|
|
2021-05-18 19:57:56 -07:00
|
|
|
core.register_entity(":" .. e, {
|
2021-05-18 18:11:24 -07:00
|
|
|
on_activate = function(self, staticdata)
|
|
|
|
self.object:remove()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|