Countdown without scheduler

Discussion in 'Plugin Development' started by Flyverse, Sep 13, 2013.

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

    Flyverse

    Hi!

    I need to make a countdown in minutes. It has to be flexible: Adding/substracting of time should be easy. In "normal" javaprograms I would do something like this (pseudocode!):

    Code:
    long started = System.currentTimeMillis();
    long timeTarget = 60000;
     
    ...
     
    in updatemethod():
    if(System.currentTimeMillis() > started + timeTarget)
    --> do something (Send expire message or things like that)
    else
    --> started = System.currentTimeMillis();
    
    ... But how can I do something like this in Bukkit, since there isn't an "update" event? Do I have to use the scheduler, if I want to send an expiremessage? If yes, how can I add time? (Note: I want a different schedule for different things. HashMap<Thing, LongStartedInTimemillis>.)

    Kindly Regards,
    -Fly
     
  2. Offline

    Flyverse

    Anyone got an idea =o?
     
  3. Offline

    CubieX

    Do you need to change the countdown while it is already running?
    Then use a scheduled task with a short cycle time (as short as you need it. A second or so) for checking your System.currentTimeMillies() - calculation.

    If you only need to create different countdowns, but without modifying the time while they are running, you can make the tasks cycle time a variable in your scheduler instead of a fixed value and schedule each task with your needed time.
     
    Flyverse likes this.
  4. Offline

    Flyverse

    Thanks for the answer! Yes, I need to change the amount of time while running.

    Wow, much better solution than mine! Just one scheduler will be running at the same time. But, wouldn't it affect the performance anyway, if the scheduler is there the whole time? Well, I guess I'll just try it :)!
     
  5. Offline

    CubieX

    As this scheduler does not much except for a simple compare of values, it will not have noticeable impact on your performace.
     
    Flyverse likes this.
  6. Offline

    Flyverse

    Okay, thanks!

    Do you think this is a good way to do it?
    Code:
    //The hashmap
    public HashMap<String, SimpleTimeStamp> data = new HashMap<String, SimpleTimeStamp>();
     
    ...
     
    //onEnable:
    new PSR(this).runTaskTimer(this, 40, 40);
     
    ...
     
    //PSR
    @Override
    public void run() {
    for(String o : _ps.data.keySet()){
    SimpleTimeStamp sts = _ps.data.get(o);
    if(sts.expired()){
    _ps.data.remove(o);
    if (_ps.getConfig().getBoolean("SendMessageOnExpire"))
    System.out.println("Expired for: " + o); //some debug...
    }
    }
    }
     
    ...
     
    //A method
    public void addTime(String o, Long timeToAdd){
    [INDENT=2]if(data.containsKey(o))
    data.put(o, data.get(o).extendExpireTime(timeToAdd));
    else
    data.put(o, new SimpleTimeStamp(timeToAdd));
    }
     
    ...
     
    //SimpleTimStamp
    public class SimpleTimeStamp {
     
    public long created = 0;
    public long millisUntilExpired = 0;
     
    public SimpleTimeStamp(long timeToGo){
    created = System.currentTimeMillis();
    millisUntilExpired = timeToGo;
    }
     
    public boolean expired(){
    if(System.currentTimeMillis() - created > millisUntilExpired)
    return true;
    else
    return false;
    }
     
    public void restart(){
    created = System.currentTimeMillis();
    }
     
    public SimpleTimeStamp extendExpireTime(long toAdd){
    millisUntilExpired += toAdd;
    return this;
    }
     
    }
    ... Just a little "noob"-question (Very tired right now =S): How can I see how much millis are left until the end of the countdown with my way?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  7. Offline

    CubieX

    target time - current time?
     
Thread Status:
Not open for further replies.

Share This Page