// admin deletearea x y z radius
        public override bool HandleCommand(ulong userId, string command)
        {
            string[] words = command.Split(' ');
            if (words.Length > 3)
            {
                return(false);
            }

            if (!words.Any())
            {
                Communication.SendPrivateInformation(userId, GetHelp());
                return(true);
            }

            int days = -1;

            if (!int.TryParse(words[0], out days) || days < 0)
            {
                Communication.SendPrivateInformation(userId, string.Format("Invalid argument.  Days argument must be an integer that is 0 or greater."));
                return(true);
            }

            // Just assume that anything after the days is going to "ignorenologin"
            bool removeNoLoginInformation = true;
            bool removeOwnerless          = true;

            if (words.Count() > 1)
            {
                foreach (string word in words)
                {
                    if (word.ToLower() == "ignorenologin")
                    {
                        removeNoLoginInformation = false;
                    }
                    if (word.ToLower() == "ignoreownerless")
                    {
                        removeOwnerless = false;
                    }
                }
            }

            Communication.SendPrivateInformation(userId, string.Format("Scanning for grids with owners that haven't logged in {0} days.  (Must Have Login Info={1})", days, removeNoLoginInformation));

            HashSet <IMyEntity> entities = new HashSet <IMyEntity>();

            Wrapper.GameAction(() =>
            {
                MyAPIGateway.Entities.GetEntities(entities, x => x is IMyCubeGrid);
            });

            HashSet <IMyEntity> entitiesFound = new HashSet <IMyEntity>();

            foreach (IMyEntity entity in entities)
            {
                if (!(entity is IMyCubeGrid))
                {
                    continue;
                }

                IMyCubeGrid              grid        = (IMyCubeGrid)entity;
                CubeGridEntity           gridEntity  = (CubeGridEntity)GameEntityManager.GetEntity(grid.EntityId);
                MyObjectBuilder_CubeGrid gridBuilder = CubeGrids.SafeGetObjectBuilder(grid);
                if (gridBuilder == null)
                {
                    continue;
                }

                // This entity is protected by whitelist
                if (PluginSettings.Instance.LoginEntityWhitelist.Length > 0 && PluginSettings.Instance.LoginEntityWhitelist.Contains(grid.EntityId.ToString()))
                {
                    continue;
                }

                if (CubeGrids.GetAllOwners(gridBuilder).Count < 1 && removeOwnerless)
                {
                    Communication.SendPrivateInformation(userId, string.Format("Found entity '{0}' ({1}) not owned by anyone.", gridEntity.Name, entity.EntityId));
                    entitiesFound.Add(entity);
                    continue;
                }

                foreach (long player in CubeGrids.GetBigOwners(gridBuilder))
                {
                    // This playerId is protected by whitelist
                    if (PluginSettings.Instance.LoginPlayerIdWhitelist.Length > 0 && PluginSettings.Instance.LoginPlayerIdWhitelist.Contains(player.ToString()))
                    {
                        continue;
                    }

                    MyObjectBuilder_Checkpoint.PlayerItem checkItem = PlayerMap.Instance.GetPlayerItemFromPlayerId(player);
                    if (checkItem.IsDead || checkItem.Name == "")
                    {
                        Communication.SendPrivateInformation(userId, string.Format("Found entity '{0}' ({1}) owned by dead player - ID: {2}", gridEntity.Name, entity.EntityId, player));
                        entitiesFound.Add(entity);
                        continue;
                    }

                    PlayerItem item = Players.Instance.GetPlayerById(player);
                    if (item == null)
                    {
                        if (removeNoLoginInformation)
                        {
                            Communication.SendPrivateInformation(userId, string.Format("Found entity '{0}' ({1}) owned by a player with no login info: {2}", gridEntity.Name, entity.EntityId, checkItem.Name));
                            entitiesFound.Add(entity);
                        }
                    }
                    else if (item.LastLogin < DateTime.Now.AddDays(days * -1))
                    {
                        Communication.SendPrivateInformation(userId, string.Format("Found entity '{0}' ({1}) owned by inactive player: {2}", gridEntity.Name, entity.EntityId, PlayerMap.Instance.GetPlayerItemFromPlayerId(player).Name));
                        entitiesFound.Add(entity);
                    }
                }
            }

            Communication.SendPrivateInformation(userId, string.Format("Found {0} grids owned by inactive users", entitiesFound.Count));

            foreach (IMyEntity entity in entitiesFound)
            {
                if (!(entity is IMyCubeGrid))
                {
                    continue;
                }

                CubeGridEntity gridEntity = new CubeGridEntity((MyObjectBuilder_CubeGrid)entity.GetObjectBuilder(), entity);
                gridEntity.Dispose();
            }

            Communication.SendPrivateInformation(userId, string.Format("Removed {0} grids owned by inactive users", entitiesFound.Count));
            return(true);
        }
Ejemplo n.º 2
0
        public override bool HandleCommand(ulong userId, string[] words)
        {
            int  page   = 1;
            bool dialog = false;

            if (words.Length > 0)
            {
                if (words[0] == "dialog")
                {
                    dialog = true;
                }

                int.TryParse(words[0], out page);
                if (page < 1)
                {
                    page = 1;
                }
            }

            if (PlayerMap.Instance.GetPlayerIdsFromSteamId(userId).Count < 1 && userId != 0)
            {
                Communication.SendPrivateInformation(userId, "Error occurred while processing this command. (1)");
                return(true);
            }

            long playerId = 0;

            if (PlayerMap.Instance.GetPlayerIdsFromSteamId(userId).Count > 0)
            {
                playerId = PlayerMap.Instance.GetPlayerIdsFromSteamId(userId).First();
            }

            HashSet <IMyEntity> entities = new HashSet <IMyEntity>();
            List <IMyPlayer>    players  = new List <IMyPlayer>();

            Wrapper.GameAction(() =>
            {
                MyAPIGateway.Entities.GetEntities(entities, x => x is IMyCubeGrid);
                MyAPIGateway.Players.GetPlayers(players);
            });

            IMyPlayer player = players.FirstOrDefault(x => x.SteamUserId == userId);

            string result = "";
            int    count  = 0;

            foreach (IMyEntity entity in entities)
            {
                IMyCubeGrid grid = (IMyCubeGrid)entity;

                MyObjectBuilder_CubeGrid gridBuilder = CubeGrids.SafeGetObjectBuilder(grid);
                if (grid == null)
                {
                    continue;
                }

                if (CubeGrids.GetAllOwners(gridBuilder).Contains(playerId))
                {
                    if (result != "")
                    {
                        result += "\r\n";
                    }

                    if (CubeGrids.IsFullOwner(gridBuilder, playerId, player) && !dialog)
                    {
                        result += string.Format("Grid '{0}' at {2}", grid.DisplayName, grid.EntityId, ShowCoordinates(grid.GetPosition()));
                    }
                    else if (CubeGrids.IsFullOwner(gridBuilder, playerId, player) && dialog)
                    {
                        result += string.Format("{0} - {1} - {2}bl - {3}", grid.DisplayName, ShowCoordinates(grid.GetPosition()), gridBuilder.CubeBlocks.Count, gridBuilder.GridSizeEnum);
                    }
                    else
                    {
                        result += string.Format("Grid '{0}'", grid.DisplayName, grid.EntityId);
                    }

                    count++;
                }
            }

            if (dialog)
            {
                Communication.SendClientMessage(userId, string.Format("/dialog \"{0}\" \"{1}\" \"{2}\" \"{3}\" \"{4}\"", "User Grid List", "Ships / Stations you ", "own:", result.Replace("\r\n", "|"), "OK"));
                return(true);
            }

            string[] resultLines = result.Split(new string[] { "\r\n" }, StringSplitOptions.None);

            int pages = ((resultLines.Length - 1) / 6) + 1;

            if (page > pages)
            {
                page = pages;
            }

            string output = "";

            for (int r = ((page - 1) * 6); r < resultLines.Length && r < ((page) * 6); r++)
            {
                string line = resultLines[r];
                if (output != "")
                {
                    output += "\r\n";
                }

                output += line;
            }
            Communication.SendPrivateInformation(userId, output);

            Communication.SendPrivateInformation(userId, string.Format("Displaying page {0} of {1} - {2} grids", page, pages, count));
            return(true);
        }