Skip to content

@minecraft/server-ui


@minecraft/server-ui / MessageFormData

Class: MessageFormData

Defined in: @minecraft/server-ui/index.d.ts:855

Builds a simple two-button modal dialog.

Examples

showBasicMessageForm.ts

typescript
import { world, DimensionLocation } from '@minecraft/server';
import { MessageFormResponse, MessageFormData } from '@minecraft/server-ui';

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

  const messageForm = new MessageFormData()
    .title('Message Form Example')
    .body('This shows a simple example using §o§7MessageFormData§r.')
    .button1('Button 1')
    .button2('Button 2');

  messageForm
    .show(players[0])
    .then((formData: MessageFormResponse) => {
      // player canceled the form, or another dialog was up and open.
      if (formData.canceled || formData.selection === undefined) {
        return;
      }

      log(`You selected ${formData.selection === 0 ? 'Button 1' : 'Button 2'}`);
    })
    .catch((error: Error) => {
      log('Failed to show form: ' + error);
      return -1;
    });
}

showTranslatedMessageForm.ts

typescript
import { world, DimensionLocation } from '@minecraft/server';
import { MessageFormResponse, MessageFormData } from '@minecraft/server-ui';

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

  const messageForm = new MessageFormData()
    .title({ translate: 'permissions.removeplayer' })
    .body({ translate: 'accessibility.list.or.two', with: ['Player 1', 'Player 2'] })
    .button1('Player 1')
    .button2('Player 2');

  messageForm
    .show(players[0])
    .then((formData: MessageFormResponse) => {
      // player canceled the form, or another dialog was up and open.
      if (formData.canceled || formData.selection === undefined) {
        return;
      }

      log(`You selected ${formData.selection === 0 ? 'Player 1' : 'Player 2'}`);
    })
    .catch((error: Error) => {
      log('Failed to show form: ' + error);
      return -1;
    });
}

Constructors

Constructor

new MessageFormData(): MessageFormData

Returns

MessageFormData

Methods

body()

body(bodyText): MessageFormData

Defined in: @minecraft/server-ui/index.d.ts:861

Parameters

bodyText

string | RawMessage

Returns

MessageFormData

Remarks

Method that sets the body text for the modal form.


button1()

button1(text): MessageFormData

Defined in: @minecraft/server-ui/index.d.ts:868

Parameters

text

string | RawMessage

Returns

MessageFormData

Remarks

Method that sets the text for the first button of the dialog.


button2()

button2(text): MessageFormData

Defined in: @minecraft/server-ui/index.d.ts:875

Parameters

text

string | RawMessage

Returns

MessageFormData

Remarks

This method sets the text for the second button on the dialog.


show()

show(player): Promise<MessageFormResponse>

Defined in: @minecraft/server-ui/index.d.ts:894

Parameters

player

Player

Player to show this dialog to.

Returns

Promise<MessageFormResponse>

Remarks

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

This function can't be called in restricted-execution mode.

Throws

This function can throw errors.

minecraftcommon.EngineError

minecraftserver.InvalidEntityError

minecraftserver.RawMessageError


title()

title(titleText): MessageFormData

Defined in: @minecraft/server-ui/index.d.ts:900

Parameters

titleText

string | RawMessage

Returns

MessageFormData

Remarks

This builder method sets the title for the modal dialog.