Пример #1
0
        /// <summary>
        /// Handles a chat message
        /// </summary>
        /// <param name="playerId">Id of chat sender</param>
        /// <param name="playerName">Name of chat sender</param>
        /// <param name="message">Chat message sent</param>
        protected void OnChatMessage(int playerId, string playerName, string message)
        {
            // Get sender and verify that the command is message
            LegacyPlayer player = LegacyCoreServer.Instance.GetPlayerByNetworkId(playerId);

            if (!Util.IsCommand(message))
            {
                return;
            }

            // Split command into arguments, extract name, and modify to exclude name
            var args = message.Split(' ').ToList();
            var name = args.First().ToLowerInvariant();

            args = args.Skip(1).ToList();

            // Check if command exists
            var command = this.commands.FirstOrDefault(c => $"/{c.Name.ToLowerInvariant()}" == name);

            if (command == null)
            {
                return;
            }

            // Execute command
            command.Execute(new CommandContext(command, player, args));
        }
Пример #2
0
        /// <inheritdoc />
        public override LegacyPlayer GeneratePlayer(string id)
        {
            LegacyPlayer player = new LegacyPlayer(id);

            base.Players.Add(id, player);
            return(player);
        }
Пример #3
0
        // Function that gets called when player has connected
        private void HandlePlayerConnecting([FromSource] Player citizen)
        {
            // Load player and set network id
            LegacyPlayer player = LegacyCoreServer.Instance.GetPlayer(citizen);

            if (int.TryParse(citizen.Handle, out int id))
            {
                player.NetworkId = id;
            }
        }
Пример #4
0
            // Called when the command is performed
            // - "sender" is the sender of the command. In this case, we are extending "PlayerCommand" which is why that
            //   is a LegacyPlayer, and not a ICommandExecutor
            //
            // - "context" is the command's context. Here you can find the sender, arguments, and many other useful methods to
            //   make working with arguments easy!
            public override void Perform(LegacyPlayer sender, CommandContext context)
            {
                // First argument is an id, which is an int, that means you can use:
                int id = context.ShiftInt();

                // However, you can also make LegacyCore get the player for you from that id specified
                // in the arguments! This should also work for name, steam id, etc :)
                LegacyPlayer target = context.ShiftPlayer();

                // Using "Shift" makes the context jump to the next argument automatically.
                // You can also get the raw ones that are not effected by shifting ;)
                string idInString  = context.GetRawArgs()[0];
                string idInString2 = context.GetRawArgs()[1];
            }
Пример #5
0
        public override void Perform(CommandContext context)
        {
            // Shift player
            LegacyPlayer target = context.ShiftPlayer();

            // Notify sender of information
            ICommandSender sender = context.Sender;

            sender.SendMessage(ChatColor.LIGHT_RED + "Checking: " + ChatColor.LIGHT_GREEN + target.GetName() + ChatColor.LIGHT_RED + ", their identifiers are:");
            sender.SendMessage(ChatColor.LIGHT_RED + "- network: " + ChatColor.LIGHT_GREEN + Convert.ToString(target.NetworkId));
            sender.SendMessage(ChatColor.LIGHT_RED + "- steam: " + ChatColor.LIGHT_GREEN + target.Id);
            sender.SendMessage(ChatColor.LIGHT_RED + "and more information:");
            sender.SendMessage(ChatColor.LIGHT_RED + "- ping: " + ChatColor.LIGHT_GREEN + target.GetCitizenPlayer().Ping);
        }
Пример #6
0
        public override void Perform(CommandContext context)
        {
            // Is sender not a player?
            LegacyPlayer sender = context.GetPlayerSender();

            if (sender is null)
            {
                sender.SendMessage("Only players can use this command");
                return;
            }

            // Perform
            Perform(sender, context);
        }
Пример #7
0
        /// <summary>
        /// Gets or creates a player from steamid
        /// </summary>
        /// <param name="steamId">SteamId of player</param>
        /// <returns>A new or retrieved player</returns>
        public static async Task <LegacyPlayer> GetOrCreate(string steamId)
        {
            // Pre-define a user
            LegacyPlayer user = null;

            // Get database and start a transaction
            Database             database    = LegacyCoreServer.Db.Database;
            DbContextTransaction transaction = database.BeginTransaction();

            // Start the dangerous stuff
            try {
                // Grab users with same steamid
                List <LegacyPlayer> users = LegacyCoreServer.Db.Players.Where(u => u.SteamId == steamId).ToList();

                // If no user is found, create them
                if (!users.Any())
                {
                    // Create user
                    user = new LegacyPlayer(steamId);

                    // Add user and save changes
                    LegacyCoreServer.Db.Players.Add(user);
                    await LegacyCoreServer.Db.SaveChangesAsync();
                }
                else
                {
                    // User found, use them instead
                    user = users.First();
                }

                // Commit to transaction
                transaction.Commit();
            } catch (Exception ex) {
                // Oops, something went wrong, rollback to previous version
                transaction.Rollback();
                Debug.Write(ex.Message);
            }

            // Finally, return out user
            return(user);
        }
Пример #8
0
 /// <summary>
 /// Performs command with player
 /// </summary>
 /// <param name="sender">Sender of command as player</param>
 /// <param name="context">Context of command</param>
 public abstract void Perform(LegacyPlayer sender, CommandContext context);