Skip to content
Prev Previous commit
Next Next commit
Don't delete NPC identities and factions.
  • Loading branch information
rexxar-tc committed Oct 2, 2018
commit 80c1bb33f38eba2a33d1e1cbe4fca0bf4e773dde
42 changes: 35 additions & 7 deletions Essentials/Commands/WorldModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void CleanIdentities(int days)
}

RemoveEmptyFactions();
FixBlockOwnership();
Context.Respond($"Removed {count} old identities");
}

Expand Down Expand Up @@ -82,6 +83,7 @@ public void PurgeIdentities(int days)
}

RemoveEmptyFactions();
FixBlockOwnership();
Context.Respond($"Removed {count} old identities and {count2} grids owned by them.");
}

Expand All @@ -104,6 +106,9 @@ private static int CleanFaction_Internal(int memberCount = 1)

foreach (var faction in MySession.Static.Factions.ToList())
{
if (faction.Value.IsEveryoneNpc() || !faction.Value.AcceptHumans)
continue;

int validmembers = 0;

//O(2n)
Expand Down Expand Up @@ -160,6 +165,26 @@ private static void RemoveFaction(MyFaction faction)
n.RaiseStaticEvent(_factionChangeSuccessInfo, MyFactionStateChange.RemoveFaction, faction.FactionId, faction.FactionId, 0L, 0L);
}

private static int FixBlockOwnership()
{
int count = 0;
foreach (var entity in MyEntities.GetEntities())
{
var grid = entity as MyCubeGrid;
if (grid == null)
continue;
foreach (var block in grid.GetFatBlocks())
{
if (block.OwnerId == 0 || MySession.Static.Players.HasIdentity(block.OwnerId))
continue;

block.ChangeOwner(0, MyOwnershipShareModeEnum.All);
count++;
}
}
return count;
}

private static readonly FieldInfo GpssField = typeof(MySession).GetField("Gpss", BindingFlags.NonPublic | BindingFlags.Instance);
private static readonly FieldInfo GpsDicField = GpssField.FieldType.GetField("m_playerGpss", BindingFlags.NonPublic | BindingFlags.Instance);
private static readonly FieldInfo SeedParamField = typeof(MyProceduralWorldGenerator).GetField("m_existingObjectsSeeds", BindingFlags.NonPublic | BindingFlags.Instance);
Expand All @@ -174,7 +199,6 @@ public void CleanSandbox()
int count = 0;
var validIdentities = new HashSet<long>();
var idCache = new HashSet<long>();
var allSteamId = new HashSet<ulong>();

//find all identities owning a block
foreach (var entity in MyEntities.GetEntities())
Expand All @@ -196,14 +220,15 @@ public void CleanSandbox()
//clean identities that don't own any blocks, or don't have a steam ID for whatever reason
foreach (var identity in MySession.Static.Players.GetAllIdentities().ToList())
{
if (MySession.Static.Players.IdentityIsNpc(identity.IdentityId))
{
validIdentities.Add(identity.IdentityId);
continue;
}

if (validIdentities.Contains(identity.IdentityId))
{
var steam = MySession.Static.Players.TryGetSteamId(identity.IdentityId);
if (steam != 0)
{
allSteamId.Add(steam);
continue;
}
continue;
}

RemoveFromFaction_Internal(identity);
Expand All @@ -212,6 +237,9 @@ public void CleanSandbox()
count++;
}

//reset ownership of blocks belonging to deleted identities
count += FixBlockOwnership();

//clean up empty factions
count += CleanFaction_Internal();

Expand Down