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

Commit 140cc15

Browse files
committed
/admin identity cleanup and /admin faction cleanup; cargo ship spawn logic
1 parent 9031738 commit 140cc15

16 files changed

+524
-148
lines changed
0 Bytes
Binary file not shown.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
//306
1+
//327
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.13.5.306")]
10-
[assembly: AssemblyVersion("1.13.5.306")]
9+
[assembly: AssemblyFileVersion("1.13.5.327")]
10+
[assembly: AssemblyVersion("1.13.5.327")]
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
namespace EssentialsPlugin.ChatHandlers.Admin
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using Sandbox.Engine.Multiplayer;
6+
using Sandbox.Game.Entities.Character;
7+
using Sandbox.ModAPI;
8+
using Utility;
9+
using VRage.Game.ModAPI;
10+
using VRage.ModAPI;
11+
using VRage.Network;
12+
using VRageMath;
13+
14+
public class HandleAdminAsteroidCleanup : ChatHandlerBase
15+
{
16+
public override string GetHelp( )
17+
{
18+
return "Removes asteroids without a player or ship within 1km. Usage: /admin asteroid cleanup";
19+
}
20+
21+
public override string GetCommandText( )
22+
{
23+
return "/admin asteroid cleanup";
24+
}
25+
26+
public override Communication.ServerDialogItem GetHelpDialog( )
27+
{
28+
Communication.ServerDialogItem dialogItem = new Communication.ServerDialogItem
29+
{
30+
title = "Help",
31+
header = "",
32+
content = GetHelp( ),
33+
buttonText = "close"
34+
};
35+
return dialogItem;
36+
}
37+
38+
public override bool IsAdminCommand( )
39+
{
40+
return true;
41+
}
42+
43+
public override bool AllowedInConsole( )
44+
{
45+
return true;
46+
}
47+
48+
private static HashSet<Vector3D> entityPositions = new HashSet<Vector3D>( );
49+
private static Dictionary<Vector3D, IMyVoxelMap> asteroidPositions = new Dictionary<Vector3D, IMyVoxelMap>( );
50+
private static HashSet<IMyVoxelMap> toRemove = new HashSet<IMyVoxelMap>( );
51+
52+
public override bool HandleCommand( ulong userId, string[] words )
53+
{
54+
Essentials.Log.Info( "Asteroid cleanup" );
55+
HashSet<IMyEntity> entities = new HashSet<IMyEntity>( );
56+
Wrapper.GameAction( ( ) =>
57+
{
58+
MyAPIGateway.Entities.GetEntities( entities );
59+
foreach ( IMyEntity entity in entities )
60+
{
61+
if ( entity == null )
62+
continue;
63+
64+
if ( entity is IMyVoxelMap )
65+
asteroidPositions.Add( entity.PositionComp.GetPosition( ), (IMyVoxelMap)entity );
66+
else
67+
entityPositions.Add( entity.PositionComp.GetPosition( ) );
68+
}
69+
} );
70+
//TODO: Use a thread pool to speed this up?
71+
DateTime profile = DateTime.Now;
72+
Communication.SendPrivateInformation( userId, $"Found {asteroidPositions.Count} asteroids." );
73+
foreach ( var asteroid in asteroidPositions )
74+
{
75+
bool found = false;
76+
BoundingSphereD bound = new BoundingSphereD( asteroid.Key, 1000 );
77+
foreach ( Vector3D checkPosition in entityPositions )
78+
{
79+
if ( bound.Contains( checkPosition ) == ContainmentType.Contains )
80+
{
81+
found = true;
82+
break;
83+
}
84+
}
85+
86+
if ( !found )
87+
toRemove.Add( asteroid.Value );
88+
}
89+
Communication.SendPrivateInformation( userId, $"Found {toRemove.Count} asteroids to remove." );
90+
int count = 0;
91+
foreach ( IMyVoxelMap asteroid in toRemove )
92+
{
93+
if ( asteroid == null || asteroid.Closed )
94+
continue;
95+
96+
count++;
97+
98+
Wrapper.GameAction( ( ) => asteroid.Close( ) );
99+
}
100+
Communication.SendPrivateInformation( userId, $"Removed {count} asteroids." );
101+
Essentials.Log.Info( "Asteroid cleanup elapsed time: " + (DateTime.Now - profile) );
102+
return true;
103+
}
104+
}
105+
}
106+
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
namespace EssentialsPlugin.ChatHandlers.Admin
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using Sandbox.Game.Multiplayer;
7+
using Sandbox.Game.World;
8+
using Sandbox.ModAPI;
9+
using Utility;
10+
11+
public class HandleAdminFactionCleanup : ChatHandlerBase
12+
{
13+
public override string GetHelp( )
14+
{
15+
return "Cleans up factions with 1 or fewer members. Usage: /admin faction cleanup (verbose)";
16+
}
17+
18+
public override string GetCommandText( )
19+
{
20+
return "/admin faction cleanup";
21+
}
22+
23+
public override Communication.ServerDialogItem GetHelpDialog( )
24+
{
25+
Communication.ServerDialogItem dialogItem = new Communication.ServerDialogItem
26+
{
27+
title = "Help",
28+
header = "",
29+
content = GetHelp( ),
30+
buttonText = "close"
31+
};
32+
return dialogItem;
33+
}
34+
35+
public override bool IsAdminCommand( )
36+
{
37+
return true;
38+
}
39+
40+
public override bool AllowedInConsole( )
41+
{
42+
return true;
43+
}
44+
45+
public override bool HandleCommand( ulong userId, string[] words )
46+
{
47+
Essentials.Log.Info( "Faction cleanup" );
48+
MyFactionCollection factionCollection = MySession.Static.Factions;
49+
if ( factionCollection == null )
50+
{
51+
Essentials.Log.Info( "Fail" );
52+
return true;
53+
}
54+
HashSet<MyFaction> toDelete = new HashSet<MyFaction>( );
55+
bool verbose = words.Any( ) && words[0].ToLower( ) == "verbose";
56+
57+
foreach ( KeyValuePair<long, MyFaction> item in factionCollection )
58+
{
59+
MyFaction faction = item.Value;
60+
if ( faction == null )
61+
continue;
62+
63+
//get factions with one or fewer members
64+
if ( faction.Members.Count( ) <= 1 )
65+
{
66+
MyPlayer.PlayerId playerId;
67+
68+
//check if the member is a valid player
69+
MyPlayer member;
70+
if ( !MySession.Static.Players.TryGetPlayerId( faction.Members.First( ).Value.PlayerId, out playerId ) )
71+
{
72+
toDelete.Add( faction );
73+
continue;
74+
}
75+
//check if the player is online
76+
//I'm not sure what happens if we delete a faction with a member logged in
77+
//probably nothing, but maybe Clang
78+
member = MySession.Static.Players.GetPlayerById( playerId );
79+
if ( member==null || member.Identity.IsDead )
80+
toDelete.Add( faction );
81+
}
82+
}
83+
84+
Essentials.Log.Info( $"Found {toDelete.Count} factions to delete." );
85+
if(verbose)
86+
Communication.SendPrivateInformation( userId, $"Found {toDelete.Count} factions to delete." );
87+
int count = 0;
88+
foreach ( MyFaction faction in toDelete )
89+
{
90+
if ( faction == null )
91+
continue;
92+
93+
//make sure the faction still exists
94+
if ( !factionCollection.Contains( faction.FactionId ) )
95+
continue;
96+
97+
//NPC factions
98+
if ( faction.IsEveryoneNpc( ) )
99+
continue;
100+
101+
count++;
102+
if ( verbose )
103+
Communication.SendPrivateInformation( userId, $"Removing faction {faction.Tag}: {faction.Name}" );
104+
105+
if(DateTime.Now.Millisecond<10)
106+
Communication.SendPrivateInformation( userId, $"Removed {count} of {toDelete.Count} factions." );
107+
108+
//delete the faction
109+
Wrapper.GameAction( ( ) => MyFactionCollection.RemoveFaction( faction.FactionId ) );
110+
}
111+
112+
Communication.SendPrivateInformation( userId, $"Removed {count} factions with one or fewer members." );
113+
114+
return true;
115+
}
116+
}
117+
}
118+
Lines changed: 79 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,114 @@
11
namespace EssentialsPlugin.ChatHandlers.Admin
22
{
3+
using System;
34
using System.Collections.Generic;
45
using System.Linq;
56
using Sandbox.Game.Multiplayer;
67
using Sandbox.Game.World;
78
using Sandbox.ModAPI;
89
using Utility;
9-
public class HandleAdminIdentityCleanup : ChatHandlerBase
10-
{
10+
using VRage.Game.ModAPI;
11+
using VRage.ModAPI;
1112

12-
public override string GetHelp()
13-
{
14-
return "For testing.";
15-
}
13+
public class HandleAdminIdentityCleanup : ChatHandlerBase
14+
{
15+
public override string GetHelp( )
16+
{
17+
return "Cleans up trash identities. Usage: /admin identity cleanup (verbose)";
18+
}
1619

17-
public override string GetCommandText()
18-
{
19-
return "/admin identity cleanup";
20-
}
20+
public override string GetCommandText( )
21+
{
22+
return "/admin identity cleanup";
23+
}
2124

2225
public override Communication.ServerDialogItem GetHelpDialog( )
2326
{
24-
Communication.ServerDialogItem DialogItem = new Communication.ServerDialogItem( );
25-
DialogItem.title = "Help";
26-
DialogItem.header = "";
27-
DialogItem.content = GetHelp( );
28-
DialogItem.buttonText = "close";
29-
return DialogItem;
27+
Communication.ServerDialogItem dialogItem = new Communication.ServerDialogItem
28+
{
29+
title = "Help",
30+
header = "",
31+
content = GetHelp( ),
32+
buttonText = "close"
33+
};
34+
return dialogItem;
3035
}
3136

32-
public override bool IsAdminCommand()
33-
{
34-
return true;
35-
}
37+
public override bool IsAdminCommand( )
38+
{
39+
return true;
40+
}
3641

37-
public override bool AllowedInConsole()
38-
{
39-
return true;
40-
}
42+
public override bool AllowedInConsole( )
43+
{
44+
return true;
45+
}
4146

42-
public override bool HandleCommand( ulong userId, string[ ] words )
43-
{/*
47+
public override bool HandleCommand( ulong userId, string[] words )
48+
{
4449
MyPlayerCollection playerCollection = MyAPIGateway.Players as MyPlayerCollection;
50+
bool verbose = words.Any( ) && words[0].ToLower( ) == "verbose";
4551

4652
if ( playerCollection == null )
4753
return true;
4854

49-
var identities = playerCollection.GetAllIdentities( ).GroupBy( x => x.DisplayName ).Where( y => y.Count( ) > 1 ).ToList( );
55+
HashSet<long> owners = new HashSet<long>( );
56+
HashSet<MyIdentity> toRemove = new HashSet<MyIdentity>( );
57+
58+
HashSet<IMyEntity> entities = new HashSet<IMyEntity>( );
5059

51-
while ( identities.Count > 0 )
60+
Wrapper.GameAction( ( ) => MyAPIGateway.Entities.GetEntities( entities ) );
61+
62+
foreach ( IMyEntity entity in entities )
5263
{
53-
string compareName = identities[0].Key;
54-
var toDelete = identities.Where(x => x. )
64+
IMyCubeGrid grid = entity as IMyCubeGrid;
65+
if ( grid == null )
66+
continue;
67+
68+
foreach ( long owner in grid.SmallOwners )
69+
owners.Add( owner );
5570
}
5671

57-
int count = identitiesToDelete.Count;
58-
foreach(MyIdentity toDelete in identitiesToDelete)
72+
Dictionary<long, MyIdentity>.ValueCollection myIdentities = playerCollection.GetAllIdentities( );
73+
74+
foreach ( MyIdentity identity in myIdentities )
5975
{
60-
Essentials.Log.Info( $"Deleted dead identity {toDelete.DisplayName}" );
61-
playerCollection.RemoveIdentity( toDelete.IdentityId );
76+
if ( !identity.IsDead )
77+
continue;
78+
79+
if ( !owners.Contains( identity.IdentityId ) )
80+
toRemove.Add( identity );
6281
}
6382

64-
if ( count != 0 )
83+
int count = toRemove.Count;
84+
int removedCount = 0;
85+
Essentials.Log.Info( count );
86+
87+
foreach ( MyIdentity identity in toRemove )
6588
{
66-
Essentials.Log.Info( $"Deleted {count} dead identities." );
89+
//make extra sure the player isn't online
90+
if ( !identity.IsDead )
91+
continue;
92+
93+
//make sure the identity still exists
94+
if ( !playerCollection.HasIdentity( identity.IdentityId ) )
95+
continue;
96+
97+
removedCount++;
98+
if ( verbose )
99+
Communication.SendPrivateInformation( userId, $"Removed identity {identity.DisplayName}: {identity.IdentityId}" );
100+
101+
if ( DateTime.Now.Millisecond <10 )
102+
Communication.SendPrivateInformation( userId, $"Removed {removedCount} of {count} identities." );
103+
104+
105+
Wrapper.GameAction( ( ) => playerCollection.RemoveIdentity( identity.IdentityId ) );
67106
}
68-
*/
69-
return true;
70-
}
71107

72-
}
108+
Communication.SendPrivateInformation( userId, $"Removed {count} identities." );
73109

110+
return true;
111+
}
112+
}
74113
}
75114

0 commit comments

Comments
 (0)