SEToolbox.Models.ExplorerModel.RemoveEntity C# (CSharp) Method

RemoveEntity() public method

public RemoveEntity ( MyObjectBuilder_EntityBase entity ) : bool
entity MyObjectBuilder_EntityBase
return bool
        public bool RemoveEntity(MyObjectBuilder_EntityBase entity)
        {
            if (entity != null)
            {
                if (ActiveWorld.SectorData.SectorObjects.Contains(entity))
                {
                    if (entity is MyObjectBuilder_VoxelMap)
                    {
                        SpaceEngineersCore.ManageDeleteVoxelList.Add(((MyObjectBuilder_VoxelMap)entity).StorageName + MyVoxelMap.V2FileExtension);
                    }

                    ActiveWorld.SectorData.SectorObjects.Remove(entity);
                    IsModified = true;
                    return true;
                }

                // TODO: write as linq;
                //var x = SectorData.SectorObjects.Where(s => s is MyObjectBuilder_CubeGrid).Cast<MyObjectBuilder_CubeGrid>().
                //    Where(s => s.CubeBlocks.Any(e => e is MyObjectBuilder_Cockpit && ((MyObjectBuilder_Cockpit)e).Pilot == entity)).Select(e => e).ToArray();

                foreach (var sectorObject in ActiveWorld.SectorData.SectorObjects)
                {
                    if (sectorObject is MyObjectBuilder_CubeGrid)
                    {
                        foreach (var cubeGrid in ((MyObjectBuilder_CubeGrid)sectorObject).CubeBlocks)
                        {
                            if (cubeGrid is MyObjectBuilder_Cockpit)
                            {
                                var cockpit = cubeGrid as MyObjectBuilder_Cockpit;
                                if (cockpit.Pilot == entity)
                                {
                                    cockpit.Pilot = null;
                                    var structure = Structures.FirstOrDefault(s => s.EntityBase == sectorObject) as StructureCubeGridModel;
                                    structure.Pilots--;
                                    return true;
                                }
                            }
                        }
                    }
                }
            }

            return false;
        }