Cooldowns and warmups?

Discussion in 'Plugin Development' started by Tauryuu, Dec 27, 2011.

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

    Tauryuu

    I see a few plugins that has this feature, but I'm not sure how it works. I tried looking in their source code, but I can't find anything.
     
  2. Offline

    hatstand

    You have two options - You can either use a thread to keep track of it, or store a timestamp and check the time against that when you need to.
     
  3. Offline

    Tauryuu

    Thanks! :)
     
  4. Offline

    Father Of Time

    Personally, I would do the time stamp method if you are trying to track an event that is being triggered 100s of times per minutes by a single individual; otherwise you will have a million timers running.

    use something like:

    Code:
    Long eventoccured = new Date().getTime();
    to store the time the event last successfully happened, then to check how long its been since that occurred you do:

    Code:
    Long lapse = new Date().getTime() - eventoccured;
    the results are in milliseconds, so if you want to make someone wait 5 seconds you do:

    Code:
        if( lapse >= 5000 )
        {
            eventoccured = new Date().getTime();
            // 5 seconds have elapsed, trigger event
        }
    So if you are trying to say, make explosion pots only throwable every 5 seconds per player, you would make a hashMap<String, Long>, the string being their name and the long being the eventoccured variable. Every time the person does the event successfully you update the hashmap with the new long, and compare the values in that hasmap to determine elapsed time.

    I hope this helps, I wrote it kinda fast and off the top of my head; so the logic may be a bit off.

    Good luck! :D
     
  5. Offline

    hatstand

    Yeah, it's also simpler and easier. Your example is pretty much how I see it being done.
     
Thread Status:
Not open for further replies.

Share This Page