crwdns132295:0crwdne132295:0

crwdns132297:0:javadoc:crwdnd132297:0:javadoc:crwdnd132297:0:ref:crwdne132297:0

crwdns132299:0crwdne132299:0

crwdns132301:0crwdne132301:0

import org.spongepowered.api.scheduler.Task;

Task.Builder taskBuilder = Task.builder();

crwdns132303:0:javadoc:crwdne132303:0

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

crwdns132305:0crwdne132305:0

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

crwdns132307:0crwdne132307:0

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

crwdns132309:0crwdne132309:0

crwdns132311:0crwdne132311:0

crwdns132313:0crwdne132313:0

crwdns132315:0crwdne132315:0

crwdns132317:0crwdne132317:0

crwdns132319:0crwdne132319:0

crwdns132321:0crwdne132321:0

crwdns132323:0crwdne132323:0

crwdns132325:0crwdne132325:0

crwdns132327:0crwdne132327:0

crwdns132329:0crwdne132329:0

crwdns132331:0crwdne132331:0

crwdns132333:0crwdne132333:0

crwdns132335:0crwdne132335:0

crwdns132337:0crwdne132337:0

crwdns132339:0crwdne132339:0

crwdns132325:0crwdne132325:0

crwdns132341:0crwdne132341:0

crwdns132343:0crwdne132343:0

crwdns132331:0crwdne132331:0

crwdns132345:0crwdne132345:0

crwdns132347:0crwdne132347:0

crwdns132349:0crwdne132349:0

crwdns132351:0crwdne132351:0

crwdns132353:0crwdne132353:0

crwdns132355:0crwdne132355:0

crwdns132357:0:javadoc:crwdne132357:0

crwdns132359:0crwdne132359: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);

crwdns132361:0:javadoc:crwdne132361:0

task.cancel();

crwdns132363:0crwdne132363: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();
        }
    }
}

crwdns132365:0crwdne132365:0

crwdns132367:0crwdne132367:0

crwdns132369:0crwdne132369:0

  • crwdns132371:0crwdne132371:0

  • crwdns132373:0crwdne132373:0

  • crwdns132375:0crwdne132375:0

crwdns132377:0crwdne132377:0

  • crwdns132379:0crwdne132379:0

  • crwdns132381:0crwdne132381:0

crwdns132383:0crwdne132383:0

crwdns132385:0crwdne132385:0

crwdns132387:0crwdne132387:0

  • crwdns132389:0:javadoc:crwdnd132389:0:javadoc:crwdne132389:0

  • crwdns132391:0:javadoc:crwdne132391:0

crwdns132393:0crwdne132393:0

import org.spongepowered.api.scheduler.SpongeExecutorService;

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

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

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

crwdns132395:0crwdne132395:0

crwdns132397:0crwdne132397:0

crwdns132399:0crwdne132399:0

crwdns132401:0crwdne132401:0

  • crwdns132403:0crwdne132403:0

  • crwdns132405:0crwdne132405:0

  • crwdns132407:0crwdne132407: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
});

crwdns132409:0crwdne132409:0

crwdns132411:0crwdne132411:0

crwdns132413:0crwdne132413:0

crwdns132415:0crwdne132415:0

crwdns132417:0crwdne132417: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"));
          });

crwdns132419:0crwdne132419:0

crwdns132421:0crwdne132421:0

crwdns132423:0crwdne132423:0

crwdns132425:0crwdne132425: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
}