项目作者: NLthijs48

项目描述 :
Small scheduling library for Bukkit/Spigot plugins
高级语言: Java
项目地址: git://github.com/NLthijs48/BukkitDo.git
创建时间: 2017-11-25T15:17:44Z
项目社区:https://github.com/NLthijs48/BukkitDo

开源协议:

下载


BukkitDo

Simple scheduling library to make use of Java 8 lamdas to produce easy to read code.

Information

Examples

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.

Synchronous task for the next tick

  1. Do.sync(this::someMethod);
  2. Do.sync(() -> someMethod(player));
  3. Do.sync(() -> {
  4. boolean result = doSomething();
  5. if(result) {
  6. doSomethingElse();
  7. }
  8. });

Synchronous task for later

  1. Do.syncLater(20, this::someMethod);
  2. Do.syncLater(20, () -> someMethod(player));
  3. Do.syncLater(20, () -> {
  4. boolean result = doSomething();
  5. if(result) {
  6. doSomethingElse();
  7. }
  8. });

Synchronous timer

Call a method synchronously every second (20 ticks):

  1. 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.

  1. BukkitTask task = Do.syncTimer(20, this::someMethod);
  2. task.cancel();
  1. Do.syncTimer(20, () -> {
  2. doSomething();
  3. if(someCondition) {
  4. return false; // Stop the task
  5. }
  6. return true; // Keep running the task
  7. });

Asynchronous versions

For the methods shown above async versions are also available, simply use async() and asyncLater().

Do a heavy operation over multiple ticks

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.

  1. Do.forall(
  2. // Do 10 items per tick (optional, defaults to 1)
  3. 10,
  4. // Collection of objects to do something for
  5. Bukkit.getOnlinePlayers(),
  6. // This method will be called once for each item from your collection, in this case with a player
  7. player -> {
  8. calculateScore(player);
  9. player.sendMessage("done");
  10. }
  11. // When everything is complete, log a message (optional)
  12. () -> plugin.getLogger().info("completed");
  13. );

Use with Maven

  1. Add Maven repository:

    1. <repositories>
    2. <repository>
    3. <id>nlthijs48</id>
    4. <url>http://maven.wiefferink.me</url>
    5. </repository>
    6. </repositories>
  2. Add Maven dependency:

    1. <dependencies>
    2. <dependency>
    3. <groupId>me.wiefferink</groupId>
    4. <artifactId>bukkitdo</artifactId>
    5. <version>1.0.0-SNAPSHOT</version>
    6. </dependency>
    7. </dependencies>
  3. Relocate the library (compatibility with other plugins):

    1. <build>
    2. <plugins>
    3. <plugin>
    4. <groupId>org.apache.maven.plugins</groupId>
    5. <artifactId>maven-shade-plugin</artifactId>
    6. <version>2.4.3</version>
    7. <executions>
    8. <execution>
    9. <phase>package</phase>
    10. <goals>
    11. <goal>shade</goal>
    12. </goals>
    13. <configuration>
    14. <relocations>
    15. <!-- Relocate BukkitDo -->
    16. <relocation>
    17. <pattern>me.wiefferink.bukkitdo</pattern>
    18. <shadedPattern>your.package.here.shaded.bukkitdo</shadedPattern>
    19. </relocation>
    20. </relocations>
    21. </configuration>
    22. </execution>
    23. </executions>
    24. </plugin>
    25. </plugins>
    26. </build>