Support alias export

This commit is contained in:
Aaron Suen 2023-11-20 16:40:16 -05:00
parent 9d558bf66e
commit af20777daf
4 changed files with 29 additions and 5 deletions

View File

@ -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)
```

2
TODO
View File

@ -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:

View File

@ -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()

View File

@ -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