@@ -70,13 +70,25 @@ void Instance_WorldSaved( )
7070 {
7171 string sandboxPath = Path . Combine ( Server . Instance . Config . LoadWorld , "Sandbox.sbc" ) ;
7272 XDocument sandboxSbc = XDocument . Load ( sandboxPath ) ;
73- XDocument sectorFile = XDocument . Load ( Path . Combine ( Server . Instance . Config . LoadWorld , "SANDBOX_0_0_0_.sbs" ) ) ;
73+ string sectorPath = Path . Combine ( Server . Instance . Config . LoadWorld , "SANDBOX_0_0_0_.sbs" ) ;
74+ XDocument sectorFile = XDocument . Load ( sectorPath ) ;
75+ if ( PluginSettings . Instance . DeleteNpcShips )
76+ {
77+ IEnumerable < string > npcIds = sandboxSbc . XPathSelectElements ( "/MyObjectBuilder_Checkpoint/Identities/MyObjectBuilder_Identity[DisplayName='Neutral NPC']" ) . Select ( n => n . Element ( XName . Get ( "IdentityId" ) ) . Value ) ;
78+ Log . Info ( "Deleting NPC ships for NPC IDs: {0}" , string . Join ( ", " , npcIds ) ) ;
79+ foreach ( string id in npcIds )
80+ {
81+ sectorFile . XPathSelectElements ( string . Format ( "/MyObjectBuilder_Sector/SectorObjects/MyObjectBuilder_EntityBase[CubeBlocks/MyObjectBuilder_CubeBlock/Owner='{0}']" , id ) ) . Remove ( ) ;
82+ }
83+ }
84+
85+
7486 IEnumerable < XElement > allIdentities = sandboxSbc . XPathSelectElements ( "/MyObjectBuilder_Checkpoint/Identities/MyObjectBuilder_Identity/IdentityId" ) ;
7587 IEnumerable < XElement > cubeBlockOwners = sectorFile . XPathSelectElements ( "/MyObjectBuilder_Sector/SectorObjects/MyObjectBuilder_EntityBase/CubeBlocks/MyObjectBuilder_CubeBlock/Owner" ) ;
7688 IEnumerable < XElement > factionMembers = sandboxSbc . XPathSelectElements ( "/MyObjectBuilder_Checkpoint/Factions/Factions/MyObjectBuilder_Faction/Members/MyObjectBuilder_FactionMember/PlayerId" ) ;
77-
7889 List < string > idsToRemove = new List < string > ( allIdentities . Select ( o => o . Value ) . Distinct ( ) . ToArray ( ) ) ;
7990
91+
8092 string [ ] cubeBlockOwnerIds = cubeBlockOwners . Select ( o => o . Value ) . Distinct ( ) . ToArray ( ) ;
8193 idsToRemove . RemoveAll ( o => cubeBlockOwnerIds . Contains ( o ) ) ;
8294
@@ -90,25 +102,27 @@ void Instance_WorldSaved( )
90102 {
91103 Log . Info ( "Removing identity {0}" , i ) ;
92104 //Remove toolbar settings, etc
93- XElement settingsElement = sandboxSbc . XPathSelectElement ( string . Format ( "/MyObjectBuilder_Checkpoint/AllPlayersData/dictionary/item[Value/IdentityId='{0}']" , i ) ) ;
94- if ( settingsElement != null )
95- {
96- settingsElement . Remove ( ) ;
97- }
105+ sandboxSbc . XPathSelectElements ( string . Format ( "/MyObjectBuilder_Checkpoint/AllPlayersData/dictionary/item[Value/IdentityId='{0}']" , i ) ) . Remove ( ) ;
98106
99107 //Remove from factions
100108 sandboxSbc . XPathSelectElements ( string . Format ( "/MyObjectBuilder_Checkpoint/Factions/Factions/MyObjectBuilder_Faction/Members/MyObjectBuilder_FactionMember[PlayerId='{0}']" , i ) ) . Remove ( ) ;
109+ sandboxSbc . XPathSelectElements ( string . Format ( "/MyObjectBuilder_Checkpoint/Factions/Players/dictionary/item[Key='{0}']" , i ) ) . Remove ( ) ;
110+
101111 //Remove chat history
102- //XElement chatHistoryElement = sandboxSbc.XPathSelectElement( string.Format( "/MyObjectBuilder_Checkpoint/ChatHistory/MyObjectBuilder_ChatHistory/PlayerChatHistory/MyObjectBuilder_PlayerChatHistory[ID='{0}']", i ) );
103- //chatHistoryElement.Remove( );
104- //sandboxSbc.XPathSelectElement( string.Format( "/MyObjectBuilder_Checkpoint/ChatHistory/MyObjectBuilder_ChatHistory/[IdentityId='{0}']", i ) ).Remove( );
112+ //Stuff other people have received
113+ sandboxSbc . XPathSelectElements ( string . Format ( "/MyObjectBuilder_Checkpoint/ChatHistory/MyObjectBuilder_ChatHistory/PlayerChatHistory/MyObjectBuilder_PlayerChatHistory[ID='{0}']" , i ) ) . Remove ( ) ;
114+ //Stuff this person has sent
115+ sandboxSbc . XPathSelectElements ( string . Format ( "/MyObjectBuilder_Checkpoint/ChatHistory/MyObjectBuilder_ChatHistory[IdentityId='{0}']" , i ) ) . Remove ( ) ;
116+
105117 //Remove GPS entries
106- sandboxSbc . XPathSelectElements ( string . Format ( "/MyObjectBuilder_Checkpoint/Gps/dictionary/item[Key='{0}']" , i ) ) . Remove ( ) ;
118+ sandboxSbc . XPathSelectElements ( string . Format ( "/MyObjectBuilder_Checkpoint/Gps/dictionary/item[Key='{0}']" , i ) ) . Remove ( ) ;
119+
107120 //Remove the identity
108- sandboxSbc . XPathSelectElement ( string . Format ( "/MyObjectBuilder_Checkpoint/Identities/MyObjectBuilder_Identity[IdentityId='{0}']" , i ) ) . Remove ( ) ;
121+ sandboxSbc . XPathSelectElements ( string . Format ( "/MyObjectBuilder_Checkpoint/Identities/MyObjectBuilder_Identity[IdentityId='{0}']" , i ) ) . Remove ( ) ;
109122 }
110123
111124 sandboxSbc . Save ( sandboxPath ) ;
125+ sectorFile . Save ( sectorPath ) ;
112126 }
113127 catch ( FileNotFoundException ex )
114128 {
@@ -176,5 +190,23 @@ public bool IgnoreFactionMembership
176190 }
177191 }
178192
193+ [ Category ( "Behavior" ) ]
194+ [ Description ( "Delete CubeGrids that are owned only by NPCs." ) ]
195+ [ Browsable ( true ) ]
196+ [ ReadOnly ( false ) ]
197+ [ DisplayName ( "Delete NPC Ships" ) ]
198+ public bool DeleteNpcShips
199+ {
200+ get
201+ {
202+ return PluginSettings . Instance . DeleteNpcShips ;
203+ }
204+
205+ set
206+ {
207+ PluginSettings . Instance . DeleteNpcShips = value ;
208+ }
209+ }
210+
179211 }
180212}
0 commit comments