GithubHelp home page GithubHelp logo

bukkitdo's Introduction

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

Do.sync(this::someMethod);

Do.sync(() -> someMethod(player));

Do.sync(() -> {
    boolean result = doSomething();
    if(result) {
        doSomethingElse();
    }
});

Synchronous task for later

Do.syncLater(20, this::someMethod);

Do.syncLater(20, () -> someMethod(player));

Do.syncLater(20, () -> {
    boolean result = doSomething();
    if(result) {
        doSomethingElse();
    }
});

Synchronous timer

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

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.

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

Use with Maven

  1. Add Maven repository:

    <repositories>
      <repository>
        <id>nlthijs48</id>
        <url>http://maven.wiefferink.me</url>
      </repository>
    </repositories>
  2. Add Maven dependency:

    <dependencies>
      <dependency>
        <groupId>me.wiefferink</groupId>
        <artifactId>bukkitdo</artifactId>
        <version>1.0.0-SNAPSHOT</version>
      </dependency>
    </dependencies>
  3. 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>

bukkitdo's People

Contributors

nlthijs48 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.