[LIB] TimerLib - Easily create delays / cooldowns!

Discussion in 'Resources' started by Milkywayz, Aug 10, 2012.

?

Do you think this is a useful library?

  1. Yes

    70.8%
  2. Somewhat

    15.4%
  3. No

    6.2%
  4. Derp...

    33.8%
Multiple votes are allowed.
Thread Status:
Not open for further replies.
  1. Offline

    WarmakerT

    How can I make it do something after the cooldown's over?
     
  2. Offline

    Milkywayz

    How i would do it is, check if they are cooling down, if they are then whatever they are trying todo should be cancelled, if they aren't cooling down then you should let them do what they are trying todo and also schedule a new timer so that its blocked for another x amount of time. Like this:
    PHP:
    if(PlayerTimer.isCoolingDown(params)) {
      
    e.setCancelled(true);
    } else {
      
    Scheduler.schedulePlayerCooldown(params);
    }
    Excuse any errors as I just typed that on the fly
     
  3. Offline

    x2nec

    Is it a possibility to add another parameter which calls another method?

    So like: (PlayerTimer.isCoolingDown(params, methodname())

    If not I understand but that'd be awesome. Awesome library btw :p
     
  4. Offline

    Milkywayz

    I believe its possible, ill try and implement that. Thanks for making a suggestion. What would the method name be used for though?
     
  5. Offline

    x2nec

    my intention for it would likely be to run something after the player has cooled and whilst they are cooling :)
     
  6. Offline

    Milkywayz

    Oh i understand, it would like call another method. I can easily make it so you can call another classes constructer. Hopefully I can do the same for methods in the same class. :D
     
  7. Offline

    x2nec

    And this would work for when the timer finishes? If so then "epic" :D
     
  8. Offline

    Milkywayz

    Yeah I can do that, ill get working on it :D

    After a little bit of coding, this is what i came up with:
    Code:
        public static void whileCooling(String player, Time time, String methodname, Object obj, Object[] args) {
            Method method = null;
            do {
                try {
                    method = obj.getClass().getMethod(methodname);
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        method.invoke(obj, args);
                    } catch (IllegalArgumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            } while(isCoolingDown(player, time));
        }
    Havent tested it yet however.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  9. Offline

    Milkywayz

    Anyone make anything cool with this library? :3
     
  10. Offline

    WarmakerT

    I copied the whole code to my IDE, does that count? :D
     
  11. Offline

    Milkywayz

    I guess lol, at least someone uses it... :'(
     
    WarmakerT likes this.
  12. Offline

    WarmakerT

    This library is actually really useful, thank you for it, I just don't have any use for it for now D=
    But again, thank you :)
     
  13. Offline

    Milkywayz

    :D It's good to hear someone likes it, it's definitely motivation to improve it. Im probably going to try to make it more light weight soon. :D
     
    WarmakerT likes this.
  14. Offline

    confuserr

    Using it for a cool-down for a command in one of my custom plugins, working great, very easy to implement to! :D
     
    WarmakerT likes this.
  15. Offline

    Scullyking

    After reaching a dead end (or so I thought) with developing my plugin because I was stumped with timers and cooldowns, this looks promising. About to try it out now!

    I'm a fairly new to programming, and can't get it to work with a command. I'm trying this:
    Code:
    if (GeneralTimer.isCoolingDown(Time.MAIN)) {
            e.setCancelled(true);
            Bukkit.broadcastMessage(ChatColor.RED + "Wait for cooldown!");
        } else {
            Scheduler.scheduleGeneralCooldown(Scheduler.schedule(this,
                    Time.MAIN));
        }
    But I of course need to declare e, but I don't know how exactly? Could you offer a solution? :)

    EDIT: I should also mention that the above code is wrapped in the usual correct command if statement.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  16. Offline

    Milkywayz

    That's using General timers which don't check for players. If your using commands it's recommended to use Per Player cool downs using PlayerTimer. However that code looks like it should work as long as 'this' is your main class.
     
  17. Offline

    Scullyking

    Milkywayz

    It's been ages since I last worked on my plugin. Heres my code for a basic command cooldown. :)

    Code:java
    1. if(cmd.getName().equalsIgnoreCase("rep")){
    2.  
    3. if (PlayerTimer.isCoolingDown(e.getPlayer().getName(), Time.EXONE)) {
    4. Bukkit.broadcastMessage(ChatColor.RED + "Wait for cooldown!");
    5. } else {
    6. sender.sendMessage(ChatColor.RED + "Running Reputation " + ChatColor.GREEN + "b0.8" + ChatColor.RED + " by " + ChatColor.GREEN + "Scullyking");
    7. Scheduler.schedulePlayerCooldown(Scheduler.schedule(this, Time.EXONE));
    8. }
    9. returntrue; }


    However I still need to declare e for the main if statement. How or where do I declare e? Thanks :) Again, I am really new to this :S
     
  18. Offline

    Giant

    Well, considering this is a command we are talking about, you could simply use the CommandSender argument...
     
  19. Offline

    Milkywayz

    ((Sender)e).getName()
     
  20. Offline

    Scullyking

    Milkywayz
    *Sigh*


    I don't get any code errors. When I run the command for the first time, its fine. But every time after that the cooldown is still in place.
    Code:java
    1.  
    2. if(cmd.getName().equalsIgnoreCase("rep")){
    3.  
    4. if (PlayerTimer.isCoolingDown(sender.getName(), Time.EXTWO)) {
    5. sender.sendMessage(ChatColor.RED + "Please wait for cooldown!");
    6. } else {
    7. sender.sendMessage(ChatColor.RED + "Running Reputation " + ChatColor.GREEN + "b0.8" + ChatColor.RED + " by " + ChatColor.GREEN + "Scullyking");
    8. Scheduler.schedulePlayerCooldown(Scheduler.schedule(this, sender.getName(), Time.EXTWO)); }
    9.  
    10. returntrue; }
    11.  
     
  21. Offline

    TheE

    Thanks for this library. I used a slightly modified version to add cooldowns and warmups to MyWarp.
     
  22. Offline

    Milkywayz

    Awesome glad you like it, and actually modifying the library is encouraged as it's basically impossible to create a "One size fits all" without having an overly bloated library.
     
  23. Offline

    bob7

    Is this timer dead? I really want to use it but the link is down :(
     
    Milkywayz likes this.
  24. Offline

    Milkywayz

    Thanks for the notice! My dropbox was wiped on accident :( I'll update the link in a minute

    Thread updated with working download and a small usage block of text for a small guideline

    Update
    • Fixed the library completely
    • Rewrote the PlayerTimer (GeneralTimer still broken, fix coming soon)
    • Must use for loops instead of while + iterator, for works, latter doesn't.
    • Updated link to the new dl file.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  25. Offline

    bob7

    Quick question, can this get the remaining time if i have multiple delays running?


    For instance, if i make a plugin that delays commands. I want to get their remaining time, but they're are like 10+ delays constantly running.

    Edit: NVM! It does! How cool is that!
     
  26. Offline

    Milkywayz

    This library was designed for that, the 'delays' or cooldowns are dependent on the player, so they are unique.

    Getting the remaining time is built in aswell.
     
  27. Offline

    bob7

    Yup, just figured that out lol. You respond to fast xD
     
  28. Offline

    Milkywayz

    Since im following this thread, I get an email whenever someone posts here.
     
  29. Offline

    bob7

    Quick question, what do these enums mean?

    Time.EXTWO
    Time.EXONE
    Time.EXTHREE

    Is that just how much it counts by.. oorr?
     
  30. Offline

    Milkywayz

    They are an example of how you set the delays.

    The Enum converts a double into minecraft longs, it's primarily for ease of use. Those example enums aren't needed.
     
Thread Status:
Not open for further replies.

Share This Page