For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /server-ui/classes/ModalFormData.md.

Class: ModalFormData

Beta

Used to create a fully customizable pop-up form for a player. 用于为玩家创建完全可自定义的弹出表单。

Example

showBasicModalForm.ts

import { world, DimensionLocation } from '@minecraft/server';
import { ModalFormData } from '@minecraft/server-ui';

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

  const modalForm = new ModalFormData().title('Example Modal Controls for §o§7ModalFormData§r');

  modalForm.toggle('Toggle w/o default');
  modalForm.toggle('Toggle w/ default', true);

  modalForm.slider('Slider w/o default', 0, 50, 5);
  modalForm.slider('Slider w/ default', 0, 50, 5, 30);

  modalForm.dropdown('Dropdown w/o default', ['option 1', 'option 2', 'option 3']);
  modalForm.dropdown('Dropdown w/ default', ['option 1', 'option 2', 'option 3'], 2);

  modalForm.textField('Input w/o default', 'type text here');
  modalForm.textField('Input w/ default', 'type text here', 'this is default');

  modalForm
    .show(players[0])
    .then(formData => {
      players[0].sendMessage(`Modal form results: ${JSON.stringify(formData.formValues, undefined, 2)}`);
    })
    .catch((error: Error) => {
      log('Failed to show form: ' + error);
      return -1;
    });
}

Constructors

Constructor

new ModalFormData(): ModalFormData

Beta

Returns

ModalFormData

Methods

divider()

divider(): ModalFormData

Beta

Returns

ModalFormData

Remarks

Adds a section divider to the form. 向表单添加一个分隔线。


dropdown(label, items, dropdownOptions?): ModalFormData

Beta

Parameters

label

any

The label to display for the dropdown. 下拉菜单显示的标签。

items

any[]

The selectable items for the dropdown. 下拉菜单的可选项目。

ModalFormDataDropdownOptions

The optional additional values for the dropdown creation. 创建下拉菜单时可选的附加参数。

Returns

ModalFormData

Remarks

Adds a dropdown with choices to the form. 向表单添加一个包含选项的下拉菜单。


header(text): ModalFormData

Beta

Parameters

text

any

Text to display. 要显示的文本。

Returns

ModalFormData

Remarks

Adds a header to the form. 向表单添加一个标题。


label()

label(text): ModalFormData

Beta

Parameters

text

any

Text to display. 要显示的文本。

Returns

ModalFormData

Remarks

Adds a label to the form. 向表单添加一个标签。


show()

show(player): Promise<ModalFormResponse>

World Mutation Beta

Parameters

player

Player

Player to show this dialog to. 要显示此对话框的玩家。

Returns

Promise<ModalFormResponse>

Remarks

Creates and shows this modal popup form. Returns asynchronously when the player confirms or cancels the dialog. 创建并显示此模态弹出表单。当玩家确认或取消对话框时异步返回。

Throws

This function can throw errors.

EngineError

InvalidEntityError

RawMessageError


slider()

slider(label, minimumValue, maximumValue, sliderOptions?): ModalFormData

Beta

Parameters

label

any

The label to display for the slider. 滑块显示的标签。

minimumValue

number

The minimum selectable possible value. 可选择的最小值。

maximumValue

number

The maximum selectable possible value. 可选择的最大值。

sliderOptions?

ModalFormDataSliderOptions

The optional additional values for the slider creation. 创建滑块时可选的附加参数。

Returns

ModalFormData

Remarks

Adds a numeric slider to the form. 向表单添加一个数值滑块。


submitButton()

submitButton(submitButtonText): ModalFormData

Beta

Parameters

submitButtonText

any

Returns

ModalFormData


textField()

textField(label, placeholderText, textFieldOptions?): ModalFormData

Beta

Parameters

label

any

The label to display for the textfield. 文本字段显示的标签。

placeholderText

any

The place holder text to display. 要显示的占位文本。

textFieldOptions?

ModalFormDataTextFieldOptions

The optional additional values for the textfield creation. 创建文本字段时可选的附加参数。

Returns

ModalFormData

Remarks

Adds a textbox to the form. 向表单添加一个文本框。


title()

title(titleText): ModalFormData

Beta

Parameters

titleText

any

Returns

ModalFormData

Remarks

This builder method sets the title for the modal dialog. 此构建器方法设置模态对话框的标题。


toggle()

toggle(label, toggleOptions?): ModalFormData

Beta

Parameters

label

any

The label to display for the toggle. 开关显示的标签。

toggleOptions?

ModalFormDataToggleOptions

The optional additional values for the toggle creation. 创建开关时可选的附加参数。

Returns

ModalFormData

Remarks

Adds a toggle checkbox button to the form. 向表单添加一个开关复选框按钮。