Example #1
0
 public void MoveClientToWorld(RemoteClient client, World world, Vector3?spawnPoint = null)
 {
     if (client.World == world)
     {
         return;
     }
     lock (client.LoadedChunks)
         client.PauseChunkUpdates = true;
     EntityManager.Despawn(client.Entity);
     while (client.KnownEntities.Any())
     {
         client.ForgetEntity(EntityManager.GetEntityById(client.KnownEntities[0]));
     }
     EntityManager.Update();
     EntityManager.SpawnEntity(world, client.Entity);
     client.UnloadAllChunks();
     // TODO: Allow player to save their positions in each world
     if (spawnPoint == null)
     {
         client.Entity.Position = world.WorldGenerator.SpawnPoint;
     }
     else
     {
         client.Entity.Position = spawnPoint.Value;
     }
     client.UpdateChunks(true);
     client.SendPacket(new PlayerPositionAndLookPacket(client.Entity.Position.X, client.Entity.Position.Y + 0.1 + PlayerEntity.Height,
                                                       client.Entity.Position.Z, client.Entity.Position.Y + 0.1, client.Entity.Yaw, client.Entity.Pitch, false));
     EntityManager.SendClientEntities(client);
     lock (client.LoadedChunks)
         client.PauseChunkUpdates = false;
 }
Example #2
0
 public void DisconnectPlayer(RemoteClient client, string reason = null)
 {
     if (!Clients.Contains(client))
     {
         throw new InvalidOperationException("The server is not aware of this client.");
     }
     lock (NetworkLock)
     {
         if (reason != null)
         {
             try
             {
                 if (client.NetworkClient != null && client.NetworkClient.Connected)
                 {
                     if (client.NetworkManager.NetworkMode == NetworkMode.Login)
                     {
                         client.NetworkManager.WritePacket(new LoginDisconnectPacket("\"" + reason + "\""), PacketDirection.Clientbound);
                     }
                     else
                     {
                         client.NetworkManager.WritePacket(new DisconnectPacket("\"" + reason + "\""), PacketDirection.Clientbound);
                     }
                 }
             }
             catch { }
         }
         try
         {
             if (client.NetworkClient != null && client.NetworkClient.Connected)
             {
                 client.NetworkClient.Close();
             }
         }
         catch { }
         if (client.IsLoggedIn)
         {
             EntityManager.Despawn(client.Entity);
         }
         Clients.Remove(client);
         if (client.IsLoggedIn)
         {
             Level.SavePlayer(client);
             var args = new PlayerLogInEventArgs(client);
             OnPlayerLoggedOut(args);
             if (!args.Handled)
             {
                 SendChat(new ChatMessage(string.Format("{0} left the game.", client.Username, ChatColor.YELLOW)));
             }
         }
         client.Dispose();
     }
 }