Only validate armor inventory after user changes

This commit is contained in:
stujones11
2018-06-02 18:59:32 +01:00
parent af4a381433
commit f960fc1a41
2 changed files with 56 additions and 39 deletions

View File

@ -387,6 +387,7 @@ armor.damage = function(self, player, index, stack, use)
self:run_callbacks("on_damage", player, index, stack)
self:set_inventory_stack(player, index, stack)
if stack:get_count() == 0 then
self:run_callbacks("on_unequip", player, index, old_stack)
self:run_callbacks("on_destroy", player, index, old_stack)
self:set_player_armor(player)
end
@ -480,45 +481,6 @@ armor.save_armor_inventory = function(self, player)
if not name then
return
end
-- Workaround for detached inventory swap exploit
local armor_prev = {}
local armor_list_string = player:get_attribute("3d_armor_inventory")
if armor_list_string then
local armor_list = self:deserialize_inventory_list(armor_list_string)
for i, stack in ipairs(armor_list) do
if stack:get_count() > 0 then
armor_prev[stack:get_name()] = i
end
end
end
local elements = {}
local player_inv = player:get_inventory()
for i = 1, 6 do
local stack = inv:get_stack("armor", i)
if stack:get_count() > 0 then
local item = stack:get_name()
local element = self:get_element(item)
if element and not elements[element] then
if armor_prev[item] then
armor_prev[item] = nil
else
-- Item was not in previous inventory
armor:run_callbacks("on_equip", player, i, stack)
end
elements[element] = true;
else
inv:remove_item("armor", stack)
if player_inv and player_inv:room_for_item("main", stack) then
player_inv:add_item("main", stack)
end
end
end
end
for item, i in pairs(armor_prev) do
local stack = ItemStack(item)
-- Previous item is not in current inventory
armor:run_callbacks("on_unequip", player, i, stack)
end
player:set_attribute("3d_armor_inventory",
self:serialize_inventory_list(inv:get_list("armor")))
end