Fix restore of banner desc after anvil rename

This commit is contained in:
Wuzzy 2018-02-04 05:52:10 +01:00
parent a7f0d8c82c
commit 2c0eee27fc
3 changed files with 31 additions and 2 deletions

8
API.md
View file

@ -9,6 +9,14 @@ Mods mods in MineClone 2 follow a simple naming convention: Mods with the prefix
## Adding items ## Adding items
### Special fields ### 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: 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 * `_mcl_hardness`: Hardness of the block, ranges from 0 to infinity (represented by -1). Determines digging times. Default: 0

View file

@ -129,8 +129,16 @@ local function update_anvil_slots(meta)
-- Don't rename if names are identical -- Don't rename if names are identical
if new_name ~= old_name then if new_name ~= old_name then
-- Rename item -- Rename item
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) meta:set_string("description", new_name)
-- Double-save the name internally, too end
-- Save the raw name internally, too
meta:set_string("name", new_name) meta:set_string("name", new_name)
new_output = name_item new_output = name_item
elseif just_rename then elseif just_rename then

View file

@ -302,6 +302,19 @@ for colorid, colortab in pairs(mcl_banners.colors) do
return itemstack return itemstack
end, 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 if minetest.get_modpath("mcl_core") and minetest.get_modpath("mcl_wool") then