Exemple #1
0
        /// <summary>
        /// Occurs when someone executes an alias command
        /// </summary>
        void ChatCommand_AliasExecuted(TShockAPI.CommandArgs e)
        {
            string commandIdentifier = e.Message;

            if (!string.IsNullOrEmpty(e.Message))
            {
                commandIdentifier = e.Message.Split(' ').FirstOrDefault();
            }

            //Get the corresponding alias in the config that matches what the user typed.
            foreach (AliasCommand alias in Configuration.CommandAliases.Where(i => i.CommandAlias == commandIdentifier))
            {
                if (alias != null)
                {
                    TimeSpan timeSinceLastUsedCommand = TimeSpan.MaxValue;
                    //cooldown key is a pair of the user's character name, and the command they have called.
                    //cooldown value is a DateTime they last used the command.
                    KeyValuePair <string, AliasCommand> cooldownReference = new KeyValuePair <string, AliasCommand>(e.Player.Name, alias);

                    if (CooldownList.ContainsKey(cooldownReference))
                    {
                        //UTC time so we don't get any daylight saving shit cuntery
                        timeSinceLastUsedCommand = DateTime.UtcNow.Subtract(CooldownList[cooldownReference]);
                    }

                    //has the time elapsed greater than the cooldown period?
                    if (timeSinceLastUsedCommand.TotalSeconds >= alias.CooldownSeconds || e.Player.Group.HasPermission("aliascmd.bypasscooldown"))
                    {
                        Money commandCost             = 0;
                        Economy.EconomyPlayer ePlayer = SEconomyPlugin.GetEconomyPlayerSafe(e.Player.Index);

                        if (!string.IsNullOrEmpty(alias.Cost) && Money.TryParse(alias.Cost, out commandCost) && !e.Player.Group.HasPermission("aliascmd.bypasscost"))
                        {
                            if (ePlayer.BankAccount != null)
                            {
                                if (!ePlayer.BankAccount.IsAccountEnabled)
                                {
                                    e.Player.SendErrorMessageFormat("You cannot use this command because your account is disabled.");
                                }
                                else if (ePlayer.BankAccount.Balance >= commandCost)
                                {
                                    //Take money off the player, and indicate that this is a payment for something tangible.
                                    Journal.BankTransferEventArgs trans = SEconomyPlugin.WorldAccount.TransferTo(ePlayer.BankAccount, -commandCost, Journal.BankAccountTransferOptions.AnnounceToReceiver | Journal.BankAccountTransferOptions.IsPayment);
                                    if (trans.TransferSucceeded)
                                    {
                                        DoCommands(alias, ePlayer.TSPlayer, e.Parameters);
                                    }
                                    else
                                    {
                                        e.Player.SendErrorMessageFormat("Your payment failed.");
                                    }
                                }
                                else
                                {
                                    e.Player.SendErrorMessageFormat("This command costs {0}. You need {1} more to be able to use this.", commandCost.ToLongString(), ((Money)(ePlayer.BankAccount.Balance - commandCost)).ToLongString());
                                }
                            }
                            else
                            {
                                e.Player.SendErrorMessageFormat("This command costs money and you don't have a bank account.  Please log in first.");
                            }
                        }
                        else
                        {
                            //Command is free
                            DoCommands(alias, e.Player, e.Parameters);
                        }

                        //populate the cooldown list.  This dictionary does not go away when people leave so they can't
                        //reset cooldowns by simply logging out or disconnecting.  They can reset it however by logging into
                        //a different account.
                        if (CooldownList.ContainsKey(cooldownReference))
                        {
                            CooldownList[cooldownReference] = DateTime.UtcNow;
                        }
                        else
                        {
                            CooldownList.Add(cooldownReference, DateTime.UtcNow);
                        }
                    }
                    else
                    {
                        e.Player.SendErrorMessageFormat("{0}: You need to wait {1:0} more seconds to be able to use that.", alias.CommandAlias, (alias.CooldownSeconds - timeSinceLastUsedCommand.TotalSeconds));
                    }
                }
            }
        }
Exemple #2
0
        internal async void JistAlias_AliasExecuted(object sender, AliasExecutedEventArgs e)
        {
            //Get the corresponding alias in the config that matches what the user typed.
            foreach (JScriptAliasCommand alias in jsAliases.Where(i => i.CommandAlias == e.CommandIdentifier))
            {
                DateTime     canRunNext  = DateTime.MinValue;
                Money        commandCost = 0;
                IBankAccount account;

                if (alias == null)
                {
                    continue;
                }

                //cooldown key is a pair of the user's character name, and the command they have called.
                KeyValuePair <string, AliasCommand> cooldownReference = new KeyValuePair <string, AliasCommand>(e.CommandArgs.Player.Name, alias);
                if (CooldownList.ContainsKey(cooldownReference))
                {
                    //UTC time so we don't get any daylight saving shit cuntery
                    canRunNext = CooldownList[cooldownReference];
                }

                //has the time elapsed greater than the cooldown period?
                if (DateTime.UtcNow <= canRunNext &&
                    e.CommandArgs.Player.Group.HasPermission("aliascmd.bypasscooldown") == false)
                {
                    e.CommandArgs.Player.SendErrorMessage("{0}: You need to wait {1:0} more seconds to be able to use that.",
                                                          alias.CommandAlias,
                                                          canRunNext.Subtract(DateTime.UtcNow).TotalSeconds);
                    return;
                }

                if (string.IsNullOrEmpty(alias.Cost) == true ||
                    e.CommandArgs.Player.Group.HasPermission("aliascmd.bypasscost") == true ||
                    Money.TryParse(alias.Cost, out commandCost) == false ||
                    commandCost == 0)
                {
                    if (Jist.JistPlugin.Instance == null)
                    {
                        return;
                    }

                    try {
                        /*
                         * Populate cooldown list first before the function's
                         * called, because the function might override it to
                         * something else.
                         */
                        PopulateCooldownList(cooldownReference);
                        Jist.JistPlugin.Instance.CallFunction(alias.func, alias, e.CommandArgs.Player, e.CommandArgs.Parameters);
                        return;
                    } catch {
                        /*
                         * Command failed.
                         */
                    }
                }

                /*
                 * SEconomy may be null.
                 * If this is the case and the command is
                 * not free, bail out.
                 */
                if (SEconomyPlugin.Instance == null)
                {
                    return;
                }


                if ((account = SEconomyPlugin.Instance.GetBankAccount(e.CommandArgs.Player)) == null)
                {
                    e.CommandArgs.Player.SendErrorMessage("This command costs money and you don't have a bank account.  Please log in first.");
                    return;
                }

                if (account.IsAccountEnabled == false)
                {
                    e.CommandArgs.Player.SendErrorMessage("This command costs money and your account is disabled.");
                    return;
                }

                if (account.Balance < commandCost)
                {
                    Money difference = commandCost - account.Balance;
                    if (e.CommandArgs.Player == TSPlayer.Server)
                    {
                        e.CommandArgs.Player.SendErrorMessage("This command costs {0}. You need {1} more to be able to use this.",
                                                              commandCost.ToLongString(),
                                                              difference.ToLongString());
                    }
                    else
                    {
                        e.CommandArgs.Player.SendErrorMessage("This command costs {0}. You need {1} more to be able to use this.",
                                                              commandCost.ToString(),
                                                              difference.ToString());
                    }
                }

                try {
                    /*
                     * Take money off the player, and indicate
                     * that this is a payment for something
                     * tangible.
                     */
                    Journal.BankTransferEventArgs trans = await account.TransferToAsync(SEconomyPlugin.Instance.WorldAccount,
                                                                                        commandCost,
                                                                                        Journal.BankAccountTransferOptions.AnnounceToSender | Journal.BankAccountTransferOptions.IsPayment,
                                                                                        "",
                                                                                        string.Format("AC: {0} cmd {1}", e.CommandArgs.Player.Name, alias.CommandAlias));

                    if (trans.TransferSucceeded == false)
                    {
                        e.CommandArgs.Player.SendErrorMessage("Your payment failed.");
                        return;
                    }

                    if (Jist.JistPlugin.Instance == null)
                    {
                        return;
                    }

                    try {
                        /*
                         * Populate cooldown list first before the function's
                         * called, because the function might override it to
                         * something else.
                         */
                        PopulateCooldownList(cooldownReference);
                        Jist.JistPlugin.Instance.CallFunction(alias.func, alias, e.CommandArgs.Player.Name, e.CommandArgs.Parameters);
                    } catch (Exception) {
                        /*
                         * Command failed but the person paid money for it.
                         * Refund the full cost of the command if an
                         * exception happens.
                         */
                        Jist.ScriptLog.ErrorFormat("alias",
                                                   "{0} paid {1} for alias {2} but it failed and was refunded.",
                                                   e.CommandArgs.Player.Name,
                                                   commandCost.ToString(),
                                                   alias.CommandAlias);
                        RefundAlias(commandCost, e.CommandArgs.Player);
                    }
                } catch (Exception ex) {
                    e.CommandArgs.Player.SendErrorMessage("An error occured in the alias.");
                    TShock.Log.ConsoleError("aliascmd error: {0} tried to execute alias {1} which failed with error {2}: {3}",
                                            e.CommandArgs.Player.Name,
                                            e.CommandIdentifier,
                                            ex.Message,
                                            ex.ToString());
                    return;
                }
            }
        }
Exemple #3
0
        protected void CmdAliasPlugin_AliasExecuted(object sender, AliasExecutedEventArgs e)
        {
            //Get the corresponding alias in the config that matches what the user typed.
            foreach (AliasCommand alias in Configuration.CommandAliases.Where(i => i.CommandAlias == e.CommandIdentifier))
            {
                DateTime     canRunNext  = DateTime.MinValue;
                Money        commandCost = 0;
                IBankAccount playerAccount;

                if (alias == null || SEconomyPlugin.Instance == null)
                {
                    continue;
                }

                //cooldown key is a pair of the user's character name, and the command they have called.
                KeyValuePair <string, AliasCommand> cooldownReference = new KeyValuePair <string, AliasCommand>(e.CommandArgs.Player.Name, alias);
                if (CooldownList.ContainsKey(cooldownReference))
                {
                    //UTC time so we don't get any daylight saving shit cuntery
                    canRunNext = CooldownList[cooldownReference];
                }

                //has the time elapsed greater than the cooldown period?
                if (DateTime.UtcNow <= canRunNext &&
                    e.CommandArgs.Player.Group.HasPermission("aliascmd.bypasscooldown") == false)
                {
                    e.CommandArgs.Player.SendErrorMessage("{0}: You need to wait {1:0} more seconds to be able to use that.",
                                                          alias.CommandAlias,
                                                          canRunNext.Subtract(DateTime.UtcNow).TotalSeconds);
                    return;
                }

                if (string.IsNullOrEmpty(alias.Cost) == true ||
                    e.CommandArgs.Player.Group.HasPermission("aliascmd.bypasscost") == true ||
                    Money.TryParse(alias.Cost, out commandCost) == false ||
                    commandCost == 0)
                {
                    DoCommands(alias, e.CommandArgs.Player, e.CommandArgs.Parameters);
                    PopulateCooldownList(cooldownReference);
                    return;
                }


                if ((playerAccount = SEconomyPlugin.Instance.GetBankAccount(e.CommandArgs.Player)) == null)
                {
                    e.CommandArgs.Player.SendErrorMessage("This command costs money and you don't have a bank account. Please log in first.");
                    return;
                }

                if (playerAccount.IsAccountEnabled == false)
                {
                    e.CommandArgs.Player.SendErrorMessage("This command costs money but your bank account is disabled.");
                    return;
                }

                if (playerAccount.Balance < commandCost)
                {
                    Money difference = commandCost - playerAccount.Balance;
                    e.CommandArgs.Player.SendErrorMessage("This command costs {0}. You need {1} more to be able to use this.", commandCost.ToString(), difference.ToString());
                }

                try {
                    //Take money off the player, and indicate that this is a payment for something tangible.
                    Journal.BankTransferEventArgs trans = playerAccount.TransferTo(SEconomyPlugin.Instance.WorldAccount, commandCost, Journal.BankAccountTransferOptions.AnnounceToSender | Journal.BankAccountTransferOptions.IsPayment,
                                                                                   "",
                                                                                   string.Format("AC: {0} cmd {1}", e.CommandArgs.Player.Name, alias.CommandAlias));
                    if (trans.TransferSucceeded)
                    {
                        DoCommands(alias, e.CommandArgs.Player, e.CommandArgs.Parameters);
                        PopulateCooldownList(cooldownReference);
                        return;
                    }

                    e.CommandArgs.Player.SendErrorMessage("Your payment failed.");
                } catch (Exception ex) {
                    e.CommandArgs.Player.SendErrorMessage("An error occured in the alias.");
                    TShock.Log.ConsoleError("aliascmd error: {0} tried to execute alias {1} which failed with error {2}: {3}", e.CommandArgs.Player.Name, e.CommandIdentifier, ex.Message, ex.ToString());
                    return;
                }
            }
        }