Skip to content
This repository was archived by the owner on Jun 23, 2023. It is now read-only.

Commit 80eb76c

Browse files
committed
Added ability to delete ships which are fully-owned by NPCs from the sector when saving.
Also allows those NPCs to be deleted, at the same time. Fixed some missing removals.
1 parent 4aaf9b6 commit 80eb76c

File tree

3 files changed

+59
-15
lines changed

3 files changed

+59
-15
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
//40
1+
//45
22
//
33
// This code was generated by a tool. Any changes made manually will be lost
44
// the next time this code is regenerated.
55
//
66

77
using System.Reflection;
88

9-
[assembly: AssemblyFileVersion("1.0.0.40")]
10-
[assembly: AssemblyVersion("1.0.0.40")]
9+
[assembly: AssemblyFileVersion("1.0.0.45")]
10+
[assembly: AssemblyVersion("1.0.0.45")]

GetOutOfMySandbox/GetOutOfMySandbox.cs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

GetOutOfMySandbox/PluginSettings.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class PluginSettings
1313
private static PluginSettings _instance;
1414
private bool _loading;
1515
private bool _ignoreFactionMembership;
16+
private bool _deleteNpcShips;
1617

1718
/// <summary>Singleton instance of the <see cref="PluginSettings"/> class.</summary>
1819
internal static PluginSettings Instance { get { return _instance ?? ( _instance = new PluginSettings( ) ); } }
@@ -118,6 +119,17 @@ public bool IgnoreFactionMembership
118119
}
119120
}
120121

122+
[XmlElement( Namespace = "SESE:Plugin:GetOutOfMySandbox", Type = typeof( bool ) )]
123+
public bool DeleteNpcShips
124+
{
125+
get { return _deleteNpcShips; }
126+
set
127+
{
128+
_deleteNpcShips = value;
129+
OnSettingsChanged( );
130+
}
131+
}
132+
121133
public event SettingsChangedEventHandler SettingsChanged;
122134

123135
protected virtual void OnSettingsChanged( )

0 commit comments

Comments
 (0)