Command cooldown

Discussion in 'Plugin Development' started by yottabyte, Jan 22, 2011.

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

    yottabyte

    Hey people, I'm currently working on my local chat plugin. Everything is working good but I'd like to add one thing.

    Currently, people are using the broadcasting command like crazy. I want to put some kind of cooldown on this, so that they can only use it, say, every 2 minutes. Now, I honestly have no idea how to create something like this so I'm asking you people [​IMG]

    Thanks.
     
  2. Offline

    Archelaus

    Java timer class might do.
     
  3. Offline

    eisental

    No need for a timer, just record the current time (System.currentTimeMillis() ) every time a player is trying to use this broadcast command. Next time the player sends this command check the time difference between the current time and the time you stored before (System.currentTimeMillis()-oldTime>2*60*1000) to see if the 2 min interval has passed.
     
  4. Offline

    darknesschaos

    This is the best way. And if you are smart you can program it so that it tells them how much longer they need to wait too.
     
  5. Offline

    yottabyte

    Thanks, I think I understand what you are saying. Still one thing though, if I store it in a variable named "time" that one would apply to all players, right? How do I make a variable that only applies to one username? I'm just guessing it has something to do with arrays. But I have no idea tbh. (Yes, java noob here)
     
  6. Offline

    eisental

    You need to make a Map object that maps a player to its time variable. something like that:
    Code:
    class X extends JavaPlugin {
       Map<Player,Long> playerTimes = new HashMap<Player,Long>();
    
    ...
    
       onPlayerCommand(...) {
          Long oldPlayerTime = playerTimes.get(player); // find the last time the player used the command
          long curTime = System.timeInMillis();
          long diff = curTime - oldPlayerTime;
          .....
          playerTimes.put(player, curTime); // update the time variable for the player
       }
    
    You also need to check that outPlayerTime is not null
    Lookup the docs for Map and HashMap for more info
     
  7. Offline

    yottabyte

    Thank you very much for your help! Got it working the way I wanted it to [​IMG]
     
  8. Offline

    Flenix

    Could someone actually make a plugin for this? It'd need a config file, but in the config file you just put any command and then how long the cooldown is.

    Just got Zeus and we want to give the lightning to specific players, but need to add a cooldown coz it can be spammed right now...
     
Thread Status:
Not open for further replies.

Share This Page