Used to create a fully customizable pop-up form for a player.

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;
});
}
import { Player, world } from "@minecraft/server";
import { ModalFormData } from "@minecraft/server-ui";
let form = new ModalFormData();
let effectList = [
{ name: "Regeneration", id: "minecraft:regeneration" },
{ name: "Resistance", id: "minecraft:resistance" },
{ name: "Fire Resistance", id: "minecraft:fire_resistance" },
{ name: "Poison", id: "minecraft:poison" },
];
form.title("Effect Generator");
form.textField("Target", "Target of Effect");
form.dropdown(
"Effect Type",
effectList.map((effect) => effect.name)
);
form.slider("Effect Level", 0, 255, 1);
form.toggle("Hide Effect Particle", true);
for (const player of world.getAllPlayers()) {
form.show(player).then((response) => {
if (response.canceled) {
player.sendMessage("Canceled due to " + response.cancelationReason);
} else {
const [targetName, dropdownValue, effectLevel, hideParticles] =
response.formValues;
if (
typeof dropdownValue !== "string" ||
typeof dropdownValue !== "number" ||
typeof effectLevel !== "number" ||
typeof hideParticles !== "boolean"
)
return player.sendMessage("Cannot process form result.");
const target = world
.getAllPlayers()
.find((player) => player.name === targetName);
if (!(target instanceof Player))
return player.sendMessage("Target does not exist.");
target.addEffect(effectList[dropdownValue].id, 50, {
amplifier: effectLevel,
showParticles: !hideParticles,
});
}
});
}

Constructors

Methods

  • Parameters

    • player: Player

      Player to show this dialog to.

    Returns Promise<ModalFormResponse>

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

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

    This function can throw errors.

  • Parameters

    • label: string | RawMessage
    • minimumValue: number
    • maximumValue: number
    • valueStep: number
    • OptionaldefaultValue: number

    Returns ModalFormData

    Adds a numeric slider to the form.