Skip to content

LivingEntity

Inherits from: Entity

Represents a living entity in the game, inheriting from Entity and including attributes specific to living creatures, such as health, age state, and hostility.

Fields

baby: boolean — Read-Only

Indicates whether the entity is a baby.

health: number — Read/Write

The current health of the entity.

hostile: boolean — Read-Only

Indicates whether the entity behaves in a hostile manner toward players or other entities.

Methods

canSee(entity: Entity, range?: number) → seen: boolean

Tells whether nothing blocks the line of sight from this entity's eyes to the other entity's eyes, the way the game decides whether a mob sees its target. Only blocks are in the way, not other entities, and which way this entity looks doesn't count: it also "sees" what is behind it. Always false for an entity in another world, or more than 128 blocks away.

With a range, the entity sees nothing that is farther away, measured from eyes to eyes.

Example

local zombie = spell:summon("zombie") --[[@as LivingEntity]]
if zombie:canSee(spell.owner) then
  print("The zombie sees you.")
end

Example: A guard that is short-sighted

local guard = spell:summon("iron_golem") --[[@as LivingEntity]]
if guard:canSee(spell.owner, 8) then
  print("The guard sees you.")
end

Parameters

  • entity: Entity — The entity to look for.
  • range?: number — The distance in blocks up to which this entity sees. Defaults to 128, which is also the most.

Returns

  • seen: boolean — true if the entity is in range and no block is in the way.

getEquipment(equipmentSlot: EquipmentSlot) → item: Item?

Returns a copy of the item currently held in the specified equipment slot.

Example

local piglin = spell:summon("piglin_brute") --[[@as LivingEntity]]
print(str(piglin:getEquipment("mainhand")))

Parameters

Returns

  • item: Item? — A copy of the item in the specified equipment slot, or nil if the slot is empty.

getItemInHand(hand: Hand) → item: Item?

Returns a copy of the item currently held in the specified hand.

Example

local item = spell.owner:getItemInHand("mainhand")
if item then
  print("Item in hand:", item.name)
end

Parameters

  • hand: Hand — The hand to check.

Returns

  • item: Item? — A copy of the item held in the specified hand, or nil if the hand is empty.

isHidden(aspect: HiddenAspect) → hidden: boolean

Tells whether the specified aspect of the entity is hidden from the players, see setHidden.

Example

if spell.owner:isHidden("equipment") then
  print("Nobody sees what you hold and wear.")
end

Parameters

Returns

  • hidden: boolean — true if the aspect is hidden.

setEquipment(equipmentSlot: EquipmentSlot, item: Item?)

Places a copy of the given item into the specified equipment slot. The provided item is not consumed or modified.

Example

local zombie = spell:summon("zombie") --[[@as LivingEntity]]
zombie:setEquipment("mainhand", Item:new("diamond_sword"))
zombie:setEquipment("head", Item:new("diamond_helmet"))

Parameters

  • equipmentSlot: EquipmentSlot — The equipment slot to equip.
  • item: Item? — The item to place in the specified slot, or nil to clear it.

setHidden(aspect: HiddenAspect|HiddenAspect[ ], hidden: boolean)

Hides the specified aspect of the entity from the players, or shows it again. A list of aspects switches all of them at once. Nothing is hidden at first. The entity itself is not changed by hiding: it keeps its items and its effects, and it still walks and sprints as before. Each aspect is switched on its own, so they can be combined freely, also with invisible, which hides the entity's body only.

The settings are not saved: they are gone when the entity is unloaded and when a player logs out. A player keeps them through death and respawn. Any other entity also loses them when it changes the dimension, because the game makes a new entity of it there.

Example

-- Nothing gives away the invisible owner of this spell
local player = spell.owner
player.invisible = true
player:setHidden({ "equipment", "arrows", "effectParticles", "stepParticles", "stepSounds" }, true)

Parameters

  • aspect: HiddenAspect|HiddenAspect[ ] — The aspect to hide or show, or a list of aspects.
  • hidden: boolean — true to hide the aspect, false to show it again.

setItemInHand(hand: Hand, item: Item?)

Places a copy of the given item into the specified hand. Pass nil to clear the hand. The provided item is not consumed or modified.

Example

local sword = Item:new("diamond_sword")
spell.owner:setItemInHand("mainhand", sword)
-- Clear the hand later:
spell.owner:setItemInHand("mainhand", nil)

Parameters

  • hand: Hand — The hand to equip.
  • item: Item? — The item to place in the specified hand, or nil to clear it.