From 2c0eee27fc3c91ddbcd675cc5b3829489d8179b1 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sun, 4 Feb 2018 05:52:10 +0100 Subject: [PATCH] Fix restore of banner desc after anvil rename --- API.md | 8 ++++++++ mods/ITEMS/mcl_anvils/init.lua | 12 ++++++++++-- mods/ITEMS/mcl_banners/init.lua | 13 +++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/API.md b/API.md index 19590ac2..0d6c9758 100644 --- a/API.md +++ b/API.md @@ -9,6 +9,14 @@ Mods mods in MineClone 2 follow a simple naming convention: Mods with the prefix ## Adding items ### Special fields +Items can have these fields: +* `_mcl_generate_description(itemstack)`: Required for any items which manipulate their + description in any way. This function takes an itemstack of its own type and must set + the proper advanced description for this itemstack. If you don't do this, anvils will + fail at properly restoring the description when their custom name gets cleared at an + anvil. + See `mcl_banners` for an example. + All nodes can have these fields: * `_mcl_hardness`: Hardness of the block, ranges from 0 to infinity (represented by -1). Determines digging times. Default: 0 diff --git a/mods/ITEMS/mcl_anvils/init.lua b/mods/ITEMS/mcl_anvils/init.lua index 902b3bed..c82c4c19 100644 --- a/mods/ITEMS/mcl_anvils/init.lua +++ b/mods/ITEMS/mcl_anvils/init.lua @@ -129,8 +129,16 @@ local function update_anvil_slots(meta) -- Don't rename if names are identical if new_name ~= old_name then -- Rename item - meta:set_string("description", new_name) - -- Double-save the name internally, too + if new_name == "" and name_item:get_definition()._mcl_generate_description then + -- _mcl_generate_description(itemstack): If defined, set custom item description of itemstack. + -- This function should be defined for items with an advanced description. + -- See mcl_banners for an example. + name_item:get_definition()._mcl_generate_description(name_item) + else + -- Set description + meta:set_string("description", new_name) + end + -- Save the raw name internally, too meta:set_string("name", new_name) new_output = name_item elseif just_rename then diff --git a/mods/ITEMS/mcl_banners/init.lua b/mods/ITEMS/mcl_banners/init.lua index 71070b6b..bf5f1531 100644 --- a/mods/ITEMS/mcl_banners/init.lua +++ b/mods/ITEMS/mcl_banners/init.lua @@ -302,6 +302,19 @@ for colorid, colortab in pairs(mcl_banners.colors) do return itemstack end, + + _mcl_generate_description = function(itemstack) + local meta = itemstack:get_meta() + local layers_raw = meta:get_string("layers") + if not layers_raw then + return nil + end + local layers = minetest.deserialize(layers_raw) + local desc = itemstack:get_definition().description + local newdesc = mcl_banners.make_advanced_banner_description(desc, layers) + meta:set_string("description", newdesc) + return newdesc + end, }) if minetest.get_modpath("mcl_core") and minetest.get_modpath("mcl_wool") then