From 95b0a495bcc5e6865033a45bca12c8f812e1c4fd Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Mon, 1 Mar 2021 10:51:57 +0100 Subject: [PATCH] Fix #1152 --- mods/ENTITIES/mobs_mc/villager.lua | 5 ++++- mods/ITEMS/mcl_enchanting/engine.lua | 24 ++++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/mods/ENTITIES/mobs_mc/villager.lua b/mods/ENTITIES/mobs_mc/villager.lua index 7e6921f90e..0021a1adba 100644 --- a/mods/ENTITIES/mobs_mc/villager.lua +++ b/mods/ENTITIES/mobs_mc/villager.lua @@ -412,6 +412,7 @@ local init_trades = function(self, inv) offered_stack = mcl_enchanting.get_uniform_randomly_enchanted_book({"soul_speed"}) else mcl_enchanting.enchant_randomly(offered_stack, math.random(5, 19), false, false, true) + mcl_enchanting.unload_enchantments(offered_stack) end end @@ -457,7 +458,9 @@ local set_trade = function(trader, player, inv, concrete_tradenum) player_tradenum[name] = concrete_tradenum local trade = trades[concrete_tradenum] inv:set_stack("wanted", 1, ItemStack(trade.wanted[1])) - inv:set_stack("offered", 1, ItemStack(trade.offered)) + local offered = ItemStack(trade.offered) + mcl_enchanting.load_enchantments(offered) + inv:set_stack("offered", 1, offered) if trade.wanted[2] then local wanted2 = ItemStack(trade.wanted[2]) inv:set_stack("wanted", 2, wanted2) diff --git a/mods/ITEMS/mcl_enchanting/engine.lua b/mods/ITEMS/mcl_enchanting/engine.lua index 5bb5884cee..e6f98fadac 100644 --- a/mods/ITEMS/mcl_enchanting/engine.lua +++ b/mods/ITEMS/mcl_enchanting/engine.lua @@ -9,14 +9,21 @@ function mcl_enchanting.get_enchantments(itemstack) return minetest.deserialize(itemstack:get_meta():get_string("mcl_enchanting:enchantments")) or {} end -function mcl_enchanting.set_enchantments(itemstack, enchantments) - itemstack:get_meta():set_string("mcl_enchanting:enchantments", minetest.serialize(enchantments)) +function mcl_enchanting.unload_enchantments(itemstack) local itemdef = itemstack:get_definition() + if itemdef.tool_capabilities then + itemstack:get_meta():set_tool_capabilities(itemdef.tool_capabilities) + end + local meta = itemstack:get_meta() + if meta:get_string("name") == "" then + meta:set_string("description", "") + end +end + +function mcl_enchanting.load_enchantments(itemstack, enchantments) if not mcl_enchanting.is_book(itemstack:get_name()) then - if itemdef.tool_capabilities then - itemstack:get_meta():set_tool_capabilities(itemdef.tool_capabilities) - end - for enchantment, level in pairs(enchantments) do + mcl_enchanting.unload_enchantments(itemstack) + for enchantment, level in pairs(enchantments or mcl_enchanting.get_enchantments(itemstack)) do local enchantment_def = mcl_enchanting.enchantments[enchantment] if enchantment_def.on_enchant then enchantment_def.on_enchant(itemstack, level) @@ -26,6 +33,11 @@ function mcl_enchanting.set_enchantments(itemstack, enchantments) tt.reload_itemstack_description(itemstack) end +function mcl_enchanting.set_enchantments(itemstack, enchantments) + itemstack:get_meta():set_string("mcl_enchanting:enchantments", minetest.serialize(enchantments)) + mcl_enchanting.load_enchantments(itemstack) +end + function mcl_enchanting.get_enchantment(itemstack, enchantment) return mcl_enchanting.get_enchantments(itemstack)[enchantment] or 0 end