Waiting X time before executing command

Discussion in 'Plugin Development' started by Jake230599, Nov 5, 2012.

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

    Jake230599

    I have a plugin which needs to delay the time before using a command. So once you use the command you have to wait X amount of time before you can use it again. But I also need it to tell the player how long is left before they can execute this command again.

    So my question is, how do I put this delay on the command?
     
  2. Offline

    Ewe Loon

    try this
    Code:
            getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                @Override
                public void run() {
                    do your stuff here
                }
            }, delay);
    
    don't rely on local vars being usable
    in fact its best to use this in a new class if its dependent on being passed parameters
     
    ZeusAllMighty11 likes this.
  3. Offline

    Jake230599

    Doesn't this make you wait X amount of time before anything happens? So if set to 10 seconds I would have to use /command then wait 10 seconds before the command took effect?
     
  4. Offline

    desht

    What you're asking for is also known as a 'cooldown'.

    To implement this, you'll need a Map<String,Long>, which records the last time a player used your command (use the player's name as the Map key). Then, every time your command is executed, check the player's last-execution time against the current time (System.currentTimeMillis() is your friend here). If the difference is too small, the cooldown hasn't expired yet, so send the player an error message and stop there. If the difference is big enough, then proceed with command execution, also remembering to update the last-executed time for the player in your Map.
     
  5. Offline

    Jake230599

    Could you show me an example implementation of this? I am not asking for the whole code, just how it needs to be set-out to work correctly.
     
  6. Offline

    fireblast709

    This should be somewhere soon in the code
    Code:java
    1. public Map<String, Long> cooldown = new HashMap<String, Long>();

    Then this for checking:
    Code:java
    1. Long time = cooldown.get(player.getName());
    2. // Checks if the player has actually performed the command and if he did, if the last time was 10 seconds ago
    3. if(time == null || time - System.currentTimeMillis() >= (10*1000))
    4. {
    5. // do whatever command
    6. cooldown.put(player.getName(), System.currentTimeMillis()); // Set current time
    7. }
    8. else
    9. {
    10. // before you ask:
    11. int remains = (int)Math.floor(10 - System.currentTimeMillis());
    12. player.sendMessage("You would have to wait "+remains+" second(s) before you can execute this command again");
    13. }

    Note that this cooldown works on ALL commands in this plugin. If you want a cooldown per command per player, I would suggest to make a class to record the times per command
     
  7. Offline

    Jake230599

    fireblast709 Thanks for the help, that's all I needed.


    EDIT:
    After messing around a bit I found that this failed to work...
    The time is in - and does not change after waiting, so I don't know what to do about it :(
     
  8. Offline

    Izbay

    Could you post the code segment in question? The time set in the map shouldn't change, as it simply logs when the command was sent. You check if the time has elapsed by comparing the map.get(player) with the system.CurrentTimeMillis(), which should always be changing. Fireblast's solution should absolutely work, and you'd change the 10's in the code to variables for however long you want the cooldown to be.
     
Thread Status:
Not open for further replies.

Share This Page