Basic API usage

ViaVersion provides an API for developers, this page will show you some basic usage.

Javadocs: https://jd.viaversion.com

Maven

ViaVersion provides a Maven repository that you can use to implement the ViaVersion API inside your project.

Repository:
ViaVersion Maven repository
<repository>
    <id>viaversion-repo</id>
    <url>https://repo.viaversion.com</url>
</repository>


Dependency:
ViaVersion Maven dependency
<dependency>
    <groupId>com.viaversion</groupId>
    <artifactId>viaversion-api</artifactId>
    <version>LATEST</version>
    <scope>provided</scope>
</dependency>

You may need to replace LATEST with the version number of the latest release, eg. 2.2.2.

Get the players protocol version

You can get the protocol version from a player, List with all the Minecraft versions + protocol numbers (http://wiki.vg/Protocol_version_numbers), if a player is not controlled by ViaVersion it will return the servers version (e.g. reloads / player not fully logged in).

ViaAPI api = Via.getAPI(); // Get the API
int version = api.getPlayerVersion(player); // Get the protocol version

Side Note

When getting the players protocol, join events may not have the protocol linked with the player in time. It is suggested to delay this check by a few ticks.

Legacy BossBar API

Mojang introduced new bossbars in Minecraft 1.9

ViaVersion provides an API for developers to use the new BossBar for 1.9 players and higher.

Side note

Our API will not give 1.8 players a BossBar for the people that use 1.8-1.8.8.

Spigot provides an API for everyone that uses Spigot 1.9 and higher. We recommend you to use that one if you are using Spigot 1.9 or higher.


Example BossBar usage:

BossBarExample.java
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import us.myles.ViaVersion.api.ViaVersion;
import us.myles.ViaVersion.api.ViaVersionAPI;
import us.myles.ViaVersion.api.boss.BossBar;
import us.myles.ViaVersion.api.boss.BossColor;
import us.myles.ViaVersion.api.boss.BossStyle;

public class BossBarExample extends JavaPlugin implements Listener {
    private BossBar bossBar;

    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this); // Register Bukkit events

        ViaAPI api = Via.getAPI(); // Get the API
        bossBar = api.legacyAPI().createBossBar(
                "Hi! This BossBar is created using the ViaVersion API!", // BossBar title
                1F, // Boss health (Float between 0 and 1)
                BossColor.BLUE,  // BossBar color
                BossStyle.SOLID); // BossBar Style
    }

    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
		// When we added the BossBar, we supported addPlayer with a Player object, that is deprecated since 1.0.0. 
		// Get the player UUID instead.
        bossBar.addPlayer(e.getPlayer().getUniqueId()); // Show the BossBar when a player joins 
    }
}