@@ -22,6 +22,8 @@ public class GetOutOfMySandbox : IPlugin
2222 private Thread _pluginThread ;
2323 private bool _running ;
2424 private static readonly object Locker = new object ( ) ;
25+ readonly List < long > _entitiesAlreadyRemoved = new List < long > ( ) ;
26+ List < long > _identitiesAlreadyRemoved = new List < long > ( ) ;
2527
2628 public void Init ( )
2729 {
@@ -74,10 +76,17 @@ void Instance_WorldSaved( )
7476 XDocument sectorFile = XDocument . Load ( sectorPath ) ;
7577 if ( PluginSettings . Instance . DeleteNpcShips )
7678 {
77- IEnumerable < string > npcIds = sandboxSbc . XPathSelectElements ( "/MyObjectBuilder_Checkpoint/Identities/MyObjectBuilder_Identity[DisplayName='Neutral NPC']" ) . Select ( n => n . Element ( XName . Get ( "IdentityId" ) ) . Value ) ;
78- if ( npcIds . Any ( ) )
79- Log . Info ( "Deleting NPC ships for NPC IDs: {0}" , string . Join ( ", " , npcIds ) ) ;
80- foreach ( string id in npcIds )
79+ long [ ] npcIds =
80+ sandboxSbc . XPathSelectElements ( "/MyObjectBuilder_Checkpoint/Identities/MyObjectBuilder_Identity[DisplayName='Neutral NPC']" )
81+ . Select ( n => long . Parse ( n . Element ( XName . Get ( "IdentityId" ) ) . Value ) )
82+ . ToArray ( ) ;
83+ long [ ] newEntitiesToRemove = npcIds . Except ( _entitiesAlreadyRemoved ) . ToArray ( ) ;
84+ if ( newEntitiesToRemove . Any ( ) )
85+ {
86+ Log . Info ( "Deleting NPC ships for NPC IDs: {0}" , string . Join ( ", " , newEntitiesToRemove ) ) ;
87+ _entitiesAlreadyRemoved . AddRange ( newEntitiesToRemove ) ;
88+ }
89+ foreach ( long id in npcIds )
8190 {
8291 sectorFile . XPathSelectElements ( string . Format ( "/MyObjectBuilder_Sector/SectorObjects/MyObjectBuilder_EntityBase[CubeBlocks/MyObjectBuilder_CubeBlock/Owner='{0}']" , id ) ) . Remove ( ) ;
8392 }
@@ -87,21 +96,24 @@ void Instance_WorldSaved( )
8796 IEnumerable < XElement > allIdentities = sandboxSbc . XPathSelectElements ( "/MyObjectBuilder_Checkpoint/Identities/MyObjectBuilder_Identity/IdentityId" ) ;
8897 IEnumerable < XElement > cubeBlockOwners = sectorFile . XPathSelectElements ( "/MyObjectBuilder_Sector/SectorObjects/MyObjectBuilder_EntityBase/CubeBlocks/MyObjectBuilder_CubeBlock/Owner" ) ;
8998 IEnumerable < XElement > factionMembers = sandboxSbc . XPathSelectElements ( "/MyObjectBuilder_Checkpoint/Factions/Factions/MyObjectBuilder_Faction/Members/MyObjectBuilder_FactionMember/PlayerId" ) ;
90- List < string > idsToRemove = new List < string > ( allIdentities . Select ( o => o . Value ) . Distinct ( ) . ToArray ( ) ) ;
91-
99+ List < long > idsToRemove = allIdentities . Select ( o => long . Parse ( o . Value ) ) . Distinct ( ) . ToList ( ) ;
92100
93- string [ ] cubeBlockOwnerIds = cubeBlockOwners . Select ( o => o . Value ) . Distinct ( ) . ToArray ( ) ;
101+ long [ ] cubeBlockOwnerIds = cubeBlockOwners . Select ( o => long . Parse ( o . Value ) ) . Distinct ( ) . ToArray ( ) ;
94102 idsToRemove . RemoveAll ( o => cubeBlockOwnerIds . Contains ( o ) ) ;
95103
96104 if ( ! PluginSettings . Instance . IgnoreFactionMembership )
97105 {
98- string [ ] factionMemberIds = factionMembers . Select ( f => f . Value ) . Distinct ( ) . ToArray ( ) ;
106+ long [ ] factionMemberIds = factionMembers . Select ( f => long . Parse ( f . Value ) ) . Distinct ( ) . ToArray ( ) ;
99107 idsToRemove . RemoveAll ( o => factionMemberIds . Contains ( o ) ) ;
100108 }
101109
102- foreach ( string i in idsToRemove )
110+ foreach ( long i in idsToRemove )
103111 {
104- Log . Info ( "Removing identity {0}" , i ) ;
112+ if ( ! _identitiesAlreadyRemoved . Contains ( i ) )
113+ {
114+ Log . Info ( "Removing identity {0}" , i ) ;
115+ _identitiesAlreadyRemoved . Add ( i ) ;
116+ }
105117 //Remove toolbar settings, etc
106118 sandboxSbc . XPathSelectElements ( string . Format ( "/MyObjectBuilder_Checkpoint/AllPlayersData/dictionary/item[Value/IdentityId='{0}']" , i ) ) . Remove ( ) ;
107119
0 commit comments