Small scheduling library for Bukkit/Spigot plugins
Simple scheduling library to make use of Java 8 lamdas to produce easy to read code.
Below are some examples for the methods available in this library. Parameters of the Run
or RunResult<>
type are interfaces and therefore a lambda or a method reference like this::someMethod
can be used. Often a calling a single method is enough, leading to really clean code.
Do.sync(this::someMethod);
Do.sync(() -> someMethod(player));
Do.sync(() -> {
boolean result = doSomething();
if(result) {
doSomethingElse();
}
});
Do.syncLater(20, this::someMethod);
Do.syncLater(20, () -> someMethod(player));
Do.syncLater(20, () -> {
boolean result = doSomething();
if(result) {
doSomethingElse();
}
});
Call a method synchronously every second (20 ticks):
Do.syncTimer(20, this::someMethod)
If you want to cancel the task at some point, either save the resulting BukkitTask
or return false from your method.
BukkitTask task = Do.syncTimer(20, this::someMethod);
task.cancel();
Do.syncTimer(20, () -> {
doSomething();
if(someCondition) {
return false; // Stop the task
}
return true; // Keep running the task
});
For the methods shown above async versions are also available, simply use async()
and asyncLater()
.
To perform a heavy operation without slowing down the server it is a good idea to spread it over multiple ticks. The idea is to handle X items each tick, which the Do.forall()
method can do for you.
Do.forall(
// Do 10 items per tick (optional, defaults to 1)
10,
// Collection of objects to do something for
Bukkit.getOnlinePlayers(),
// This method will be called once for each item from your collection, in this case with a player
player -> {
calculateScore(player);
player.sendMessage("done");
}
// When everything is complete, log a message (optional)
() -> plugin.getLogger().info("completed");
);
Add Maven repository:
<repositories>
<repository>
<id>nlthijs48</id>
<url>http://maven.wiefferink.me</url>
</repository>
</repositories>
Add Maven dependency:
<dependencies>
<dependency>
<groupId>me.wiefferink</groupId>
<artifactId>bukkitdo</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
Relocate the library (compatibility with other plugins):
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<!-- Relocate BukkitDo -->
<relocation>
<pattern>me.wiefferink.bukkitdo</pattern>
<shadedPattern>your.package.here.shaded.bukkitdo</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>