Created TaskBuilder (markdown)

Jesse Boyd 2016-11-01 23:57:25 +11:00
parent 050c35c93b
commit c1126ad38a

57
TaskBuilder.md Normal file

@ -0,0 +1,57 @@
### 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
```Java
new TaskBuilder()
.async((ReturnTask<Integer>) () -> 5 + 1)
.sync((ReceiveTask) input -> System.out.println(input))
.build();
```
### SplitTask example
```Java
//In reality you'd use one of FAWE's async world manipulators (EditSession, AsyncWorld, FaweQueue etc.)
// 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()
.syncWhenFree(new TaskBuilder.SplitTask(20) { // Execution will be split into multiple 20ms tasks
@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
split();
}
}
}
// We don't really need the result for any next tasks
return null;
}
})
.execute();
```
### 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
```Java
final TaskBuilder task = new TaskBuilder();
task.sync(() -> {
task.setMeta("blah", 5);
return task.<Integer>getMeta("blah") + 56;
});
```