MessageFormData Class
Builds a simple two-button modal dialog.
Examples
messageFormSimple.ts
import { Player } from '@minecraft/server';
import { MessageFormResponse, MessageFormData } from '@minecraft/server-ui';
function showMessage(player: Player) {
const messageForm = new MessageFormData()
.title({ translate: 'permissions.removeplayer' }) // "Remove player"
.body({ translate: 'accessibility.list.or.two', with: ['Player 1', 'Player 2'] }) // "Player 1 or Player 2"
.button1('Player 1')
.button2('Player 2');
messageForm
.show(player)
.then((formData: MessageFormResponse) => {
// player canceled the form, or another dialog was up and open.
if (formData.canceled || formData.selection === undefined) {
return;
}
player.sendMessage(`You selected ${formData.selection === 0 ? 'Player 1' : 'Player 2'}`);
})
.catch((error: Error) => {
player.sendMessage('Failed to show form: ' + error);
});
}
Methods
constructor
new MessageFormData()
Creates a new modal form builder.
Returns MessageFormData
body
body(bodyText: minecraftserver.RawMessage | string): MessageFormData
Method that sets the body text for the modal form.
Parameters
- bodyText: @minecraft/server.RawMessage | string
Returns MessageFormData
button1
button1(text: minecraftserver.RawMessage | string): MessageFormData
Method that sets the text for the first button of the dialog.
Parameters
- text: @minecraft/server.RawMessage | string
Returns MessageFormData
button2
button2(text: minecraftserver.RawMessage | string): MessageFormData
This method sets the text for the second button on the dialog.
Parameters
- text: @minecraft/server.RawMessage | string
Returns MessageFormData
show
show(player: minecraftserver.Player): Promise<MessageFormResponse>
Creates and shows this modal popup form. Returns asynchronously when the player confirms or cancels the dialog.
Parameters
player: @minecraft/server.Player
Player to show this dialog to.
Returns Promise<MessageFormResponse>
Important
This function can't be called in read-only mode.
Warning
This function can throw errors.
title
title(titleText: minecraftserver.RawMessage | string): MessageFormData
This builder method sets the title for the modal dialog.
Parameters
- titleText: @minecraft/server.RawMessage | string
Returns MessageFormData
Examples
messageFormSimple.ts
import { Player } from '@minecraft/server';
import { MessageFormResponse, MessageFormData } from '@minecraft/server-ui';
function showMessage(player: Player) {
const messageForm = new MessageFormData()
.title({ translate: 'permissions.removeplayer' }) // "Remove player"
.body({ translate: 'accessibility.list.or.two', with: ['Player 1', 'Player 2'] }) // "Player 1 or Player 2"
.button1('Player 1')
.button2('Player 2');
messageForm
.show(player)
.then((formData: MessageFormResponse) => {
// player canceled the form, or another dialog was up and open.
if (formData.canceled || formData.selection === undefined) {
return;
}
player.sendMessage(`You selected ${formData.selection === 0 ? 'Player 1' : 'Player 2'}`);
})
.catch((error: Error) => {
player.sendMessage('Failed to show form: ' + error);
});
}
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for