From 9c8ee0c6c9399f4c16fdcbbebdf8534e70ce35cf Mon Sep 17 00:00:00 2001 From: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> Date: Sun, 7 May 2023 11:38:56 +1200 Subject: [PATCH] Fix invalid followers being saved in maps (#16146) --- Content.Server/Maps/MapMigrationSystem.cs | 1 + Content.Shared/Follower/FollowerSystem.cs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/Content.Server/Maps/MapMigrationSystem.cs b/Content.Server/Maps/MapMigrationSystem.cs index ce11f15fdb..f0c19e1414 100644 --- a/Content.Server/Maps/MapMigrationSystem.cs +++ b/Content.Server/Maps/MapMigrationSystem.cs @@ -4,6 +4,7 @@ using System.Linq; using Robust.Server.GameObjects; using Robust.Server.Maps; using Robust.Shared.ContentPack; +using Robust.Shared.Map.Events; using Robust.Shared.Prototypes; using Robust.Shared.Serialization.Markdown; using Robust.Shared.Serialization.Markdown.Mapping; diff --git a/Content.Shared/Follower/FollowerSystem.cs b/Content.Shared/Follower/FollowerSystem.cs index 9547cca0ce..b7dbb28cf9 100644 --- a/Content.Shared/Follower/FollowerSystem.cs +++ b/Content.Shared/Follower/FollowerSystem.cs @@ -5,6 +5,7 @@ using Content.Shared.Movement.Events; using Content.Shared.Movement.Systems; using Content.Shared.Verbs; using Robust.Shared.Map; +using Robust.Shared.Map.Events; using Robust.Shared.Utility; namespace Content.Shared.Follower; @@ -20,6 +21,25 @@ public sealed class FollowerSystem : EntitySystem SubscribeLocalEvent>(OnGetAlternativeVerbs); SubscribeLocalEvent(OnFollowerMove); SubscribeLocalEvent(OnFollowedTerminating); + SubscribeLocalEvent(OnBeforeSave); + } + + private void OnBeforeSave(BeforeSaveEvent ev) + { + // Some followers will not be map savable. This ensures that maps don't get saved with empty/invalid + // followers, but just stopping any following on the map being saved. + + var query = AllEntityQuery(); + while (query.MoveNext(out var uid, out var follower, out var xform, out var meta)) + { + if (meta.EntityPrototype == null || meta.EntityPrototype.MapSavable) + continue; + + if (xform.MapUid != ev.Map) + continue; + + StopFollowingEntity(uid, follower.Following); + } } private void OnGetAlternativeVerbs(GetVerbsEvent ev) -- 2.51.2