[Simple Question] Cooldown for Commands

Discussion in 'Plugin Development' started by zorro1o1, Nov 6, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    zorro1o1

    Hello there!

    I am currently developing a plugin and i want to know how can i add cooldown for my command?
    Code:
    Code:java
    1. if (commandLabel.equalsIgnoreCase("report")) {
    2. if ((!(sender instanceof Player)) || (player == null))
    3. sender.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN + "Positive" + ChatColor.RED + "Reporter" + ChatColor.GOLD + "]" + ChatColor.BLUE + "Only players can report others!");
    4. else if ((player.hasPermission("reporter.report")) || (player.hasPermission("reporter.*")) || (player.isOp()) || (player.isOp())) {
    5. if ((args.length == 0) || (args.length == 1)) {
    6. player.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN + "Positive" + ChatColor.RED + "Reporter" + ChatColor.GOLD + "]" + " " + ChatColor.BLUE + "Unknown command! type '/preport help' for a list of commands");
    7. } else if ((args.length >= 2) && (args.length <= 26)) {
    8. String user = player.getName();
    9. String target = args[0];


    Could someone give me an example? Thanks!
     
  2. Offline

    1Rogue

    pseudocode:
    Code:
    collection of player names on cooldown
     
    oncommand:
      if playername not in collection:
        run command;
        add to collection;
        remove from collection in x seconds with a runnable;
      else:
        state a cooldown is in effect;
     
  3. Offline

    zorro1o1

    what o.o
     
  4. Offline

    Ranil

    He told you how to do it without giving you code. If you want someone to give you code, you aren't going to learn much. To make a cooldown, you have to start a runnable.
     
  5. Offline

    Wout_

    It's verry easy...

    First create a arraylist that contains Players.

    After the
    Code:java
    1. if (commandLabel.equalsIgnoreCase("report")) {
    you simpel add a if statement. If the ArrayList contains the player you simpel denied the command by a message. If the ArrayList does not contain the player you let him do the command. After the command you add the player to the ArrayList.

    If the player is added to the ArrayList, a scheduler starts. If the scheduler stops you remove the player from the ArrayList. If this works you got a simple cooldown. =)
     
  6. Offline

    calebbfmv

    I will give you a hint:
    Code:
    Bukkit.getScheduler().scheduleSync___Task(this, new Runnable(),
    public void run(){
    
     
  7. Offline

    NathanWolf

    I just want to add- people always try to use scheduled tasks for cooldowns. It's probably overcomplicating things unless you plan on showing some sort of running timer (like XP bar or on-screen messages or something).

    Otherwise, if all you want to do is prevent them from re-using the command within N seconds, you only need to check for cooldowns when they try to use the command. No scheduling required.

    Also, the solution I always see provided for this does not allow for player-specific cooldowns. Here's an example of why this was broken (Let's assume a 5-second cooldown):

    1. You use the command, 5 seconds left on cooldown
    2. I use the command 4 seconds later, there is 1 second left on cooldown
    3. 1 second later we can both use the command again, though you had to wait longer than me

    The longer the cooldown, the worse this problem will be.

    Anyway, here's the basic idea- just track timers for each player, rather than throwing them in a list:

    Code:java
    1. // in your class
    2. private static HashMap<String, Long> lastUse = new HashMap<String, Long>();
    3. private static int cooldownMillis = 5000;
    4.  
    5. // in your command handler, on command:
    6. // This assumes you have a "player" variable
    7. String playerName = player.getName();
    8. boolean allowUse = true
    9. long now = System.currentTimeMillis();
    10. // Check to see if the player has used this command before
    11. if (lastUsse.containsKey(playerName)) {
    12. // if they have, see if they've used it long enough ago
    13. allowUse = lastUse.get(playerName) + cooldownMillis < now;
    14. }
    15.  
    16. // Let them use it
    17. if (allowUse) {
    18. // Mark the last time they used the command
    19. lastUse.put(playerName, now);
    20.  
    21. // Do your thing here
    22. // (put your command's code here)
    23. }


    I hope that helps!
     
  8. Offline

    wouterrr

    My way of doing this is first adding the player to an List<String>, after that I create a delayed task (runnable) which will delete the player from that list after x ticks. Then add a if statement before the player can execute command, which checks wheter the player is in the list or not.
     
    1Rogue and geNAZt like this.
Thread Status:
Not open for further replies.

Share This Page