Fixed item duplication. Again

This commit is contained in:
IQuant 2024-05-15 17:05:50 +03:00
parent 6d5dd0a039
commit 3295b0eff0
3 changed files with 19 additions and 6 deletions

View file

@ -208,9 +208,8 @@ function inventory_helper.set_item_data(item_data, player_data)
return return
end end
EntityRemoveTag(item, "ew_global_item")
if(itemInfo.is_wand)then if(itemInfo.is_wand)then
EntityAddTag(item.entity_id, "ew_client_item")
item:PickUp(player) item:PickUp(player)
local itemComp = EntityGetFirstComponentIncludingDisabled(item.entity_id, "ItemComponent") local itemComp = EntityGetFirstComponentIncludingDisabled(item.entity_id, "ItemComponent")
if (itemComp ~= nil) then if (itemComp ~= nil) then
@ -221,6 +220,7 @@ function inventory_helper.set_item_data(item_data, player_data)
active_item_entity = item.entity_id active_item_entity = item.entity_id
end end
else else
EntityAddTag(item, "ew_client_item")
pickup_item(player, item) pickup_item(player, item)
local itemComp = EntityGetFirstComponentIncludingDisabled(item, "ItemComponent") local itemComp = EntityGetFirstComponentIncludingDisabled(item, "ItemComponent")
if (itemComp ~= nil) then if (itemComp ~= nil) then
@ -268,7 +268,7 @@ function inventory_helper.has_inventory_changed(player_data)
for _, item in ipairs(GameGetAllInventoryItems(player_data.entity)) do for _, item in ipairs(GameGetAllInventoryItems(player_data.entity)) do
local item_comp = EntityGetFirstComponentIncludingDisabled(item, "ItemComponent") local item_comp = EntityGetFirstComponentIncludingDisabled(item, "ItemComponent")
local slot_x, slot_y = ComponentGetValue2(item_comp, "inventory_slot") local slot_x, slot_y = ComponentGetValue2(item_comp, "inventory_slot")
inventory_hash = (inventory_hash + (item % 1024 + slot_x + slot_y)) % (math.pow(2, 20) - 1) inventory_hash = (inventory_hash*19 + (item % 65000 + slot_x + slot_y)) % (math.pow(2, 20) - 1)
end end
player_data.prev_inventory_hash = inventory_hash player_data.prev_inventory_hash = inventory_hash
return inventory_hash ~= prev_inventory return inventory_hash ~= prev_inventory

View file

@ -88,6 +88,17 @@ local function get_global_ent(key)
end end
end end
local function remove_client_items_from_world()
if GameGetFrameNum() % 5 ~= 3 then
return
end
for _, item in ipairs(EntityGetWithTag("ew_client_item")) do
if is_item_on_ground(item) then
EntityKill(item)
end
end
end
function item_sync.host_upload_items(my_player) function item_sync.host_upload_items(my_player)
if GameGetFrameNum() % 5 == 4 then if GameGetFrameNum() % 5 == 4 then
mark_in_inventory(my_player) mark_in_inventory(my_player)
@ -102,6 +113,7 @@ function item_sync.host_upload_items(my_player)
local g_id = item_sync.get_global_item_id(picked_item) local g_id = item_sync.get_global_item_id(picked_item)
item_sync.host_localize_item(g_id, ctx.my_id) item_sync.host_localize_item(g_id, ctx.my_id)
end end
remove_client_items_from_world()
end end
function item_sync.client_tick(my_player) function item_sync.client_tick(my_player)
@ -109,7 +121,7 @@ function item_sync.client_tick(my_player)
mark_in_inventory(my_player) mark_in_inventory(my_player)
end end
local thrown_item = get_global_ent("ew_thrown") local thrown_item = get_global_ent("ew_thrown")
if thrown_item ~= nil then if thrown_item ~= nil and not EntityHasTag(thrown_item, "ew_client_item") then
ctx.lib.net.send_item_upload(inventory_helper.serialize_single_item(thrown_item)) ctx.lib.net.send_item_upload(inventory_helper.serialize_single_item(thrown_item))
EntityKill(thrown_item) EntityKill(thrown_item)
end end
@ -119,6 +131,7 @@ function item_sync.client_tick(my_player)
local gid = item_sync.get_global_item_id(picked_item) local gid = item_sync.get_global_item_id(picked_item)
ctx.lib.net.send_localize_request(gid) ctx.lib.net.send_localize_request(gid)
end end
remove_client_items_from_world()
end end
function item_sync.upload(item_data) function item_sync.upload(item_data)

View file

@ -232,11 +232,11 @@ local function on_world_pre_update_inner()
net.send_host_player_info(player_info) net.send_host_player_info(player_info)
end end
if ctx.events.new_player_just_connected or ctx.events.inventory_maybe_just_changed or GameGetFrameNum() % 20 == 0 then if ctx.events.new_player_just_connected or ctx.events.inventory_maybe_just_changed or GameGetFrameNum() % 5 == 0 then
if ctx.events.new_player_just_connected or inventory_helper.has_inventory_changed(my_player) then if ctx.events.new_player_just_connected or inventory_helper.has_inventory_changed(my_player) then
local inventory_state = player_fns.serialize_items(my_player) local inventory_state = player_fns.serialize_items(my_player)
if inventory_state ~= nil then if inventory_state ~= nil then
GamePrint("Sending updated inventory") -- GamePrint("Sending updated inventory")
net.send_player_inventory(inventory_state) net.send_player_inventory(inventory_state)
end end
end end