From 0daee12f5706f398855111264cc64a0e0a0ad81b Mon Sep 17 00:00:00 2001 From: Jordan Irwin Date: Wed, 26 May 2021 20:54:43 -0700 Subject: [PATCH] Add method for cleaning/replacing items --- TODO.txt | 1 - changelog.txt | 1 + init.lua | 1 + items.lua | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 items.lua diff --git a/TODO.txt b/TODO.txt index dc3f7e3..9b8ef00 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,4 +1,3 @@ TODO: - allow nodes to be cleaned to be replaced by another node -- methods for cleaning/replacing items diff --git a/changelog.txt b/changelog.txt index 3ac980a..f0c1692 100644 --- a/changelog.txt +++ b/changelog.txt @@ -3,6 +3,7 @@ - changed license to MIT - "clean_entities" & "clean_nodes" files now use json format - nodes can be replaced with other nodes +- items can be replaced with other items (/clean_items.json file) 0.4 diff --git a/init.lua b/init.lua index 42e47c0..cde2fe8 100644 --- a/init.lua +++ b/init.lua @@ -34,6 +34,7 @@ end local scripts = { "entities", "nodes", + "items", } for _, script in ipairs(scripts) do diff --git a/items.lua b/items.lua new file mode 100644 index 0000000..47da345 --- /dev/null +++ b/items.lua @@ -0,0 +1,55 @@ + +local misc = dofile(cleaner.modpath .. "/misc_functions.lua") + +-- populate nodes list from file in world path +local i_list = {replace={}} +local i_path = core.get_worldpath() .. "/clean_items.json" +local i_file = io.open(i_path, "r") + +if i_file then + local data_in = core.parse_json(i_file:read("*a")) + i_file:close() + if data_in then + i_list = data_in + end +end + +-- update json file with any changes +i_file = io.open(i_path, "w") +if i_file then + local data_out = core.write_json(i_list, true) + + data_out = data_out:gsub("\"replace\" : null", "\"replace\" : {}") + + i_file:write(data_out) + i_file:close() +end + +-- register actions for after server startup +core.after(0, function() + for i_old, i_new in pairs(i_list.replace) do + cleaner.log("action", "replacing item \"" .. i_old .. "\" with \"" .. i_new .. "\"") + + if not core.registered_items[i_old] then + cleaner.log("info", "\"" .. i_old .. "\" not registered, not unregistering") + else + cleaner.log("warning", "overriding registered item \"" .. i_old .. "\"") + + core.unregister_item(i_old) + if core.registered_items[i_old] then + cleaner.log("error", "could not unregister \"" .. i_old .. "\"") + end + end + + if not core.registered_items[i_new] then + cleaner.log("warning", "adding alias for unregistered item \"" .. i_new .. "\"") + end + + core.register_alias(i_old, i_new) + if core.registered_aliases[i_old] == i_new then + cleaner.log("info", "registered alias \"" .. i_old .. "\" for \"" .. i_new .. "\"") + else + cleaner.log("error", "could not register alias \"" .. i_old .. "\" for \"" .. i_new .. "\"") + end + end +end)