Resource Tasky: A "Short-Cut" for a/sync Code BukkitRunnable, using Lambda in Java 8

Discussion in 'Spigot Plugin Development' started by Wowserman, Jun 30, 2018.

  1. I was just playing around with lambda expressions and thought this code might be an easier approach to a/synchronized tasks with the Bukkit API as it removes a lot of boiler code with BukkitRunnables, might make your life a little better ;). Hopefully, you're starting to think about where else you can use lambda expressions.

    –I've added support for timer/delayed/sycnhronized tasks, check the gist–

    Code (Java):
    Tasky.async(task -> {
        // ...
    }, delay, period);

    Check out the source to this, there's 6 utility methods you can use, all mirror BukkitRunnable's.
    Github Gist Source

    Have a good day
    #1 Wowserman, Jun 30, 2018
    Last edited: Jun 30, 2018
    • Useful Useful x 3
    • Like Like x 1
    • Optimistic Optimistic x 1
  2. It's incredibly simple, but I actually really like it.
    • Like Like x 1
  3. FrostedSnowman

    Resource Staff

    how about passing the task through a consumer to also allow self cancelling in there. this is if you decided to implement the task timers :p
    • Useful Useful x 1
  4. I liked that idea, my original source was pretty bare, so I've recreated it. Check out the gist and the updated usage, it includes timers/delays/sync tasks along with each lambda function passes the BukkitRunnable instance used so you can implement cancelable tasks. :giggle:;)
  5. FrostedSnowman

    Resource Staff

    Probably should choose a better name for the class, since lambda is more of an umbrella P: Maybe something like:

    Code (Java):
    BScheduler.sync(task -> Bukkit.broadcastMessage("super"), 3, 2);
    • Like Like x 1
  6. It is a very nice idea but the naming urrghh
    • Funny Funny x 1
  7. I don't see the point to this as you can just use this already:
    Code (Text):
    Bukkit.getScheduler().runTaskAsynchronously(() -> {
    Code here
    Unless you're doing something else here??
    • Agree Agree x 1
  8. uh why did you use Function<BukkitRunnable,Void>
    you can use Consumer<BukkitRunnable>
    • Agree Agree x 1
  9. You cant self-cancel that AFAIK.
  10. Okx


    Why do you have a non-static method for storing the plugin, and the other methods are static? You shouldn't use static to store state, and definitely not a non-static method to store an object used by static methods.
  11. And using the (shitty) paradigm the language was designed for should be preferred.
  12. Only difference is a BukkitRunnable is passed as the argument for the block.
  13. You could wrap the plugin in a WeakReference to prevent people from forgetting to clear the reference to the plugin when the plugin is disabled and blowing their foot off.
    • Like Like x 1
  14. Or... you could just avoid anonymous classes altogether, especially since 9/10 times you're going to be passing in some params to the BukkitRunnable anyways. ^_^
    • Optimistic Optimistic x 1
  15. Someone should overload the BukkitScheduler class to accept Consumer<BukkitTask>, so we don't need BukkitRunnable the majority of the time, and get good lambda support :)
    • Agree Agree x 1