Skip to content

Target Selector Internal Logic

TheSalt_ edited this page Jan 14, 2026 · 1 revision

Target Selector Internal Logic

Overview

This document describes the filtering process of target selectors. It does not distinguish between pre/post parsing and integrates various elements, so performance may differ from actual measured performance.

Prerequisites

  1. Basic programming syntax (if, else, forEach, function)
  2. AABB (essential for understanding dx, dy, dz, distance related logic)

Processing Order

[1]

When a selector is invoked, the following FIND_ENTITIES function is executed.

if (@a):
    if (no limit): limit = 2147483647
    if (no sort):  sort = arbitrary
if (@e):
    if (no limit): limit = 2147483647
    if (no sort):  sort = arbitrary
if (@n):
    if (no limit): limit = 1
    if (no sort):  sort = nearest
if (@p):
    if (no limit): limit = 1
    if (no sort):  sort = nearest
if (@r):
    if (no limit): limit = 1
    if (no sort):  sort = random
if (@s):
    if (no limit): limit = 1


if (any of dx, dy, dz exists):
    if (no dx): dx = 0
    if (no dy): dy = 0
    if (no dz): dz = 0
    Create AABB of size [[min(dx, 0), min(dy, 0), min(dz, 0)], [max(dx, 0) + 1, max(dy, 0) + 1, max(dz, 0) + 1]]
else if (distance max is specified):
    r = max distance
    Create AABB of size [[-r, -r, -r], [r + 1, r + 1, r + 1]]


if (any of @a, @r, @p, type = player, advancements = {...}*, level, gamemode exists):
    if (@s):
        if (executor type == player):
            CONDITION_CHECK(executor)
    else if (any of x, y, z, dx, dy, dz, distance exists):
        -- // ServerLevel.getPlayers
        forEach (current player in all players list of context dimension):
            CONDITION_CHECK(current player)
    else:
        -- // PlayerList.getPlayers
        forEach (current player in all players list of server):
            CONDITION_CHECK(current player)
else:
    if (@s):
        CONDITION_CHECK(executor)
    else:
        Enable enabledFeatures check
        if (any of x, y, z, dx, dy, dz, distance exists):
            dimension list = [current context dimension]
        else:
            dimension list = all dimensions list of server
        forEach (current dimension in dimension list):
            if (AABB range exists):
                -- // EntitySectionStorage.getEntities
                forEach (current section in sections near AABB range in current dimension***):
                    forEach (current entity in all entities list of current section):
                        if (current entity type == type**):
                            if (current entity hitbox intersects with AABB):
                                CONDITION_CHECK(current entity)
                                if (current entity type is ender dragon):
                                    forEach (part in dragon parts of current entity):
                                        if (part type == type**):
                                            CONDITION_CHECK(part)
            else:
                -- // EntityLookup.getEntities
                forEach (current entity in all entities list of context dimension):
                    if (current entity type == type**):
                        CONDITION_CHECK(current entity)


* Empty braces like [advancements = {}] are ignored.
** Only type = <entity> format is recognized here. type = !, type = !#, type = # are not recognized.
*** The process of finding sections near AABB range in current dimension is as follows. A section is defined as a 16×16×16 area, and due to this section lookup method, if an entity's hitbox is too large, dx, dy, dz may sometimes fail to select that entity.
    (xMin, yMin, zMin, xMax, yMax, zMax) = Decompose AABB into min and max vertices
    (xMin, yMin, zMin, xMax, yMax, zMax) += (-2, -4, -2, 2, 0, 2)
    -- // floor division
    (xMin, yMin, zMin, xMax, yMax, zMax) /= 16
    for (int x = xMin; x <= xMax; x++):
        -- // LongAVLTreeSet.subSet
        forEach (current section in section set of current dimension where [x, 0, 0] <= section < [x + 1, 0, 0]):
            y = y coordinate of current section
            z = z coordinate of current section
            if (yMin <= y <= yMax and zMin <= z <= zMax):
                if (section.getStatus().isAccessible()):
                    yield current section

[2]

The CONDITION_CHECK function checks whether the given entity satisfies conditions in the following order, and if all conditions are met, the entity is added to the "result list".

1. (if selector is @n or @e) Check if this entity is alive
2. Check in the order placed in selector: name, team, tag, predicate, nbt, gamemode, scores, type*, advancements
3. x_rotation check
4. y_rotation check
5. level check
6. (if "Enable enabledFeatures check" was executed in [1]) Check if current entity type is allowed
7. AABB check**
8. distance check
9. (if sort = arbitrary) limit check***

* Since type may have already been checked before CONDITION_CHECK function execution,
 this type check may be a duplicate check.
** Since AABB range may have already been checked before CONDITION_CHECK function execution,
 this AABB check may be a duplicate check.
*** Added in JE 1.19.3, when sort = arbitrary, optimizes by terminating the target selector
 process early when result list length reaches the limit.

[3]

After the FIND_ENTITIES function completes, the completed "result list" goes through the following process before being finally returned.

1. sort
2. limit

Here are some special situations where selectors behave somewhat uniquely. This may help in understanding the code above.

  1. @e[level = 0..] selects all players.
  2. @e[type = player] cannot select dead players, but @a can select dead players.
  3. @s can select dead entities.
  4. @e[x = 0] only selects entities in the executing dimension.
  5. @s ignores dimension restrictions caused by x, y, z, dx, dy, dz, distance.
  6. @p can select players in other dimensions.
  7. Even if all players are in the same dimension as the executor, the order of @a and @a[x = 0] may differ.
  8. Even if all entities are in the same dimension as the executor and within 5 blocks from the execution point, the order of @e and @e[distance = ..5] may differ.
  9. A selector that excludes all entities with @e[type = !pig, type = !sheep, type = !...] leaving only players may have a different player order than @e[type = player].

References

by Triton365 (서브블록)

https://discord.com/channels/811588451556261950/1130087804500717670

https://discord.gg/FtJCECDcqn

Clone this wiki locally