private void OnRespawn(ref MyEventRespawn msg)
        {
            var player = (MyPlayerRemote)msg.SenderConnection.Tag;
            if (player != null)
            {
                player.Faction = msg.Faction;

                MySmallShip playerShip;
                MyEntityIdentifier.TryGetEntity<MySmallShip>(new MyEntityIdentifier(msg.EntityId), out playerShip);

                // Close old player ship
                if (player.Ship != null)
                {
                    player.Ship.MarkForClose();
                    player.Ship = null;
                }

                // If ship exists
                if (playerShip != null)
                {
                    Log("OnRespawn: " + player.GetDisplayName().ToString() + ", ship exists, faction: " + MyFactionConstants.GetFactionProperties(player.Faction).Name);

                    player.Ship = playerShip;
                    OnNewPlayerShip(player.Ship);
                }
                else
                {
                    Log("OnRespawn: " + player.GetDisplayName().ToString() + ", new ship, faction: " + MyFactionConstants.GetFactionProperties(player.Faction).Name);

                    var ship = CreateShip(ChooseShip(player.Faction), player.Faction);
                    ship.EntityId = msg.EntityId;
                    ship.DisplayName = player.GetDisplayName().ToString();
                    ship.Inventory = msg.Inventory;
                    ship.ShipType = msg.ShipType;
                    player.Ship = (MySmallShip)MyEntities.CreateFromObjectBuilderAndAdd(player.GetDisplayName().ToString(), ship, msg.Position.GetMatrix());
                    OnNewPlayerShip(player.Ship);
                }

                // Don't save, when sending checkpoint to other players, ingame ships are added manually
                player.Ship.Save = false;

                // When I'm host, set AI priority for respawned player same as I have
                if (IsStory() && IsHost)
                {
                    player.Ship.AIPriority = MySession.PlayerShip.AIPriority;
                }

                UpdateCoopTarget();
            }
            else
            {
                Log("ON RESPAWN, UNKNOWN PLAYER: " + msg.SenderEndpoint.ToString());
            }
        }
        public void Respawn(MyMwcObjectBuilder_SmallShip shipBuilder, Matrix respawnPosition)
        {
            Log("Respawn faction: " + MyFactionConstants.GetFactionProperties(MySession.PlayerShip.Faction).Name);

            int oldPriority = 0;

            if (m_lastConfig == null && MySession.PlayerShip != null)
            {
                m_lastConfig = MySession.PlayerShip.Config.GetObjectBuilder();
            }

            if (MySession.PlayerShip != null)
            {
                oldPriority = MySession.PlayerShip.AIPriority;
                if (MySession.PlayerShip.IsDummy || MySession.PlayerShip.IsExploded())
                {
                    MySession.PlayerShip.Activate(false, false);
                    MySession.PlayerShip.MarkForClose();
                    MySession.PlayerShip = null;
                }
            }

            MySession.Static.Player.RestoreHealth();

            // To proper respawn
            shipBuilder.ShipHealthRatio = MathHelper.Clamp(shipBuilder.ShipHealthRatio, 0.2f, 1.0f);
            shipBuilder.Oxygen = MathHelper.Clamp(shipBuilder.Oxygen, 100.0f, float.MaxValue);

            //Debug.Assert(!shipBuilder.EntityId.HasValue);
            var playership = (MySmallShip)MyEntities.CreateFromObjectBuilderAndAdd(MyClientServer.LoggedPlayer.GetDisplayName().ToString(), shipBuilder, respawnPosition);
            playership.AIPriority = oldPriority; // Restore bot priority

            if (IsSandBox())
            {
                if (m_lastConfig != null)
                {
                    playership.Config.Init(m_lastConfig);
                }
            }
            else
            {
                if (MyGuiScreenGamePlay.Static.Checkpoint.PlayerObjectBuilder.ShipConfigObjectBuilder != null)
                {
                    playership.Config.Init(MyGuiScreenGamePlay.Static.Checkpoint.PlayerObjectBuilder.ShipConfigObjectBuilder);
                }
            }

            playership.ConfigChanged += m_onConfigChanged;
            playership.OnDie += m_onEntityDie;
            playership.Activate(true);
            MyCockpitGlassDecals.Clear();

            var respawnMsg = new MyEventRespawn();
            respawnMsg.EntityId = playership.EntityId.Value.NumericValue;
            respawnMsg.Position = new MyMwcPositionAndOrientation(playership.WorldMatrix);
            respawnMsg.Inventory = GetInventory(playership, true);
            respawnMsg.ShipType = playership.ShipType;
            respawnMsg.Faction = playership.Faction;

            float ratio = (IsStory() && StoredShip != null) ? 0.2f : 1.0f;
            playership.ArmorHealth = MathHelper.Clamp(playership.ArmorHealth, playership.MaxArmorHealth * ratio, playership.MaxArmorHealth);
            playership.Fuel = MathHelper.Clamp(playership.Fuel, playership.MaxFuel * ratio, playership.MaxFuel);
            playership.HealthRatio = MathHelper.Clamp(playership.HealthRatio, ratio, 1.0f);
            playership.Oxygen = MathHelper.Clamp(playership.Oxygen, playership.MaxOxygen * ratio, playership.MaxOxygen);

            MySession.Static.Player.RestoreHealth();
            playership.PilotHealth = MySession.Static.Player.MaxHealth;

            if (MyMultiplayerGameplay.IsSandBox())
            {
                MakeInventoryItemsNontradeable(playership);
            }

            if (m_lastAmmoAssignment != null)
            {
                playership.Weapons.AmmoAssignments.Init(m_lastAmmoAssignment);
            }

            // As last to prevent many changes when doing something
            playership.InventoryChanged += m_onInventoryChanged;

            LogDevelop("RESPAWN end");
            Peers.SendToAll(ref respawnMsg, NetDeliveryMethod.ReliableOrdered);
        }