Include stack index it armor item callbacks

This commit is contained in:
stujones11 2017-04-15 17:54:39 +01:00
parent 16b0ee2d9e
commit 065815eec1
4 changed files with 38 additions and 38 deletions

@ -107,7 +107,7 @@ armor:register_armor("mod_name:speed_boots", {
armor_groups = {fleshy=10, radiation=10}, armor_groups = {fleshy=10, radiation=10},
damage_groups = {cracky=3, snappy=3, choppy=3, crumbly=3, level=1}, damage_groups = {cracky=3, snappy=3, choppy=3, crumbly=3, level=1},
reciprocate_damage = true, reciprocate_damage = true,
on_destroy = function(player, stack) on_destroy = function(player, stack, index)
local pos = player:getpos() local pos = player:getpos()
if pos then if pos then
minetest.sound_play({ minetest.sound_play({
@ -140,10 +140,10 @@ Reciprocal tool damage will be done only by the first armor inventory item
Item Callbacks: Item Callbacks:
on_equip = func(player, stack) on_equip = func(player, stack, index)
on_unequip = func(player, stack) on_unequip = func(player, stack, index)
on_destroy = func(player, stack) on_destroy = func(player, stack, index)
on_damage = func(player, stack) on_damage = func(player, stack, index)
on_punch = func(player, hitter, time_from_last_punch, tool_capabilities) on_punch = func(player, hitter, time_from_last_punch, tool_capabilities)
Notes: Notes:
@ -157,9 +157,9 @@ When armor is destroyed `stack` will contain a copy of the previous stack.
Global Callbacks: Global Callbacks:
armor:register_on_update(func(player)) armor:register_on_update(func(player))
armor:register_on_equip(func(player, stack)) armor:register_on_equip(func(player, stack, index))
armor:register_on_unequip(func(player, stack)) armor:register_on_unequip(func(player, stack, index))
armor:register_on_destroy(func(player, stack)) armor:register_on_destroy(func(player, stack, index))
Global Callback Example: Global Callback Example:

@ -140,17 +140,17 @@ armor.register_on_destroy = function(self, func)
end end
end end
armor.run_callbacks = function(self, callback, player, stack) armor.run_callbacks = function(self, callback, player, stack, index)
if stack then if stack then
local def = stack:get_definition() or {} local def = stack:get_definition() or {}
if type(def[callback]) == "function" then if type(def[callback]) == "function" then
def[callback](player, stack) def[callback](player, stack, index)
end end
end end
local callbacks = self.registered_callbacks[callback] local callbacks = self.registered_callbacks[callback]
if callbacks then if callbacks then
for _, func in pairs(callbacks) do for _, func in pairs(callbacks) do
func(player, stack) func(player, stack, index)
end end
end end
end end
@ -180,12 +180,12 @@ armor.init_player_armor = function(self, player)
on_put = function(inv, listname, index, stack, player) on_put = function(inv, listname, index, stack, player)
player:get_inventory():set_stack(listname, index, stack) player:get_inventory():set_stack(listname, index, stack)
armor:set_player_armor(player) armor:set_player_armor(player)
armor:run_callbacks("on_equip", player, stack) armor:run_callbacks("on_equip", player, stack, index)
end, end,
on_take = function(inv, listname, index, stack, player) on_take = function(inv, listname, index, stack, player)
player:get_inventory():set_stack(listname, index, nil) player:get_inventory():set_stack(listname, index, nil)
armor:set_player_armor(player) armor:set_player_armor(player)
armor:run_callbacks("on_unequip", player, stack) armor:run_callbacks("on_unequip", player, stack, index)
end, end,
on_move = function(inv, from_list, from_index, to_list, to_index, count, player) on_move = function(inv, from_list, from_index, to_list, to_index, count, player)
local plaver_inv = player:get_inventory() local plaver_inv = player:get_inventory()
@ -220,7 +220,7 @@ armor.init_player_armor = function(self, player)
for i=1, 6 do for i=1, 6 do
local stack = player_inv:get_stack("armor", i) local stack = player_inv:get_stack("armor", i)
armor_inv:set_stack("armor", i, stack) armor_inv:set_stack("armor", i, stack)
self:run_callbacks("on_equip", player, stack) self:run_callbacks("on_equip", player, stack, i)
end end
self.def[name] = { self.def[name] = {
init_time = minetest.get_gametime(), init_time = minetest.get_gametime(),
@ -446,10 +446,10 @@ armor.punch = function(self, player, hitter, time_from_last_punch, tool_capabili
local old_stack = ItemStack(stack) local old_stack = ItemStack(stack)
stack:add_wear(use) stack:add_wear(use)
self:set_inventory_stack(player, i, stack) self:set_inventory_stack(player, i, stack)
self:run_callbacks("on_damage", player, stack) self:run_callbacks("on_damage", player, stack, i)
if stack:get_count() == 0 then if stack:get_count() == 0 then
self:run_callbacks("on_unequip", player, old_stack) self:run_callbacks("on_unequip", player, old_stack, i)
self:run_callbacks("on_destroy", player, old_stack) self:run_callbacks("on_destroy", player, old_stack, i)
self:set_player_armor(player) self:set_player_armor(player)
end end
end end

@ -164,7 +164,7 @@ if armor.config.drop == true or armor.config.destroy == true then
if stack:get_count() > 0 then if stack:get_count() > 0 then
table.insert(drop, stack) table.insert(drop, stack)
armor:set_inventory_stack(player, i, nil) armor:set_inventory_stack(player, i, nil)
armor:run_callbacks("on_unequip", player, stack) armor:run_callbacks("on_unequip", player, stack, i)
end end
end end
armor:set_player_armor(player) armor:set_player_armor(player)

@ -50,10 +50,10 @@ if armor.materials.wood then
armor_groups = {fleshy=5}, armor_groups = {fleshy=5},
damage_groups = {cracky=3, snappy=2, choppy=3, crumbly=2, level=1}, damage_groups = {cracky=3, snappy=2, choppy=3, crumbly=2, level=1},
reciprocate_damage = true, reciprocate_damage = true,
on_damage = function(player, stack) on_damage = function(player, stack, index)
play_sound_effect(player, "default_wood_footstep") play_sound_effect(player, "default_wood_footstep")
end, end,
on_destroy = function(player, stack) on_destroy = function(player, stack, index)
play_sound_effect(player, "default_wood_footstep") play_sound_effect(player, "default_wood_footstep")
end, end,
}) })
@ -64,10 +64,10 @@ if armor.materials.wood then
armor_groups = {fleshy=8}, armor_groups = {fleshy=8},
damage_groups = {cracky=3, snappy=2, choppy=3, crumbly=2, level=2}, damage_groups = {cracky=3, snappy=2, choppy=3, crumbly=2, level=2},
reciprocate_damage = true, reciprocate_damage = true,
on_damage = function(player, stack) on_damage = function(player, stack, index)
play_sound_effect(player, "default_dig_metal") play_sound_effect(player, "default_dig_metal")
end, end,
on_destroy = function(player, stack) on_destroy = function(player, stack, index)
play_sound_effect(player, "default_dug_metal") play_sound_effect(player, "default_dug_metal")
end, end,
}) })
@ -89,10 +89,10 @@ if armor.materials.cactus then
armor_groups = {fleshy=5}, armor_groups = {fleshy=5},
damage_groups = {cracky=3, snappy=3, choppy=2, crumbly=2, level=1}, damage_groups = {cracky=3, snappy=3, choppy=2, crumbly=2, level=1},
reciprocate_damage = true, reciprocate_damage = true,
on_damage = function(player, stack) on_damage = function(player, stack, index)
play_sound_effect(player, "default_wood_footstep") play_sound_effect(player, "default_wood_footstep")
end, end,
on_destroy = function(player, stack) on_destroy = function(player, stack, index)
play_sound_effect(player, "default_wood_footstep") play_sound_effect(player, "default_wood_footstep")
end, end,
}) })
@ -103,10 +103,10 @@ if armor.materials.cactus then
armor_groups = {fleshy=8}, armor_groups = {fleshy=8},
damage_groups = {cracky=3, snappy=3, choppy=2, crumbly=2, level=2}, damage_groups = {cracky=3, snappy=3, choppy=2, crumbly=2, level=2},
reciprocate_damage = true, reciprocate_damage = true,
on_damage = function(player, stack) on_damage = function(player, stack, index)
play_sound_effect(player, "default_dig_metal") play_sound_effect(player, "default_dig_metal")
end, end,
on_destroy = function(player, stack) on_destroy = function(player, stack, index)
play_sound_effect(player, "default_dug_metal") play_sound_effect(player, "default_dug_metal")
end, end,
}) })
@ -129,10 +129,10 @@ if armor.materials.steel then
armor_groups = {fleshy=10}, armor_groups = {fleshy=10},
damage_groups = {cracky=2, snappy=3, choppy=2, crumbly=1, level=2}, damage_groups = {cracky=2, snappy=3, choppy=2, crumbly=1, level=2},
reciprocate_damage = true, reciprocate_damage = true,
on_damage = function(player, stack) on_damage = function(player, stack, index)
play_sound_effect(player, "default_dig_metal") play_sound_effect(player, "default_dig_metal")
end, end,
on_destroy = function(player, stack) on_destroy = function(player, stack, index)
play_sound_effect(player, "default_dug_metal") play_sound_effect(player, "default_dug_metal")
end, end,
}) })
@ -147,10 +147,10 @@ if armor.materials.bronze then
armor_groups = {fleshy=10}, armor_groups = {fleshy=10},
damage_groups = {cracky=2, snappy=3, choppy=2, crumbly=1, level=2}, damage_groups = {cracky=2, snappy=3, choppy=2, crumbly=1, level=2},
reciprocate_damage = true, reciprocate_damage = true,
on_damage = function(player, stack) on_damage = function(player, stack, index)
play_sound_effect(player, "default_dig_metal") play_sound_effect(player, "default_dig_metal")
end, end,
on_destroy = function(player, stack) on_destroy = function(player, stack, index)
play_sound_effect(player, "default_dug_metal") play_sound_effect(player, "default_dug_metal")
end, end,
}) })
@ -164,10 +164,10 @@ if armor.materials.diamond then
armor_groups = {fleshy=15}, armor_groups = {fleshy=15},
damage_groups = {cracky=2, snappy=1, choppy=1, level=3}, damage_groups = {cracky=2, snappy=1, choppy=1, level=3},
reciprocate_damage = true, reciprocate_damage = true,
on_damage = function(player, stack) on_damage = function(player, stack, index)
play_sound_effect(player, "default_glass_footstep") play_sound_effect(player, "default_glass_footstep")
end, end,
on_destroy = function(player, stack) on_destroy = function(player, stack, index)
play_sound_effect(player, "default_break_glass") play_sound_effect(player, "default_break_glass")
end, end,
}) })
@ -182,10 +182,10 @@ if armor.materials.gold then
armor_groups = {fleshy=10}, armor_groups = {fleshy=10},
damage_groups = {cracky=1, snappy=2, choppy=2, crumbly=3, level=2}, damage_groups = {cracky=1, snappy=2, choppy=2, crumbly=3, level=2},
reciprocate_damage = true, reciprocate_damage = true,
on_damage = function(player, stack) on_damage = function(player, stack, index)
play_sound_effect(player, "default_dig_metal") play_sound_effect(player, "default_dig_metal")
end, end,
on_destroy = function(player, stack) on_destroy = function(player, stack, index)
play_sound_effect(player, "default_dug_metal") play_sound_effect(player, "default_dug_metal")
end, end,
}) })
@ -199,10 +199,10 @@ if armor.materials.mithril then
armor_groups = {fleshy=15}, armor_groups = {fleshy=15},
damage_groups = {cracky=2, snappy=1, level=3}, damage_groups = {cracky=2, snappy=1, level=3},
reciprocate_damage = true, reciprocate_damage = true,
on_damage = function(player, stack) on_damage = function(player, stack, index)
play_sound_effect(player, "default_glass_footstep") play_sound_effect(player, "default_glass_footstep")
end, end,
on_destroy = function(player, stack) on_destroy = function(player, stack, index)
play_sound_effect(player, "default_break_glass") play_sound_effect(player, "default_break_glass")
end, end,
}) })
@ -216,10 +216,10 @@ if armor.materials.crystal then
armor_groups = {fleshy=15}, armor_groups = {fleshy=15},
damage_groups = {cracky=2, snappy=1, level=3}, damage_groups = {cracky=2, snappy=1, level=3},
reciprocate_damage = true, reciprocate_damage = true,
on_damage = function(player, stack) on_damage = function(player, stack, index)
play_sound_effect(player, "default_glass_footstep") play_sound_effect(player, "default_glass_footstep")
end, end,
on_destroy = function(player, stack) on_destroy = function(player, stack, index)
play_sound_effect(player, "default_break_glass") play_sound_effect(player, "default_break_glass")
end, end,
}) })