private static void Process(IMyEntity entity, MessageSyncEntity syncEntity)
 {
     if (MyAPIGateway.Multiplayer.MultiplayerActive)
         ConnectionHelper.SendMessageToAll(syncEntity);
     else
         syncEntity.CommonProcess(entity);
 }
 private static void Process(IMyEntity entity, MessageSyncEntity syncEntity)
 {
     if (MyAPIGateway.Multiplayer.MultiplayerActive)
     {
         ConnectionHelper.SendMessageToAll(syncEntity);
     }
     else
     {
         CommonProcess(entity, syncEntity.SyncType, syncEntity.Velocity, syncEntity.Position, syncEntity.Matrix);
     }
 }
        private void CommonProcess(ulong steamId, SyncGridChangeType syncType, long entityId, string searchEntity, long playerId, bool switchOn)
        {
            List <IMyCubeGrid> selectedShips = new List <IMyCubeGrid>();

            if (entityId != 0)
            {
                var selectedShip = MyAPIGateway.Entities.GetEntityById(entityId) as IMyCubeGrid;
                if (selectedShip != null)
                {
                    selectedShips.Add(selectedShip);
                }
            }
            else if (!string.IsNullOrEmpty(searchEntity))
            {
                // exact name search.
                var currentShipList = new HashSet <IMyEntity>();
                MyAPIGateway.Entities.GetEntities(currentShipList, e => e is IMyCubeGrid && e.DisplayName.Equals(searchEntity, StringComparison.InvariantCultureIgnoreCase));
                selectedShips.AddRange(currentShipList.Cast <IMyCubeGrid>());

                if (currentShipList.Count == 0)
                {
                    // hotlist search.
                    int index;
                    List <IMyEntity> shipCache = CommandListShips.GetShipCache(steamId);
                    if (searchEntity.Substring(0, 1) == "#" && int.TryParse(searchEntity.Substring(1), out index) && index > 0 && index <= shipCache.Count)
                    {
                        selectedShips.Add((IMyCubeGrid)shipCache[index - 1]);
                    }
                }
            }

            if (selectedShips.Count == 0)
            {
                MessageClientTextMessage.SendMessage(steamId, "Server", "No ships selected or found.");
                return;
            }

            switch (syncType)
            {
            case SyncGridChangeType.OwnerClaim:
            {
                var players = new List <IMyPlayer>();
                MyAPIGateway.Players.GetPlayers(players, p => p != null && p.PlayerID == playerId);
                IMyPlayer player = players.FirstOrDefault();

                if (player == null)
                {
                    return;
                }

                foreach (var selectedShip in selectedShips)
                {
                    var grids = selectedShip.GetAttachedGrids(AttachedGrids.Static);
                    foreach (var grid in grids)
                    {
                        grid.ChangeGridOwnership(player.PlayerID, MyOwnershipShareModeEnum.All);
                    }

                    MessageClientTextMessage.SendMessage(steamId, "Server", string.Format("Grid {0} Claimed by player {1}.", selectedShip.DisplayName, player.DisplayName));
                }
            }
            break;

            case SyncGridChangeType.OwnerRevoke:
            {
                foreach (var selectedShip in selectedShips)
                {
                    var grids = selectedShip.GetAttachedGrids(AttachedGrids.Static);
                    foreach (var grid in grids)
                    {
                        grid.ChangeGridOwnership(0, MyOwnershipShareModeEnum.All);
                    }
                    MessageClientTextMessage.SendMessage(steamId, "Server", string.Format("Grid {0} Revoked of all ownership.", selectedShip.DisplayName));
                }
            }
            break;

            case SyncGridChangeType.OwnerShareAll:
            {
                foreach (var selectedShip in selectedShips)
                {
                    var grids = selectedShip.GetAttachedGrids(AttachedGrids.Static);
                    foreach (var grid in grids)
                    {
                        var blocks = new List <IMySlimBlock>();
                        grid.GetBlocks(blocks, f => f.FatBlock != null && f.FatBlock.OwnerId != 0);

                        foreach (var block in blocks)
                        {
                            block.FatBlock.ChangeOwner(block.FatBlock.OwnerId, MyOwnershipShareModeEnum.All);
                        }
                    }
                    MessageClientTextMessage.SendMessage(steamId, "Server", string.Format("Grid {0} Shared.", selectedShip.DisplayName));
                }
            }
            break;

            case SyncGridChangeType.SwitchOnPower:
            {
                int reactors;
                int batteries;
                TurnOnShips(selectedShips, out reactors, out batteries);
                MyAPIGateway.Utilities.SendMessage(steamId, selectedShips.First().DisplayName, "{0} Reactors, {1} Batteries turned on.", reactors, batteries);
            }
            break;

            case SyncGridChangeType.SwitchOffPower:
            {
                int reactors;
                int batteries;
                TurnOffShips(selectedShips, out reactors, out batteries);
                MyAPIGateway.Utilities.SendMessage(steamId, selectedShips.First().DisplayName, "{0} Reactors, {1} Batteries turned off.", reactors, batteries);
            }
            break;

            case SyncGridChangeType.DeleteShip:
            {
                if (selectedShips.Count == 1)
                {
                    DeleteShip(steamId, selectedShips.First());
                }
                else if (selectedShips.Count > 1)
                {
                    MyAPIGateway.Utilities.SendMessage(steamId, "deleteship", "{0} Ships match that name.", selectedShips.Count);
                }
            }
            break;

            case SyncGridChangeType.Destructible:
            {
                if (selectedShips.Count == 1)
                {
                    SetDestructible(selectedShips.First(), switchOn, steamId);
                }
            }
            break;

            case SyncGridChangeType.Stop:
            {
                foreach (var selectedShip in selectedShips)
                {
                    MessageSyncEntity.Process(selectedShip, SyncEntityType.Stop);
                    MyAPIGateway.Utilities.SendMessage(steamId, selectedShip.DisplayName, "Is stopping.");
                }
            }
            break;

            case SyncGridChangeType.ScaleDown:
            {
                ScaleShip(steamId, selectedShips.First(), MyCubeSize.Small);
            }
            break;

            case SyncGridChangeType.ScaleUp:
            {
                ScaleShip(steamId, selectedShips.First(), MyCubeSize.Large);
            }
            break;
            }
        }
        private void CommonProcess(ulong steamId, SyncGridChangeType syncType, long entityId, string searchEntity, long playerId, bool switchOn)
        {
            List <IMyCubeGrid> selectedShips = new List <IMyCubeGrid>();
            bool allSelectedShips            = false;

            if (entityId != 0)
            {
                var selectedShip = MyAPIGateway.Entities.GetEntityById(entityId) as IMyCubeGrid;
                if (selectedShip != null)
                {
                    selectedShips.Add(selectedShip);
                }
            }
            else if (searchEntity == "**")  // All ships in the players hot list.
            {
                List <IMyEntity> shipCache = CommandListShips.GetShipCache(steamId);
                foreach (IMyEntity ship in shipCache)
                {
                    selectedShips.Add((IMyCubeGrid)ship);
                }
                allSelectedShips = true;
            }
            else if (!string.IsNullOrEmpty(searchEntity))
            {
                // exact name search.
                var currentShipList = new HashSet <IMyEntity>();
                MyAPIGateway.Entities.GetEntities(currentShipList, e => e is IMyCubeGrid && e.DisplayName.Equals(searchEntity, StringComparison.InvariantCultureIgnoreCase));
                selectedShips.AddRange(currentShipList.Cast <IMyCubeGrid>());

                if (currentShipList.Count == 0)
                {
                    // hotlist search.
                    int index;
                    List <IMyEntity> shipCache = CommandListShips.GetShipCache(steamId);
                    if (searchEntity.Substring(0, 1) == "#" && int.TryParse(searchEntity.Substring(1), out index) && index > 0 && index <= shipCache.Count)
                    {
                        selectedShips.Add((IMyCubeGrid)shipCache[index - 1]);
                    }
                }
            }

            if (selectedShips.Count == 0)
            {
                MyAPIGateway.Utilities.SendMessage(steamId, "Server", "No ships selected or found.");
                return;
            }

            switch (syncType)
            {
            case SyncGridChangeType.OwnerClaim:
            {
                var players = new List <IMyPlayer>();
                MyAPIGateway.Players.GetPlayers(players, p => p != null && p.IdentityId == playerId);
                IMyPlayer player = players.FirstOrDefault();

                if (player == null)
                {
                    return;
                }

                foreach (var selectedShip in selectedShips)
                {
                    var grids = selectedShip.GetAttachedGrids(AttachedGrids.Static);
                    foreach (var grid in grids)
                    {
                        grid.ChangeGridOwnership(player.IdentityId, MyOwnershipShareModeEnum.All);
                    }

                    MyAPIGateway.Utilities.SendMessage(steamId, "Server", string.Format("Grid {0} Claimed by player {1}.", selectedShip.DisplayName, player.DisplayName));
                }
            }
            break;

            case SyncGridChangeType.OwnerRevoke:
            {
                foreach (var selectedShip in selectedShips)
                {
                    var grids = selectedShip.GetAttachedGrids(AttachedGrids.Static);
                    foreach (var grid in grids)
                    {
                        grid.ChangeGridOwnership(0, MyOwnershipShareModeEnum.All);
                    }
                    MyAPIGateway.Utilities.SendMessage(steamId, "Server", string.Format("Grid {0} Revoked of all ownership.", selectedShip.DisplayName));
                }
            }
            break;

            case SyncGridChangeType.OwnerShareAll:
            {
                foreach (var selectedShip in selectedShips)
                {
                    var grids = selectedShip.GetAttachedGrids(AttachedGrids.Static);
                    foreach (var grid in grids)
                    {
                        var blocks = new List <IMySlimBlock>();
                        grid.GetBlocks(blocks, f => f.FatBlock != null && f.FatBlock.OwnerId != 0);

                        foreach (var block in blocks)
                        {
                            block.FatBlock.ChangeOwner(block.FatBlock.OwnerId, MyOwnershipShareModeEnum.All);
                        }
                    }
                    MyAPIGateway.Utilities.SendMessage(steamId, "Server", string.Format("Grid {0} Shared.", selectedShip.DisplayName));
                }
            }
            break;

            case SyncGridChangeType.OwnerShareNone:
            {
                foreach (var selectedShip in selectedShips)
                {
                    var grids = selectedShip.GetAttachedGrids(AttachedGrids.Static);
                    foreach (var grid in grids)
                    {
                        var blocks = new List <IMySlimBlock>();
                        grid.GetBlocks(blocks, f => f.FatBlock != null && f.FatBlock.OwnerId != 0);

                        foreach (var block in blocks)
                        {
                            block.FatBlock.ChangeOwner(block.FatBlock.OwnerId, MyOwnershipShareModeEnum.None);
                        }
                    }
                    MyAPIGateway.Utilities.SendMessage(steamId, "Server", string.Format("Grid {0} Shared.", selectedShip.DisplayName));
                }
            }
            break;

            case SyncGridChangeType.SwitchOnPower:
            {
                int reactors;
                int batteries;
                TurnOnShips(steamId, selectedShips, out reactors, out batteries);
            }
            break;

            case SyncGridChangeType.SwitchOffPower:
            {
                int reactors;
                int batteries;
                TurnOffShips(steamId, selectedShips, out reactors, out batteries);
            }
            break;

            case SyncGridChangeType.DeleteShip:
            {
                if (allSelectedShips)
                {
                    foreach (var selectedShip in selectedShips)
                    {
                        DeleteShip(steamId, selectedShip);
                    }
                }
                else if (selectedShips.Count == 1)
                {
                    DeleteShip(steamId, selectedShips.First());
                }
                else if (selectedShips.Count > 1)
                {
                    MyAPIGateway.Utilities.SendMessage(steamId, "deleteship", "{0} Ships match that name.", selectedShips.Count);
                }
            }
            break;

            case SyncGridChangeType.Destructible:
            {
                if (selectedShips.Count == 1)
                {
                    SetDestructible(selectedShips.First(), switchOn, steamId);
                }
            }
            break;

            case SyncGridChangeType.Stop:
            {
                foreach (var selectedShip in selectedShips)
                {
                    MessageSyncEntity.Process(selectedShip, SyncEntityType.Stop);
                    MyAPIGateway.Utilities.SendMessage(steamId, selectedShip.DisplayName, "Is stopping.");
                }
            }
            break;

            case SyncGridChangeType.ScaleDown:
            {
                ScaleShip(steamId, selectedShips.First(), MyCubeSize.Small);
            }
            break;

            case SyncGridChangeType.ScaleUp:
            {
                ScaleShip(steamId, selectedShips.First(), MyCubeSize.Large);
            }
            break;

            case SyncGridChangeType.BuiltBy:
            {
                string playerName = SearchEntity;

                var players = new List <IMyPlayer>();
                MyAPIGateway.Players.GetPlayers(players, p => p != null);
                IMyIdentity selectedPlayer = null;

                var identities = new List <IMyIdentity>();
                MyAPIGateway.Players.GetAllIdentites(identities, i => i.DisplayName.Equals(playerName, StringComparison.InvariantCultureIgnoreCase));
                selectedPlayer = identities.FirstOrDefault();

                int index;
                List <IMyIdentity> cacheList = CommandPlayerStatus.GetIdentityCache(steamId);
                if (playerName.Substring(0, 1) == "#" && int.TryParse(playerName.Substring(1), out index) && index > 0 && index <= cacheList.Count)
                {
                    selectedPlayer = cacheList[index - 1];
                }

                List <IMyIdentity> botCacheList = CommandListBots.GetIdentityCache(steamId);
                if (playerName.Substring(0, 1).Equals("B", StringComparison.InvariantCultureIgnoreCase) && int.TryParse(playerName.Substring(1), out index) && index > 0 && index <= botCacheList.Count)
                {
                    selectedPlayer = botCacheList[index - 1];
                }

                if (selectedPlayer == null)
                {
                    MyAPIGateway.Utilities.SendMessage(steamId, "Server", string.Format("Player or Bot '{0}' could not be found.", playerName));
                    return;
                }

                // Using the identity list is a crap way, but since we don't have access to BuiltBy for non-functional blocks, this has to do.
                var listIdentites = new List <IMyIdentity>();
                MyAPIGateway.Players.GetAllIdentites(listIdentites);
                foreach (var selectedShip in selectedShips)
                {
                    var grids = selectedShip.GetAttachedGrids(AttachedGrids.Static);
                    foreach (var grid in grids)
                    {
                        foreach (IMyIdentity identity in listIdentites)
                        {
                            if (identity.IdentityId != selectedPlayer.IdentityId)
                            {
                                // The current API doesn't allow the setting of the BuiltBy to anything but an existing Identity (player or NPC).
                                ((MyCubeGrid)grid).TransferBlocksBuiltByID(identity.IdentityId, selectedPlayer.IdentityId);
                            }
                        }
                    }
                    MyAPIGateway.Utilities.SendMessage(steamId, "Server", string.Format("Grid '{0}' Changed of all built to '{1}'.", selectedShip.DisplayName, selectedPlayer.DisplayName));
                }
            }
            break;

            case SyncGridChangeType.Repair:
            {
                var players = new List <IMyPlayer>();
                MyAPIGateway.Players.GetPlayers(players, p => p != null && p.IdentityId == playerId);
                IMyPlayer player = players.FirstOrDefault();

                if (player == null)
                {
                    return;
                }

                foreach (var selectedShip in selectedShips)
                {
                    var grids = selectedShip.GetAttachedGrids(AttachedGrids.Static);
                    foreach (var grid in grids)
                    {
                        RepairShip(steamId, grid);
                    }

                    MyAPIGateway.Utilities.SendMessage(steamId, "Server", string.Format("Grid {0} Repaired.", selectedShip.DisplayName));
                }
            }
            break;
            }
        }