[Util][Class] Call a method delayed by seconds

Discussion in 'Resources' started by GameplayJDK, Mar 22, 2014.

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

    GameplayJDK

    TimedFunctionCall.java gist: https://gist.github.com/GameplayJDK/9709226

    I've created the class to call a method, just like I'd be able to call with the Bukkit scheduler, but I wanted it to be delayed by seconds, not ticks (because timing by real time seconds is more precise).

    Notice that I haven't tested it with a plugin!

    Example, on how to use it:
    Code:java
    1. // This will call the function after 10 seconds
    2. TimedFunctionCall callLater = new TimedFuntionCall(new Callable<Object>() {
    3. public Object call() {
    4. System.out.println("Hello, now we have 10 seconds later than 10 seconds before.");
    5. return null; // Or anything you want
    6. }
    7. }, 10).call();
    8.  
    9. // If you want to call it earlier that these 10 seconds, that you had set
    10. callLater.callNow();
    11. // Notice: This will also stop the timer from calling it later (to prevent double execution)
    12.  
    13. // If you want to cancel it
    14. callLater.cancel();
    15. // Notice: This will stop the execution if you've already use .call(); else it won't do anything
    16.  
    17. // You can also reuse the TimedFunctionCall by using .call() again
     
  2. Offline

    macguy8

    Although I did read your explanation, I'm just not getting how this is better than the Bukkit system which does the same thing.
     
  3. Offline

    GameplayJDK

    macguy8
    There is no real difference between the bukkit scheduled tasks and the TimedFunctionCall class above. The difference is, that you can use seconds to time the delay. Plus a few other extras (like .callNow() or that you can reuse the TimedFunctionCall instance).
     
  4. Offline

    macguy8

    You can use seconds as well in Bukkit, however, if the server lags your timer will also. Not the biggest issue, though, as tbh if a server is lagging I notice... the lag more than the timers. I believe Bukkit as well has a .callNow() equivalent. As well, you can reuse Runnables in Bukkit.
     
  5. Offline

    RawCode

    ticks * 20 anyone?
     
  6. Offline

    macguy8

    RawCode The only thing I could possibly think of is that due to lag the timer could be inaccurate and you'd need accuracy to a point beyond what Bukkit can deliver - But it's still not that good of a reason to make this, and anyone needing that kind of precision should have the ability to develop something themselves.
     
  7. Offline

    RawCode

    If you server lag like there is no tomorrow, high precision async tasks wont work anyway.
    Under heavy load everything starts to lag.
     
  8. Offline

    xTrollxDudex

    GameplayJDK
    Infinite while loop time stamping? Seriously, there's a ScheduledExecutorService for a reason...
     
Thread Status:
Not open for further replies.

Share This Page