forked from mtcontrib/3d_armor
Add callbacks, closes #46
This commit is contained in:
parent
2b840fbe38
commit
75a7181e0f
@ -64,3 +64,54 @@ armor_radiation_multiplier = 1
|
|||||||
-- Enable fire protection (defaults true if using ethereal mod)
|
-- Enable fire protection (defaults true if using ethereal mod)
|
||||||
armor_fire_protect = false
|
armor_fire_protect = false
|
||||||
|
|
||||||
|
API
|
||||||
|
---
|
||||||
|
|
||||||
|
Armor Registration:
|
||||||
|
|
||||||
|
minetest.register_tool("your_mod_name:speed_boots", {
|
||||||
|
description = "Speed Boots",
|
||||||
|
inventory_image = "your_mod_name_speed_boots_inv.png",
|
||||||
|
texture = "your_mod_name_speed_boots.png",
|
||||||
|
groups = {armor_feet=10, physics_speed=0.5, armor_use=2000},
|
||||||
|
on_destroy = function(player, item_name)
|
||||||
|
minetest.sound_play("your_mod_name_break_sound", {
|
||||||
|
to_player = player:get_player_name(),
|
||||||
|
})
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
|
See armor.lua, technic_armor and shields mods for more examples.
|
||||||
|
|
||||||
|
Default groups:
|
||||||
|
|
||||||
|
Elements: armor_head, armor_torso, armor_legs, armor_feet
|
||||||
|
Physics: physics_jump, physics_speed, physics_gravity
|
||||||
|
Durability: armor_use
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
|
||||||
|
Elements may be modified by dependent mods, eg shields adds armor_shield.
|
||||||
|
|
||||||
|
Physics values are 'stackable', durability is determined
|
||||||
|
by the level of armor_use. total uses == approx (65535/armor_use)
|
||||||
|
|
||||||
|
Item Callbacks:
|
||||||
|
|
||||||
|
on_equip = func(player, stack)
|
||||||
|
on_unequip = func(player, stack)
|
||||||
|
on_destroy = func(player, item_name)
|
||||||
|
|
||||||
|
Global Callbacks:
|
||||||
|
|
||||||
|
armor:register_on_update(func(player))
|
||||||
|
armor:register_on_equip(ifunc(player, stack))
|
||||||
|
armor:register_on_unequip(func(player, stack))
|
||||||
|
armor:register_on_destroy(func(player, stack))
|
||||||
|
|
||||||
|
Global Callback Example:
|
||||||
|
|
||||||
|
armor:register_on_update(function(player)
|
||||||
|
print(player:get_player_name().." armor updated!")
|
||||||
|
end)
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ local use_armor_monoid = minetest.global_exists("armor_monoid")
|
|||||||
armor = {
|
armor = {
|
||||||
timer = 0,
|
timer = 0,
|
||||||
elements = {"head", "torso", "legs", "feet"},
|
elements = {"head", "torso", "legs", "feet"},
|
||||||
physics = {"jump","speed","gravity"},
|
physics = {"jump", "speed", "gravity"},
|
||||||
formspec = "size[8,8.5]"..
|
formspec = "size[8,8.5]"..
|
||||||
default.gui_bg..
|
default.gui_bg..
|
||||||
default.gui_bg_img..
|
default.gui_bg_img..
|
||||||
@ -42,6 +42,12 @@ armor = {
|
|||||||
{"default:torch_ceiling", 1, 1},
|
{"default:torch_ceiling", 1, 1},
|
||||||
{"default:torch_wall", 1, 1},
|
{"default:torch_wall", 1, 1},
|
||||||
},
|
},
|
||||||
|
registered_callbacks = {
|
||||||
|
on_update = {},
|
||||||
|
on_equip = {},
|
||||||
|
on_unequip = {},
|
||||||
|
on_destroy = {},
|
||||||
|
},
|
||||||
version = "0.4.8",
|
version = "0.4.8",
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,6 +72,47 @@ armor.config = {
|
|||||||
fire_protect = minetest.get_modpath("ethereal") ~= nil
|
fire_protect = minetest.get_modpath("ethereal") ~= nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
-- Armor callbacks
|
||||||
|
|
||||||
|
armor.register_on_update = function(self, func)
|
||||||
|
if type(func) == "function" then
|
||||||
|
table.insert(self.registered_callbacks.on_update, func)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
armor.register_on_equip = function(self, func)
|
||||||
|
if type(func) == "function" then
|
||||||
|
table.insert(self.registered_callbacks.on_equip, func)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
armor.register_on_unequip = function(self, func)
|
||||||
|
if type(func) == "function" then
|
||||||
|
table.insert(self.registered_callbacks.on_unequip, func)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
armor.register_on_destroy = function(self, func)
|
||||||
|
if type(func) == "function" then
|
||||||
|
table.insert(self.registered_callbacks.on_destroy, func)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
armor.run_callbacks = function(self, callback, player, stack)
|
||||||
|
if stack then
|
||||||
|
local def = stack:get_definition() or {}
|
||||||
|
if type(def[callback]) == "function" then
|
||||||
|
def[callback](player, stack)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local callbacks = self.registered_callbacks[callback]
|
||||||
|
if callbacks then
|
||||||
|
for _, func in pairs(callbacks) do
|
||||||
|
func(player, stack)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
armor.update_player_visuals = function(self, player)
|
armor.update_player_visuals = function(self, player)
|
||||||
if not player then
|
if not player then
|
||||||
return
|
return
|
||||||
@ -188,6 +235,7 @@ armor.set_player_armor = function(self, player)
|
|||||||
self.def[name].water = armor_water
|
self.def[name].water = armor_water
|
||||||
self.def[name].radiation = armor_radiation
|
self.def[name].radiation = armor_radiation
|
||||||
self:update_player_visuals(player)
|
self:update_player_visuals(player)
|
||||||
|
self:run_callbacks("on_update", player)
|
||||||
end
|
end
|
||||||
|
|
||||||
armor.get_player_skin = function(self, name)
|
armor.get_player_skin = function(self, name)
|
||||||
|
@ -191,11 +191,13 @@ minetest.register_on_joinplayer(function(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:update_inventory(player)
|
armor:update_inventory(player)
|
||||||
|
armor:run_callbacks("on_equip", player, stack)
|
||||||
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:update_inventory(player)
|
armor:update_inventory(player)
|
||||||
|
armor:run_callbacks("on_unequip", player, stack)
|
||||||
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()
|
||||||
@ -223,6 +225,7 @@ minetest.register_on_joinplayer(function(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)
|
||||||
|
armor:run_callbacks("on_equip", player, stack)
|
||||||
end
|
end
|
||||||
armor.def[name] = {
|
armor.def[name] = {
|
||||||
state = 0,
|
state = 0,
|
||||||
@ -296,6 +299,7 @@ if armor.config.drop == true or armor.config.destroy == true then
|
|||||||
table.insert(drop, stack)
|
table.insert(drop, stack)
|
||||||
armor_inv:set_stack("armor", i, nil)
|
armor_inv:set_stack("armor", i, nil)
|
||||||
player_inv:set_stack("armor", i, nil)
|
player_inv:set_stack("armor", i, nil)
|
||||||
|
armor:run_callbacks("on_unequip", player, stack)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
armor:set_player_armor(player)
|
armor:set_player_armor(player)
|
||||||
@ -348,8 +352,9 @@ minetest.register_on_player_hpchange(function(player, hp_change)
|
|||||||
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)
|
||||||
if stack:get_count() > 0 then
|
if stack:get_count() > 0 then
|
||||||
local use = stack:get_definition().groups["armor_use"] or 0
|
local def = stack:get_definition() or {}
|
||||||
local heal = stack:get_definition().groups["armor_heal"] or 0
|
local use = def.groups["armor_use"] or 0
|
||||||
|
local heal = def.groups["armor_heal"] or 0
|
||||||
local item = stack:get_name()
|
local item = stack:get_name()
|
||||||
stack:add_wear(use)
|
stack:add_wear(use)
|
||||||
armor_inv:set_stack("armor", i, stack)
|
armor_inv:set_stack("armor", i, stack)
|
||||||
@ -363,6 +368,8 @@ minetest.register_on_player_hpchange(function(player, hp_change)
|
|||||||
end
|
end
|
||||||
armor:set_player_armor(player)
|
armor:set_player_armor(player)
|
||||||
armor:update_inventory(player)
|
armor:update_inventory(player)
|
||||||
|
armor:run_callbacks("on_unequip", player, stack)
|
||||||
|
armor:run_callbacks("on_destroy", player, stack)
|
||||||
end
|
end
|
||||||
heal_max = heal_max + heal
|
heal_max = heal_max + heal
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user