Task-Chain Tool icon

Task-Chain Tool -----

A util designed to chain tasks together. Works with plain java and bukkit.



What is this library?
TaskChainTool is a simple library that allows developers to chain together different tasks. Yes, that is redundant but that's truly what it does.

In short, TCT allows you, a developer, to execute tasks in an expected order by wrapping your code in java runnable and then waiting for those runnable to finish without having to write any logic. A similar resource, and frankly an inspiration, is Aikar's TaskChain.

How to import it?
In order to use the base TCT, you must import TaskChainTool.java and DelayTask.java onto your project. This will allow you to chain plain java tasks together. A limitation of the base TCT is that tasks are always asynchronous and thus many Bukkit methods used within this will throw an Exception.

If you need to run synchronous Bukkit code, you must import BukkitTCT.java onto your project.

But how do you use it?
Assuming you're using BukkitTCT.java, here's an example:

Code (Java):

        /** Create your TCT object to play with */
       var task = BukkitTCT. create ( ) ;

        /** Now let's say hello to all players. */
        task. add ( ( ) -> Bukkit. getOnlinePlayers ( ). forEach (p -> p. sendMessage ( "Hello from TCT!" ) ) ) ;
        /** Let's introduce you to delay by waiting 420 milliseconds. */
        task. delay ( 420 ) ;
        /**
         * It really is that easy. Now how about running a task and then waiting? That's
         * a common task right? Welp, there's a method for that too.
         */

        task. addWithDelay (
                ( ) -> Bukkit. getOnlinePlayers ( ). forEach (all -> all. sendMessage ( "This will happen and then wait 69ms!" ) ),
                69 ) ;
        /**
         * Now, if you want to run a task after waiting those 69ms, you can do that too.
         */

        task. add ( ( ) -> Bukkit. getOnlinePlayers ( ). forEach (p -> p. sendMessage ( "Wow, this actually happend 69ms later!" ) ) ) ;
        /** Now let's run it by calling the method execute() */
        var result = task. execute ( ) ;
        /**
         * Note that this method returns a CompletableFuture<Boolean>, so if you ever
         * need to wait for the task to finish to execute more code you can. You can use
         * .thenAccept(b -> { Logic here }) to do that.
         */

        result. thenAccept (b -> {
            // Once the code gets here, the task has finished executing.
            Bukkit. getOnlinePlayers ( ). forEach (p -> p. sendMessage ( "Bye bye from TCT." ) ) ;
        } ) ;


 
Of course, this is a silly example, but the tool itself is actually pretty useful. For instance, instead of sending a message when the task completes execution, you could just repeat the whole task if you wanted it to loop forever, this can be useful when building text animations and such.

Also, to use BukkitTCT you must call BukkitTCT.registerPlugin(Plugin) at least once.

Here's a shorter version of how to do the exact same thing as the example above.
Code (Java):

BukkitTCT. create ( )
. add ( ( ) -> Bukkit. getOnlinePlayers ( ). forEach (p -> p. sendMessage ( "Hello from TCT!" ) ) )
. delay ( 420 )
. addWithDealy ( ( ) -> Bukkit. getOnlinePlayers ( ). forEach (all -> all. sendMessage ( "This will happen after waiting 420ms and then will wait 69ms!" ) ), 69 ) )
. add ( ( ) -> Bukkit. getOnlinePlayers ( ). forEach (p -> p. sendMessage ( "Wow, this actually happend 69ms later!" ) ) )
. execute ( )
. thenAccept (b -> Bukkit. getOnlinePlayers ( ). forEach (p -> p. sendMessage ( "Bye bye from TCT." ) ) ) ;

 


How does it work?
Fundamentally, this library uses java runnable(s) and completable futures to perform all the logic. When you add a task, you are actually pushing a Runnable onto a LinkedQueue. A delay is just a task that runs Thread.Sleep(long).

Now in terms of execution, the method execute() just runs a while loop inside a CompletableFuture.supplyAsync(), that is constantly polling and running the tasks in the LinkedQueue, one at a time, on a separate thread until they finish and then moving onto to the next task until all tasks are done. BukkitTCT does something similar but instead of being constantly asking if the task has finished, it only asks every 1 server tick.

It is important to note that this probably isn't the best way of doing this but I got it done in an afternoon and so far it works pretty great. Remember that this is an open-source project so you can fork it and PR whatever features or improvements you want to see added to it.
Resource Information
Author:
----------
Total Downloads: 140
First Release: Oct 24, 2021
Last Update: Oct 24, 2021
Category: ---------------
All-Time Rating:
0 ratings
Version -----
Released: --------------------
Downloads: ------
Version Rating:
----------------------
-- ratings