Skip to content

Entity Object

The entity object represents an individual entity in the world (item, TNT, arrow, mob, player, floating text, etc.). It is obtained through world methods or from entity events.

js
// Get entities via world
var entities = world.getEntities();
var entity = entities[0];

// Get nearby entities
var nearby = world.getEntitiesInRadius(x, y, z, 10);

Base methods (all entities)

These methods are available on any type of entity.

MethodReturnsDescription
getUUID()stringEntity's unique UUID
getType()stringEntity type (e.g. "minecraft:item", "minecraft:tnt")
getX()numberX position
getY()numberY position
getZ()numberZ position
remove()Remove the entity from the world
teleport(x, y, z)Teleport the entity (if supported)
setVelocity(x, y, z)Change the entity's velocity (if supported)
js
var entities = world.getEntities();
for (var i = 0; i < entities.length; i++) {
    var e = entities[i];
    console.log(e.getType() + " UUID=" + e.getUUID());
    console.log("  pos: " + e.getX().toFixed(1) + "," + e.getY().toFixed(1) + "," + e.getZ().toFixed(1));
}

Living entity methods (mobs)

Available when typeof entity.getHealth === "function". Applies to mobs, animals and players.

MethodReturnsDescription
getHealth()numberCurrent health
getMaxHealth()numberMaximum health
setMaxHealth(n)Change max health
isDead()booleanWhether the entity is dead
hurt(damage)Apply attack damage
heal(health)Heal the entity
knockBack(x, y, z, force, height)Push the entity from a source position
getSpeed()numberMovement speed
addEffect(name, level, seconds)Apply a potion effect
removeEffect(name)Remove an active effect
clearEffects()Remove all active effects
js
var nearby = world.getEntitiesInRadius(x, y, z, 10);
for (var i = 0; i < nearby.length; i++) {
    var e = nearby[i];
    if (typeof e.getHealth === "function") {
        console.log(e.getType() + " health=" + e.getHealth().toFixed(1) + "/" + e.getMaxHealth().toFixed(1));
        e.addEffect("slowness", 1, 10);
        e.hurt(5);
    }
}

Player-only methods

Available when typeof entity.getName === "function". Players also have all living entity methods.

MethodReturnsDescription
getName()stringPlayer name
sendMessage(msg)Send a chat message
sendTitle(text, subtitle)Show a title on screen
disconnect(msg)Disconnect the player
js
var entities = world.getEntities();
for (var i = 0; i < entities.length; i++) {
    var e = entities[i];
    if (typeof e.getName === "function") {
        // It's a player
        e.sendMessage("§aHello " + e.getName() + "!");
        e.sendTitle("§aEvent", "§7Starting...");
    }
}

Common entity types

TypeDescriptionLiving
"minecraft:item"Item dropped on the groundNo
"minecraft:tnt"Primed TNTNo
"minecraft:arrow"ArrowNo
"minecraft:xp_orb"Experience orbNo
"minecraft:lightning_bolt"LightningNo
"minecraft:falling_block"Falling blockNo
"minecraft:fireworks_rocket"FireworkNo
"dragonfly:text"Floating text (created with world.spawnEntity("text", ...))No

Checking type before using optional methods

js
var entities = world.getEntities();
for (var i = 0; i < entities.length; i++) {
    var e = entities[i];

    // Always available
    console.log(e.getType() + " at " + e.getX().toFixed(0) + "," + e.getY().toFixed(0) + "," + e.getZ().toFixed(0));

    // Only on mobs/players
    if (typeof e.getHealth === "function") {
        console.log("  health: " + e.getHealth().toFixed(1));
    }

    // Only on players
    if (typeof e.getName === "function") {
        console.log("  player: " + e.getName());
    }
}

Removing entities

js
// Remove by UUID (from world)
world.removeEntityByUUID("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");

// Remove from the entity object directly
var entities = world.getEntities();
for (var i = 0; i < entities.length; i++) {
    if (entities[i].getType() === "minecraft:tnt") {
        entities[i].remove(); // Remove all TNT
    }
}

// Remove item entities within 5 blocks
var nearby = world.getEntitiesInRadius(x, y, z, 5);
for (var i = 0; i < nearby.length; i++) {
    if (nearby[i].getType() === "minecraft:item") {
        nearby[i].remove();
    }
}

Dragonfly Script API