void GridClosed(IMyEntity ent)
 {
     try
     {
         var grid = (IMyCubeGrid)ent;
         grid.OnBlockAdded -= BlockAdded;
         grid.OnClose      -= GridClosed;
     }
     catch (Exception e)
     {
         SimpleLog.Error(this, e);
     }
 }
        void BlockMarkedForClose(IMyEntity ent)
        {
            try
            {
                var block = (IMyCubeBlock)ent;
                block.OnMarkForClose -= BlockMarkedForClose;

                blockLogics.GetValueOrDefault(block.EntityId, null)?.Close();
                blockLogics.Remove(block.EntityId);
                ViewDistanceChecks.Remove(block.EntityId);
            }
            catch (Exception e)
            {
                SimpleLog.Error(this, e);
            }
        }
        void WorkingChanged(IMyCubeBlock block)
        {
            try
            {
                if (!inViewRange)
                {
                    return;
                }

                Session.UpdateOnce.Add(this); // update next frame
                SetLights(block.IsWorking);
            }
            catch (Exception e)
            {
                SimpleLog.Error(this, e);
            }
        }
        public void Close()
        {
            try
            {
                if (lights != null)
                {
                    foreach (var light in lights.Values)
                    {
                        MyLights.RemoveLight(light);
                    }

                    lights.Clear();
                }
            }
            catch (Exception e)
            {
                SimpleLog.Error(this, e);
            }
        }
        void EntitySpawned(IMyEntity ent)
        {
            try
            {
                var grid = ent as MyCubeGrid;

                if (grid == null || grid.Physics == null || grid.IsPreview)
                {
                    return;
                }

                grid.OnBlockAdded += BlockAdded;
                grid.OnClose      += GridClosed;

                foreach (IMySlimBlock slim in grid.GetBlocks())
                {
                    BlockAdded(slim);
                }
            }
            catch (Exception e)
            {
                SimpleLog.Error(this, e);
            }
        }