Fix crash when another player is picking up an item while we are in pickup ui.

This commit is contained in:
IQuant 2024-06-29 19:01:56 +03:00
parent c05c274589
commit a96a1cda32
3 changed files with 24 additions and 0 deletions

View file

@ -23,6 +23,7 @@ ctx.init = function()
ctx.events = {} ctx.events = {}
ctx.is_inventory_open = false ctx.is_inventory_open = false
ctx.rpc_peer_id = nil ctx.rpc_peer_id = nil
ctx.is_wand_pickup = false
end end
function ctx.dofile_and_add_hooks(path) function ctx.dofile_and_add_hooks(path)

View file

@ -6,6 +6,8 @@ dofile_once("data/scripts/lib/coroutines.lua")
local item_sync = {} local item_sync = {}
local pending_remove = {}
function item_sync.ensure_notify_component(ent) function item_sync.ensure_notify_component(ent)
local notify = EntityGetFirstComponentIncludingDisabled(ent, "LuaComponent", "ew_notify_component") local notify = EntityGetFirstComponentIncludingDisabled(ent, "LuaComponent", "ew_notify_component")
if notify == nil then if notify == nil then
@ -46,6 +48,10 @@ function item_sync.get_global_item_id(item)
end end
function item_sync.remove_item_with_id(g_id) function item_sync.remove_item_with_id(g_id)
table.insert(pending_remove, g_id)
end
function item_sync.remove_item_with_id_now(g_id)
local global_items = EntityGetWithTag("ew_global_item") local global_items = EntityGetWithTag("ew_global_item")
for _, item in ipairs(global_items) do for _, item in ipairs(global_items) do
local i_g_id = item_sync.get_global_item_id(item) local i_g_id = item_sync.get_global_item_id(item)
@ -60,6 +66,12 @@ function item_sync.host_localize_item(gid, peer_id)
GamePrint("Item localize for "..gid.." prevented") GamePrint("Item localize for "..gid.." prevented")
end end
ctx.item_prevent_localize[gid] = true ctx.item_prevent_localize[gid] = true
if table.contains(pending_remove, gid) then
GamePrint("Item localize prevented, already taken")
return
end
if peer_id ~= ctx.my_id then if peer_id ~= ctx.my_id then
item_sync.remove_item_with_id(gid) item_sync.remove_item_with_id(gid)
end end
@ -158,6 +170,16 @@ function item_sync.on_world_update_client()
remove_client_items_from_world() remove_client_items_from_world()
end end
function item_sync.on_world_update()
-- TODO check that we not removing item we are going to pick now, instead of checking if picker gui is open.
if not ctx.is_wand_pickup then
if #pending_remove > 0 then
local g_id = table.remove(pending_remove)
item_sync.remove_item_with_id_now(g_id)
end
end
end
function item_sync.upload(item_data) function item_sync.upload(item_data)
local item = inventory_helper.deserialize_single_item(item_data) local item = inventory_helper.deserialize_single_item(item_data)
EntityAddTag(item, "ew_global_item") EntityAddTag(item, "ew_global_item")

View file

@ -98,6 +98,7 @@ function OnProjectileFiredPost(shooter_id, projectile_id, rng, position_x, posit
end end
function OnPausedChanged(paused, is_wand_pickup) function OnPausedChanged(paused, is_wand_pickup)
ctx.is_wand_pickup = is_wand_pickup
local players = EntityGetWithTag("ew_current_player") or {} local players = EntityGetWithTag("ew_current_player") or {}
if (players[1]) then if (players[1]) then