Represents a player within the world.

Hierarchy

Constructors

Properties

dimension: Dimension

Remarks

Dimension that the entity is currently within.

Throws

This property can throw when used.

id: string

Remarks

Unique identifier of the entity. This identifier is intended to be consistent across loads of a world instance. No meaning should be inferred from the value and structure of this unique identifier - do not parse or interpret it. This property is accessible even if Entity.isValid is false.

Throws

This property can throw when used.

location: Vector3

Remarks

Current location of the entity.

Throws

This property can throw when used.

name: string

Remarks

Name of the player.

Throws

This property can throw when used.

nameTag: string

Remarks

Given name of the entity.

This property can't be edited in read-only mode.

typeId: string

Remarks

Unique identifier of the type of the entity - for example, 'minecraft:skeleton'. This property is accessible even if Entity.isValid is false.

Throws

This property can throw when used.

Methods

  • Parameters

    • effectType: string | EffectType

      Type of effect to add to the entity.

    • duration: number

      Amount of time, in ticks, for the effect to apply. There are 20 ticks per second. Use TicksPerSecond constant to convert between ticks and seconds. The value must be within the range [0, 20000000].

    • Optional options: EntityEffectOptions

      Additional options for the effect.

    Returns void

    Returns nothing if the effect was added or updated successfully. This can throw an error if the duration or amplifier are outside of the valid ranges, or if the effect does not exist.

    Remarks

    Adds or updates an effect, like poison, to the entity.

    This function can't be called in read-only mode.

    Throws

    This function can throw errors.

    Example

    addEffect.js

    const villagerId = 'minecraft:villager_v2<minecraft:ageable_grow_up>';
    const villagerLoc: mc.Vector3 = { x: 1, y: 2, z: 1 };
    const villager = test.spawn(villagerId, villagerLoc);
    const duration = 20;

    villager.addEffect(EffectTypes.get('poison'), duration, { amplifier: 1 });

    Example

    quickFoxLazyDog.ts

      const overworld = mc.world.getDimension("overworld");

    const fox = overworld.spawnEntity("minecraft:fox", {
    x: targetLocation.x + 1,
    y: targetLocation.y + 2,
    z: targetLocation.z + 3,
    });

    fox.addEffect("speed", 10, {
    amplifier: 2,
    });
    log("Created a fox.");

    const wolf = overworld.spawnEntity("minecraft:wolf", {
    x: targetLocation.x + 4,
    y: targetLocation.y + 2,
    z: targetLocation.z + 3,
    });
    wolf.addEffect("slowness", 10, {
    amplifier: 2,
    });
    wolf.isSneaking = true;
    log("Created a sneaking wolf.", 1);
  • Parameters

    • tag: string

      Content of the tag to add. The tag must be less than 256 characters.

    Returns boolean

    Returns true if the tag was added successfully. This can fail if the tag already exists on the entity.

    Remarks

    Adds a specified tag to an entity.

    This function can't be called in read-only mode.

    Throws

    This function can throw errors.

    Example

    tagsQuery.ts

      let mobs = ["creeper", "skeleton", "sheep"];

    // create some sample mob data
    for (let i = 0; i < 10; i++) {
    let mobTypeId = mobs[i % mobs.length];
    let entity = overworld.spawnEntity(mobTypeId, targetLocation);
    entity.addTag("mobparty." + mobTypeId);
    }

    let eqo: mc.EntityQueryOptions = {
    tags: ["mobparty.skeleton"],
    };

    for (let entity of overworld.getEntities(eqo)) {
    entity.kill();
    }
  • Parameters

    Returns boolean

    Whether the entity takes any damage. This can return false if the entity is invulnerable or if the damage applied is less than or equal to 0.

    Remarks

    Applies a set of damage to an entity.

    This function can't be called in read-only mode.

    Throws

    This function can throw errors.

    Example

    applyDamageThenHeal.ts

      const skelly = overworld.spawnEntity("minecraft:skeleton", targetLocation);

    skelly.applyDamage(19); // skeletons have max damage of 20 so this is a near-death skeleton

    mc.system.runTimeout(() => {
    let health = skelly.getComponent("health") as mc.EntityHealthComponent;
    log("Skeleton health before heal: " + health.currentValue);
    health.resetToMaxValue();
    log("Skeleton health after heal: " + health.currentValue);
    }, 20);
  • Parameters

    Returns void

    Remarks

    Applies impulse vector to the current velocity of the entity.

    This function can't be called in read-only mode.

    Throws

    This function can throw errors.

    Example

    applyImpulse.ts

      const zombie = overworld.spawnEntity("minecraft:zombie", targetLocation);

    zombie.clearVelocity();

    // throw the zombie up in the air
    zombie.applyImpulse({ x: 0, y: 0.5, z: 0 });
  • Parameters

    • directionX: number

      X direction in horizontal plane.

    • directionZ: number

      Z direction in horizontal plane.

    • horizontalStrength: number

      Knockback strength for the horizontal vector.

    • verticalStrength: number

      Knockback strength for the vertical vector.

    Returns void

    Remarks

    Applies impulse vector to the current velocity of the entity.

    This function can't be called in read-only mode.

    Throws

    This function can throw errors.

    Example

    bounceSkeletons.ts

      let mobs = ["creeper", "skeleton", "sheep"];

    // create some sample mob data
    for (let i = 0; i < 10; i++) {
    overworld.spawnEntity(mobs[i % mobs.length], targetLocation);
    }

    let eqo: mc.EntityQueryOptions = {
    type: "skeleton",
    };

    for (let entity of overworld.getEntities(eqo)) {
    entity.applyKnockback(0, 0, 0, 1);
    }
  • Returns void

    Remarks

    Sets the current velocity of the Entity to zero. Note that this method may not have an impact on Players.

    This function can't be called in read-only mode.

    Throws

    This function can throw errors.

    Example

    applyImpulse.ts

      const zombie = overworld.spawnEntity("minecraft:zombie", targetLocation);

    zombie.clearVelocity();

    // throw the zombie up in the air
    zombie.applyImpulse({ x: 0, y: 0.5, z: 0 });
  • Parameters

    • componentId: string

      The identifier of the component (e.g., 'minecraft:rideable') to retrieve. If no namespace prefix is specified, 'minecraft:' is assumed. If the component is not present on the entity, undefined is returned.

    Returns undefined | EntityComponent

    Returns the component if it exists on the entity, otherwise undefined.

    Remarks

    Gets a component (that represents additional capabilities) for an entity.

  • Returns EntityComponent[]

    Returns all components that are both present on this entity and supported by the API.

    Remarks

    Returns all components that are both present on this entity and supported by the API.

  • Parameters

    • effectType: string | EffectType

      The effect identifier.

    Returns undefined | Effect

    Effect object for the specified effect, undefined if the effect is not present, or throws an error if the effect does not exist.

    Remarks

    Returns the effect for the specified EffectType on the entity, undefined if the effect is not present, or throws an error if the effect does not exist.

    Throws

    This function can throw errors.

  • Returns Effect[]

    List of effects.

    Remarks

    Returns a set of effects applied to this entity.

    Throws

    This function can throw errors.

  • Returns Vector3

    Returns the current location of the head component of this entity.

    Remarks

    Returns the current location of the head component of this entity.

    Throws

    This function can throw errors.

  • Returns string[]

    Returns the current rotation component of this entity.

    Remarks

    Returns all tags associated with an entity.

    Throws

    This function can throw errors.

  • Returns Vector3

    Returns the current velocity vector of the entity.

    Remarks

    Returns the current velocity vector of the entity.

    Throws

    This function can throw errors.

    Example

    getFireworkVelocity.ts

      const fireworkRocket = overworld.spawnEntity("minecraft:fireworks_rocket", targetLocation);

    mc.system.runTimeout(() => {
    let velocity = fireworkRocket.getVelocity();

    log("Velocity of firework is: (x: " + velocity.x + ", y:" + velocity.y + ", z:" + velocity.z + ")");
    }, 5);
  • Returns Vector3

    Returns the current view direction of the entity.

    Remarks

    Returns the current view direction of the entity.

    Throws

    This function can throw errors.

  • Parameters

    • componentId: string

      The identifier of the component (e.g., 'minecraft:rideable') to retrieve. If no namespace prefix is specified, 'minecraft:' is assumed.

    Returns boolean

    Returns true if the specified component is present on this entity.

    Remarks

    Returns true if the specified component is present on this entity.

  • Parameters

    • tag: string

      Identifier of the tag to test for.

    Returns boolean

    Returns whether an entity has a particular tag.

    Remarks

    Returns whether an entity has a particular tag.

    Throws

    This function can throw errors.

  • Returns boolean

    Returns true if entity can be killed (even if it is already dead), otherwise it returns false.

    Remarks

    Kills this entity. The entity will drop loot as normal.

    This function can't be called in read-only mode.

    Throws

    This function can throw errors.

    Example

    tagsQuery.ts

      let mobs = ["creeper", "skeleton", "sheep"];

    // create some sample mob data
    for (let i = 0; i < 10; i++) {
    let mobTypeId = mobs[i % mobs.length];
    let entity = overworld.spawnEntity(mobTypeId, targetLocation);
    entity.addTag("mobparty." + mobTypeId);
    }

    let eqo: mc.EntityQueryOptions = {
    tags: ["mobparty.skeleton"],
    };

    for (let entity of overworld.getEntities(eqo)) {
    entity.kill();
    }
  • Parameters

    • soundID: string

      Identifier of the sound to play.

    • Optional soundOptions: PlayerSoundOptions

      Additional optional options for the sound.

    Returns void

    Remarks

    Plays a sound that only this particular player can hear.

    This function can't be called in read-only mode.

    Throws

    This function can throw errors.

    Example

    playMusicAndSound.ts

      let players = mc.world.getPlayers();

    const musicOptions: mc.MusicOptions = {
    fade: 0.5,
    loop: true,
    volume: 1.0,
    };
    mc.world.playMusic("music.menu", musicOptions);

    const worldSoundOptions: mc.WorldSoundOptions = {
    pitch: 0.5,
    volume: 4.0,
    };
    mc.world.playSound("ambient.weather.thunder", targetLocation, worldSoundOptions);

    const playerSoundOptions: mc.PlayerSoundOptions = {
    pitch: 1.0,
    volume: 1.0,
    };

    players[0].playSound("bucket.fill_water", playerSoundOptions);
  • Parameters

    • effectType: string | EffectType

      The effect identifier.

    Returns boolean

    Returns true if the effect has been removed. Returns false if the effect is not found or does not exist.

    Remarks

    Removes the specified EffectType on the entity, or returns false if the effect is not present.

    This function can't be called in read-only mode.

    Throws

    This function can throw errors.

  • Parameters

    • tag: string

      Content of the tag to remove.

    Returns boolean

    Returns whether the tag existed on the entity.

    Remarks

    Removes a specified tag from an entity.

    This function can't be called in read-only mode.

    Throws

    This function can throw errors.

  • Parameters

    • commandString: string

      The command string. Note: This should not include a leading forward slash.

    Returns CommandResult

    A command result containing whether the command was successful.

    Remarks

    Runs a synchronous command on the entity.

    This function can't be called in read-only mode.

    Throws

    This function can throw errors.

  • Parameters

    • commandString: string

      Command to run. Note that command strings should not start with slash.

    Returns Promise<CommandResult>

    For commands that return data, returns a JSON structure with command response values.

    Remarks

    Runs a particular command asynchronously from the context of this entity. Note that there is a maximum queue of 128 asynchronous commands that can be run in a given tick.

    Throws

    This function can throw errors.

  • Parameters

    Returns void

    Remarks

    Sends a message to the player.

    Throws

    This method can throw if the provided RawMessage is in an invalid format. For example, if an empty name string is provided to score.

    Example

    nestedTranslation.ts

    // Displays "Apple or Coal"
    let rawMessage = {
    translate: "accessibility.list.or.two",
    with: { rawtext: [{ translate: "item.apple.name" }, { translate: "item.coal.name" }] },
    };
    player.sendMessage(rawMessage);

    Example

    scoreWildcard.ts

    // Displays the player's score for objective "obj". Each player will see their own score.
    const rawMessage = { score: { name: "*", objective: "obj" } };
    world.sendMessage(rawMessage);

    Example

    sendBasicMessage.ts

      let players = mc.world.getPlayers();

    players[0].sendMessage("Hello World!");

    Example

    sendTranslatedMessage.ts

      let players = mc.world.getPlayers();

    players[0].sendMessage({ translate: "authentication.welcome", with: ["Amazing Player 1"] });

    Example

    simpleString.ts

    // Displays "Hello, world!"
    world.sendMessage("Hello, world!");

    Example

    translation.ts

    // Displays "First or Second"
    const rawMessage = { translate: "accessibility.list.or.two", with: ["First", "Second"] };
    player.sendMessage(rawMessage);
  • Parameters

    • location: Vector3

      New location for the entity.

    • Optional teleportOptions: TeleportOptions

      Options regarding the teleport operation.

    Returns void

    Remarks

    Teleports the selected entity to a new location

    This function can't be called in read-only mode.

    Throws

    This function can throw errors.

    Example

    teleportMovement.ts

      const pig = overworld.spawnEntity("minecraft:pig", targetLocation);

    let inc = 1;
    let runId = mc.system.runInterval(() => {
    pig.teleport(
    { x: targetLocation.x + inc / 4, y: targetLocation.y + inc / 4, z: targetLocation.z + inc / 4 },
    {
    facingLocation: targetLocation,
    }
    );

    if (inc > 100) {
    mc.system.clearRun(runId);
    }
    inc++;
    }, 4);
  • Parameters

    • location: Vector3

      Location to teleport the entity to.

    • Optional teleportOptions: TeleportOptions

      Options regarding the teleport operation.

    Returns boolean

    Returns whether the teleport succeeded. This can fail if the destination chunk is unloaded or if the teleport would result in intersecting with blocks.

    Remarks

    Attempts to try a teleport, but may not complete the teleport operation (for example, if there are blocks at the destination.)

    This function can't be called in read-only mode.

    Throws

    This function can throw errors.