Would it be right to do this?

Discussion in 'Plugin Development' started by Sheepii, Sep 6, 2014.

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

    Sheepii

    I'm trying to make a countdown timer where you can check how much time is left.

    Would I need to do:

    #Sudo code#
    int time = input;
    while(running){
    bukkitrunnable delayed task 1 second
    if(time <= 0){
    running = false;
    }
    time--;
    command that checks if the args are "time"
    return time;
    #Sudo code#

    Or would it be better to do:

    #Sudo code#

    int time = input;
    bukkitrunnable repeating task 1 second
    if(time <= 0){
    cancel the timer
    }else{
    time--;
    command that checks if the args are "time"
    return time;


    I guess what I'm asking is what would be a good coding ethic?
     
  2. Offline

    CraftBang

    Well at the first part, you're going to make a timer over and over???
    Because while running is true, so at the end you will end up with <input> timers? xD.
     
  3. Offline

    Totom3

    Sheepii The best would be to not use Runnables at all. Store in a HashMap the last time they did <what you want to set a cooldown on>. Use System.currentTimeMillis() / 1000 for that. To check the remaining time, you do [value in map] - ((System.currentTimeMillis() / 1000) - cooldown). To check if a player can perform the action, test if the remaining time is less than or equal to 0.
    Code:java
    1. public void applyCooldown(Player p, long cd) {
    2. cooldowns.put(p.getName(), System.currentTimeMillis() / 1000);
    3. }

    Code:java
    1. public long getRemainingTime(Player p) {
    2. if (cooldowns.contains(p.getName()) {
    3. return cooldowns.get(p.getName()) - ((System.currentTimeMillis() / 1000) - cooldown);
    4. }
    5. }

    Note that if you use this technique, you'll have to delete old & unused entries manually
     
  4. Offline

    Sheepii

    Something needs to happen at the end of the time, so I need some way to catch when the time ends. Thus the while loop.
     
  5. Offline

    Totom3

    Sheepii Ah, I see. You can schedule a task for when it'll end & use the technique I suggested to tell how many time is left. Also, the scheduler would solve the problem of old entries since you can simply delete the entry within the run() method.
     
Thread Status:
Not open for further replies.

Share This Page