Add on_break callback

This commit is contained in:
Elias Fleckenstein 2021-04-29 11:40:09 +02:00
parent 199488cc74
commit ec08032b62
2 changed files with 14 additions and 4 deletions

View File

@ -92,6 +92,7 @@ function mcl_armor.register_set(def)
local groups = def.groups or {} local groups = def.groups or {}
local on_equip_callbacks = def.on_equip_callbacks or {} local on_equip_callbacks = def.on_equip_callbacks or {}
local on_unequip_callbacks = def.on_unequip_callbacks or {} local on_unequip_callbacks = def.on_unequip_callbacks or {}
local on_break_callbacks = def.on_break_callbacks or {}
local textures = def.textures or {} local textures = def.textures or {}
local previews = def.previews or {} local previews = def.previews or {}
local durabilities = def.durabilities or {} local durabilities = def.durabilities or {}
@ -125,6 +126,7 @@ function mcl_armor.register_set(def)
on_secondary_use = mcl_armor.equip_on_use, on_secondary_use = mcl_armor.equip_on_use,
_on_equip = on_equip_callbacks[name] or def.on_equip, _on_equip = on_equip_callbacks[name] or def.on_equip,
_on_unequip = on_unequip_callbacks[name] or def.on_unequip, _on_unequip = on_unequip_callbacks[name] or def.on_unequip,
_on_break = on_break_callbacks[name] or def.on_break,
_mcl_armor_element = name, _mcl_armor_element = name,
_mcl_armor_texture = textures[name] or modname .. "_" .. itemname .. ".png", _mcl_armor_texture = textures[name] or modname .. "_" .. itemname .. ".png",
_mcl_armor_preview = previews[name] or modname .. "_" .. itemname .. "_preview.png", _mcl_armor_preview = previews[name] or modname .. "_" .. itemname .. "_preview.png",

View File

@ -1,3 +1,12 @@
local function use_durability(obj, inv, index, stack, uses)
local def = stack:get_definition()
mcl_util.use_item_durability(stack, uses)
if stack:is_empty() and def and def._on_break then
stack = def._on_break(obj) or stack
end
inv:set_stack("armor", index, stack)
end
mcl_damage.register_modifier(function(obj, damage, reason) mcl_damage.register_modifier(function(obj, damage, reason)
local flags = reason.flags local flags = reason.flags
@ -28,8 +37,7 @@ mcl_damage.register_modifier(function(obj, damage, reason)
points = points + minetest.get_item_group(itemname, "mcl_armor_points") points = points + minetest.get_item_group(itemname, "mcl_armor_points")
toughness = toughness + minetest.get_item_group(itemname, "mcl_armor_toughness") toughness = toughness + minetest.get_item_group(itemname, "mcl_armor_toughness")
mcl_util.use_item_durability(itemstack, uses) use_durability(obj, inv, element.index, itemstack, uses)
inv:set_stack("armor", element.index, itemstack)
end end
if not flags.bypasses_magic then if not flags.bypasses_magic then
@ -84,8 +92,8 @@ mcl_damage.register_modifier(function(obj, damage, reason)
mcl_util.deal_damage(reason.source, thorns_damage, {type = "thorns", direct = obj}) mcl_util.deal_damage(reason.source, thorns_damage, {type = "thorns", direct = obj})
local thorns_item = thorns_pieces[math.random(#thorns_pieces)] local thorns_item = thorns_pieces[math.random(#thorns_pieces)]
mcl_util.use_item_durability(thorns_item.itemstack, 2)
inv:set_stack("armor", thorns_item.index, thorns_item.itemstack) use_durability(obj, inv, thorns_item.index, thorns_item.itemstack, 2)
end end
mcl_armor.update(obj) mcl_armor.update(obj)