From 15a5d8d48a68d633c30dbc501f3b1f373a12a762 Mon Sep 17 00:00:00 2001 From: oof2win2 Date: Sat, 14 May 2022 15:38:03 +0200 Subject: [PATCH] fix: LuaItemStack could be invalid Fixed an issue with clearing inventories of players where the command failed due to it attempting to access LuaItemStacks that were invalid for read --- expcore/common.lua | 10 ++++++---- modules/addons/inventory-clear.lua | 6 +++--- modules/commands/clear-inventory.lua | 16 ++++++++-------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/expcore/common.lua b/expcore/common.lua index 95011cb6..9b0078c2 100644 --- a/expcore/common.lua +++ b/expcore/common.lua @@ -645,10 +645,12 @@ function Common.move_items_stack(items, surface, position, radius, chest_type) local last_chest for i=1,#items do local item = items[i] - local chest = next_chest(item) - if not chest or not chest.valid then return error(string.format('Cant move item %s to %s{%s, %s} no valid chest in radius', item_name, surface.name, p.x, p.y)) end - chest.insert(item) - last_chest = chest + if item.valid_for_read then + local chest = next_chest(item) + if not chest or not chest.valid then return error(string.format('Cant move item %s to %s{%s, %s} no valid chest in radius', item_name, surface.name, p.x, p.y)) end + chest.insert(item) + last_chest = chest + end end return last_chest end diff --git a/modules/addons/inventory-clear.lua b/modules/addons/inventory-clear.lua index 57ae4958..51dc18e2 100644 --- a/modules/addons/inventory-clear.lua +++ b/modules/addons/inventory-clear.lua @@ -3,13 +3,13 @@ local Event = require 'utils.event' --- @dep utils.event local events = require 'config.inventory_clear' --- @dep config.inventory_clear -local move_items = _C.move_items --- @dep expcore.common +local move_items_stack = _C.move_items_stack --- @dep expcore.common local function clear_items(event) local player = game.players[event.player_index] local inv = player.get_main_inventory() - move_items(inv.get_contents()) + move_items_stack(inv) inv.clear() end -for _, event_name in ipairs(events) do Event.add(event_name, clear_items) end \ No newline at end of file +for _, event_name in ipairs(events) do Event.add(event_name, clear_items) end diff --git a/modules/commands/clear-inventory.lua b/modules/commands/clear-inventory.lua index 04155978..ceb6b3e9 100644 --- a/modules/commands/clear-inventory.lua +++ b/modules/commands/clear-inventory.lua @@ -4,7 +4,7 @@ ]] local Commands = require 'expcore.commands' --- @dep expcore.commands -local move_items = _C.move_items --- @dep expcore.common +local move_items_stack = _C.move_items_stack --- @dep expcore.common require 'config.expcore.command_role_parse' --- Clears a players inventory @@ -14,10 +14,10 @@ Commands.new_command('clear-inventory', 'Clears a players inventory') :add_param('player', false, 'player-role') :add_alias('clear-inv', 'move-inventory', 'move-inv') :register(function(_, player) - local inv = player.get_main_inventory() - if not inv then - return Commands.error{'expcore-commands.reject-player-alive'} - end - move_items(inv.get_contents()) - inv.clear() -end) \ No newline at end of file + local inv = player.get_main_inventory() + if not inv then + return Commands.error{'expcore-commands.reject-player-alive'} + end + move_items_stack(inv) + inv.clear() +end)