Call on_equip and on_unequip everytime needed

This commit is contained in:
Elias Fleckenstein 2021-04-14 20:39:05 +02:00
parent ec6d68322a
commit 727d7be6ab
2 changed files with 30 additions and 6 deletions

View File

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

View File

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