ActionFormData Class

Builds a simple player form with buttons that let the player take action.

Examples

showActionForm.ts
import { world, DimensionLocation } from "@minecraft/server";
import { ActionFormData, ActionFormResponse } from "@minecraft/server-ui";

function showActionForm(log: (message: string, status?: number) => void, targetLocation: DimensionLocation) {
  const playerList = world.getPlayers();

  if (playerList.length >= 1) {
    const form = new ActionFormData()
      .title("Test Title")
      .body("Body text here!")
      .button("btn 1")
      .button("btn 2")
      .button("btn 3")
      .button("btn 4")
      .button("btn 5");

    form.show(playerList[0]).then((result: ActionFormResponse) => {
      if (result.canceled) {
        log("Player exited out of the dialog. Note that if the chat window is up, dialogs are automatically canceled.");
        return -1;
      } else {
        log("Your result was: " + result.selection);
      }
    });
  }
}

(preview) Work with this sample on the MCTools.dev code sandbox.

showFavoriteMonth.ts
import { world, DimensionLocation } from "@minecraft/server";
import { ActionFormData, ActionFormResponse } from "@minecraft/server-ui";

function showFavoriteMonth(log: (message: string, status?: number) => void, targetLocation: DimensionLocation) {
  const players = world.getPlayers();

  if (players.length >= 1) {
    const form = new ActionFormData()
      .title("Months")
      .body("Choose your favorite month!")
      .button("January")
      .button("February")
      .button("March")
      .button("April")
      .button("May");

    form.show(players[0]).then((response: ActionFormResponse) => {
      if (response.selection === 3) {
        log("I like April too!");
        return -1;
      }
    });
  }
}

(preview) Work with this sample on the MCTools.dev code sandbox.

Methods

constructor

new ActionFormData()

Creates a new modal form builder.

Returns ActionFormData

body

body(bodyText: minecraftserver.RawMessage | string): ActionFormData

Method that sets the body text for the modal form.

Parameters

Returns ActionFormData

button

button(text: minecraftserver.RawMessage | string, iconPath?: string): ActionFormData

Adds a button to this form with an icon from a resource pack.

Parameters

Returns ActionFormData

header(text: minecraftserver.RawMessage | string): ActionFormData

Adds a header to the form.

Parameters

Returns ActionFormData

Caution

This function is still in pre-release. Its signature may change or it may be removed in future releases.

label

label(text: minecraftserver.RawMessage | string): ActionFormData

Adds a label to the form.

Parameters

Returns ActionFormData

Caution

This function is still in pre-release. Its signature may change or it may be removed in future releases.

show

show(player: minecraftserver.Player): Promise<ActionFormResponse>

Creates and shows this modal popup form. Returns asynchronously when the player confirms or cancels the dialog.

Parameters

Returns Promise<ActionFormResponse>

Notes:

  • This function can't be called in read-only mode.
  • This function can throw errors.

title

title(titleText: minecraftserver.RawMessage | string): ActionFormData

This builder method sets the title for the modal dialog.

Parameters

Returns ActionFormData

Examples

showActionForm.ts
import { world, DimensionLocation } from "@minecraft/server";
import { ActionFormData, ActionFormResponse } from "@minecraft/server-ui";

function showActionForm(log: (message: string, status?: number) => void, targetLocation: DimensionLocation) {
  const playerList = world.getPlayers();

  if (playerList.length >= 1) {
    const form = new ActionFormData()
      .title("Test Title")
      .body("Body text here!")
      .button("btn 1")
      .button("btn 2")
      .button("btn 3")
      .button("btn 4")
      .button("btn 5");

    form.show(playerList[0]).then((result: ActionFormResponse) => {
      if (result.canceled) {
        log("Player exited out of the dialog. Note that if the chat window is up, dialogs are automatically canceled.");
        return -1;
      } else {
        log("Your result was: " + result.selection);
      }
    });
  }
}

(preview) Work with this sample on the MCTools.dev code sandbox.

showFavoriteMonth.ts
import { world, DimensionLocation } from "@minecraft/server";
import { ActionFormData, ActionFormResponse } from "@minecraft/server-ui";

function showFavoriteMonth(log: (message: string, status?: number) => void, targetLocation: DimensionLocation) {
  const players = world.getPlayers();

  if (players.length >= 1) {
    const form = new ActionFormData()
      .title("Months")
      .body("Choose your favorite month!")
      .button("January")
      .button("February")
      .button("March")
      .button("April")
      .button("May");

    form.show(players[0]).then((response: ActionFormResponse) => {
      if (response.selection === 3) {
        log("I like April too!");
        return -1;
      }
    });
  }
}

(preview) Work with this sample on the MCTools.dev code sandbox.