crwdns141553:0crwdne141553:0

Warning

crwdns141555:0crwdne141555:0

crwdns141557:0:javadoc:crwdnd141557:0:javadoc:crwdnd141557:0:ref:crwdne141557:0

crwdns141559:0crwdne141559:0

crwdns141561:0crwdne141561:0

import org.spongepowered.api.scheduler.Task;

Task.Builder taskBuilder = Task.builder();

crwdns141563:0:javadoc:crwdne141563:0

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

crwdns141565:0crwdne141565:0

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

crwdns141567:0crwdne141567:0

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

crwdns141569:0crwdne141569:0

crwdns141571:0crwdne141571:0

crwdns141573:0crwdne141573:0

crwdns141575:0crwdne141575:0

crwdns141577:0crwdne141577:0

crwdns141579:0crwdne141579:0

crwdns141581:0crwdne141581:0

crwdns141583:0crwdne141583:0

crwdns141585:0crwdne141585:0

crwdns141587:0crwdne141587:0

crwdns141589:0crwdne141589:0

crwdns141591:0crwdne141591:0

crwdns141593:0crwdne141593:0

crwdns141595:0crwdne141595:0

crwdns141597:0crwdne141597:0

crwdns141599:0crwdne141599:0

crwdns141585:0crwdne141585:0

crwdns141601:0crwdne141601:0

crwdns141603:0crwdne141603:0

crwdns141591:0crwdne141591:0

crwdns141605:0crwdne141605:0

crwdns141607:0crwdne141607:0

crwdns141609:0crwdne141609:0

crwdns141611:0crwdne141611:0

crwdns141613:0crwdne141613:0

crwdns141615:0crwdne141615:0

crwdns141617:0:javadoc:crwdne141617:0

crwdns141619:0crwdne141619: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);

crwdns141621:0:javadoc:crwdne141621:0

task.cancel();

crwdns141623:0crwdne141623: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(Component.text("Remaining Time: "+seconds+"s"));
        if (seconds < 1) {
            task.cancel();
        }
    }
}

Warning

crwdns141625:0:javadoc:crwdnd141625:0{state}crwdnd141625:0:javadoc:crwdnd141625:0:javadoc:crwdne141625:0

crwdns141627:0crwdne141627:0

crwdns141629:0crwdne141629:0

crwdns141631:0crwdne141631:0

  • crwdns141633:0crwdne141633:0

  • crwdns141635:0crwdne141635:0

  • crwdns141637:0crwdne141637:0

crwdns141639:0crwdne141639:0

  • crwdns141641:0crwdne141641:0

  • crwdns141643:0crwdne141643:0

Warning

crwdns141645:0crwdne141645:0

Warning

crwdns141625:0:javadoc:crwdnd141625:0{state}crwdnd141625:0:javadoc:crwdnd141625:0:javadoc:crwdne141625:0

crwdns141647:0crwdne141647:0

crwdns141649:0crwdne141649:0

crwdns141651:0crwdne141651:0

  • crwdns141653:0:javadoc:crwdnd141653:0:javadoc:crwdne141653:0

  • crwdns141655:0:javadoc:crwdne141655:0

crwdns141657:0crwdne141657:0

import org.spongepowered.api.scheduler.SpongeExecutorService;

PluginContainer plugin = ...;

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

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

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

crwdns141659:0crwdne141659:0

crwdns141661:0crwdne141661:0

crwdns141663:0crwdne141663:0

crwdns141665:0crwdne141665:0

  • crwdns141667:0crwdne141667:0

  • crwdns141669:0crwdne141669:0

  • crwdns141671:0crwdne141671: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
});

crwdns141673:0crwdne141673:0

crwdns141675:0crwdne141675:0

crwdns141677:0crwdne141677:0

crwdns141679:0crwdne141679:0

crwdns141681:0crwdne141681: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(Component.text("Computer says no"));
          });

crwdns141683:0crwdne141683:0

crwdns141685:0crwdne141685:0

crwdns141687:0crwdne141687:0

crwdns141689:0crwdne141689: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
}