Git Product home page Git Product logo

paperlib's Introduction

PaperLib

PaperLib is a plugin library for interfacing with Paper specific APIs (such as async chunk loading), with graceful fallbacks maintaining compatibility with both the Bukkit and Spigot API's.

API

All API calls can be found as static util methods in the PaperLib class.

getChunkAtAsync

public class PaperLib {
  public static CompletableFuture<Chunk> getChunkAtAsync(Location loc);
  public static CompletableFuture<Chunk> getChunkAtAsync(Location loc, boolean gen);
  public static CompletableFuture<Chunk> getChunkAtAsync(World world, int x, int z);
  public static CompletableFuture<Chunk> getChunkAtAsync(World world, int x, int z, boolean gen); 
}

On Paper, loads (or generates on 1.13.1+) the chunk asynchronously if it is not loaded yet, then completes the returned future. Chunk will load synchronous on Spigot.

teleportAsync

public class PaperLib {
  public static CompletableFuture<Boolean> teleportAsync(Entity entity, Location location);
  public static CompletableFuture<Boolean> teleportAsync(Entity entity, Location location, TeleportCause cause);
}

Uses the Async Chunk Load API, and if possible, loads/generates the chunk asynchronously before teleporting. Will load synchronous on Spigot.

On 1.15+, this has an added improvement of also loading neighbor chunks to avoid collision checks loading chunks too.

isChunkGenerated

public class PaperLib {
  public static boolean isChunkGenerated(Location loc);
  public static boolean isChunkGenerated(World world, int x, int z);
}

Returns whether or not the chunk is generated. Only Supported in Paper 1.12+ and Spigot 1.13.1+

getBlockState

public class PaperLib {
  public static BlockStateSnapshotResult getBlockState(Block block, boolean useSnapshot);
}

Allows you to optionally avoid taking a snapshot of a TileEntity in a BlockState. Versions prior to 1.12 will always be false for the snapshot. In versions 1.12+ on Spigot, the snapshot will always be true. In Paper 1.12+, the snapshot will be whether or not you requested one in the API call.

suggestPaper

public class PaperLib {
  public static void suggestPaper(Plugin plugin);
}

Helps inform users who run your plugin on Spigot that your plugin will behave better on Paper! Calling this method will print out an informational message in the logs that they should switch to Paper, and will help users discover our software. We would appreciate it if you call this method, but it is optional.

Example Plugin

public class MyPlugin extends JavaPlugin {
    public void onEnable() {
        PaperLib.suggestPaper(this);
    }
    
    public void doSomething(Entity entity, Location location) {
        PaperLib.teleportAsync(entity, location).thenAccept(result -> {
            if (result) {
                player.sendMessage("Teleported!");
            } else {
                player.sendMessage("Something went wrong!");
            }
        });
    }
}

Build Script Setup

Add the Paper repository and the PaperLib dependency, then shade and relocate it to your own package. Relocation helps avoid version conflicts with other plugins using PaperLib.

Gradle

Repo:

repositories {
    maven {
        name "papermc"
        url "https://papermc.io/repo/repository/maven-public/"
    }
}

Dependency:

dependencies {
    implementation "io.papermc:paperlib:1.0.6"
}

Shadow Jar and Relocate:

plugins {
  id "com.github.johnrengelman.shadow" version "7.0.0"
  // Make sure to always use the latest version (https://plugins.gradle.org/plugin/com.github.johnrengelman.shadow)
}
shadowJar {
   relocate "io.papermc.lib", "[YOUR PLUGIN PACKAGE].paperlib"
}

Maven

Repo:

<repositories>
    <repository>
        <id>papermc</id>
        <url>https://papermc.io/repo/repository/maven-public/</url>
    </repository>
</repositories>

Dependency:

<dependencies>
    <dependency>
        <groupId>io.papermc</groupId>
        <artifactId>paperlib</artifactId>
        <version>1.0.6</version>
        <scope>compile</scope>
     </dependency>
 </dependencies>

Shade & Relocate:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.4</version> <!-- Make sure to always use the latest version (https://maven.apache.org/plugins/maven-shade-plugin/) -->
            <configuration>
                <dependencyReducedPomLocation>${project.build.directory}/dependency-reduced-pom.xml</dependencyReducedPomLocation>
                <relocations>
                    <relocation>
                        <pattern>io.papermc.lib</pattern>
                        <shadedPattern>[YOUR PLUGIN PACKAGE].paperlib</shadedPattern> <!-- Replace this -->
                    </relocation>
                </relocations>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

License

PaperLib is licensed under the MIT license

paperlib's People

Contributors

aikar avatar hexedhero avatar jroy avatar notmyfault avatar billygalbreath avatar donotspampls avatar jwpjrdev avatar denwav avatar me4502 avatar hafarooki avatar electronicboy avatar egg82 avatar theminecoder avatar toffikk avatar viomck avatar

Watchers

James Cloos 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.