Add recipe notification sync to inventory sync

This commit is contained in:
Cooldude2606 committed 2026-03-01 23:58:57 +00:00
1 parent 94692dc418
commit 412c291193
2 files changed
+48 -18

No files matched your search

+1
View File
@@ -18,6 +18,7 @@ export type IpcPlayerData = {
hotbar?: string[], hotbar?: string[],
personal_logistic_slots?: {name:string, min:number, max:number}[], personal_logistic_slots?: {name:string, min:number, max:number}[],
crafting_queue?: any, crafting_queue?: any,
recipe_notifications?: string[],
} }
const jsonPlayerData = Type.Object({ const jsonPlayerData = Type.Object({
+47 -18
View File
@@ -6,6 +6,7 @@ local serialize = {}
local v2_logistic_api = compat.version_ge("2.0.0") local v2_logistic_api = compat.version_ge("2.0.0")
local v2_storage_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) function serialize.serialize_inventories(source, inventories)
local serialized = {} local serialized = {}
@@ -347,22 +348,27 @@ for name, value in pairs(defines.controllers) do
controller_to_name[value] = name controller_to_name[value] = name
end end
-- Players are serialized into a table with the following fields: --- @class SerializedPlayerData
-- name --- @field name string
-- controller: may not be cutscene or editor --- @field controller string
-- color --- @field color Color
-- chat_color --- @field chat_color Color
-- tag --- @field tag string
-- force --- @field force string
-- cheat_mod --- @field cheat_mode boolean
-- flashlight --- @field flashlight boolean
-- ticks_to_respawn (optional) --- @field ticks_to_respawn number
-- character (optional) --- @field character table<string, any>?
-- inventories: non-character inventories --- @field inventories table<string, table>?
-- hotbar: table of string indexes to names (optional) --- @field hotbar table<string, string>?
-- personal_logistic_slots (optional) --- @field personal_logistic_slots table?
-- crafting_queue (optional) --- @field crafting_queue table?
--- @field recipe_notifications string[]?
--- @param player LuaPlayer
--- @return SerializedPlayerData
function serialize.serialize_player(player) function serialize.serialize_player(player)
--- @type SerializedPlayerData
local serialized = { local serialized = {
controller = controller_to_name[player.controller_type], controller = controller_to_name[player.controller_type],
name = player.name, name = player.name,
@@ -385,7 +391,9 @@ function serialize.serialize_player(player)
-- Go back to waiting for respawn -- Go back to waiting for respawn
local character = player.character local character = player.character
player.ticks_to_respawn = serialized.ticks_to_respawn player.ticks_to_respawn = serialized.ticks_to_respawn
character.destroy() if character and character.valid then
character.destroy()
end
end end
-- Serialize character -- Serialize character
@@ -406,7 +414,7 @@ function serialize.serialize_player(player)
if not serialized.hotbar then if not serialized.hotbar then
serialized.hotbar = {} serialized.hotbar = {}
end end
serialized.hotbar[tostring(i)] = slot.name serialized.hotbar[tostring(i)] = slot.name --[[@as string]]
end end
end end
@@ -415,9 +423,20 @@ function serialize.serialize_player(player)
serialized.crafting_queue = serialize.serialize_crafting_queue(player) serialized.crafting_queue = serialize.serialize_crafting_queue(player)
end 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 return serialized
end end
--- @param player LuaPlayer
--- @param serialized SerializedPlayerData
function serialize.deserialize_player(player, serialized) function serialize.deserialize_player(player, serialized)
if player.controller_type ~= defines.controllers[serialized.controller] or serialized.controller == "ghost" then 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 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.god })
player.set_controller({ type = defines.controllers.ghost }) player.set_controller({ type = defines.controllers.ghost })
end end
character.destroy() if character and character.valid then
character.destroy()
end
end end
else else
@@ -496,6 +517,14 @@ function serialize.deserialize_player(player, serialized)
serialize.deserialize_crafting_queue(player, serialized.crafting_queue) serialize.deserialize_crafting_queue(player, serialized.crafting_queue)
end 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 end
return serialize return serialize