2017-08-30 15:15:03 -07:00
|
|
|
--[[ Cleaner mod
|
2021-05-18 17:46:18 -07:00
|
|
|
License: MIT
|
2017-08-30 14:38:48 -07:00
|
|
|
]]
|
|
|
|
|
|
|
|
|
2017-08-30 15:15:03 -07:00
|
|
|
cleaner = {}
|
|
|
|
cleaner.name = core.get_current_modname()
|
2017-08-30 14:38:48 -07:00
|
|
|
|
2021-05-18 17:57:07 -07:00
|
|
|
local debug = core.settings:get_bool("enable_debug_mods")
|
2017-08-30 14:38:48 -07:00
|
|
|
|
|
|
|
local function log(level, msg)
|
2021-05-18 17:57:07 -07:00
|
|
|
core.log(level, "[" .. cleaner.name .. "] " .. msg)
|
2017-08-30 14:38:48 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local function logDebug(msg)
|
|
|
|
if debug then
|
2021-05-18 17:57:07 -07:00
|
|
|
core.log("DEBUG: [" .. cleaner.name .. "] " .. msg)
|
2017-08-30 14:38:48 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-18 15:06:02 -07:00
|
|
|
|
2017-08-30 15:26:59 -07:00
|
|
|
-- ENTITIES
|
|
|
|
|
2017-08-30 14:57:18 -07:00
|
|
|
local old_entities = {}
|
2017-08-30 14:39:12 -07:00
|
|
|
|
|
|
|
-- Populate entities list from file in world path
|
|
|
|
local e_list = nil
|
2021-05-18 17:57:07 -07:00
|
|
|
local e_path = core.get_worldpath() .. "/clean_entities.txt"
|
|
|
|
local e_file = io.open(e_path, "r")
|
2017-08-30 14:39:12 -07:00
|
|
|
if e_file then
|
2021-05-18 17:57:07 -07:00
|
|
|
e_list = e_file:read("*a")
|
2017-08-30 14:39:12 -07:00
|
|
|
e_file:close()
|
|
|
|
else
|
|
|
|
-- Create empty file
|
2021-05-18 17:57:07 -07:00
|
|
|
e_file = io.open(e_path, "w")
|
2017-08-30 14:39:12 -07:00
|
|
|
if e_file then
|
|
|
|
e_file:close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if e_list then
|
2021-05-18 17:57:07 -07:00
|
|
|
logDebug("Loading entities to clean from file ...")
|
|
|
|
|
|
|
|
e_list = string.split(e_list, "\n")
|
2017-08-30 14:39:12 -07:00
|
|
|
for _, entity_name in ipairs(e_list) do
|
|
|
|
table.insert(old_entities, entity_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-30 14:14:55 -07:00
|
|
|
for _, entity_name in ipairs(old_entities) do
|
2021-05-18 17:57:07 -07:00
|
|
|
logDebug("Cleaning entity: " .. entity_name)
|
|
|
|
|
|
|
|
core.register_entity(":" .. entity_name, {
|
2017-05-18 15:06:02 -07:00
|
|
|
on_activate = function(self, staticdata)
|
|
|
|
self.object:remove()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
2017-08-30 15:26:59 -07:00
|
|
|
|
|
|
|
|
|
|
|
-- NODES
|
|
|
|
|
|
|
|
local old_nodes = {}
|
|
|
|
|
|
|
|
-- Populate nodes list from file in world path
|
|
|
|
local n_list = nil
|
2021-05-18 17:57:07 -07:00
|
|
|
local n_path = core.get_worldpath() .. "/clean_nodes.txt"
|
|
|
|
local n_file = io.open(n_path, "r")
|
2017-08-30 15:26:59 -07:00
|
|
|
if n_file then
|
2021-05-18 17:57:07 -07:00
|
|
|
n_list = n_file:read("*a")
|
2017-08-30 15:26:59 -07:00
|
|
|
n_file:close()
|
|
|
|
else
|
|
|
|
-- Create empty file
|
2021-05-18 17:57:07 -07:00
|
|
|
n_file = io.open(n_path, "w")
|
2017-08-30 15:26:59 -07:00
|
|
|
if n_file then
|
|
|
|
n_file:close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if n_list then
|
2021-05-18 17:57:07 -07:00
|
|
|
logDebug("Loading nodes to clean from file ...")
|
|
|
|
|
|
|
|
n_list = string.split(n_list, "\n")
|
2017-08-30 15:26:59 -07:00
|
|
|
for _, node_name in ipairs(n_list) do
|
|
|
|
table.insert(old_nodes, node_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, node_name in ipairs(old_nodes) do
|
2021-05-18 17:57:07 -07:00
|
|
|
logDebug("Cleaning node: " .. node_name)
|
|
|
|
|
|
|
|
core.register_node(":" .. node_name, {
|
2017-08-30 15:26:59 -07:00
|
|
|
groups = {old=1},
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
core.register_abm({
|
2021-05-18 17:57:07 -07:00
|
|
|
nodenames = {"group:old"},
|
2017-08-30 15:26:59 -07:00
|
|
|
interval = 1,
|
|
|
|
chance = 1,
|
|
|
|
action = function(pos, node)
|
|
|
|
core.remove_node(pos)
|
|
|
|
end,
|
|
|
|
})
|