Player-based countdown timer.

Discussion in 'Plugin Development' started by Razorcane, Nov 1, 2011.

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

    Razorcane

    I'm trying to code a functional afk command, which will be accessible by everyone. The problem is, they would be able to spam this command over and over again, and I don't want that to happen. I want to apply a 2 minute cooldown on the use of the command. I would assume that I would have to do this in a Player Listener, but I haven't the faintest idea how. I tried a Timer/TimerTask but that didn't work. I've been looking into the Bukkit Scheduler but I haven't the faintest idea of how it works, even after using the Javadoc.
     
  2. Offline

    Windwaker

    Well Bukkit's schedular works like this (assuming you want to use a delayed task, other methods here)
    Code:java
    1. myPlugin.getServer().getSchedular().scheduleSyncDelayedTask(myPlugin, new Runnable() {
    2. public void run() {
    3. // do something after time is up
    4. }
    5. }, 20L); // how long the task is (20 server ticks == 1 second)

    Now your logic should be:
    1. When a CommandSender sends the afk command, put him in a <Player, Boolean> HashMap
    2. Use above code in your onCommand and start the delayed task (yes the run() method goes inside your onCommand)
    3. When the delay ends remove the player from the HashMap.
    Also make sure you check if the player is in the HashMap each time he sends the command:

    Code:java
    1. if(!myHashMap.contains(player){
    2. // do afk stuff here
    3. }else{
    4. // player is cooling down

    If you have any questions just ask
     
  3. Offline

    Razorcane

    I noticed you have an "L" after the 20 ticks. Is that a typo or does that mean something?
    Also, someone told me I had to initialize a scheduler in OnEnable(). Is that true?
     
  4. Offline

    Windwaker

    L is not a typo its for Long as in a number that can be like 9.999999999999999999999999999999

    Second question: No you do not.
     
  5. Offline

    desht

    You're thinking of float/double there. Java longs are signed 64-bit integral quantities (as opposed to ints, which are signed 32-bit integral quantities).
     
  6. Offline

    Lolmewn

    I really don't know what the L is, I just know it works <3
     
  7. Offline

    Windwaker

    Right my bad ^^
     
  8. Offline

    desht

    An 'L' after any numeric literal indicates that the number is a long (and not an int). scheduleSyncDelayedTask() expects a long for the delay.

    (It's not strictly necessary to specify 20L there, since Java will auto-convert an int to a long if it's passed as a parameter, but it's good practice to match your formal and actual parameter types up).
     
  9. HashMap<Player, Boolean> makes absolutely no sense. You never need to check the boolean, as the information that the Player is in the HashMap is enough for that. You can simply use HashSet<Player>, it is faster.
     
  10. Offline

    Lolmewn

    Oh ye! That was it! HashSet! I forgot what it was, and am now using ArrayLists, but ah well :p
     
  11. Offline

    Windwaker

    HashSet works but HashMap<Player. Boolean> does make sense. You can put a player in the HashMap with the value of true or false.
     
  12. And what's the point of it? (I was refering to this particular situation, by the way, even though you can replace it with a Set in most situations)
     
  13. Offline

    Windwaker

    I agree a HashSet makes more sense... I admit that.
     
    Bone008 likes this.
  14. Offline

    BlueFreakLP

    Hey, i know that theard is Old, but i get error by:
    myPlugin.getServer().getSchedular().scheduleSyncDelayedTask
    Eclipse cant resolve myplugin
     
  15. Offline

    Serilum

    You need to replace myPlugin with something, but you can also use Bukkit.getServer().getSchedular().scheduleSyncDelayedTask
     
  16. Offline

    BlueFreakLP

    Ok but I get an error:
    (myPlugin, new Runnable()
    Eclipse give me by myPlugin an error and to replace it with Bukkit not resolve the Problem. I get still errors
     
  17. Offline

    fireblast709

    replace it with an instance of your main class. Or in the case it is in your main class, use the 'this' pointer
     
Thread Status:
Not open for further replies.

Share This Page