1
0
mirror of https://github.com/minetest-mods/3d_armor.git synced 2024-12-27 19:20:39 +01:00

fix armor equip/unequip (#17)

This commit is contained in:
OgelGames 2020-09-24 22:08:17 +10:00 committed by GitHub
parent 294338d3b0
commit 7f63df230c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -103,25 +103,13 @@ armor.config = {
-- Armor Registration -- Armor Registration
armor.register_armor = function(self, name, def) armor.register_armor = function(self, name, def)
local wear_on_rightclick = { def.on_secondary_use = function(itemstack, player)
on_secondary_use = function(itemstack, player) return armor:equip(player, itemstack)
armor:equip(player, itemstack:get_name())
end,
on_place = function(itemstack, player)
armor:equip(player, itemstack:get_name())
end end
} def.on_place = function(itemstack, player)
local function merge_tables(a, b) return armor:equip(player, itemstack)
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 minetest.register_tool(name, def)
end
return a
end
minetest.register_tool(name, merge_tables(def, wear_on_rightclick))
end end
armor.register_armor_group = function(self, group, base) armor.register_armor_group = function(self, group, base)
@ -440,40 +428,38 @@ armor.get_weared_armor_elements = function(self, player)
return weared_armor return weared_armor
end end
armor.equip = function(self, player, armor_name) armor.equip = function(self, player, itemstack)
local name, inv = self:get_valid_player(player, "[equip]") local name, armor_inv = self:get_valid_player(player, "[equip]")
local weared_armor = self:get_weared_armor_elements(player) local weared_armor = self:get_weared_armor_elements(player)
local armor_element = self:get_element(armor_name) local armor_element = self:get_element(itemstack:get_name())
if not name then if name and armor_element 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 if weared_armor[armor_element] ~= nil then
self:unequip(player, weared_armor[armor_element]) self:unequip(player, armor_element)
end end
inv:add_item("armor", ItemStack(armor_name)) armor_inv:add_item("armor", itemstack:take_item())
minetest.after(0, function() player:get_inventory():remove_item("main", ItemStack(armor_name)) end)
self:set_player_armor(player) self:set_player_armor(player)
self:save_armor_inventory(player) self:save_armor_inventory(player)
end end
return itemstack
end
armor.unequip = function(self, player, armor_name) armor.unequip = function(self, player, armor_element)
local name, inv = self:get_valid_player(player, "[unequip]") local name, armor_inv = self:get_valid_player(player, "[unequip]")
if not name then local weared_armor = self:get_weared_armor_elements(player)
return if not name or not weared_armor[armor_element] then
elseif self:get_element(armor_name) == nil then
return
elseif not inv:contains_item("armor", ItemStack(armor_name)) then
return return
end end
inv:remove_item("armor", ItemStack(armor_name)) local itemstack = armor_inv:remove_item("armor", ItemStack(weared_armor[armor_element]))
minetest.after(0, function() player:get_inventory():add_item("main", ItemStack(armor_name)) end) minetest.after(0, function()
local inv = player:get_inventory()
if inv:room_for_item("main", itemstack) then
inv:add_item("main", itemstack)
else
minetest.add_item(player:get_pos(), itemstack)
end
end)
self:set_player_armor(player) self:set_player_armor(player)
self:save_armor_inventory(player) self:save_armor_inventory(player)
self:save_armor_inventory(player)
end end
armor.remove_all = function(self, player) armor.remove_all = function(self, player)