Esempio n. 1
0
 /// <summary>
 /// Sends a channel message to the given client.
 /// </summary>
 public void SendMessage(byte[] data, MinecraftClient client)
 {
     if (data.Length > short.MaxValue)
     {
         throw new ArgumentOutOfRangeException("Maximum plugin message length is " + short.MaxValue);
     }
     client.SendPacket(new PluginMessagePacket(Channel, data));
 }
Esempio n. 2
0
        public void SendWeatherToClient(WeatherState state, MinecraftClient client)
        {
            if (client.World != World)
            {
                throw new InvalidOperationException("Client must be in same world to change WeatherState!");
            }

            switch (state)
            {
            case WeatherState.Clear:
                client.SendPacket(new ChangeGameStatePacket(ChangeGameStatePacket.GameState.EndRaining));
                break;

            case WeatherState.Raining:
            case WeatherState.Thundering:
                client.SendPacket(new ChangeGameStatePacket(ChangeGameStatePacket.GameState.BeginRaining));
                break;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Calculates which entities the client should be aware
        /// of, and sends them.
        /// </summary>
        public void SendClientEntities(MinecraftClient client)
        {
            var world   = GetEntityWorld(client.Entity);
            var clients = GetClientsInWorld(world)
                          .Where(c => c.Entity.Position.DistanceTo(client.Entity.Position) <
                                 (c.ViewDistance * Chunk.Width) && c != client);
            var entities = world.Entities.Where(e => !(e is PlayerEntity) &&
                                                e.Position.DistanceTo(client.Entity.Position) < (client.ViewDistance * Chunk.Width)); // TODO: Refactor into same thing

            foreach (var _client in clients)
            {
                client.KnownEntities.Add(_client.Entity.Id);
                client.SendPacket(new SpawnPlayerPacket(_client.Entity.Id,
                                                        _client.Username, (int)_client.Entity.Position.X, (int)_client.Entity.Position.Y,
                                                        (int)_client.Entity.Position.Z, MathHelper.CreateRotationByte(_client.Entity.Yaw),
                                                        MathHelper.CreateRotationByte(_client.Entity.Pitch), _client.Entity.SelectedItem.Id,
                                                        _client.Entity.Metadata));
                client.SendPacket(new EntityEquipmentPacket(_client.Entity.Id, 0,
                                                            _client.Entity.Inventory[_client.Entity.SelectedSlot]));
                for (int i = 0; i < 4; i++)
                {
                    var item = _client.Entity.Inventory[InventoryWindow.ArmorIndex + i];
                    if (!item.Empty)
                    {
                        client.SendPacket(new EntityEquipmentPacket(_client.Entity.Id,
                                                                    (EntityEquipmentPacket.EntityEquipmentSlot)(4 - i), item));
                    }
                }
            }
            foreach (var entity in entities)
            {
                client.KnownEntities.Add(entity.Id);
                if (entity is ObjectEntity)
                {
                    var objectEntity = entity as ObjectEntity;
                    client.SendPacket(new SpawnObjectPacket(objectEntity.Id, objectEntity.EntityType,
                                                            MathHelper.CreateAbsoluteInt(objectEntity.Position.X),
                                                            MathHelper.CreateAbsoluteInt(objectEntity.Position.Y),
                                                            MathHelper.CreateAbsoluteInt(objectEntity.Position.Z),
                                                            MathHelper.CreateRotationByte(objectEntity.Yaw), MathHelper.CreateRotationByte(objectEntity.Pitch),
                                                            objectEntity.Data, (short)objectEntity.Velocity.X,
                                                            (short)objectEntity.Velocity.Y, (short)objectEntity.Velocity.Z));
                    if (entity.IncludeMetadataOnClient)
                    {
                        client.SendPacket(new EntityMetadataPacket(entity.Id, entity.Metadata));
                    }
                }
                else if (entity is PaintingEntity)
                {
                    var painting = entity as PaintingEntity;
                    client.SendPacket(new SpawnPaintingPacket(painting.Id, painting.Painting.Name,
                                                              (int)painting.Center.X, (int)painting.Center.Y, (int)painting.Center.Z, (int)painting.Direction));
                    client.KnownEntities.Add(painting.Id);
                }
            }
        }
Esempio n. 4
0
 private void DoClientUpdates(MinecraftClient client)
 {
     // Update keep alive, chunks, etc
     if (client.LastKeepAliveSent.AddSeconds(20) < DateTime.Now)
     {
         client.SendPacket(new KeepAlivePacket(MathHelper.Random.Next()));
         client.LastKeepAliveSent = DateTime.Now;
         // TODO: Confirm keep alive
     }
     if (client.World.Level.Time % 20 == 0) // Once per second
     {
         // Update chunks
         if (client.ViewDistance < client.MaxViewDistance)
         {
             client.ViewDistance++;
             client.ForceUpdateChunksAsync();
         }
     }
 }
Esempio n. 5
0
        internal void LogInPlayer(MinecraftClient client)
        {
            client.IsLoggedIn = true;
            // Spawn player
            client.Entity                   = DefaultLevel.LoadPlayer(client.Username);
            client.Entity.Username          = client.Username;
            client.Entity.InventoryChanged += EntityInventoryChanged;
            EntityManager.SpawnEntity(DefaultWorld, client.Entity);
            client.SendPacket(new LoginRequestPacket(client.Entity.Id,
                                                     DefaultWorld.LevelType, client.Entity.GameMode,
                                                     client.Entity.Dimension, Settings.Difficulty,
                                                     Settings.MaxPlayers));
            client.SendPacket(new SpawnPositionPacket((int)client.Entity.SpawnPoint.X, (int)client.Entity.SpawnPoint.Y, (int)client.Entity.SpawnPoint.Z));
            client.SendPacket(new TimeUpdatePacket(DefaultLevel.Time, DefaultLevel.Time));
            UpdatePlayerList(null);
            client.SendPacket(new SetWindowItemsPacket(0, client.Entity.Inventory.GetSlots()));

            // Send initial chunks
            client.UpdateChunks(true);
            client.SendPacket(new PlayerPositionAndLookPacket(client.Entity.Position.X, client.Entity.Position.Y + client.Entity.Size.Height,
                                                              client.Entity.Position.Z, client.Entity.Position.Y - client.Entity.Size.Height, client.Entity.Yaw, client.Entity.Pitch, true));
            // TODO: Move 1.62 somewhere else

            // Send entities
            EntityManager.SendClientEntities(client);

            //Send Weather
            GetWeatherManagerForWorld(client.World).SendCurrentWeatherToClient(client);


            client.SendPacket(new UpdateHealthPacket(client.Entity.Health, client.Entity.Food, client.Entity.FoodSaturation));

            var args = new PlayerLogInEventArgs(client);

            OnPlayerLoggedIn(args);
            LogProvider.Log(client.Username + " joined the game.");
            if (!args.Handled)
            {
                SendChat(ChatColors.Yellow + client.Username + " joined the game.");
            }
        }
Esempio n. 6
0
 public void SendClientEntities(MinecraftClient client)
 {
     var world = GetEntityWorld(client.Entity);
     var clients = GetClientsInWorld(world)
         .Where(c => !c.IsDisconnected && c.Entity.Position.DistanceTo(client.Entity.Position) < 
             (c.ViewDistance * Chunk.Width) && c != client);
     foreach (var _client in clients)
     {
         client.KnownEntities.Add(_client.Entity.Id);
         client.SendPacket(new SpawnNamedEntityPacket(_client));
         client.SendPacket(new EntityEquipmentPacket(_client.Entity.Id, EntityEquipmentSlot.HeldItem, _client.Entity.Inventory[_client.Entity.SelectedSlot]));
     }
 }
Esempio n. 7
0
        internal void LogInPlayer(MinecraftClient client)
        {
            client.IsLoggedIn = true;
            // Spawn player
            client.Entity = DefaultLevel.LoadPlayer(client.Username);
            client.Entity.Username = client.Username;
            client.Entity.InventoryChanged += Entity_InventoryChanged;
            EntityManager.SpawnEntity(DefaultWorld, client.Entity);
            client.SendPacket(new LoginPacket(client.Entity.Id,
                                              DefaultWorld.LevelType, DefaultLevel.GameMode,
                                              client.Entity.Dimension, this.Difficulty,
                                              MaxPlayers));

            // Send initial chunks
            client.UpdateChunks(true);
            client.SendPacket(new PlayerPositionAndLookPacket(
                                  client.Entity.Position, client.Entity.Yaw, client.Entity.Pitch, true));
            client.SendQueue.Last().OnPacketSent += (sender, e) => { client.ReadyToSpawn = true; };

            // Send entities
            EntityManager.SendClientEntities(client);

            client.SendPacket(new SetWindowItemsPacket(0, client.Entity.Inventory));
            client.SendPacket(new UpdateHealthPacket(client.Entity.Health, client.Entity.Food, client.Entity.FoodSaturation));
            client.SendPacket(new SpawnPositionPacket(client.Entity.SpawnPoint));
            client.SendPacket(new TimeUpdatePacket(DefaultLevel.Time));

            UpdatePlayerList(null); // Should also process send queue

            var args = new PlayerLogInEventArgs(client);
            OnPlayerLoggedIn(args);
            Log(client.Username + " logged in.");
            if (!args.Handled)
                SendChat(client.Username + " logged in.");
        }
Esempio n. 8
0
 /// <summary>
 /// Calculates which entities the client should be aware
 /// of, and sends them.
 /// </summary>
 public void SendClientEntities(MinecraftClient client)
 {
     var world = GetEntityWorld(client.Entity);
     var clients = GetClientsInWorld(world)
         .Where(c => c.Entity.Position.DistanceTo(client.Entity.Position) <
             (c.ViewDistance * Chunk.Width) && c != client);
     var entities = world.Entities.Where(e => !(e is PlayerEntity) && 
         e.Position.DistanceTo(client.Entity.Position) < (client.ViewDistance * Chunk.Width)); // TODO: Refactor into same thing
     foreach (var _client in clients)
     {
         client.KnownEntities.Add(_client.Entity.Id);
         client.SendPacket(new SpawnPlayerPacket(_client.Entity.Id,
                     _client.Username, (int)_client.Entity.Position.X, (int)_client.Entity.Position.Y,
                     (int)_client.Entity.Position.Z, MathHelper.CreateRotationByte(_client.Entity.Yaw),
                     MathHelper.CreateRotationByte(_client.Entity.Pitch), _client.Entity.SelectedItem.Id,
                     _client.Entity.Metadata));
         client.SendPacket(new EntityEquipmentPacket(_client.Entity.Id, 0,
             _client.Entity.Inventory[_client.Entity.SelectedSlot]));
         for (int i = 0; i < 4; i ++)
         {
             var item = _client.Entity.Inventory[InventoryWindow.ArmorIndex + i];
             if (!item.Empty)
                 client.SendPacket(new EntityEquipmentPacket(_client.Entity.Id, 
                     (EntityEquipmentPacket.EntityEquipmentSlot)(4 - i), item));
         }
     }
     foreach (var entity in entities)
     {
         client.KnownEntities.Add(entity.Id);
         if (entity is ObjectEntity)
         {
             var objectEntity = entity as ObjectEntity;
             client.SendPacket(new SpawnObjectPacket(objectEntity.Id, objectEntity.EntityType,
                 MathHelper.CreateAbsoluteInt(objectEntity.Position.X),
                     MathHelper.CreateAbsoluteInt(objectEntity.Position.Y),
                     MathHelper.CreateAbsoluteInt(objectEntity.Position.Z),
                     MathHelper.CreateRotationByte(objectEntity.Yaw), MathHelper.CreateRotationByte(objectEntity.Pitch), 
                     objectEntity.Data, (short)objectEntity.Velocity.X,
                     (short)objectEntity.Velocity.Y, (short)objectEntity.Velocity.Z));
             if (entity.IncludeMetadataOnClient)
                 client.SendPacket(new EntityMetadataPacket(entity.Id, entity.Metadata));
         }
         else if (entity is PaintingEntity)
         {
             var painting = entity as PaintingEntity;
             client.SendPacket(new SpawnPaintingPacket(painting.Id, painting.Painting.Name,
                 (int)painting.Center.X, (int)painting.Center.Y, (int)painting.Center.Z, (int)painting.Direction));
             client.KnownEntities.Add(painting.Id);
         }
     }
 }
Esempio n. 9
0
        internal void LogInPlayer(MinecraftClient client)
        {
            client.IsLoggedIn = true;
            // Spawn player
            client.Entity = DefaultLevel.LoadPlayer(client.Username);
            client.Entity.Username = client.Username;
            client.Entity.InventoryChanged += EntityInventoryChanged;
            EntityManager.SpawnEntity(DefaultWorld, client.Entity);
            client.SendPacket(new LoginRequestPacket(client.Entity.Id,
                                              DefaultWorld.LevelType, DefaultLevel.GameMode,
                                              client.Entity.Dimension, Settings.Difficulty,
                                              Settings.MaxPlayers));
            client.SendPacket(new SpawnPositionPacket((int)client.Entity.SpawnPoint.X, (int)client.Entity.SpawnPoint.Y, (int)client.Entity.SpawnPoint.Z));
            client.SendPacket(new TimeUpdatePacket(DefaultLevel.Time, DefaultLevel.Time));
            UpdatePlayerList(null);
            client.SendPacket(new SetWindowItemsPacket(0, client.Entity.Inventory.GetSlots()));

            // Send initial chunks
            client.UpdateChunks(true);
            client.SendPacket(new PlayerPositionAndLookPacket(client.Entity.Position.X, client.Entity.Position.Y,
                client.Entity.Position.Z, client.Entity.Position.Y - 1.62, client.Entity.Yaw, client.Entity.Pitch, true));
            // TODO: Move 1.62 somewhere else

            // Send entities
            EntityManager.SendClientEntities(client);

            client.SendPacket(new UpdateHealthPacket(client.Entity.Health, client.Entity.Food, client.Entity.FoodSaturation));

            var args = new PlayerLogInEventArgs(client);
            OnPlayerLoggedIn(args);
            LogProvider.Log(client.Username + " joined the game.");
            if (!args.Handled)
                SendChat(ChatColors.Yellow + client.Username + " joined the game.");

            client.StartWorkers();
        }
Esempio n. 10
0
 private void DoClientUpdates(MinecraftClient client)
 {
     // Update keep alive, chunks, etc
     if (client.LastKeepAliveSent.AddSeconds(20) < DateTime.Now)
     {
         client.SendPacket(new KeepAlivePacket(MathHelper.Random.Next()));
         client.LastKeepAliveSent = DateTime.Now;
         // TODO: Confirm keep alive
     }
     if (client.World.Level.Time % 20 == 0) // Once per second
     {
         // Update chunks
         if (client.ViewDistance < client.MaxViewDistance)
         {
             client.ViewDistance++;
             client.ForceUpdateChunksAsync();
         }
     }
 }
Esempio n. 11
0
 /// <summary>
 /// Sends a channel message to the given client.
 /// </summary>
 public void SendMessage(byte[] data, MinecraftClient client)
 {
     if (data.Length > short.MaxValue)
         throw new ArgumentOutOfRangeException("Maximum plugin message length is " + short.MaxValue);
     client.SendPacket(new PluginMessagePacket(Channel, data));
 }
Esempio n. 12
0
        public void SendWeatherToClient(WeatherState state, MinecraftClient client)
        {
            if (client.World != World)
                throw new InvalidOperationException("Client must be in same world to change WeatherState!");

            switch (state)
            {
                case WeatherState.Clear:
                    client.SendPacket(new ChangeGameStatePacket(ChangeGameStatePacket.GameState.EndRaining));
                    break;
                case WeatherState.Raining:
                case WeatherState.Thundering:
                    client.SendPacket(new ChangeGameStatePacket(ChangeGameStatePacket.GameState.BeginRaining));
                    break;
            }
        }
Esempio n. 13
0
        internal void LogInPlayer(MinecraftClient Client)
        {
            this.Log(Client.Username + " logged in.");
            Client.IsLoggedIn = true;
            // Spawn player
            Client.Entity = new PlayerEntity(Client);
            Client.Entity.Position = this.DefaultWorld.SpawnPoint;
            Client.Entity.Position += new Vector3(0, PlayerEntity.Height, 0);
            this.DefaultWorld.EntityManager.SpawnEntity(Client.Entity);
            Client.SendPacket(new LoginPacket(Client.Entity.Id,
                   this.DefaultWorld.LevelType, this.DefaultWorld.GameMode,
                   Client.Entity.Dimension, this.DefaultWorld.Difficulty,
                   this.MaxPlayers));

            // Send initial chunks
            Client.UpdateChunks(true);
            MinecraftClient client = Client;
            Client.SendQueue.Last().OnPacketSent += (sender, e) => { client.ReadyToSpawn = true; };
            Client.SendPacket(new PlayerPositionAndLookPacket(
                Client.Entity.Position, Client.Entity.Yaw, Client.Entity.Pitch, true));

            this.UpdatePlayerList(null); // Should also process send queue
        }
Esempio n. 14
0
 /// <summary>
 /// Calculates which entities the client should be aware
 /// of, and sends them.
 /// </summary>
 public void SendClientEntities(MinecraftClient client)
 {
     var world = GetEntityWorld(client.Entity);
     var clients = GetClientsInWorld(world)
         .Where(c => !c.IsDisconnected && c.Entity.Position.DistanceTo(client.Entity.Position) <
             (c.ViewDistance * Chunk.Width) && c != client);
     var entities = world.Entities.Where(e => !(e is PlayerEntity) && 
         e.Position.DistanceTo(client.Entity.Position) < (client.ViewDistance * Chunk.Width)); // TODO: Refactor into same thing
     foreach (var _client in clients)
     {
         client.KnownEntities.Add(_client.Entity.Id);
         client.SendPacket(new SpawnNamedEntityPacket(_client));
         client.SendPacket(new EntityEquipmentPacket(_client.Entity.Id, EntityEquipmentSlot.HeldItem, 
             _client.Entity.Inventory[_client.Entity.SelectedSlot]));
         for (int i = 0; i < 4; i ++)
         {
             var item = _client.Entity.Inventory[InventoryWindow.ArmorIndex + i];
             if (!item.Empty)
                 client.SendPacket(new EntityEquipmentPacket(_client.Entity.Id, 
                     (EntityEquipmentSlot)(4 - i), item));
         }
     }
     foreach (var entity in entities)
     {
         client.KnownEntities.Add(entity.Id);
         if (entity is ItemEntity)
             client.SendPacket(new SpawnDroppedItemPacket(entity as ItemEntity));
     }
 }