Asking a portal where it leads

Ask a portal block where an entity entering it would arrive, without moving anything, and, on the server, have the exit portal built when none stands there yet. Two calls on LevelRef share one search with dimension travel.

From Sunrise's modding book716c272

On this page
I want toUseSide
Know where a portal leads, changing nothingLevelRef::portal_destinationServer
Reach the exit, building a nether portal when none existsLevelRef::find_or_create_portal_exitServer

Both take the portal block's position and the entity's box as [width, height]. They answer for an entity that is not a player, centred in the portal block, as an item or a mob travels. Vanilla sends a player to an End portal's destination through the respawn search, which these calls do not run.

rust
let size = [0.98, 0.98];
match level.find_or_create_portal_exit(host, portal_pos, size) {
    Ok(exit) => {
        // `exit.level` is the destination level, `exit.pos` the bottom centre of the box there and
        // `exit.rotation` what happens to the entity's yaw and pitch (see below).
        let _ = (exit.level, exit.pos, exit.rotation);
    }
    Err(PortalError::ChunksNotLoaded) => { /* retry on a later tick */ }
    Err(_error) => { /* the other refusals are final for this call */ }
}

What is answered

  • A nether portal: the other of the Overworld and the Nether, the approximate exit (the coordinates scaled by the two dimensions' scales and clamped to the destination's world border), then the closest portal within 16 blocks into the Nether or 128 blocks out of it. pos and rotation follow vanilla's placement in that portal's rectangle: the entity keeps its relative place, and the yaw turns 90 degrees when the two portals' axes differ. exit_portal contains the portal's bottom_left, width, height and x_axis.
  • An End portal: into the End, the End's fixed spawn point; out of it, the Overworld's shared spawn. The obsidian platform is not rebuilt.
  • An End gateway: the exit stored in the gateway, when it has one. A gateway that has not found its exit yet gives NoExit.

The read has none of travel's side effects: no platform, no portal ticket, no sound, and no hook fires.

The rotation

PortalExit::rotation is a PortalRotation with a yaw and a pitch, each a PortalAngle: Turn(degrees) adds to the entity's angle, Set(degrees) replaces it. Clamp the resulting pitch to -90 to 90, as travel does.

ThroughYawPitch
nether portalTurn(0), or Turn(90) when the two portals' axes differTurn(0)
End gatewayTurn(0)Turn(0)
End portal into the EndSet(90), westTurn(0)
End portal out of the EndTurn by the world spawn's yawTurn by the world spawn's pitch

Refusals

PortalErrorMeaning
NotAPortalThe block at portal_pos is not a nether portal, End portal or End gateway.
LevelMissingThe handle is stale, or the destination level does not exist.
EnteringNotAllowedThe allow_entering_nether_using_portals game rule forbids the trip into the Nether.
NoExitNo exit portal was found. For find_or_create_portal_exit: none was found and none could be built (no room, or the world border).
InvalidSizeThe width or height is negative, not finite, or more than an f32 holds. Large boxes are fine.
UnavailableThis side or build cannot answer.
ChunksNotLoadedThe destination level is not open, or a chunk the search or the build reads is not loaded.
InTransactionfind_or_create_portal_exit ran inside a transaction: a built portal cannot be rolled back.

Diverges from vanilla

Vanilla loads the chunks around the exit while it searches. These calls read loaded state only, in the level in place or a parked one, and read nothing from the save. When the destination level is not open or a chunk within the search radius (16 blocks, or 128 blocks, plus a chunk) is not loaded, they answer ChunksNotLoaded; keep the area loaded, for example with a chunk ticket, and retry on a later tick. The search reads every chunk in that radius, so a call out of the Nether looks at about 360 chunks, and the 21 blocks a portal's rectangle reaches around the entry and the exit are read too.

find_or_create_portal_exit never opens a level either, and building needs the chunks around the exit to be loaded too. The portal_create hook fires for a portal it builds, as it does for travel; it cannot veto the build.

Refusing dimension travel

Return Flow::Cancel from entity_travel_to_dimension to stop a server entity moving to the target level. Discarding the entity through its handle in the callback also stops the move. A falling block ticks outside the level's entity list, so its handle is not accessible during the callback.

Passengers are asked recursively. A refused passenger and its own passengers stay in the source level without stopping the original vehicle. The refused passenger dismounts. A callback can change riding relationships: the original passenger edge is restored only if the checked mount operation permits it. Otherwise the passenger stays behind, and a new vehicle chosen by the callback is not overwritten.

Edit this page on GitHub · Spotted something wrong? Open an issue or ask on Discord.