crwdns19952:0crwdne19952:0

crwdns19953:0:javadoc:crwdnd19953:0:javadoc:crwdnd19953:0:ref:crwdne19953:0

crwdns19954:0crwdne19954:0

crwdns19955:0crwdne19955:0

import org.spongepowered.api.scheduler.Task;

Task.Builder taskBuilder = Task.builder();

crwdns28471:0:javadoc:crwdne28471:0

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

crwdns19957:0crwdne19957:0

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

crwdns19958:0crwdne19958:0

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

crwdns19959:0crwdne19959:0

crwdns19960:0crwdne19960:0

crwdns19961:0crwdne19961:0 crwdns19962:0crwdne19962:0 crwdns19963:0crwdne19963:0
crwdns19964:0crwdne19964:0

crwdns19965:0crwdne19965:0

crwdns42870:0crwdne42870:0
crwdns42872:0crwdne42872:0

crwdns19968:0crwdne19968:0

crwdns19969:0crwdne19969:0

crwdns19970:0crwdne19970:0

crwdns19971:0crwdne19971:0
crwdns42874:0crwdne42874:0
crwdns42876:0crwdne42876:0
crwdns42878:0crwdne42878:0
crwdns42872:0crwdne42872:0

crwdns19975:0crwdne19975:0

crwdns19976:0crwdne19976:0

crwdns19970:0crwdne19970:0

crwdns19977:0crwdne19977:0 crwdns19978:0crwdne19978:0 crwdns19979:0crwdne19979:0
crwdns19980:0crwdne19980:0 crwdns19981:0crwdne19981:0 crwdns37082:0crwdne37082:0

crwdns19983:0:javadoc:crwdne19983:0

crwdns19984:0crwdne19984: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);

crwdns19985:0:javadoc:crwdne19985:0

task.cancel();

crwdns42209:0crwdne42209: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

crwdns42880:0:javadoc:crwdnd42880:0{state}crwdnd42880:0:javadoc:crwdnd42880:0:javadoc:crwdne42880:0

crwdns19987:0crwdne19987:0

crwdns19988:0crwdne19988:0

crwdns42099:0crwdne42099:0

  • crwdns19990:0crwdne19990:0
  • crwdns19991:0crwdne19991:0
  • crwdns19992:0crwdne19992:0

crwdns19993:0crwdne19993:0

  • crwdns19994:0crwdne19994:0
  • crwdns19995:0crwdne19995:0

Warning

crwdns37110:0crwdne37110:0

Warning

crwdns42880:0:javadoc:crwdnd42880:0{state}crwdnd42880:0:javadoc:crwdnd42880:0:javadoc:crwdne42880:0

crwdns19996:0crwdne19996:0

crwdns19997:0crwdne19997:0

crwdns19998:0crwdne19998:0

  • crwdns19999:0:javadoc:crwdnd19999:0:javadoc:crwdne19999:0
  • crwdns20000:0:javadoc:crwdne20000:0

crwdns20001:0crwdne20001:0

import org.spongepowered.api.scheduler.SpongeExecutorService;

PluginContainer plugin = ...;

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

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

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

crwdns20002:0crwdne20002:0

crwdns20003:0crwdne20003:0

crwdns20004:0crwdne20004:0

crwdns20005:0crwdne20005:0

  • crwdns20006:0crwdne20006:0
  • crwdns20007:0crwdne20007:0
  • crwdns20008:0crwdne20008: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
});

crwdns20009:0crwdne20009:0

crwdns20010:0crwdne20010:0

crwdns20011:0crwdne20011:0

crwdns20012:0crwdne20012:0

crwdns20013:0crwdne20013: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"));
          });

crwdns20014:0crwdne20014:0

crwdns28472:0crwdne28472:0

crwdns42623:0crwdne42623:0

crwdns20017:0crwdne20017: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
}