diff --git a/SEModAPIExtensions/API/ChatManager.cs b/SEModAPIExtensions/API/ChatManager.cs index ec51d101..5f086616 100644 --- a/SEModAPIExtensions/API/ChatManager.cs +++ b/SEModAPIExtensions/API/ChatManager.cs @@ -613,89 +613,75 @@ protected void Command_Delete(ChatEvent chatEvent) //All entities #region "All Entities" if (paramCount > 1 && commandParts[1].ToLower().Equals("all")) - { - //All cube grids that have no beacon or only a beacon with no name - if (commandParts[2].ToLower().Equals("nobeacon")) - { - List entities = SectorObjectManager.Instance.GetTypedInternalData(); - List entitiesToDispose = new List(); - foreach (CubeGridEntity entity in entities) - { - while (entity.CubeBlocks.Count == 0) - { - Thread.Sleep(20); - } - List blocks = entity.CubeBlocks; - if (blocks.Count > 0) - { - bool foundBeacon = false; - foreach (CubeBlockEntity cubeBlock in blocks) - { - if (cubeBlock is BeaconEntity) - { - foundBeacon = true; - break; - } - } - if (!foundBeacon) - { - entitiesToDispose.Add(entity); - } - } - } - - foreach (CubeGridEntity entity in entitiesToDispose) - { - bool isLinkedShip = false; - List blocks = entity.CubeBlocks; - foreach (CubeBlockEntity cubeBlock in blocks) - { - if (cubeBlock is MergeBlockEntity) - { - MergeBlockEntity block = (MergeBlockEntity)cubeBlock; - if (block.IsAttached) - { - if (!entitiesToDispose.Contains(block.AttachedCubeGrid)) - { - isLinkedShip = true; - break; - } - } - } - if (cubeBlock is PistonEntity) - { - PistonEntity block = (PistonEntity)cubeBlock; - CubeBlockEntity topBlock = block.TopBlock; - if (topBlock != null) - { - if (!entitiesToDispose.Contains(topBlock.Parent)) - { - isLinkedShip = true; - break; - } - } - } - if (cubeBlock is RotorEntity) - { - RotorEntity block = (RotorEntity)cubeBlock; - CubeBlockEntity topBlock = block.TopBlock; - if (topBlock != null) - { - if (!entitiesToDispose.Contains(topBlock.Parent)) - { - isLinkedShip = true; - break; - } - } - } - } - if (isLinkedShip) - continue; - - entity.Dispose(); - } - - SendPrivateChatMessage(remoteUserId, entitiesToDispose.Count.ToString() + " cube grids have been removed"); + { + //All cube grids that have no beacon except for those attached to a grid with a beacon + if (commandParts[2].ToLower().Equals("nobeacon")) + { + while (SectorObjectManager.Instance.GetTypedInternalData().Count == 0) + { + Thread.Sleep(20); + } + + List entities = SectorObjectManager.Instance.GetTypedInternalData(); + List entitiesToDispose = SectorObjectManager.Instance.GetTypedInternalData(); + + if (entities.Count == 0) + { + ChatManager.Instance.SendPrivateChatMessage(remoteUserId, "No grids found. Try again later."); + return; + } + + foreach (CubeGridEntity entity in entities) + { + while (entity.CubeBlocks.Count == 0) + { + Thread.Sleep(20); + } + + List blocks = entity.CubeBlocks; + + //scan each grid for beacons + foreach (CubeBlockEntity cubeBlock in blocks) + { + if (cubeBlock is BeaconEntity) + { + entitiesToDispose.Remove(entity); + + //if the grid has a beacon remove all grids from entitiesToDispose that are attached to it + foreach (CubeBlockEntity cubeBlock1 in blocks) + { + if (cubeBlock1 is PistonEntity) + { + PistonEntity piston = (PistonEntity)cubeBlock1; + CubeBlockEntity pistonTop = piston.TopBlock; + + if (pistonTop != null) + { + entitiesToDispose.Remove(pistonTop.Parent); + } + } + else if (cubeBlock1 is RotorEntity) + { + RotorEntity rotor = (RotorEntity)cubeBlock1; + CubeBlockEntity rotorTop = rotor.TopBlock; + if (rotorTop != null) + { + entitiesToDispose.Remove(rotorTop.Parent); + } + } + } + //we dont need to scan for further beacons + break; + } + } + } + + foreach (CubeGridEntity entity in entitiesToDispose) + { + entity.Dispose(); + } + + SendPrivateChatMessage(remoteUserId, entitiesToDispose.Count.ToString() + " cube grids have been removed."); } //All cube grids that have no power else if (commandParts[2].ToLower().Equals("nopower"))