From 4ff7445fc1da5b12dd3c6fa79182919cdfc11378 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Tue, 18 Jun 2024 20:21:12 -0500 Subject: [PATCH] Implement vl_legacy deprecated function and item conversion APIs --- mods/CORE/vl_legacy/API.md | 19 ++++++++++ mods/CORE/vl_legacy/init.lua | 52 ++++++++++++++++++++++++++ mods/CORE/vl_legacy/mod.conf | 3 ++ mods/ENTITIES/mcl_entity_invs/init.lua | 10 ++--- mods/ENTITIES/mcl_entity_invs/mod.conf | 2 +- 5 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 mods/CORE/vl_legacy/API.md create mode 100644 mods/CORE/vl_legacy/init.lua create mode 100644 mods/CORE/vl_legacy/mod.conf diff --git a/mods/CORE/vl_legacy/API.md b/mods/CORE/vl_legacy/API.md new file mode 100644 index 000000000..4bbc4658e --- /dev/null +++ b/mods/CORE/vl_legacy/API.md @@ -0,0 +1,19 @@ +# Legacy Code Support Functions + +## vl\_legacy.deprecated(description, replacement) + +Creates a wrapper than logs calls to deprecated function. + +Arguments: +* `description`: The text logged when the deprecated function is called. +* `replacement`: The function that should be called instead. This is invoked passing + along the parameters exactly as provided. + +## vl\_legacy.register\_item\_conversion + +Allows automatic conversion of items. + +Arguments: +* `old`: Itemstring to be converted +* `new`: New item string + diff --git a/mods/CORE/vl_legacy/init.lua b/mods/CORE/vl_legacy/init.lua new file mode 100644 index 000000000..7ac12067c --- /dev/null +++ b/mods/CORE/vl_legacy/init.lua @@ -0,0 +1,52 @@ +local mod = {} +vl_legacy = mod + +function mod.deprecated(description, func) + return function(...) + minetest.log("warning",description .. debug.traceback()) + return func(...) + end +end + +local item_conversions = {} +mod.registered_item_conversions = item_conversions + +function mod.register_item_conversion(old, new, func) + item_conversions[old] = {new, func} +end +function mod.convert_inventory_lists(lists) + for _,list in pairs(lists) do + for i = 1,#list do + local itemstack = list[i] + local conversion = item_conversions[itemstack:get_name()] + if conversion then + local new_name,func = conversion[1],conversion[2] + if func then + func(itemstack) + else + itemstack:set_name(new_name) + end + end + end + end +end +function mod.convert_inventory(inv) + local lists = inv:get_lists() + mod.convert_inventory_lists(lists) + inv:set_lists(lists) +end + +minetest.register_on_joinplayer(function(player) + mod.convert_inventory(player:get_inventory()) +end) + +minetest.register_lbm({ + name = "vl_legacy:convert_container_inventories", + nodenames = "group:containers", + run_at_every_load = true, + action = function(pos, node) + local meta = minetest.get_meta(pos) + mod.convert_inventory(meta:get_inventory()) + end +}) + diff --git a/mods/CORE/vl_legacy/mod.conf b/mods/CORE/vl_legacy/mod.conf new file mode 100644 index 000000000..11b6cd01b --- /dev/null +++ b/mods/CORE/vl_legacy/mod.conf @@ -0,0 +1,3 @@ +name = vl_legacy +author = teknomunk +description = API to ease conversion of items, deprecated function logging and similar functions diff --git a/mods/ENTITIES/mcl_entity_invs/init.lua b/mods/ENTITIES/mcl_entity_invs/init.lua index 601ec2019..ee9b50318 100644 --- a/mods/ENTITIES/mcl_entity_invs/init.lua +++ b/mods/ENTITIES/mcl_entity_invs/init.lua @@ -27,21 +27,19 @@ local inv_callbacks = { } function mcl_entity_invs.load_inv(ent,size) - mcl_log("load_inv") if not ent._inv_id then return end - mcl_log("load_inv 2") local inv = minetest.get_inventory({type="detached", name=ent._inv_id}) if not inv then - mcl_log("load_inv 3") inv = minetest.create_detached_inventory(ent._inv_id, inv_callbacks) inv:set_size("main", size) if ent._mcl_entity_invs_load_items then - inv:set_list("main",ent:_mcl_entity_invs_load_items()) + local lists = ent:_mcl_entity_invs_load_items() + vl_legacy.convert_inventory_lists(lists) + inv:set_list("main", lists) elseif ent._items then + vl_legacy.convert_inventory_lists(ent._items) inv:set_list("main",ent._items) end - else - mcl_log("load_inv 4") end return inv end diff --git a/mods/ENTITIES/mcl_entity_invs/mod.conf b/mods/ENTITIES/mcl_entity_invs/mod.conf index 8e94d6b1e..64f92aea9 100644 --- a/mods/ENTITIES/mcl_entity_invs/mod.conf +++ b/mods/ENTITIES/mcl_entity_invs/mod.conf @@ -1,3 +1,3 @@ name = mcl_entity_invs author = cora -depends = mcl_formspec +depends = mcl_formspec, vl_legacy