From 2d7c5aee1fbc24af1bbcf1d7f87721fe41306bed Mon Sep 17 00:00:00 2001 From: Jordan Irwin Date: Mon, 12 Jul 2021 15:44:43 -0700 Subject: [PATCH] Add support for unregistering ores --- TODO.txt | 2 +- api.lua | 37 +++++++++++++++++++++++++++++++++++++ changelog.txt | 1 + chat.lua | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) diff --git a/TODO.txt b/TODO.txt index 5fff10e..6d8bd25 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,5 +1,5 @@ TODO: - add localization support -- add support for unregistering/replacing ores +- add support for unregistering ores via world path file - change API method names to "register_[replace/remove]_*" diff --git a/api.lua b/api.lua index 1fe64f8..5fabbf0 100644 --- a/api.lua +++ b/api.lua @@ -62,3 +62,40 @@ function cleaner.replace_node(src, tgt) replace_nodes[src] = tgt end + + +--- Unsafe methods. +-- +-- Enabled with `cleaner.unsafe` setting. +-- +-- @section unsafe + + +if cleaner.unsafe then + --- Removes an ore definition. + -- + -- @tparam string src Ore technical name. + function cleaner.remove_ore(src) + local remove_ids = {} + local total_removed = 0 + local registered = false + + for id, def in pairs(core.registered_ores) do + if def.ore == src then + table.insert(remove_ids, id) + registered = true + end + end + + for _, id in ipairs(remove_ids) do + core.registered_ores[id] = nil + if core.registered_ores[id] then + cleaner.log("error", "unable to unregister ore " .. id) + else + total_removed = total_removed + 1 + end + end + + return registered, total_removed + end +end diff --git a/changelog.txt b/changelog.txt index 4779fb4..90a5055 100644 --- a/changelog.txt +++ b/changelog.txt @@ -9,6 +9,7 @@ v1.2 - replace_node - find_unknown_nodes - added setting for enabling "unsafe" methods & commands +- added support for unregistering ores v1.1 ---- diff --git a/chat.lua b/chat.lua index 4f20209..7873c63 100644 --- a/chat.lua +++ b/chat.lua @@ -239,3 +239,40 @@ core.register_chatcommand("find_unknown_nodes", { return true end, }) + + +--- Unsafe commands. +-- +-- Enabled with `cleaner.unsafe` setting. +-- +-- @section unsafe + + +if cleaner.unsafe then + --- Registers an ore to be removed. + -- + -- @chatcmd remove_ore + -- @param ore Ore technical name. + core.register_chatcommand("remove_ore", { + privs = {server=true}, + description = "Remove an ore from game.", + params = "", + func = function(name, param) + if param:find(" ") then + return false, "Too many parameters." + end + + core.after(0, function() + local registered, total_removed = cleaner.remove_ore(param) + + if not registered then + core.chat_send_player(name, "Ore \"" .. param .. "\" not found, not unregistering.") + else + core.chat_send_player(name, "Unregistered " .. total_removed .. " ores (this will be undone after server restart).") + end + end) + + return true + end + }) +end