4 TaskBuilder
Jesse Boyd edited this page 2016-11-02 04:05:02 +11:00

Overview

The TaskBuilder simplifies creating sequential sync/async tasks. https://github.com/boy0001/FastAsyncWorldedit/blob/master/core/src/main/java/com/boydti/fawe/util/task/TaskBuilder.java

Methods:

  • sync([Runnable|ReturnTask|ReceiveTask|Task]) - Run a task on the main thread
  • async([Runnable|ReturnTask|ReceiveTask|Task]) - Run a task off the main thread
  • delay([int|DelayedTask]) - Delay the next task by a number of ticks
  • syncParallel([Runnable|ReturnTask|ReceiveTask|Task]) - Run all the next syncParallel tasks in parallel
  • asyncParallel([Runnable|ReturnTask|ReceiveTask|Task]) - Run all the next asyncParallel tasks in parallel
  • syncWhenFree([SplitTask|ReturnTask|ReceiveTask|Task]) - Run a task when there's free time on the main thread
  • abortIfTrue(Task<Boolean,Object>) - Abort all tasks if true
  • build() - Starts execution
  • buildAsync() - Starts execution from an async thread

Multiple tasks

Just prints the number 6

new TaskBuilder()
.async((ReturnTask<Integer>) () -> 5 + 1)
.sync((ReceiveTask) input -> System.out.println(input))
.build();

SplitTask example

Split up a task and run it when the main thread is free.

// In reality you'd use one of EditSession, AsyncWorld, FaweQueue instead.
// But this is just an example of how you can split up a task with the FAWE API

final World world = Bukkit.getWorld("world");
new TaskBuilder()
// Execution will be split into multiple 20ms tasks
.syncWhenFree(new TaskBuilder.SplitTask(20) {
    @Override
    public Object exec(Object previous) {
        for (int x = 0; x < 100; x++) {
            for (int y = 0; y < 100; y++) {
                for (int z = 0; z < 100; z++) {
                    world.getBlockAt(x, y, z).setType(Material.STONE);
                    // FAWE will use this point to split the task
                    // You can have multiple split points
                    split();
                }
            }
        }
        // We don't really need the result for any next tasks
        return null;
    }
})
.build();

Task meta

The TaskBuilder extends Metadatable, so you can use it to store temporary data: https://github.com/boy0001/FastAsyncWorldedit/blob/master/core/src/main/java/com/boydti/fawe/object/Metadatable.java

final TaskBuilder task = new TaskBuilder();
task.sync(() -> {
    task.setMeta("blah", 5);
    return task.<Integer>getMeta("blah") + 56;
});