What is command context?

All commands exist in a context.

This context is first set when the command is run and consists of multiple parts that all describe the “situation” that a command is run in.

  • the execution position (x / y / z and dimension)
  • the executing entity
  • the execution rotation
  • (the anchor)

All of which can be modified individually with the 1.13+ Java as well as 1.19.1+ Bedrock execute command.

See the Minecraft wiki on the execute command for which subcommands modify which context.

Defaults

When a command is run, it will get some default values depending on how it is run.

  • A command run from chat is executed as and at (so positioned and rotated as) the player who runs it.
  • A command run from a commandblock is executed with the position set to the center of the commandblock in all three axis. It is always rotated 0 0, so facing straight forward in south (positive z) direction. There is no executing entity.
  • A command run from a function can have various values, as functions themselves keep context. See below.
    • A scheduled command does not keep context
    • Tick and load functions run at world spawn
  • A command run from the server console is executed at world spawn.
  • A command run from an NPC [Bedrock Edition Only] is run as and at the NPC

Context in Functions

Functions keep the context that they’re run in.

So a function run in chat will have the same defaults as any other command run from a player in chat. The same is true for a function command run from a commandblock, it will have the same context as any command run from a commandblock would.

A command run from either the #minecraft:load or #minecraft:tick function tags will run positioned at world spawn (at the lowest end of the block, unlike the commandblock), rotated 0 0 and without an executing entity.

This allows a lot of optimization since you can use a selector once, running the function as that entity and then refer to that entity as @s for the rest of the function. It also allows for entityless raycasting as the position and rotation is preserved between function calls.