crwdns121975:0crwdne121975:0

crwdns121977:0:javadoc:crwdnd121977:0:javadoc:crwdnd121977:0:ref:crwdne121977:0

crwdns121979:0crwdne121979:0

crwdns121981:0crwdne121981:0

import org.spongepowered.api.scheduler.Task;

Task.Builder taskBuilder = Task.builder();

crwdns121983:0:javadoc:crwdne121983:0

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

crwdns121985:0crwdne121985:0

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

crwdns121987:0crwdne121987:0

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

crwdns121989:0crwdne121989:0

crwdns121991:0crwdne121991:0

crwdns121993:0crwdne121993:0

crwdns121995:0crwdne121995:0

crwdns121997:0crwdne121997:0

crwdns121999:0crwdne121999:0

crwdns122001:0crwdne122001:0

crwdns122003:0crwdne122003:0

crwdns122005:0crwdne122005:0

crwdns122007:0crwdne122007:0

crwdns122009:0crwdne122009:0

crwdns122011:0crwdne122011:0

crwdns122013:0crwdne122013:0

crwdns122015:0crwdne122015:0

crwdns122017:0crwdne122017:0

crwdns122019:0crwdne122019:0

crwdns122005:0crwdne122005:0

crwdns122021:0crwdne122021:0

crwdns122023:0crwdne122023:0

crwdns122011:0crwdne122011:0

crwdns122025:0crwdne122025:0

crwdns122027:0crwdne122027:0

crwdns122029:0crwdne122029:0

crwdns122031:0crwdne122031:0

crwdns122033:0crwdne122033:0

crwdns122035:0crwdne122035:0

crwdns122037:0:javadoc:crwdne122037:0

crwdns122039:0crwdne122039:0

import java.util.concurrent.TimeUnit;

PluginContainer plugin = ...;

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);

crwdns122041:0:javadoc:crwdne122041:0

task.cancel();

crwdns122043:0crwdne122043: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();
        }
    }
}

Warning

crwdns122045:0:javadoc:crwdnd122045:0{state}crwdnd122045:0:javadoc:crwdnd122045:0:javadoc:crwdne122045:0

crwdns122047:0crwdne122047:0

crwdns122049:0crwdne122049:0

crwdns122051:0crwdne122051:0

  • crwdns122053:0crwdne122053:0

  • crwdns122055:0crwdne122055:0

  • crwdns122057:0crwdne122057:0

crwdns122059:0crwdne122059:0

  • crwdns122061:0crwdne122061:0

  • crwdns122063:0crwdne122063:0

Warning

crwdns122065:0crwdne122065:0

Warning

crwdns122045:0:javadoc:crwdnd122045:0{state}crwdnd122045:0:javadoc:crwdnd122045:0:javadoc:crwdne122045:0

crwdns122067:0crwdne122067:0

crwdns122069:0crwdne122069:0

crwdns122071:0crwdne122071:0

  • crwdns122073:0:javadoc:crwdnd122073:0:javadoc:crwdne122073:0

  • crwdns122075:0:javadoc:crwdne122075:0

crwdns122077:0crwdne122077:0

import org.spongepowered.api.scheduler.SpongeExecutorService;

PluginContainer plugin = ...;

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

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

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

crwdns122079:0crwdne122079:0

crwdns122081:0crwdne122081:0

crwdns122083:0crwdne122083:0

crwdns122085:0crwdne122085:0

  • crwdns122087:0crwdne122087:0

  • crwdns122089:0crwdne122089:0

  • crwdns122091:0crwdne122091:0

import java.util.concurrent.CompletableFuture;

PluginContainer plugin = ...;

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

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

crwdns122093:0crwdne122093:0

crwdns122095:0crwdne122095:0

crwdns122097:0crwdne122097:0

crwdns122099:0crwdne122099:0

crwdns122101:0crwdne122101:0

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

PluginContainer plugin = ...;

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"));
          });

crwdns122103:0crwdne122103:0

crwdns122105:0crwdne122105:0

crwdns122107:0crwdne122107:0

crwdns122109:0crwdne122109: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
}