private void OnCommandPosExecuted(CommandPosExecuteEvent e)
        {
            var commandName = e.Command.Name.ToLowerInvariant();

            CommandOptions.CommandEntry cmdEntry;

            // It will only apply cooldown/cost if the command was sucessfully executed.
            if (e.Source.IsConsole || e.Result.Type != CommandResult.ResultType.SUCCESS ||
                !EssCore.Instance.CommandOptions.Commands.TryGetValue(commandName, out cmdEntry))
            {
                return;
            }

            if (!e.Source.HasPermission("essentials.bypass.commandcooldown") && cmdEntry.Cooldown > 0)
            {
                var playerId = e.Source.ToPlayer().CSteamId.m_SteamID;

                if (!CommandCooldowns.ContainsKey(playerId))
                {
                    CommandCooldowns.Add(playerId, new Dictionary <string, DateTime>());
                }

                CommandCooldowns[playerId][commandName] = DateTime.Now.AddSeconds(cmdEntry.Cooldown);
            }

            if (UEssentials.EconomyProvider.IsPresent && !e.Source.HasPermission("essentials.bypass.commandcost") &&
                cmdEntry.Cost > 0)
            {
                UEssentials.EconomyProvider.Value.Withdraw(e.Source.ToPlayer(), cmdEntry.Cost);
                EssLang.Send(e.Source, "COMMAND_PAID", cmdEntry.Cost, UEssentials.EconomyProvider.Value.CurrencySymbol);
            }
        }
        private void OnCommandPosExecuted(CommandPosExecuteEvent e)
        {
            if (_cachedEconomyProvider == null ||
                e.Source.IsConsole || e.Source.HasPermission("essentials.bypass.commandcost"))
            {
                return;
            }

            var commands = EssCore.Instance.CommandsConfig.Commands;

            /*
             * He will not charge if command was not successfully executed.
             */
            if (e.Result?.Type != CommandResult.ResultType.SUCCESS ||
                !commands.ContainsKey(e.Command.Name))
            {
                return;
            }

            var cost = commands[e.Command.Name].Cost;

            if (cost <= 0)
            {
                return;
            }

            _cachedEconomyProvider.Value.Withdraw(e.Source.ToPlayer(), cost);
            EssLang.Send(e.Source, "COMMAND_PAID", cost);
        }
Example #3
0
        private void OnCommandExecuted(CommandPosExecuteEvent e)
        {
            if (e.Source.IsConsole)
            {
                return;
            }

            var playerId = ulong.Parse(e.Source.Id);
            var sb       = new StringBuilder();

            sb.Append($"[{DateTime.Now}] ")
            .Append($"[{e.Result.Type}")
            .Append(string.IsNullOrEmpty(e.Result.Message) ? "] " : $"({e.Result.Message})] ")
            .Append(e.Source.DisplayName)
            .Append(": ")
            .Append($"\"/{e.Command.Name}")
            .Append(e.Arguments.IsEmpty ? "\"" : $" {e.Arguments.Join(0)}\"");

            var text = sb.ToString();

            if (Cache.ContainsKey(playerId))
            {
                Cache[playerId].Add(text);
            }
            else
            {
                Cache.Add(playerId, new List <string> {
                    text
                });
            }
        }
Example #4
0
        internal static CommandPosExecuteEvent CallCommandPosExecute(ICommand command, ref ICommandArgs cmdArgs,
                                                                     ref ICommandSource commandSource, ref CommandResult result)
        {
            var evt = new CommandPosExecuteEvent(command, cmdArgs, commandSource, result);

            OnCommandPosExecute?.Invoke(evt);
            cmdArgs       = evt.Arguments;
            commandSource = evt.Source;
            result        = evt.Result;
            return(evt);
        }
Example #5
0
        private void HandleCost(CommandPosExecuteEvent e, CommandOptions.CommandEntry commandOptions)
        {
            // Make sure it has an EconomyProvider and check if the player can bypass the cost
            if (!UEssentials.EconomyProvider.IsPresent || e.Source.HasPermission("essentials.bypass.commandcost"))
            {
                return;
            }
            var commandCost = GetCommandCost(commandOptions, e.Source.ToPlayer());

            if (commandCost > 0)
            {
                UEssentials.EconomyProvider.Value.Withdraw(e.Source.ToPlayer(), commandCost);
                EssLang.Send(e.Source, "COMMAND_PAID", commandCost, UEssentials.EconomyProvider.Value.CurrencySymbol);
            }
        }
Example #6
0
        private void OnCommandPosExecuted(CommandPosExecuteEvent e)
        {
            if (
                e.Source.IsConsole ||
                // It will only apply cooldown/cost if the command was sucessfully executed.
                e.Result.Type != CommandResult.ResultType.SUCCESS ||
                // Make sure there is options for the command being executed
                !EssCore.Instance.CommandOptions.Commands.TryGetValue(e.Command.Name.ToLowerInvariant(), out var commandOptions)
                )
            {
                return;
            }

            HandleCooldown(e, commandOptions);
            HandleCost(e, commandOptions);
        }
Example #7
0
        private void HandleCooldown(CommandPosExecuteEvent e, CommandOptions.CommandEntry commandOptions)
        {
            var commandName = e.Command.Name.ToLowerInvariant();

            // Check if the player can bypass the cooldown
            if (e.Source.HasPermission("essentials.bypass.commandcooldown"))
            {
                return;
            }

            var playerId      = e.Source.ToPlayer().CSteamId.m_SteamID;
            var cooldownValue = commandOptions.Cooldown;

            if (commandOptions.PerGroupCooldown != null)
            {
                R.Permissions.GetGroups(e.Source.ToPlayer().RocketPlayer, false)
                .OrderBy(g => - g.Priority)
                .FirstOrDefault(g =>
                {
                    // Check if there is a cooldown specified to the player's group.
                    var result = commandOptions.PerGroupCooldown.TryGetValue(g.Id, out var groupCooldown);
                    // If there is, use that cooldown.
                    if (result)
                    {
                        cooldownValue = groupCooldown;
                    }
                    return(result);
                });
            }

            if (cooldownValue < 1)
            {
                return;
            }

            if (!CommandCooldowns.ContainsKey(playerId))
            {
                CommandCooldowns.Add(playerId, new Dictionary <string, DateTime>());
            }

            CommandCooldowns[playerId][commandName] = DateTime.Now.AddSeconds(cooldownValue);
        }
Example #8
0
        public void Execute(IRocketPlayer caller, string[] args)
        {
            try
            {
                var commandSource = caller is UnturnedPlayer
                                    ? UPlayer.From((UnturnedPlayer)caller)
                                    : UEssentials.ConsoleSource;

                if (commandSource.IsConsole && Command.AllowedSource == AllowedSource.PLAYER)
                {
                    EssLang.CONSOLE_CANNOT_EXECUTE.SendTo(commandSource);
                }
                else if (!commandSource.IsConsole && Command.AllowedSource == AllowedSource.CONSOLE)
                {
                    EssLang.PLAYER_CANNOT_EXECUTE.SendTo(commandSource);
                }
                else
                {
                    var cmdArgs = new CommandArgs(args);
                    var preExec = new CommandPreExecuteEvent(Command, cmdArgs, commandSource);
                    EssentialsEvents._OnCommandPreExecute?.Invoke(preExec);

                    if (preExec.Cancelled)
                    {
                        return;
                    }

                    var result = Command.OnExecute(commandSource, cmdArgs);

                    if (result == null)
                    {
                        return;
                    }

                    if (result.Type == CommandResult.ResultType.SHOW_USAGE)
                    {
                        commandSource.SendMessage($"Use /{Command.Name} {Command.Usage}");
                    }
                    else if (result.Message != null)
                    {
                        var message = result.Message;

                        var color = ColorUtil.GetColorFromString(ref message);

                        commandSource.SendMessage(message, color);
                    }

                    var posExec = new CommandPosExecuteEvent(Command, cmdArgs, commandSource, result);
                    EssentialsEvents._OnCommandPosExecute?.Invoke(posExec);
                }
            }
            catch (Exception e)
            {
                if (caller is UnturnedPlayer)
                {
                    UPlayer.TryGet((UnturnedPlayer)caller, EssLang.COMMAND_ERROR_OCURRED.SendTo);
                }

                UEssentials.Logger.LogError(e.ToString());
            }
        }