Implement vl_legacy deprecated function and item conversion APIs

This commit is contained in:
teknomunk 2024-06-18 20:21:12 -05:00
parent 5987ab7efe
commit e0539b354a
5 changed files with 79 additions and 7 deletions

View File

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

View File

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

View File

@ -0,0 +1,3 @@
name = vl_legacy
author = teknomunk
description = API to ease conversion of items, deprecated function logging and similar functions

View File

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

View File

@ -1,3 +1,3 @@
name = mcl_entity_invs
author = cora
depends = mcl_formspec
depends = mcl_formspec, vl_legacy