From 412c2911933ae116e05754b3847f3ce04657aa0d Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Sun, 1 Mar 2026 23:58:57 +0000 Subject: [PATCH 1/8] Add recipe notification sync to inventory sync --- plugins/inventory_sync/messages.ts | 1 + plugins/inventory_sync/module/serialize.lua | 65 +++++++++++++++------ 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/plugins/inventory_sync/messages.ts b/plugins/inventory_sync/messages.ts index 532dfe2f..f2c8fdc4 100644 --- a/plugins/inventory_sync/messages.ts +++ b/plugins/inventory_sync/messages.ts @@ -18,6 +18,7 @@ export type IpcPlayerData = { hotbar?: string[], personal_logistic_slots?: {name:string, min:number, max:number}[], crafting_queue?: any, + recipe_notifications?: string[], } const jsonPlayerData = Type.Object({ diff --git a/plugins/inventory_sync/module/serialize.lua b/plugins/inventory_sync/module/serialize.lua index 989315e9..3e470db0 100644 --- a/plugins/inventory_sync/module/serialize.lua +++ b/plugins/inventory_sync/module/serialize.lua @@ -6,6 +6,7 @@ local serialize = {} local v2_logistic_api = compat.version_ge("2.0.0") local v2_storage_api = compat.version_ge("2.0.0") +local recipe_notifications_api = compat.version_ge("2.0.67") function serialize.serialize_inventories(source, inventories) local serialized = {} @@ -347,22 +348,27 @@ for name, value in pairs(defines.controllers) do controller_to_name[value] = name end --- Players are serialized into a table with the following fields: --- name --- controller: may not be cutscene or editor --- color --- chat_color --- tag --- force --- cheat_mod --- flashlight --- ticks_to_respawn (optional) --- character (optional) --- inventories: non-character inventories --- hotbar: table of string indexes to names (optional) --- personal_logistic_slots (optional) --- crafting_queue (optional) +--- @class SerializedPlayerData +--- @field name string +--- @field controller string +--- @field color Color +--- @field chat_color Color +--- @field tag string +--- @field force string +--- @field cheat_mode boolean +--- @field flashlight boolean +--- @field ticks_to_respawn number +--- @field character table? +--- @field inventories table? +--- @field hotbar table? +--- @field personal_logistic_slots table? +--- @field crafting_queue table? +--- @field recipe_notifications string[]? + +--- @param player LuaPlayer +--- @return SerializedPlayerData function serialize.serialize_player(player) + --- @type SerializedPlayerData local serialized = { controller = controller_to_name[player.controller_type], name = player.name, @@ -385,7 +391,9 @@ function serialize.serialize_player(player) -- Go back to waiting for respawn local character = player.character player.ticks_to_respawn = serialized.ticks_to_respawn - character.destroy() + if character and character.valid then + character.destroy() + end end -- Serialize character @@ -406,7 +414,7 @@ function serialize.serialize_player(player) if not serialized.hotbar then serialized.hotbar = {} end - serialized.hotbar[tostring(i)] = slot.name + serialized.hotbar[tostring(i)] = slot.name --[[@as string]] end end @@ -415,9 +423,20 @@ function serialize.serialize_player(player) serialized.crafting_queue = serialize.serialize_crafting_queue(player) end + -- Serialize recipe notifications + if recipe_notifications_api then + local recipe_notifications = {} + serialized.recipe_notifications = recipe_notifications + for i, recipe in ipairs(player.get_recipe_notifications()) do + recipe_notifications[i] = recipe.name + end + end + return serialized end +--- @param player LuaPlayer +--- @param serialized SerializedPlayerData function serialize.deserialize_player(player, serialized) if player.controller_type ~= defines.controllers[serialized.controller] or serialized.controller == "ghost" then -- If targeting the character or ghost controller then create a character @@ -443,7 +462,9 @@ function serialize.deserialize_player(player, serialized) player.set_controller({ type = defines.controllers.god }) player.set_controller({ type = defines.controllers.ghost }) end - character.destroy() + if character and character.valid then + character.destroy() + end end else @@ -496,6 +517,14 @@ function serialize.deserialize_player(player, serialized) serialize.deserialize_crafting_queue(player, serialized.crafting_queue) end + -- Deserialize recipe notifications + if recipe_notifications_api and serialized.recipe_notifications then + player.clear_recipe_notifications() + local add_notification = player.add_recipe_notification + for _, recipe_name in ipairs(serialized.recipe_notifications) do + add_notification(recipe_name) + end + end end return serialize From 54557a79c57aa0d5a28f86c34039e23ebe6fcb35 Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Mon, 2 Mar 2026 16:39:20 +0000 Subject: [PATCH 2/8] Do not add empty array for recipe notifications --- plugins/inventory_sync/module/serialize.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugins/inventory_sync/module/serialize.lua b/plugins/inventory_sync/module/serialize.lua index 3e470db0..53b2a9da 100644 --- a/plugins/inventory_sync/module/serialize.lua +++ b/plugins/inventory_sync/module/serialize.lua @@ -426,10 +426,13 @@ function serialize.serialize_player(player) -- Serialize recipe notifications if recipe_notifications_api then local recipe_notifications = {} - serialized.recipe_notifications = recipe_notifications for i, recipe in ipairs(player.get_recipe_notifications()) do recipe_notifications[i] = recipe.name end + if #recipe_notifications > 0 then + -- Do no add if length is zero as this breaks with table_to_json + serialized.recipe_notifications = recipe_notifications + end end return serialized From 3e7b65264e600af9299318897c6b1674e0986385 Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Mon, 2 Mar 2026 17:58:45 +0000 Subject: [PATCH 3/8] Invert notifications array to prevent loss of new notifications --- plugins/inventory_sync/module/serialize.lua | 61 ++++++++++++++++----- 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/plugins/inventory_sync/module/serialize.lua b/plugins/inventory_sync/module/serialize.lua index 53b2a9da..04ae5d88 100644 --- a/plugins/inventory_sync/module/serialize.lua +++ b/plugins/inventory_sync/module/serialize.lua @@ -343,6 +343,50 @@ function serialize.deserialize_crafting_queue(player, serialized) player.crafting_queue_progress = serialized.crafting_queue_progress end +---@param player LuaPlayer +---@return string? +function serialize.serialize_crafting_notifications(player) + -- Get all unlocked recipes + local recipes = {} + for _, recipe in pairs(player.force.recipes) do + if recipe.enabled and not recipe.hidden then + recipes[recipe.name] = true + end + end + + -- Remove those with notifications + for _, recipe in pairs(player.get_recipe_notifications()) do + recipes[recipe.name] = nil + end + + -- Convert to a list of names + local index = 1 + local recipe_names = {} + for name in pairs(recipes) do + recipe_names[index] = name + index = index + 1 + end + + -- Do no return if length is zero as this breaks with table_to_json + if index > 1 then + return helpers.encode_string(helpers.table_to_json(recipe_names)) + end + + return nil +end + +--- @param player LuaPlayer +--- @param serialized string +function serialize.deserialize_crafting_notifications(player, serialized) + -- Decode the recipe name strings + local recipe_names = helpers.json_to_table(assert(helpers.decode_string(serialized))) + assert(type(recipe_names) == "table", "wrong type decoded from json_to_table") + local clear_notification = player.clear_recipe_notification + for _, recipe_name in pairs(recipe_names) do + clear_notification(recipe_name) + end +end + local controller_to_name = {} for name, value in pairs(defines.controllers) do controller_to_name[value] = name @@ -363,7 +407,7 @@ end --- @field hotbar table? --- @field personal_logistic_slots table? --- @field crafting_queue table? ---- @field recipe_notifications string[]? +--- @field recipe_notifications string? --- @param player LuaPlayer --- @return SerializedPlayerData @@ -425,14 +469,7 @@ function serialize.serialize_player(player) -- Serialize recipe notifications if recipe_notifications_api then - local recipe_notifications = {} - for i, recipe in ipairs(player.get_recipe_notifications()) do - recipe_notifications[i] = recipe.name - end - if #recipe_notifications > 0 then - -- Do no add if length is zero as this breaks with table_to_json - serialized.recipe_notifications = recipe_notifications - end + serialized.recipe_notifications = serialize.serialize_crafting_notifications(player) end return serialized @@ -522,11 +559,7 @@ function serialize.deserialize_player(player, serialized) -- Deserialize recipe notifications if recipe_notifications_api and serialized.recipe_notifications then - player.clear_recipe_notifications() - local add_notification = player.add_recipe_notification - for _, recipe_name in ipairs(serialized.recipe_notifications) do - add_notification(recipe_name) - end + serialize.deserialize_crafting_notifications(player, serialized.recipe_notifications) end end From cdfa9860bb76bf140ee114acfa98492a03223033 Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Tue, 3 Mar 2026 20:30:18 +0000 Subject: [PATCH 4/8] Fix IPC type --- plugins/inventory_sync/messages.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/inventory_sync/messages.ts b/plugins/inventory_sync/messages.ts index f2c8fdc4..53178b56 100644 --- a/plugins/inventory_sync/messages.ts +++ b/plugins/inventory_sync/messages.ts @@ -18,7 +18,7 @@ export type IpcPlayerData = { hotbar?: string[], personal_logistic_slots?: {name:string, min:number, max:number}[], crafting_queue?: any, - recipe_notifications?: string[], + recipe_notifications?: string, } const jsonPlayerData = Type.Object({ From 3f6203f125d74baa32f4d50b8a0b193a404b1834 Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Fri, 6 Mar 2026 13:27:45 +0000 Subject: [PATCH 5/8] Add valid recipe check and remove empty table check --- plugins/inventory_sync/module/serialize.lua | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/plugins/inventory_sync/module/serialize.lua b/plugins/inventory_sync/module/serialize.lua index 04ae5d88..c6fed8c2 100644 --- a/plugins/inventory_sync/module/serialize.lua +++ b/plugins/inventory_sync/module/serialize.lua @@ -367,12 +367,7 @@ function serialize.serialize_crafting_notifications(player) index = index + 1 end - -- Do no return if length is zero as this breaks with table_to_json - if index > 1 then - return helpers.encode_string(helpers.table_to_json(recipe_names)) - end - - return nil + return helpers.encode_string(helpers.table_to_json(recipe_names)) end --- @param player LuaPlayer @@ -382,8 +377,11 @@ function serialize.deserialize_crafting_notifications(player, serialized) local recipe_names = helpers.json_to_table(assert(helpers.decode_string(serialized))) assert(type(recipe_names) == "table", "wrong type decoded from json_to_table") local clear_notification = player.clear_recipe_notification + local valid_recipes = compat.prototypes.recipe for _, recipe_name in pairs(recipe_names) do - clear_notification(recipe_name) + if valid_recipes[recipe_name] then + clear_notification(recipe_name) + end end end From 4badc2e9c1676976e561a54453abc1f82f96f04a Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Fri, 6 Mar 2026 15:35:49 +0000 Subject: [PATCH 6/8] Fix sync when joining a new server --- plugins/inventory_sync/module/serialize.lua | 31 +++++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/plugins/inventory_sync/module/serialize.lua b/plugins/inventory_sync/module/serialize.lua index c6fed8c2..f2ad87dd 100644 --- a/plugins/inventory_sync/module/serialize.lua +++ b/plugins/inventory_sync/module/serialize.lua @@ -377,10 +377,35 @@ function serialize.deserialize_crafting_notifications(player, serialized) local recipe_names = helpers.json_to_table(assert(helpers.decode_string(serialized))) assert(type(recipe_names) == "table", "wrong type decoded from json_to_table") local clear_notification = player.clear_recipe_notification - local valid_recipes = compat.prototypes.recipe + local add_notification = player.add_recipe_notification + local notifications = {} + + -- Assume all recipes need a notification + for _, recipe in pairs(player.force.recipes) do + if recipe.enabled and not recipe.hidden then + notifications[recipe.name] = true + end + end + + -- Remove recipes the player has cleared for _, recipe_name in pairs(recipe_names) do - if valid_recipes[recipe_name] then - clear_notification(recipe_name) + if notifications[recipe_name] then + notifications[recipe_name] = false + end + end + + -- Clear ones which are ones which are not required + for _, recipe in pairs(player.get_recipe_notifications()) do + if notifications[recipe.name] == false then + clear_notification(recipe.name) + end + notifications[recipe.name] = nil -- Prevents double add below + end + + -- Add any which the player did not have + for recipe_name, should_add in pairs(notifications) do + if should_add then + add_notification(recipe_name) end end end From f2833eec33361cd6863dc2b9e6a8045adb712fee Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Fri, 6 Mar 2026 16:05:57 +0000 Subject: [PATCH 7/8] Add script data to allow persistence of unknown recipes --- .../inventory_sync/module/get_script_data.lua | 1 + .../inventory_sync/module/inventory_sync.lua | 7 ++-- plugins/inventory_sync/module/serialize.lua | 33 +++++++++++++++---- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/plugins/inventory_sync/module/get_script_data.lua b/plugins/inventory_sync/module/get_script_data.lua index eb92ad16..c51a198f 100644 --- a/plugins/inventory_sync/module/get_script_data.lua +++ b/plugins/inventory_sync/module/get_script_data.lua @@ -11,6 +11,7 @@ return function(no_early_return) inventory_sync.players = inventory_sync.players or {} inventory_sync.players_waiting_for_acquire = inventory_sync.players_waiting_for_acquire or {} inventory_sync.players_in_cutscene_to_sync = inventory_sync.players_in_cutscene_to_sync or {} + inventory_sync.failed_deserialization = inventory_sync.failed_deserialization or {} inventory_sync.active_downloads = inventory_sync.active_downloads or {} inventory_sync.finished_downloads = inventory_sync.finished_downloads or {} diff --git a/plugins/inventory_sync/module/inventory_sync.lua b/plugins/inventory_sync/module/inventory_sync.lua index 55a2ce3e..3ec89305 100644 --- a/plugins/inventory_sync/module/inventory_sync.lua +++ b/plugins/inventory_sync/module/inventory_sync.lua @@ -73,7 +73,8 @@ function inventory_sync.serialize_player(player, player_record) end end - local serialized_player = serialize.serialize_player(player) + local failed_deserialization = get_script_data().failed_deserialization[player.name] or {} + local serialized_player = serialize.serialize_player(player, failed_deserialization) serialized_player.generation = player_record.generation return serialized_player @@ -106,7 +107,8 @@ function inventory_sync.deserialize_player(player, finished_record) -- Deserialize downloaded player data local serialized_player = compat.json_to_table(finished_record.data) - serialize.deserialize_player(player, serialized_player) + assert(type(serialized_player) == "table", "wrong type for serialized_player") + script_data.failed_deserialization[player.name] = serialize.deserialize_player(player, serialized_player) -- Restore player position and driving state restore_position(player, finished_record) @@ -385,6 +387,7 @@ inventory_sync.events[defines.events.on_pre_player_left_game] = function(event) player.print("ERROR: Serializing player data failed: " .. result) return end + script_data.failed_deserialization[player.name] = nil script_data.active_uploads[player.name] = { serialized = result, last_attempt = game.ticks_played, diff --git a/plugins/inventory_sync/module/serialize.lua b/plugins/inventory_sync/module/serialize.lua index f2ad87dd..28f4fa06 100644 --- a/plugins/inventory_sync/module/serialize.lua +++ b/plugins/inventory_sync/module/serialize.lua @@ -344,8 +344,9 @@ function serialize.deserialize_crafting_queue(player, serialized) end ---@param player LuaPlayer +---@param failed string[]? ---@return string? -function serialize.serialize_crafting_notifications(player) +function serialize.serialize_crafting_notifications(player, failed) -- Get all unlocked recipes local recipes = {} for _, recipe in pairs(player.force.recipes) do @@ -360,8 +361,8 @@ function serialize.serialize_crafting_notifications(player) end -- Convert to a list of names - local index = 1 - local recipe_names = {} + local recipe_names = failed or {} -- Includes previous recipes if any + local index = #recipe_names + 1 for name in pairs(recipes) do recipe_names[index] = name index = index + 1 @@ -372,6 +373,7 @@ end --- @param player LuaPlayer --- @param serialized string +--- @return string[]? failed Recipe names that do not exist function serialize.deserialize_crafting_notifications(player, serialized) -- Decode the recipe name strings local recipe_names = helpers.json_to_table(assert(helpers.decode_string(serialized))) @@ -379,6 +381,7 @@ function serialize.deserialize_crafting_notifications(player, serialized) local clear_notification = player.clear_recipe_notification local add_notification = player.add_recipe_notification local notifications = {} + local failed = {} -- Assume all recipes need a notification for _, recipe in pairs(player.force.recipes) do @@ -391,6 +394,9 @@ function serialize.deserialize_crafting_notifications(player, serialized) for _, recipe_name in pairs(recipe_names) do if notifications[recipe_name] then notifications[recipe_name] = false + else + -- Recipe does not exist on this server + failed[#failed+1] = recipe_name end end @@ -408,6 +414,9 @@ function serialize.deserialize_crafting_notifications(player, serialized) add_notification(recipe_name) end end + + -- Return failed recipes if any + return next(failed) and failed or nil end local controller_to_name = {} @@ -416,6 +425,7 @@ for name, value in pairs(defines.controllers) do end --- @class SerializedPlayerData +--- @field generation number --- @field name string --- @field controller string --- @field color Color @@ -433,10 +443,12 @@ end --- @field recipe_notifications string? --- @param player LuaPlayer +--- @param failed_deserialization FailedDeserializationPlayerData --- @return SerializedPlayerData -function serialize.serialize_player(player) +function serialize.serialize_player(player, failed_deserialization) --- @type SerializedPlayerData local serialized = { + generation = 0, -- Gets replaced later controller = controller_to_name[player.controller_type], name = player.name, color = player.color, @@ -492,15 +504,21 @@ function serialize.serialize_player(player) -- Serialize recipe notifications if recipe_notifications_api then - serialized.recipe_notifications = serialize.serialize_crafting_notifications(player) + serialized.recipe_notifications = serialize.serialize_crafting_notifications(player, failed_deserialization.recipe_notifications) end return serialized end +--- @class FailedDeserializationPlayerData +--- @field recipe_notifications string[]? + --- @param player LuaPlayer --- @param serialized SerializedPlayerData +--- @return FailedDeserializationPlayerData? function serialize.deserialize_player(player, serialized) + local failed_deserialization = {} + if player.controller_type ~= defines.controllers[serialized.controller] or serialized.controller == "ghost" then -- If targeting the character or ghost controller then create a character if serialized.controller == "character" or serialized.controller == "ghost" then @@ -582,8 +600,11 @@ function serialize.deserialize_player(player, serialized) -- Deserialize recipe notifications if recipe_notifications_api and serialized.recipe_notifications then - serialize.deserialize_crafting_notifications(player, serialized.recipe_notifications) + failed_deserialization.recipe_notifications = + serialize.deserialize_crafting_notifications(player, serialized.recipe_notifications) end + + return next(failed_deserialization) and failed_deserialization or nil end return serialize From 5d9a6cdff634cefbfdccd84a73b8bf2208748c45 Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Fri, 6 Mar 2026 17:17:15 +0000 Subject: [PATCH 8/8] Moved nil failed_deserialization to remove_player --- plugins/inventory_sync/module/inventory_sync.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/inventory_sync/module/inventory_sync.lua b/plugins/inventory_sync/module/inventory_sync.lua index 3ec89305..9805601c 100644 --- a/plugins/inventory_sync/module/inventory_sync.lua +++ b/plugins/inventory_sync/module/inventory_sync.lua @@ -36,6 +36,7 @@ local function remove_player(player) local script_data = get_script_data() script_data.players_waiting_for_acquire[player.name] = nil script_data.players_in_cutscene_to_sync[player.name] = nil + script_data.failed_deserialization[player.name] = nil script_data.active_downloads[player.name] = nil script_data.finished_downloads[player.name] = nil script_data.active_uploads[player.name] = nil @@ -387,7 +388,6 @@ inventory_sync.events[defines.events.on_pre_player_left_game] = function(event) player.print("ERROR: Serializing player data failed: " .. result) return end - script_data.failed_deserialization[player.name] = nil script_data.active_uploads[player.name] = { serialized = result, last_attempt = game.ticks_played,