Skip to content
ZERNYTH.

Entity API

Entities come from World.getEntities(), Dimension.spawnEntity and Player.asEntity().

package dev.example;

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

public final class EntitiesPlugin extends JavaPlugin {
    void spawnNear(Player player) {
        var origin = player.asEntity().getLocation();
        for (var world : getServer().getWorlds()) {
            world.getDimension(origin.dimension())
                .flatMap(dimension -> dimension.spawnEntity(origin, "minecraft:pig"))
                .ifPresent(pig -> pig.setName("Zernyth"));
        }
    }
}

Lifetime

An Entity is a checked view, not a pointer. isValid() returns false once the entity is gone, and other calls then fail with IllegalStateException. Do not keep views longer than needed.

getHealth returns an empty value for entities without health. asPlayer returns the player when the entity is one.

Was this page helpful?