private void grid_OnBlockOwnershipChanged(MyCubeGrid obj) { bool flag = false; foreach (long num in obj.BigOwners) { MyFaction playerFaction = MySession.Static.Factions.GetPlayerFaction(num); if ((playerFaction != null) && !playerFaction.IsEveryoneNpc()) { flag = true; break; } } foreach (long num2 in obj.SmallOwners) { MyFaction playerFaction = MySession.Static.Factions.GetPlayerFaction(num2); if ((playerFaction != null) && !playerFaction.IsEveryoneNpc()) { flag = true; break; } } if (flag) { obj.Components.Remove <MyUpdateTriggerComponent>(); obj.OnBlockOwnershipChanged -= new Action <MyCubeGrid>(this.grid_OnBlockOwnershipChanged); } }
public override bool HandleCommand(ulong userId, string[] words) { Essentials.Log.Info("Faction cleanup"); MyFactionCollection factionCollection = MySession.Static.Factions; if (factionCollection == null) { Essentials.Log.Info("Fail"); return(true); } HashSet <MyFaction> toDelete = new HashSet <MyFaction>( ); bool verbose = words.Any( ) && words[0].ToLower( ) == "verbose"; foreach (KeyValuePair <long, MyFaction> item in factionCollection) { MyFaction faction = item.Value; if (faction == null) { continue; } //get factions with one or fewer members if (faction.Members.Count( ) <= 1) { MyPlayer.PlayerId playerId; //check if the member is a valid player MyPlayer member; if (!MySession.Static.Players.TryGetPlayerId(faction.Members.First( ).Value.PlayerId, out playerId)) { toDelete.Add(faction); continue; } //check if the player is online //I'm not sure what happens if we delete a faction with a member logged in //probably nothing, but maybe Clang member = MySession.Static.Players.GetPlayerById(playerId); if (member == null || member.Identity.IsDead) { toDelete.Add(faction); } } } Essentials.Log.Info($"Found {toDelete.Count} factions to delete."); if (verbose) { Communication.SendPrivateInformation(userId, $"Found {toDelete.Count} factions to delete."); } int count = 0; foreach (MyFaction faction in toDelete) { if (faction == null) { continue; } //make sure the faction still exists if (!factionCollection.Contains(faction.FactionId)) { continue; } //NPC factions if (faction.IsEveryoneNpc( )) { continue; } count++; if (verbose) { Communication.SendPrivateInformation(userId, $"Removing faction {faction.Tag}: {faction.Name}"); } if (DateTime.Now.Millisecond < 10) { Communication.SendPrivateInformation(userId, $"Removed {count} of {toDelete.Count} factions."); } //delete the faction Wrapper.GameAction(() => MyFactionCollection.RemoveFaction(faction.FactionId)); } Communication.SendPrivateInformation(userId, $"Removed {count} factions with one or fewer members."); return(true); }
private Dictionary <long, PurchaseCollection> FindBuyables() { var factionCollection = MySession.Static.Factions; var playerCollection = MySession.Static.Players; var identities = playerCollection.GetAllIdentities(); var result = new Dictionary <long, PurchaseCollection>(); var config = Plugin.Config; foreach (var identity in identities) { long identityId = identity.IdentityId; MyFaction faction = factionCollection.GetPlayerFaction(identityId); /* If we dont want to include factionless players we have to filter here now */ if (!config.IncludePlayersWithoutFaction && faction == null) { continue; } bool isNpc = playerCollection.IdentityIsNpc(identityId); bool mustCheckPcu = !isNpc || config.MinPCUAlsoForNPC; /* If we dont want to find players below a certain PCU limit filter here now */ if (mustCheckPcu && config.MinPCUToBeFound > identity.BlockLimits.PCUBuilt) { continue; } var modes = new PurchaseCollection(); if (isNpc) { bool validFaction = faction != null && faction.IsEveryoneNpc() && faction.Stations.Count > 0; if (validFaction) { modes.Add(PurchaseMode.NPC); } } else { if (playerCollection.IsPlayerOnline(identityId)) { modes.Add(PurchaseMode.ONLINE); } else { var lastSeen = PlayerUtils.GetLastSeenDate(identity); /* How many minutes ago does still count as online? */ var maxOnlineDate = DateTime.Now.AddMinutes(-config.LastOnlineMinutes); if (lastSeen >= maxOnlineDate) { modes.Add(PurchaseMode.ONLINE); } if (config.OfflineLongerThanHours > 0) { /* How many hours ago does mean its inactive? */ var minOnlineInactiveDate = DateTime.Now.AddHours(-config.OfflineLongerThanHours); if (lastSeen < minOnlineInactiveDate) { modes.Add(PurchaseMode.INACTIVE); } } } } if (modes.Count > 0) { result.Add(identityId, modes); } } return(result); }