Entity Spawning & Management
Spawn, control, and manage entities in the world.
Understanding Entities
Entities are anything that moves or has state: players, mobs, armor stands, items. You can spawn them, control their properties (health, velocity, effects), track them, and listen to their events. This is how you create custom NPCs, bosses, or world events.
Key concept: Entities are objects you get a reference to and can manipulate. Every action updates them in real-time.
Spawning Entities
Use spawnEntity to create new mobs or objects in the world:
import { world } from "@minecraft/server";
// Spawn a mob
const dimension = world.getDimension("overworld");
const entity = dimension.spawnEntity("minecraft:zombie", {
x: 0,
y: 64,
z: 0
});
// Spawn with specific type
const villager = dimension.spawnEntity("minecraft:villager", {
x: 10,
y: 64,
z: 10
});
// Custom name
entity.nameTag = "§cZombie Boss";
// Set health
const health = entity.getComponent("health");
health.setCurrentValue(50);
Entity Properties
// Get entity position
const pos = entity.location;
console.log(`Entity at: ${pos.x}, ${pos.y}, ${pos.z}`);
// Get entity dimension
const dim = entity.dimension;
// Get entity type
console.log(`Entity type: ${entity.typeId}`);
// Check if alive
const isDead = entity.getComponent("health").currentValue <= 0;
// Get velocity
const velocity = entity.getVelocity();
console.log(`Velocity: ${velocity.x}, ${velocity.y}, ${velocity.z}`);
// Set velocity
entity.setVelocity({ x: 1, y: 0.5, z: 0 });
// Teleport entity
entity.teleport({
x: 100,
y: 64,
z: 100
}, { dimension: world.getDimension("nether") });
Entity Effects
import { EntityEffectType } from "@minecraft/server";
// Add effect
entity.addEffect(
EntityEffectType.Speed,
60, // Duration in ticks (3 seconds)
{ amplifier: 2 } // Amplifier (Speed II)
);
// Get effects
const effects = entity.getEffects();
effects.forEach(effect => {
console.log(`${effect.type}: ${effect.duration} ticks`);
});
// Remove effect
entity.removeEffect(EntityEffectType.Poison);
Equipment & Items
// Give item to entity (if it's a player)
if (entity.typeId === "minecraft:player") {
const player = entity;
const item = new ItemStack("diamond_sword", 1);
player.container.addItem(item);
}
// Set held item
const armor = entity.getComponent("equippable");
if (armor) {
const helmet = new ItemStack("diamond_helmet", 1);
armor.setEquipment(EquipmentSlot.Head, helmet);
}
Damage & Combat
// Deal damage to entity
entity.applyDamage(10, {
cause: "entityAttack",
damagingEntity: player
});
// Heal entity
const health = entity.getComponent("health");
health.setCurrentValue(health.currentValue + 5);
// Set maximum health
health.setCurrentValue(health.currentValue);
Entity Events
// When entity dies
world.afterEvents.entityDie.subscribe((event) => {
const entity = event.deadEntity;
// Drop items
if (entity.typeId === "minecraft:zombie") {
const dimension = entity.dimension;
dimension.spawnItem(
new ItemStack("rotten_flesh", 2),
entity.location
);
}
});
// When entity spawns
world.afterEvents.entitySpawn.subscribe((event) => {
const entity = event.entity;
// Custom behavior on spawn
if (entity.typeId === "minecraft:zombie") {
entity.nameTag = "§cSpawned Zombie";
}
});
// When entity hurts another
world.afterEvents.entityHurt.subscribe((event) => {
const damager = event.damageSource.damagingEntity;
const victim = event.hurtEntity;
console.log(`${damager?.typeId} hurt ${victim.typeId}`);
});
Particle Effects
// Spawn particles at location
const dimension = world.getDimension("overworld");
dimension.spawnParticle(
"minecraft:explosion_particle",
{ x: 0, y: 64, z: 0 }
);
// Multiple particles
for (let i = 0; i < 20; i++) {
dimension.spawnParticle(
"minecraft:flame",
{
x: Math.random() * 10,
y: 64 + Math.random() * 5,
z: Math.random() * 10
}
);
}
Entity Queries
// Get all entities
const allEntities = dimension.getEntities();
// Get entities of specific type
const zombies = dimension.getEntities({
type: "minecraft:zombie"
});
// Get entities in range
const playersNearby = dimension.getEntities({
type: "minecraft:player",
location: { x: 0, y: 64, z: 0 },
maxDistance: 50 // 50 block radius
});
// Filter entities
const livingZombies = zombies.filter(entity => {
const health = entity.getComponent("health");
return health.currentValue > 0;
});
console.log(`Found ${livingZombies.length} living zombies`);