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
}
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 end
minetest.register_tool(name, merge_tables(def, wear_on_rightclick)) def.on_place = function(itemstack, player)
return armor:equip(player, itemstack)
end
minetest.register_tool(name, def)
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 if weared_armor[armor_element] ~= nil then
elseif self:get_element(armor_name) == nil then self:unequip(player, armor_element)
return end
elseif inv:contains_item("armor", ItemStack(armor_name)) then armor_inv:add_item("armor", itemstack:take_item())
return self:set_player_armor(player)
end self:save_armor_inventory(player)
if weared_armor[armor_element] ~= nil then end
self:unequip(player, weared_armor[armor_element]) return itemstack
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 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 return
elseif not inv:contains_item("armor", ItemStack(armor_name)) then end
return local itemstack = armor_inv:remove_item("armor", ItemStack(weared_armor[armor_element]))
end minetest.after(0, function()
inv:remove_item("armor", ItemStack(armor_name)) local inv = player:get_inventory()
minetest.after(0, function() player:get_inventory():add_item("main", ItemStack(armor_name)) end) 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)