Skip to content
ZERNYTH.

Player API

Server.getOnlinePlayers() lists players, and events provide them directly. Player extends CommandSender.

package dev.example;

import dev.zernyth.api.player.GameMode;
import dev.zernyth.api.player.Location;
import dev.zernyth.api.player.Player;
import dev.zernyth.api.plugin.JavaPlugin;

public final class PlayersPlugin extends JavaPlugin {
    void lift(Player player) {
        Location at = player.getLocation();
        player.teleport(new Location(at.dimension(), at.x(), at.y() + 10, at.z(), at.pitch(), at.yaw()));
        if (player.getHealth() < 5) player.setGameMode(GameMode.ADVENTURE);
        if (player.hasPermission("example.notify")) player.sendMessage("Lifted");
    }

    @Override
    public void onEnable() {
        getServer().getOnlinePlayers().forEach(this::lift);
    }
}

Lifetime

A Player belongs to one session. After a reconnect with the same UUID, old views are invalid, and even reads can throw IllegalStateException. Store the UUID, and look the player up again from getOnlinePlayers().

All native access requires the server thread and a live plugin.

Was this page helpful?