.. _scripts: Scripts ======= A script is an ordered list of operations that execute in sequence. Scripts can execute commands, manipulate variables, control game state, and respond to player actions. Unlike functions, which must be called explicitly, scripts are triggered automatically when certain events occur. .. contents:: Contents :local: Script Types ------------ Scripts must be bound to a trigger: a block, entity, or area. The type of trigger determines when the script runs. - **interact** scripts trigger when a player right-clicks on the associated block. - **walk** scripts trigger when a player enters the space above the block. The block itself doesn't need to exist; the script triggers based on the saved coordinates. - **ground** scripts trigger only when a player is standing on the block. Unlike walk scripts, these require the player to actually be on the block, not jumping over it. - **entity** scripts trigger when a player right-clicks on the associated entity. - **area** scripts trigger when a player enters a WorldGuard region. The script runs once per entry. Script Variables ^^^^^^^^^^^^^^^^ Depending on their type, some scripts have special built-in variables with context about the trigger. - ``player`` (of type Player) is the player triggering the script, available in non-function scripts. - ``block`` (of type Block) is the block the script is on, available in interact/walk/ground scripts. - ``entity`` (of type Entity) is the entity the script is bound to, available only in entity scripts. - ``region`` (of type Region) is the region the script is bound to, available only in area scripts. .. code-block:: msc @player Hi, {{player.getName()}}! You clicked the block at {{block.getX()}}, {{block.getY()}}, {{block.getZ()}}. @bypass /teleport @s {{player.getLocation().getX()}} 100 {{player.getLocation().getZ()}} .. code-block:: output Hi, Rickyboy320! You clicked the block at 100, 64, -200. (This example assumes the script is bound to a block at those coordinates.) Script Operators ---------------- Every line in a script begins with an operator that determines what the line does. The exception to this is blank lines or comments (lines starting with ``#``), which are ignored. Operators fall into several categories. Command Operators ^^^^^^^^^^^^^^^^^ Three operators execute Minecraft commands: ``@command `` executes with the player's permissions. If the player doesn't have permission to run the command, it fails. ``@bypass `` elevates the player to semi-admin permissions, allowing most Minecraft and Minr commands. This is the most commonly used command operator. ``@console `` executes from the server console. This has full permissions but cannot use player-relative features like ``@s``. .. code-block:: msc @command /spawn @bypass /teleport @s 100 64 200 @console /say Hello from the server! All command operators introduce a one-tick (0.05s) delay before continuing. A script with 20 commands takes at least one second to complete. Branching Operators ^^^^^^^^^^^^^^^^^^^ Branching operators control which parts of a script execute based on conditions. ``@if `` starts a conditional block. If the condition is true, the following lines execute. If false, execution skips to the matching ``@elseif``, ``@else``, or ``@fi``. ``@elseif `` provides an alternative condition if the previous ``@if`` or ``@elseif`` was false. ``@else`` executes if all previous conditions were false. ``@fi`` ends the conditional block. .. code-block:: msc @if score >= 100 @player &aYou win! @elseif score >= 50 @player &eAlmost there... @else @player &cKeep trying! @fi Conditional blocks can be nested: .. code-block:: msc @if hasKey @if doorOpen @player The door is already open. @else @player You unlock the door. @var doorOpen = true @fi @else @player You need a key. @fi ``@return`` stops the script immediately. In functions, it can also return a value. .. code-block:: msc @if !authorized @player &cAccess denied. @return @fi @player Welcome to the secret room! Control Operators ^^^^^^^^^^^^^^^^^ Control operators affect script timing and execution. ``@delay