Esempio n. 1
0
        public override void Execute(Server server, MinecraftClient user, string text, params string[] parameters)
        {
            bool add = false;
            var current = server.MinecraftServer.GetLevel(user).Time;
            long? time = null;
            if (parameters.Length == 0)
            {
                user.SendChat("The current time is " + current + ", or " + LongToTimeString(current));
                return;
            }
            if (parameters[0].ToLower() == "day")
                time = 0;
            else if (parameters[0].ToLower() == "night")
                time = 12000;
            else if (parameters[0].ToLower() == "noon")
                time = 6000;
            else if (parameters[0].ToLower() == "midnight")
                time = 18000;
            else
            {
                string timeString;
                if (parameters[0].ToLower() == "set" && parameters.Length > 1)
                    timeString = parameters[1];
                else if (parameters[0].ToLower() == "add" && parameters.Length > 1)
                {
                    timeString = parameters[1];
                    add = true;
                }
                else
                    timeString = parameters[0];
                if (timeString.Contains(":"))
                {
                    try
                    {
                        time = TimeStringToLong(timeString);
                    }
                    catch { }
                }
                else
                {
                    long _time;
                    if (long.TryParse(timeString, out _time))
                        time = _time;
                }
                if (add)
                    time += current;
            }

            if (time == null)
            {
                user.SendChat(ChatColors.Red + "Invalid time specified.");
                return;
            }

            time = time.Value % 24000;

            server.MinecraftServer.GetLevel(user).Time = time.Value; // TODO: Add event listener in Craft.Net
            server.SendChatToGroup("server.op", ChatColors.Gray + user.Username + " set the time in " + user.World.Name +
                " to " + time.Value + ", or " + LongToTimeString(time.Value));
        }
Esempio n. 2
0
        public static void ExecuteCommand(Server server, MinecraftClient user, string text)
        {
            string name = text.Substring(1);
            if (string.IsNullOrEmpty(name))
                return;
            var parameters = new string[0];
            var userText = "";
            if (name.Contains(" "))
            {
                name = name.Remove(name.IndexOf(' '));
                userText = text.Substring(text.IndexOf(' ') + 1);
                parameters = userText.Split(' ');
            }
            var command = GetCommand(name);

            if (command == null)
            {
                user.SendChat(ChatColors.Red + "That command was not found.");
                return;
            }

            if (!MayUseCommand(command, user, server))
            {
                user.SendChat(ChatColors.Red + "You do not have permission to use that command.");
                return;
            }
            command.Execute(server, user, userText, parameters);
        }
Esempio n. 3
0
 public override void Execute(Server server, MinecraftClient user, string text, params string[] parameters)
 {
     if (parameters.Length != 1)
     {
         user.SendChat(ChatColors.Red + "Incorrect usage. Use /help difficulty for more information.");
         return;
     }
     Difficulty value;
     int num;
     if (int.TryParse(text, out num))
     {
         if (num < 0 || num > 3)
         {
             user.SendChat(ChatColors.Red + "Invalid difficulty specified.");
             return;
         }
         value = (Difficulty)num;
     }
     else
     {
         if (!Enum.TryParse<Difficulty>(text, true, out value))
         {
             user.SendChat(ChatColors.Red + "Invalid difficulty specified.");
             return;
         }
     }
     server.MinecraftServer.GetLevel(user.World).Difficulty = value;
     server.SendChatToGroup("server.op", ChatColors.Gray + user.Username + " sets difficulty of " + user.World.Name + " to " + value.ToString());
 }
Esempio n. 4
0
        public static void ExecuteCommand(Server server, MinecraftClient user, string text)
        {
            string name = text.Substring(1);
            if (string.IsNullOrEmpty(name))
                return;
            var groups = (List<string>)user.Tags["PartyCraft.UserGroups"];
            var parameters = new string[0];
            var userText = "";
            if (name.Contains(" "))
            {
                name = name.Remove(name.IndexOf(' '));
                userText = text.Substring(text.IndexOf(' ') + 1);
                parameters = userText.Split(' ');
            }
            var command = GetCommand(name);

            if (command == null)
            {
                user.SendChat(ChatColors.Red + "That command was not found.");
                return;
            }

            bool allowed = false;
            foreach (var group in groups)
            {
                if (command.AllowedGroups.Contains(group))
                    allowed = true;
            }
            if (!allowed)
            {
                user.SendChat(ChatColors.Red + "You do not have permission to use that command.");
                return;
            }
            command.Execute(server, user, userText, parameters);
        }
Esempio n. 5
0
 public override void MessageRecieved(MinecraftClient client, byte[] data)
 {
     server.DefaultLevel.GameMode = (GameMode)data[0];
     server.EnableCheats = data[1] == 1;
     server.OpenLocalServer();
     foreach (var c in server.Clients.Where(c => c.IsLoggedIn))
         c.Entity.GameMode = server.DefaultLevel.GameMode;
 }
Esempio n. 6
0
 public override void Execute(Server server, MinecraftClient user, string text, params string[] parameters)
 {
     if (parameters.Length < 2)
     {
         user.SendChat(ChatColors.Red + "Insufficient parameters. Use \"/help tell\" for more information.");
         return;
     }
 }
Esempio n. 7
0
 public override void Execute(Server server, MinecraftClient user, string text, params string[] parameters)
 {
     if (parameters.Length != 1)
     {
         user.SendChat(ChatColors.Red + "Invalid parameters. Use /help op for more information.");
         return;
     }
     // TODO
 }
Esempio n. 8
0
 public override void Execute(Server server, MinecraftClient user, string text, params string[] parameters)
 {
     if (parameters.Length == 0)
     {
         user.SendChat(ChatColors.Red + "Incorrect usage. Use /help say for more information.");
         return;
     }
     var format = server.SettingsProvider.Get<string>("chat.broadcast.format");
     server.MinecraftServer.SendChat(string.Format(format, text));
 }
Esempio n. 9
0
 public static bool MayUseCommand(Command command, MinecraftClient user, Server server)
 {
     if (user is ConsoleClient)
         return true;
     var groups = server.GetUserGroups(user.Username);
     foreach (var group in groups)
     {
         if (command.AllowedGroups.Contains(group))
             return true;
     }
     return false;
 }
Esempio n. 10
0
        public override void Execute(Server server, MinecraftClient user, string text, params string[] parameters)
        {
            string player = user.Username;
            Vector3 target;
            if (parameters.Length < 1 || parameters.Length > 4)
            {
                user.SendChat(ChatColors.Red + "Incorrect usage. Use /help tp for more information.");
                return;
            }
            if (parameters.Length == 1)
            {
                var client = server.MinecraftServer.GetClient(parameters[0]);

            }
        }
Esempio n. 11
0
 public override void Execute(Server server, MinecraftClient user, string text, params string[] parameters)
 {
     if (parameters.Length < 2)
     {
         user.SendChat(ChatColors.Red + "Insufficient parameters. Use \"/help tell\" for more information.");
         return;
     }
     var client = server.MinecraftServer.GetClient(parameters[0]);
     if (client == null)
     {
         user.SendChat(parameters[0] + " is not online.");
         return;
     }
     var format = server.SettingsProvider.Get<string>("chat.private.format");
     client.SendChat(string.Format(format, user.Username, client.Username, text));
 }
Esempio n. 12
0
 public override void Execute(Server server, MinecraftClient user, string text, params string[] parameters)
 {
     if (parameters.Length != 1)
     {
         user.SendChat(ChatColors.Red + "Invalid parameters. Use /help op for more information.");
         return;
     }
     var groups = server.GetUserGroups(parameters[0]);
     if (groups.Contains("server.op"))
     {
         user.SendChat(ChatColors.Red + "User is already an op.");
         return;
     }
     groups.Add("server.op");
     server.SetUserGroups(parameters[0], groups);
     server.SendChatToGroup("server.op", ChatColors.Gray + user.Username + " adds " + parameters[0] + " to server.op group.");
 }
Esempio n. 13
0
 public override void Execute(Server server, MinecraftClient user, string text, params string[] parameters)
 {
     if (parameters.Length > 2 || parameters.Length == 0)
     {
         user.SendChat(ChatColors.Red + "Incorrect usage. Use /help speed for more information.");
         return;
     }
     if (parameters[0].ToLower() == "reset")
     {
         user.Entity.Abilities.WalkingSpeed = 12;
         user.Entity.Abilities.FlyingSpeed = 24;
         return;
     }
     string player = user.Username;
     int speed;
     if (parameters.Length == 2)
     {
         player = parameters[0];
         if (!int.TryParse(parameters[1], out speed))
         {
             user.SendChat(ChatColors.Red + "Incorrect usage. Use /help speed for more information.");
             return;
         }
     }
     else
     {
         if (!int.TryParse(parameters[0], out speed))
         {
             user.SendChat(ChatColors.Red + "Incorrect usage. Use /help speed for more information.");
             return;
         }
     }
     var client = server.MinecraftServer.GetClient(player);
     if (client == null)
     {
         user.SendChat(ChatColors.Red + player + " is not online.");
         return;
     }
     client.Entity.Abilities.WalkingSpeed = (byte)speed;
     client.Entity.Abilities.FlyingSpeed = (byte)(speed * 2);
 }
Esempio n. 14
0
 public override void Execute(Server server, MinecraftClient user, string text, params string[] parameters)
 {
     Server = server;
     if (parameters.Length != 0)
     {
         user.SendChat(ChatColors.Red + "Invalid parameters. Use /help repl for more information.");
         return;
     }
     if (ReplContext.Self != null)
     {
         user.SendChat(ChatColors.Red + ReplContext.Self.Username + " is currently in REPL mode. Only one user may be in REPL mode at a time.");
         // TODO: Upgrade Mono.CSharp to Mono 3.0 and support several REPLs at once
         return;
     }
     server.ChatMessage -= HandleChatMessage;
     server.ChatMessage += HandleChatMessage;
     server.MinecraftServer.PlayerLoggedOut += (s, e) =>
         {
             if (Evaluators.ContainsKey(e.Username))
                 Evaluators.Remove(e.Username);
             ReplContext.Self = null;
         };
     Evaluators[user.Username] = new Evaluator(new CompilerContext(new CompilerSettings(), new MinecraftReportPrinter(user)));
     Evaluators[user.Username].ReferenceAssembly(typeof(Server).Assembly);
     Evaluators[user.Username].ReferenceAssembly(typeof(MinecraftServer).Assembly);
     Evaluators[user.Username].ReferenceAssembly(typeof(Craft.Net.IPacket).Assembly);
     Evaluators[user.Username].ReferenceAssembly(typeof(World).Assembly);
     Evaluators[user.Username].ReferenceAssembly(typeof(IServer).Assembly);
     Evaluators[user.Username].InteractiveBaseClass = typeof(ReplContext);
     Evaluators[user.Username].Run("using Craft.Net");
     Evaluators[user.Username].Run("using Craft.Net.Data");
     Evaluators[user.Username].Run("using Craft.Net.Data.Blocks");
     Evaluators[user.Username].Run("using Craft.Net.Data.Items");
     Evaluators[user.Username].Run("using Craft.Net.Server");
     Evaluators[user.Username].Run("using PartyCraft");
     ReplContext.Self = user;
     ReplContext.Server = server;
     user.SendChat(ChatColors.Blue + "Entering C# Interactive Mode");
     user.SendChat(ChatColors.Blue + "Use `Exit()` to exit REPL mode.");
 }
Esempio n. 15
0
 public override void Execute(Server server, MinecraftClient user, string text, params string[] parameters)
 {
     string player = user.Username;
     GameMode gameMode;
     if (parameters.Length == 0 || parameters.Length > 2)
     {
         user.SendChat(ChatColors.Red + "Incorrect usage. Use /help gamemode for more information.");
         return;
     }
     int value;
     if (int.TryParse(parameters[0], out value))
     {
         if (value < 0 || value > 2)
         {
             user.SendChat(ChatColors.Red + "Incorrect usage. Use /help gamemode for more information.");
             return;
         }
         gameMode = (GameMode)value;
     }
     else
     {
         if (!Enum.TryParse<GameMode>(parameters[0], true, out gameMode))
         {
             user.SendChat(ChatColors.Red + "Incorrect usage. Use /help gamemode for more information.");
             return;
         }
     }
     if (parameters.Length == 2)
         player = parameters[1];
     var client = server.MinecraftServer.GetClient(player);
     if (client == null)
     {
         user.SendChat(ChatColors.Red + player + " is not online."); // TODO: Set it anyway
         return;
     }
     client.Entity.GameMode = gameMode;
     server.SendChatToGroup("server.op", ChatColors.Gray + user.Username + " sets " + player + " to " + gameMode + " mode.");
 }
Esempio n. 16
0
 public override void Execute(Server server, MinecraftClient user, string text, params string[] parameters)
 {
     string player = user.Username;
     int x = (int)user.Entity.Position.X;
     int y = (int)user.Entity.Position.Y;
     int z = (int)user.Entity.Position.Z;
     if (parameters.Length == 1)
         player = parameters[0];
     else if (parameters.Length == 3)
     {
         if (!int.TryParse(parameters[0], out x) &&
             !int.TryParse(parameters[1], out y) &&
             !int.TryParse(parameters[2], out z))
         {
             user.SendChat(ChatColors.Red + "Incorrect usage. Use /help spawnpoint for more information.");
             return;
         }
     }
     else if (parameters.Length == 4)
     {
         player = parameters[0];
         if (!int.TryParse(parameters[1], out x) &&
             !int.TryParse(parameters[2], out y) &&
             !int.TryParse(parameters[3], out z))
         {
             user.SendChat(ChatColors.Red + "Incorrect usage. Use /help spawnpoint for more information.");
             return;
         }
     }
     var client = server.MinecraftServer.GetClient(player);
     if (player == null)
     {
         user.SendChat(ChatColors.Red + player + " is not online."); // TODO: Set it anyway
         return;
     }
     client.Entity.SpawnPoint = new Vector3(x, y, z);
     server.SendChatToGroup("server.op", user.Username + " set " + player + " spawn point to " + client.Entity.SpawnPoint);
 }
Esempio n. 17
0
 public override void Execute(Server server, MinecraftClient user, string text, params string[] parameters)
 {
     if (parameters.Length < 1)
     {
         user.SendChat(ChatColors.Red + "Insufficient parameters. Use \"/help\" for more information.");
         return;
     }
     if (parameters.Length > 1)
     {
         user.SendChat(ChatColors.Red + "Too many parameters. Use \"/help\" for more information.");
         return;
     }
     var command = GetCommand(parameters[0]);
     if (command == null)
     {
         user.SendChat(ChatColors.Red + "Command not found.");
         return;
     }
     string[] lines = command.Documentation.Split('\n');
     user.SendChat(ChatColors.DarkGreen + "Documentation for " + ChatColors.Bold + "/" + command.DefaultCommand + ":");
     foreach (var line in lines)
         user.SendChat(ChatColors.DarkCyan + line);
 }
Esempio n. 18
0
 public override void Execute(Server server, MinecraftClient user, string text, params string[] parameters)
 {
     int page = -1;
     if (parameters.Length == 0)
         page = 1;
     if (parameters.Length > 1)
     {
         user.SendChat(ChatColors.Red + "Incorrect usage. Use \"/help\" for more information.");
         return;
     }
     if (page != -1 || int.TryParse(parameters[0], out page))
     {
         page--;
         int pages = Commands.Count / CommandsPerPage;
         if (page >= pages)
         {
             user.SendChat(ChatColors.Red + "No further commands. Use \"/help\" for more information.");
             return;
         }
         user.SendChat(ChatColors.DarkGreen + "Command List (page " + (page + 1) + " of " + pages + ")");
         foreach (var c in Commands.Skip(page * CommandsPerPage).Take(CommandsPerPage))
             user.SendChat(ChatColors.DarkCyan + "/" + c.DefaultCommand);
         return;
     }
     var command = GetCommand(parameters[0]);
     if (command == null)
     {
         user.SendChat(ChatColors.Red + "Command not found.");
         return;
     }
     string[] lines = command.Documentation.Split('\n');
     user.SendChat(ChatColors.DarkGreen + "Documentation for /" + command.DefaultCommand + ":");
     foreach (var line in lines)
         user.SendChat(ChatColors.DarkCyan + line);
     if (!MayUseCommand(command, user, server))
         user.SendChat(ChatColors.Red + "You are not permitted to use this command.");
 }
Esempio n. 19
0
 private void AcceptConnectionAsync(IAsyncResult result)
 {
     Socket connection = socket.EndAccept(result);
     var client = new MinecraftClient(connection, this);
     Clients.Add(client);
     client.Socket.SendTimeout = 5000;
     client.Socket.BeginReceive(client.RecieveBuffer, client.RecieveBufferIndex,
                                client.RecieveBuffer.Length,
                                SocketFlags.None, SocketRecieveAsync, client);
     socket.BeginAccept(AcceptConnectionAsync, null);
 }
Esempio n. 20
0
 /// <summary>
 /// Gets the World that the given client is present in.
 /// </summary>
 public World GetClientWorld(MinecraftClient client)
 {
     return DefaultWorld; // TODO
 }
Esempio n. 21
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. 22
0
 private void HandlePacket(MinecraftClient client, IPacket packet)
 {
     if (PacketHandlerDelegates[packet.Id] == null)
         throw new InvalidOperationException("No packet handler set for 0x" + packet.Id.ToString("X2"));
     PacketHandlerDelegates[packet.Id](client, this, packet);
 }
Esempio n. 23
0
 private void AcceptConnectionAsync(IAsyncResult result)
 {
     if (Listener == null || result == null) return; // This happens sometimes on shut down, it's weird
     var tcpClient = Listener.EndAcceptTcpClient(result);
     var client = new MinecraftClient(tcpClient, this);
     lock (NetworkLock)
     {
         Clients.Add(client);
     }
     Listener.BeginAcceptTcpClient(AcceptConnectionAsync, null);
 }
Esempio n. 24
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. 25
0
 /// <summary>
 /// Gets the level that the specified client resides in.
 /// </summary>
 public Level GetLevel(MinecraftClient client)
 {
     return GetLevel(client.World);
 }
Esempio n. 26
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. 27
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. 28
0
 // TODO: CommandContext class
 public abstract void Execute(Server server, MinecraftClient user, string text, params string[] parameters);
Esempio n. 29
0
 public override void Execute(Server server, MinecraftClient user, string text, params string[] parameters)
 {
     user.Entity.LastDamageType = DamageType.Suicide;
     user.Entity.Health = 0;
 }
Esempio n. 30
0
        /// <summary>
        /// Attempts to parse all packets in the given client and update
        /// its buffer.
        /// </summary>
        /// <returns>
        /// The read packets.
        /// </returns>
        public static IEnumerable<Packet> TryReadPackets(MinecraftClient client, int length)
        {
            // TODO: Investigate buffer overflow problem

            var results = new List<Packet>();
            // Get a buffer to parse that is the length of the recieved data
            byte[] buffer = client.RecieveBuffer.Take(length).ToArray();
            // Decrypt the buffer if needed
            if (client.EncryptionEnabled)
                buffer = client.Decrypter.ProcessBytes(buffer);

            while (buffer.Length > 0)
            {
                Type packetType = PacketTypes[buffer[0]]; // Get the correct type to parse this packet
                if (packetType == null)
                {
                    throw new InvalidOperationException("Invalid packet ID 0x" +
                                                        buffer[0].ToString("x").ToUpper());
                }
                var workingPacket = (Packet)Activator.CreateInstance(packetType);
                workingPacket.PacketContext = PacketContext.ClientToServer;
                workingPacket.Socket = client.Socket;
                // Attempt to read the packet
                int workingLength = workingPacket.TryReadPacket(buffer, length);
                if (workingLength == -1) // Incomplete packet
                {
                    // Copy the incomplete packet into the recieve buffer and recieve more data
                    // TODO: Test if this can be avoided
                    Array.Copy(buffer, client.RecieveBuffer, buffer.Length);
                    client.RecieveBufferIndex = buffer.Length;
                    client.Socket.ReceiveTimeout = 500;
                    return results;
                }
                // Log the packet
                LogProvider.Log("[CLIENT->SERVER] " + client.Socket.RemoteEndPoint, LogImportance.Low);
                LogProvider.Log("Raw: " + DataUtility.DumpArray(buffer.Take(workingLength).ToArray()), LogImportance.Low);
                LogProvider.Log(workingPacket.ToString(), LogImportance.Low);

                client.Socket.ReceiveTimeout = 30000;
                // Add this packet to the results
                results.Add(workingPacket);
                // Shift the buffer over and remove the packet just parsed
                buffer = buffer.Skip(workingLength).ToArray();
            }

            return results;
        }