forked from mtcontrib/3d_armor
Added the possibility to wear the armor on rightclick (#16)
* Added the possibility to wear the armor on rightclick * Added equip, unequip and get_weared_armor_elements functions Co-authored-by: Giov4 <brancacciogiovanni1@gmail.com>
This commit is contained in:
parent
9459400474
commit
294338d3b0
|
@ -164,6 +164,14 @@ armor:remove_all(player)
|
|||
|
||||
Removes all armors from the player's inventory without triggering any callback.
|
||||
|
||||
armor:equip(player, armor_name)
|
||||
|
||||
Equip the armor, removing the itemstack from the main inventory if there's one.
|
||||
|
||||
armor:unequip(player, armor_name)
|
||||
|
||||
Unequip the armor, adding the itemstack to the main inventory.
|
||||
|
||||
Item Callbacks:
|
||||
|
||||
on_equip = func(player, index, stack)
|
||||
|
|
|
@ -103,7 +103,25 @@ armor.config = {
|
|||
-- Armor Registration
|
||||
|
||||
armor.register_armor = function(self, name, def)
|
||||
minetest.register_tool(name, def)
|
||||
local wear_on_rightclick = {
|
||||
on_secondary_use = function(itemstack, player)
|
||||
armor:equip(player, itemstack:get_name())
|
||||
end,
|
||||
on_place = function(itemstack, player)
|
||||
armor:equip(player, itemstack:get_name())
|
||||
end
|
||||
}
|
||||
local function merge_tables(a, b)
|
||||
if type(a) == 'table' and type(b) == 'table' then
|
||||
for k,v in pairs(b) do
|
||||
if type(v)=='table' and type(a[k] or false)=='table' then
|
||||
merge_tables(a[k],v) else a[k]=v
|
||||
end
|
||||
end
|
||||
end
|
||||
return a
|
||||
end
|
||||
minetest.register_tool(name, merge_tables(def, wear_on_rightclick))
|
||||
end
|
||||
|
||||
armor.register_armor_group = function(self, group, base)
|
||||
|
@ -406,12 +424,64 @@ armor.damage = function(self, player, index, stack, use)
|
|||
end
|
||||
end
|
||||
|
||||
armor.get_weared_armor_elements = function(self, player)
|
||||
local name, inv = self:get_valid_player(player, "[get_weared_armor]")
|
||||
local weared_armor = {}
|
||||
if not name then
|
||||
return
|
||||
end
|
||||
for i=1, inv:get_size("armor") do
|
||||
local item_name = inv:get_stack("armor", i):get_name()
|
||||
local element = self:get_element(item_name)
|
||||
if element ~= nil then
|
||||
weared_armor[element] = item_name
|
||||
end
|
||||
end
|
||||
return weared_armor
|
||||
end
|
||||
|
||||
armor.equip = function(self, player, armor_name)
|
||||
local name, inv = self:get_valid_player(player, "[equip]")
|
||||
local weared_armor = self:get_weared_armor_elements(player)
|
||||
local armor_element = self:get_element(armor_name)
|
||||
if not name then
|
||||
return
|
||||
elseif self:get_element(armor_name) == nil then
|
||||
return
|
||||
elseif inv:contains_item("armor", ItemStack(armor_name)) then
|
||||
return
|
||||
end
|
||||
if weared_armor[armor_element] ~= nil then
|
||||
self:unequip(player, weared_armor[armor_element])
|
||||
end
|
||||
inv:add_item("armor", ItemStack(armor_name))
|
||||
minetest.after(0, function() player:get_inventory():remove_item("main", ItemStack(armor_name)) end)
|
||||
self:set_player_armor(player)
|
||||
self:save_armor_inventory(player)
|
||||
end
|
||||
|
||||
armor.unequip = function(self, player, armor_name)
|
||||
local name, inv = self:get_valid_player(player, "[unequip]")
|
||||
if not name then
|
||||
return
|
||||
elseif self:get_element(armor_name) == nil then
|
||||
return
|
||||
elseif not inv:contains_item("armor", ItemStack(armor_name)) then
|
||||
return
|
||||
end
|
||||
inv:remove_item("armor", ItemStack(armor_name))
|
||||
minetest.after(0, function() player:get_inventory():add_item("main", ItemStack(armor_name)) end)
|
||||
self:set_player_armor(player)
|
||||
self:save_armor_inventory(player)
|
||||
self:save_armor_inventory(player)
|
||||
end
|
||||
|
||||
armor.remove_all = function(self, player)
|
||||
local name, armor_inv = self:get_valid_player(player, "[remove_all]")
|
||||
local name, inv = self:get_valid_player(player, "[remove_all]")
|
||||
if not name then
|
||||
return
|
||||
end
|
||||
armor_inv:set_list("armor", {})
|
||||
inv:set_list("armor", {})
|
||||
self:set_player_armor(player)
|
||||
self:save_armor_inventory(player)
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user