Skip to main content

Entities

session.entityState is an EntityStateTracker: the live model of every entity the client knows about, plus the player list.

EntityStateTracker​

MemberTypeDescription
entitiesMap<bigint, AbstractEntity>All tracked entities, keyed by runtime id.
playerListMap<string, PlayerListEntry>The player (tab) list, keyed by UUID string.
localPlayerEntityLocalPlayerYou.
ctx.on("tick", () => {
for (const entity of ctx.session.entityState.entities.values()) {
console.log(entity.name, entity.position);
}
});

The entity hierarchy​

Every value in entities is an AbstractEntity. The subclasses are value exports, so you can narrow with instanceof:

AbstractEntity
├── EntityPlayer
│ ├── EntityNetworkPlayer // other players
│ └── EntityLocalPlayer // you — see the Local Player page
├── EntityCreature // mobs
└── EntityItem // dropped items
import { EntityCreature, EntityNetworkPlayer } from "@protohax/userscript";

for (const e of ctx.session.entityState.entities.values()) {
if (e instanceof EntityNetworkPlayer) {
console.log("player", e.username, e.health);
} else if (e instanceof EntityCreature && e.isHostile) {
console.log("hostile mob", e.identifier);
}
}

AbstractEntity​

The members every entity has.

MemberTypeDescription
runtimeIdbigintNetwork runtime id (the key in entities).
positionVector3World position.
prevPositionVector3Position on the previous tick.
motionVector3Current velocity.
rotationVector3Rotation as (pitch, yaw, headYaw).
isOnGroundbooleanWhether the entity is on the ground.
aabbAABBBounding box in world space.
get aabbShapeAABBBounding box before translation. Add position to get world space.
tickLivedbigintHow many ticks the entity has existed.
healthnumberCurrent health.
maxHealthnumberMaximum health.
absorptionnumberAbsorption (yellow) health.
scalenumberModel scale.
effectsMap<string, MobEffect>Active status effects, keyed by handle (e.g. minecraft:invisibility).
hurtTimenumberTicks left in the hurt animation.
get canBeAttackedbooleanWhether this is a valid attack target.
get namestringDisplay name.
entityEventsEventEmitter<EntityEvents>This entity's own event emitter (see below).
getMetadata<T>(key)T | undefinedRead a raw metadata value by key.

EntityPlayer (adds)​

The base class for both other players and the local player.

MemberTypeDescription
uuidUUIDPlayer UUID.
usernamestringPlayer name.
gamemodeGameModeCurrent game mode.
permissionLevelPermissionLevelPermission level.
get playerListEntryPlayerListEntry | undefinedThis player's tab-list entry, if there is one.
get eyePositionVector3Eye position. Accounts for sneaking, swimming and crawling.
get nameColorstring | nullChat/team color code parsed from the name, if any.
get armorColorColor | undefinedTeam armor color, if any.

EntityNetworkPlayer adds nothing to EntityPlayer. It is simply the type of other players in the world.

EntityCreature (adds)​

MemberTypeDescription
identifierstringNormalized entity identifier, e.g. minecraft:zombie.
isLivingbooleanWhether this is a living entity (a mob).
isHostilebooleanWhether this is a hostile mob.
isPickablebooleanWhether this entity can be picked or attacked.
isCustombooleanWhether this is a non-vanilla (custom) entity.

EntityItem (adds)​

MemberTypeDescription
itemItemStackThe dropped item stack.

Per-entity events — entityEvents​

Each entity has its own EventEmitter that fires when that entity changes:

ctx.on("entity_spawn", (entity) => {
entity.entityEvents.on("health", () => {
console.log(entity.name, "health ->", entity.health);
});
});

The EntityEvents map:

EventPayloadFires when
move()Position or rotation changed.
health()Health changed.
hurt()The entity took damage.
destroy()The entity was removed.
name()Display name changed.
attribute(key: string)An attribute updated.
effect("add" | "remove" | "update", key)A status effect changed.
inventory_update()The inventory changed. Does not fire for the local player.
metadata_<key>(value)A specific metadata field changed.

PlayerListEntry​

A tab-list entry. It exists even for players who aren't currently spawned as entities.

MemberType
uuidUUID
entity_unique_idbigint
usernamestring
build_platformnumber
skinPlayerSkin

UUID​

MemberTypeDescription
bytesUint8ArrayThe raw 16 bytes.
equals(other)booleanCompare two UUIDs.
toString()stringCanonical string form.
UUID.parse(str)static UUIDParse from a string.
UUID.ZEROstatic UUIDThe all-zero UUID.

PlayerSkin​

MemberTypeDescription
get hasVisibleGeometrybooleanWhether the skin has any visible geometry.
get hasValidGeometrybooleanWhether the skin has valid humanoid geometry.
extractAvatar()Uint8Array | undefinedThe face/avatar as raw RGBA pixels, if it can be derived.

GameMode and PermissionLevel​

These are string-union types, not enums. Compare against the string:

type GameMode =
| "survival" | "creative" | "adventure"
| "survival_spectator" | "creative_spectator"
| "fallback" | "spectator" | number;

type PermissionLevel =
| "visitor" | "member" | "operator" | "custom" | number;

if (player.gamemode === "creative") { /* ... */ }