crwdns151619:0crwdne151619:0

crwdns151621:0:javadoc:crwdnd151621:0:javadoc:crwdnd151621:0:ref:crwdne151621:0

crwdns151623:0crwdne151623:0

crwdns151625:0crwdne151625:0

import org.spongepowered.api.scheduler.Task;

Task.Builder taskBuilder = Task.builder();

crwdns151627:0:javadoc:crwdne151627:0

taskBuilder.execute(new Runnable() {
    public void run() {
        logger.info("Yay! Schedulers!");
    }
});

crwdns151629:0crwdne151629:0

taskBuilder.execute(
    () -> {
        logger.info("Yay! Schedulers!");
    }
);

crwdns151631:0crwdne151631:0

taskBuilder.execute(
    task -> {
        logger.info("Yay! Schedulers! :" + task.getName());
    }
);

crwdns151633:0crwdne151633:0

crwdns151635:0crwdne151635:0

crwdns151637:0crwdne151637:0

crwdns151639:0crwdne151639:0

crwdns151641:0crwdne151641:0

crwdns151643:0crwdne151643:0

crwdns151645:0crwdne151645:0

crwdns151647:0crwdne151647:0

crwdns151649:0crwdne151649:0

crwdns151651:0crwdne151651:0

crwdns151653:0crwdne151653:0

crwdns151655:0crwdne151655:0

crwdns151657:0crwdne151657:0

crwdns151659:0crwdne151659:0

crwdns151661:0crwdne151661:0

crwdns151663:0crwdne151663:0

crwdns151649:0crwdne151649:0

crwdns151665:0crwdne151665:0

crwdns151667:0crwdne151667:0

crwdns151655:0crwdne151655:0

crwdns151669:0crwdne151669:0

crwdns151671:0crwdne151671:0

crwdns151673:0crwdne151673:0

crwdns151675:0crwdne151675:0

crwdns151677:0crwdne151677:0

crwdns151679:0crwdne151679:0

crwdns151681:0:javadoc:crwdne151681:0

crwdns151683:0crwdne151683:0

import java.util.concurrent.TimeUnit;

Task task = Task.builder().execute(() -> logger.info("Yay! Schedulers!"))
    .async().delay(100, TimeUnit.MILLISECONDS).interval(5, TimeUnit.MINUTES)
    .name("ExamplePlugin - Fetch Stats from Database").submit(plugin);

crwdns151685:0:javadoc:crwdne151685:0

task.cancel();

crwdns151687:0crwdne151687:0

@Listener
public void onGameInit(GameInitializationEvent event) {
    Task task = Task.builder().execute(new CancellingTimerTask())
        .interval(1, TimeUnit.SECONDS)
        .name("Self-Cancelling Timer Task").submit(plugin);
}

private class CancellingTimerTask implements Consumer<Task> {
    private int seconds = 60;
    @Override
    public void accept(Task task) {
        seconds--;
        Sponge.getServer()
            .getBroadcastChannel()
            .send(Text.of("Remaining Time: "+seconds+"s"));
        if (seconds < 1) {
            task.cancel();
        }
    }
}

crwdns151689:0crwdne151689:0

crwdns151691:0crwdne151691:0

crwdns151693:0crwdne151693:0

  • crwdns151695:0crwdne151695:0

  • crwdns151697:0crwdne151697:0

  • crwdns151699:0crwdne151699:0

crwdns151701:0crwdne151701:0

  • crwdns151703:0crwdne151703:0

  • crwdns151705:0crwdne151705:0

Warning

crwdns151707:0crwdne151707:0

crwdns151709:0crwdne151709:0

crwdns151711:0crwdne151711:0

crwdns151713:0crwdne151713:0

  • crwdns151715:0:javadoc:crwdnd151715:0:javadoc:crwdne151715:0

  • crwdns151717:0:javadoc:crwdne151717:0

crwdns151719:0crwdne151719:0

import org.spongepowered.api.scheduler.SpongeExecutorService;

SpongeExecutorService minecraftExecutor = Sponge.getScheduler().createSyncExecutor(plugin);

minecraftExecutor.submit(() -> { ... });

minecraftExecutor.schedule(() -> { ... }, 10, TimeUnit.SECONDS);

crwdns151721:0crwdne151721:0

crwdns151723:0crwdne151723:0

crwdns151725:0crwdne151725:0

crwdns151727:0crwdne151727:0

  • crwdns151729:0crwdne151729:0

  • crwdns151731:0crwdne151731:0

  • crwdns151733:0crwdne151733:0

import java.util.concurrent.CompletableFuture;

SpongeExecutorService minecraftExecutor = Sponge.getScheduler().createSyncExecutor(plugin);

CompletableFuture.supplyAsync(() -> {
    // ASYNC: ForkJoinPool.commonPool()
    return 42;
}).thenAcceptAsync((awesomeValue) -> {
    // SYNC: minecraftExecutor
}, minecraftExecutor).thenRun(() -> {
    // SYNC: minecraftExecutor
});

crwdns151735:0crwdne151735:0

crwdns151737:0crwdne151737:0

crwdns151739:0crwdne151739:0

crwdns151741:0crwdne151741:0

crwdns151743:0crwdne151743:0

import rx.Observable;
import rx.Scheduler;
import rx.schedulers.Schedulers;

SpongeExecutorService executor = Sponge.getScheduler().createSyncExecutor(plugin);
Scheduler minecraftScheduler = Schedulers.from(executor);

Observable.defer(() -> Observable.from(Sponge.getServer().getOnlinePlayers()))
          .subscribeOn(minecraftScheduler) // defer -> SYNC: minecraftScheduler
          .observeOn(Schedulers.io()) // -> ASYNC: Schedulers.io()
          .filter(player -> {
              // ASYNC: Schedulers.io()
              return "Flards".equals(player.getName());
          })
          .observeOn(minecraftScheduler) // -> SYNC: minecraftScheduler
          .subscribe(player -> {
              // SYNC: minecraftScheduler
              player.kick(Text.of("Computer says no"));
          });

crwdns151745:0crwdne151745:0

crwdns151747:0crwdne151747:0

crwdns151749:0crwdne151749:0

crwdns151751:0crwdne151751:0

import scala.concurrent.ExecutionContext

val executor = Sponge.getScheduler().createSyncExecutor(plugin)

import ExecutionContext.Implicits.global
val ec = ExecutionContext.fromExecutorService(executor)

val future = Future {
    // ASYNC: ExecutionContext.Implicits.global
}

future foreach {
    case value => // SYNC: ec
}(ec)

future map {
    case value => 42 // SYNC: ec
}(ec).foreach {
    case value => println(value) // ASYNC: ExecutionContext.Implicits.global
}