From af20777daf9ebcfc75ab3182b508cb2a7f2c2ef5 Mon Sep 17 00:00:00 2001 From: Aaron Suen Date: Mon, 20 Nov 2023 16:40:16 -0500 Subject: [PATCH] Support alias export --- README.md | 12 ++++++++++-- TODO | 2 -- commands.lua | 5 +++++ exportall.lua | 15 ++++++++++++++- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 32ec375..e9e7298 100644 --- a/README.md +++ b/README.md @@ -23,15 +23,23 @@ You will get a folder in your worldpath containing a skeletal mod: You will need to provide your own `init.lua`, `mod.conf` and other infrastructure, but the exported definitions are kept in a separate file so you can safely overwrite it later (e.g. if you add definitions) without destroying your custom logic. -`exported.lua` takes a register_item-like function as a file-level parameter. This function will receive a single parameter with the definition table; it does NOT get an item name. There is a `_raw_name` key inside the definition from which you will need to derive your own name in a manner you deem appropriate (you have an opportunity to customize before registration here). +`exported.lua` takes a register_item-like function as a file-level parameter. + +- This function will receive a single parameter with the definition table; it does NOT get an item name. +- There is a `_raw_name` key inside the definition from which you will need to derive your own name in a manner you deem appropriate (you have an opportunity to customize before registration here). +- Aliases are exported using the same format, but they will only have an `_alias_to` that will point to the _raw_name of the node the alias is pointing to. Example: ```lua local modname = minetest.get_current_modname() loadfile(minetest.get_modpath(modname) .. "/exported.lua")(function(def) local myname = modname .. ":" .. def._raw_name:gsub(":", "__") + if def._alias_to then + local myalias = modname .. ":" .. def._alias_to:gsub(":", "__") + return minetest.register_alias(myname, myalias) + end def._raw_name = nil - minetest.register_item(myname, def) + return minetest.register_item(myname, def) end) ``` diff --git a/TODO b/TODO index f57332a..c4c9991 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,5 @@ ------------------------------------------------------------------------ -- Support aliases - - Handle tables with mixed key types, e.g. some strings, some numbers. - Right now it crashes trying to sort keys when it can't compare them. - Possible approaches: diff --git a/commands.lua b/commands.lua index 51ef292..b9683f2 100644 --- a/commands.lua +++ b/commands.lua @@ -81,6 +81,11 @@ do configdb.items[k] = setto end end + for k in pairs(minetest.registered_aliases) do + if string_match(k, param) then + configdb.items[k] = setto + end + end end) if not ok then return false, string_gsub(err, ".*:%d+:%s*", "") end return save_export_report() diff --git a/exportall.lua b/exportall.lua index 4c7b6e1..d76a633 100644 --- a/exportall.lua +++ b/exportall.lua @@ -93,7 +93,7 @@ local function exportall() local skipped = {} do local props = configdb.props or nodekeys - for k, v in pairs(minetest.registered_items) do + local function process(k, v) if not blocked[k] and configdb.items[k] then local t = {} for k2, v2 in pairs(nodekeys) do @@ -102,6 +102,7 @@ local function exportall() end end t._raw_name = k + t._alias_to = v._alias_to if custom then local r = custom(t, k, v) if r == false then @@ -118,6 +119,18 @@ local function exportall() filtered[k] = t end end + for k, v in pairs(minetest.registered_items) do + process(k, v) + end + for k, v in pairs(minetest.registered_aliases) do + local def = minetest.registered_items[v] + if def then + process(k, {_alias_to = v}) + if not filtered[v] then + process(v, def) + end + end + end end local missing_defs = {} for k in pairs(configdb.items) do