From 727d7be6abfcc9dd663a7c49e14cc26f5d632a70 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Wed, 14 Apr 2021 20:39:05 +0200 Subject: [PATCH] Call on_equip and on_unequip everytime needed --- mods/ITEMS/mcl_armor/api.lua | 23 +++++++++++++++++++---- mods/ITEMS/mcl_armor/player.lua | 13 +++++++++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/mods/ITEMS/mcl_armor/api.lua b/mods/ITEMS/mcl_armor/api.lua index 3f0641a54a..a5d1ac0e50 100644 --- a/mods/ITEMS/mcl_armor/api.lua +++ b/mods/ITEMS/mcl_armor/api.lua @@ -18,6 +18,24 @@ function mcl_armor.play_equip_sound(stack, obj, pos, unequip) end end +function mcl_armor.on_equip(itemstack, obj) + local def = itemstack:get_definition() + mcl_armor.play_equip_sound(itemstack, obj) + if def._on_equip then + def._on_equip(obj, itemstack) + end + mcl_armor.update(obj) +end + +function mcl_armor.on_unequip(itemstack, obj) + local def = itemstack:get_definition() + mcl_armor.play_equip_sound(itemstack, obj, nil, true) + if def._on_unequip then + def._on_unequip(obj, itemstack) + end + mcl_armor.update(obj) +end + function mcl_armor.equip(itemstack, obj) local def = itemstack:get_definition() local element = mcl_armor.elements[def._mcl_armor_element or ""] @@ -27,10 +45,7 @@ function mcl_armor.equip(itemstack, obj) if inv:get_stack("armor", element.index):is_empty() then local equipping_item = itemstack:take_item() inv:set_stack("armor", element.index, equipping_item) - if def._on_equip then - def._on_equip(equipping_item) - end - mcl_armor.update(obj) + mcl_armor.on_equip(equipping_item, obj) end end diff --git a/mods/ITEMS/mcl_armor/player.lua b/mods/ITEMS/mcl_armor/player.lua index 92494b88fe..651f8f7765 100644 --- a/mods/ITEMS/mcl_armor/player.lua +++ b/mods/ITEMS/mcl_armor/player.lua @@ -122,10 +122,19 @@ minetest.register_allow_player_inventory_action(function(player, action, invento end end) --- ToDo: Call unequip callbacks & play uneqip sound minetest.register_on_player_inventory_action(function(player, action, inventory, inventory_info) if is_armor_action(inventory_info) then - mcl_armor.update(player) + if action == "put" then + mcl_armor.on_equip(inventory_info.stack, player) + elseif action == "take" then + mcl_armor.on_unequip(inventory_info.stack, player) + else + if inventory_info.to_list == "armor" then + mcl_armor.on_equip(inventory:get_stack(inventory_info.to_list, inventory_info.to_index), player) + elseif inventory_info.from_list == "armor" then + mcl_armor.on_unequip(inventory:get_stack(inventory_info.to_list, inventory_info.to_index), player) + end + end end end)