Framework

Spatial interaction routing

Spatial.Interaction (Source/Spatial/Interaction.ts) turns native input into control events for spatial widgets. It follows a started pointer tracker and handles two input routes:

  • Ordinary Use. The router places an invisible interaction proxy entity where the player's crosshair meets an eligible control. A backend can also provide stationary native targets tied to committed layout.
  • Registered gesture items. Items you register drive held gestures through their real native item-use start and release events. No proxy is involved.

Importing the module starts nothing. Interaction.Start subscribes to native events, and proxies appear only once players aim at eligible controls.

The router hides each player's native crosshair while their current aim permits widget input. Ordinary Use requires a matching native target under the ray within reach. Registered gesture items use the pointer tracker's eligibility and range. The router restores the HUD default when the player loses that aim or access, and leaves a crosshair that was already hidden alone. HUD changes follow server ticks.

Starting a router

import { Spatial } from "bedform";
import { MolangVariableMap, system, world } from "@minecraft/server";

const tracker = Spatial.Pointer.Start({
    World: world,
    System: system,
    Sources: () => roots,
    CreateVariables: () => new MolangVariableMap()
});

const router = Spatial.Interaction.Start({
    World: world,
    System: system,
    Pointer: tracker,
    // Items whose native start/release lifecycle drives held gestures.
    Items: [ "minecraft:bow" ],
    OnError: error => console.warn(error)
});

// More gesture items can be registered later; registration is reference counted.
const unregister = router.Register("example:wand");

router.Dispose();
tracker.Dispose();

Controls receive events through their props:

<Button OnActivate={event => save(event.ViewerId)}>
    <Text>Save</Text>
</Button>

<Slider
    Value={volume}
    OnChange={(value, event) => setVolume(value)}
    OnUseStart={event => beginPreview(event.ViewerId)}
    OnUseRelease={event => endPreview(event.ViewerId)}
    OnCancel={event => revert(event.ViewerId, event.Reason)}
/>

Every callback receives a frozen Activation. It contains the ViewerId, the control's committed TargetId, the Kind, the Part ("track" for a ScrollView scrollbar track), the local Point aimed at, the gesture Item and, for cancellations, a Reason. Any item with OnActivate, OnUseStart, OnUseRelease or OnCancel is an interactive control. Router.Subscribe observes the same events, with the source and control type added.

Ordinary Use through proxies

After every tracker update, the router gives each player one proxy when all of the following hold:

  • the player is aiming at an eligible interactive control;
  • the hit is within native entity reach (Reach, default 3 blocks from the eye);
  • the player is not holding a registered gesture item;
  • the player has no active gesture.

The proxy (Interaction.ProxyType, BedformCanvas's InteractionProxy) is invisible, does not collide, has no gravity and takes no damage. Its 0.1-block collision box is centred on the hit point, moved 0.05 blocks toward the eye. It is teleported only when the hit point moves more than 0.01 blocks. It is removed when the conditions stop holding, when the player leaves or changes dimension, and when the router is disposed. A Proxies backend can replace native spawning. Proxies: false disables following proxy placement.

A native Use of a managed target works as follows:

  1. The before-event handler cancels native handling and captures the server's current view ray. The hit must match the viewer's following proxy or be inside the stationary target's declared widget coverage.
  2. After the handler returns, the router checks the captured point from the player's current eye against committed layout, audience, reach, occlusion, disabled state and required items. Aim changes cannot redirect the Use.
  3. Admission checks current policies and reserves any required seats before dispatch. An eligible stationary target can receive a first Use before an earlier tracker sample has hovered it.
  4. A valid Use calls OnActivate, then the control's change callback:
    • Toggle: OnChange with the inverted Checked;
    • Slider: OnChange with the value at the hit point (click-to-set);
    • ScrollView: OnScroll with a 16-pixel arrow step, a rail click that centers the thumb on the hit, or the current offset when clicking the thumb.
    • Input and TextArea: no change callback. The router opens the player's modal editor instead (see Text fields).
  5. The router reports the activated control as pressed for PressTicks updates (default 4). The pointer line shows an observed Use while aiming at that control.

Ordinary Use never reports a release, because the native event provides none.

ResolveTarget recognizes stationary entities and returns their source and covered widget IDs. Return null for a stale managed target and undefined for an ordinary entity. FollowProxy can suppress the following proxy for a widget covered by a stationary target.

Router.Activate(player) routes an ordinary Use immediately using the current ray and the same admission rules. An untracked following proxy is removed when used without dispatching a callback. At startup the router also sweeps the overworld, nether and end for such proxies; the Sweep option changes or disables this.

Text fields

Input and TextArea are the spatial counterparts of HTML <input> and <textarea>. Neither can be typed into in the world. Activating one opens a modal editor for that player: a native form with one text box, Confirm and Cancel. The field stays controlled, so Value changes only when your OnChange handler updates it.

Pass a TextEditor when you start the router. Spatial.TextEditor.Create builds the default form editor from the @minecraft/server-ui module you inject:

import * as ServerUI from "@minecraft/server-ui";

const router = Spatial.Interaction.Start({
    World: world,
    System: system,
    Pointer: tracker,
    TextEditor: Spatial.TextEditor.Create(ServerUI, { Confirm: "Save", Cancel: "Back" })
});
<Input
    Value={name}
    Placeholder="Name"
    EditorTitle="Rename sign"
    MaxLength={32}
    EditingDisplay="live"
    OnChange={(value, event) => setName(value)}
/>

<TextArea Value={notes} Rows={5} EditingDisplay="concealed" OnChange={setNotes} />

Callbacks fire in this order. Each receives the activation that opened the editor:

  1. OnActivate.
  2. OnFocus when the editor opens.
  3. OnInput(value) for each draft change while the player types.
  4. OnChange(value) when the player confirms.
  5. OnBlur(outcome) when the editor closes.
OutcomeCause
confirmThe player confirmed. OnChange has received the draft.
cancelThe player pressed Cancel or dismissed the form.
busyThe client was showing another screen, so the form never opened.
removedThe field was unmounted, hidden, disabled or pointer-disabled, or the player lost the root's audience. The router closes the form.
disconnectThe player left.
disposedThe router was disposed.
errorThe form failed to show. The router also reports the error to OnError.

Each player can have one editor open. Another Use while the form is open does nothing. Several players can edit the same field at once, and each confirmation calls OnChange. MaxLength truncates drafts and writes the truncated text back to the form. EditorTitle defaults to "Edit text". EditorLabel labels the text box and defaults to the Placeholder, then to "Text". Without a TextEditor, a text field only calls OnActivate, and the router reports one error.

EditingDisplay sets what other players see while a form is open:

ValueField while editing
"live" (default)The draft and a _ caret, updated as the player types. The border uses the theme's Field.ActiveBorder.
"unchanged"No change. The field keeps showing Value with its normal border.
"concealed"A pending style: Field.ActiveBorder, Field.PendingSurface and a ... marker in place of the text. This differs from the disabled style, and it never shows the draft.

The native text box is a single line. A TextArea wraps its text and keeps line breaks already in Value, but players cannot type new ones.

Registered gesture items

When a player starts using a registered item while aiming at an eligible control, a gesture begins:

  • Start. OnUseStart is called. Reach does not apply.
  • Drag. A Slider sets its value at the start, then follows the ray across the control's plane on every update. A ScrollView rail or thumb reports through OnScroll, preserving the point on the thumb where it was grabbed. An arrow performs one step at the start. A "hover" scrollbar stays shown to the gesture's player until the gesture ends. During a drag, the change callback receives the gesture's useStart event with the current Point. Unchanged values are not repeated.
  • Release. The native release calls OnUseRelease. Press-style controls (buttons, toggles, tab buttons, text fields and plain OnActivate items) are then activated if the player is still aiming at them and is still eligible.
  • Stop or completion. These are ambiguous. If no release follows within one further update, the gesture is cancelled with reason "stop".

Gesture state is per player, so any number of players can hold gestures on the same control. Within each update, gestures are processed in the order they started, and each change is delivered in that order. A shared controlled value therefore follows the serialized order of events, and the last change wins.

Cancellation

Every update revalidates each gesture. The first failing condition cancels it with OnCancel and the matching Reason. A cancelled gesture is never released.

ReasonCause
itemThe held item changed, the gesture item was unregistered, or another item was released.
requirementA RequiredItem on the control or an ancestor no longer matches the held item.
accessAn access policy no longer admits the player (see below).
disabledThe control, or its pointer, was disabled.
distanceThe control now lies beyond its pointer's MaxDistance.
removedThe root unmounted or lost its layout, or the control was removed, hidden, clipped out of its ScrollView viewport, or lost its scrollbar track.
audienceThe player no longer holds a presentation lease on the root.
disconnectThe player left or became invalid.
dimensionThe player changed dimension, or the View is in another dimension.
deathThe player died.
stopA native stop or completion was not followed by a release.
replacedThe player started another item use before the gesture ended.
disposedThe router was disposed.

Access is checked in two ways:

  • While the player aims at the control, the tracker's admission decides. An admission denial, such as a lock to someone else or a lost place under a player limit, cancels the gesture.
  • While the player aims elsewhere (for example, overshooting a slider), the router checks the policies directly: a lock to another player, a zero player limit or a rejecting Allows predicate cancels the gesture.

Lock changes are staged, so they take effect on the next input update. That update cancels every newly unauthorized gesture.

Native limitations

  • The 3-block reach default follows the native entity-interaction reach. It awaits native qualification, and it can depend on the player through a Reach function.
  • Proxies are world-visible entities. Revalidation keeps one player's proxy from activating anything for another player.
  • Whether the native Use of a proxy reaches playerInteractWithEntity, and the ordering of item stop and release events, are qualified by the native fixture (ty2k.17). The stop grace period tolerates either order.

On this page