Beispiel #1
0
        internal static CommandPreExecuteEvent CallCommandPreExecute(ICommand command, ref ICommandArgs cmdArgs,
                                                                     ref ICommandSource commandSource)
        {
            var evt = new CommandPreExecuteEvent(command, cmdArgs, commandSource);

            OnCommandPreExecute?.Invoke(evt);
            cmdArgs       = evt.Arguments;
            commandSource = evt.Source;
            return(evt);
        }
Beispiel #2
0
        void OnCommandPreExecuted(CommandPreExecuteEvent e)
        {
            if (e.Source.IsConsole || e.Source.HasPermission("essentials.bypass.commandcost"))
            {
                return;
            }

            if (_cachedEconomyProvider == null)
            {
                _cachedEconomyProvider = UEssentials.EconomyProvider;
            }

            if (_cachedEconomyProvider.IsAbsent)
            {
                /*
                 *  If economy hook is not present, this "handler" will be unregistered.
                 */
                EssCore.Instance.EventManager.Unregister(GetType(), "OnCommandPreExecuted");
                EssCore.Instance.EventManager.Unregister(GetType(), "OnCommandPosExecuted");
            }

            if (_cachedCommands == null)
            {
                _cachedCommands = EssCore.Instance.CommandsConfig.Commands;
            }

            if (!_cachedCommands.ContainsKey(e.Command.Name))
            {
                return;
            }

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

            if (cost <= 0)
            {
                return;
            }

            /*
             *  Check if player has sufficient money to use this command.
             */
            if (_cachedEconomyProvider.Value.Has(e.Source.ToPlayer(), cost))
            {
                return;
            }

            EssLang.COMMAND_NO_MONEY.SendTo(e.Source, cost);
            e.Cancelled = true;
        }
Beispiel #3
0
        private void OnCommandPreExecuted(CommandPreExecuteEvent e)
        {
            var commandName = e.Command.Name.ToLowerInvariant();

            if (
                e.Source.IsConsole ||
                !EssCore.Instance.CommandOptions.Commands.TryGetValue(commandName, out var commandOptions)
                )
            {
                return;
            }

            // Check cooldown
            if (!e.Source.HasPermission("essentials.bypass.commandcooldown"))
            {
                var playerId = e.Source.ToPlayer().CSteamId.m_SteamID;

                if (
                    CommandCooldowns.ContainsKey(playerId) &&
                    CommandCooldowns[playerId].TryGetValue(commandName, out var nextUse) &&
                    nextUse > DateTime.Now
                    )
                {
                    var diffSec = (uint)(nextUse - DateTime.Now).TotalSeconds;
                    EssLang.Send(e.Source, "COMMAND_COOLDOWN", TimeUtil.FormatSeconds(diffSec));
                    e.Cancelled = true;
                    return;
                }
            }

            // Check if player has money enough to run this command
            if (UEssentials.EconomyProvider.IsPresent && !e.Source.HasPermission("essentials.bypass.commandcost"))
            {
                var cost        = GetCommandCost(commandOptions, e.Source.ToPlayer());
                var ecoProvider = UEssentials.EconomyProvider.Value;

                if (cost > 0 && !ecoProvider.Has(e.Source.ToPlayer(), cost))
                {
                    EssLang.Send(e.Source, "COMMAND_NO_MONEY", cost, ecoProvider.CurrencySymbol);
                    e.Cancelled = true;
                }
            }
        }
Beispiel #4
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());
            }
        }