Command Cooldowns?

Discussion in 'Plugin Development' started by Vinceguy1, Jun 25, 2012.

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

    Vinceguy1

    How do you make a cooldown for a command?
     
  2. Offline

    ItsHarry

    You can use the bukkit scheduler to time stuff.
     
  3. Offline

    Vinceguy1

    not a scheduler, a cooldown for a specific player.
     
  4. Offline

    hockeygoalie5

    Create a HashMap<String, Long>. When a player executes the command successfully, put their name and System.currentTimeMillis() into the map. Then, when the command is executed, you check if the player is in the map with map.containsKey(player.getName()). If they are, check if the time you stored minus the current time is less than your cool down. Here's some code:
    Code:
    HashMap<String, Long> map = new HashMap<String, Long>();
    int coolDown = 20 // seconds
    public boolean onCommand(CommandSender sender, Command cmd, String commnadLabel, String[] args) {
        if(cmd.getName().equalsIgnoreCase("command")) {
            if(map.containsKey(sender.getName()) {
                long diff = (System.currentTimeMillis() - map.get(sender.getName()))/1000;
                if(diff < coolDown) {
                    // cool down still on
                    return true;
                }
            }
            // command successful
            map.put(sender.getName(), System.currentTimeMillis());
        }
    }
    
     
  5. Offline

    Njol

  6. Offline

    Vinceguy1

    ty, ill try it later, but it looks like it works, again, thanks.
     
Thread Status:
Not open for further replies.

Share This Page