#Modal form error

1 messages · Page 1 of 1 (latest)

harsh salmon
#
import { world } from "@minecraft/server";
import { ModalFormData } from "@minecraft/server-ui";

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

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

  modalForm
    .show(player)
    .then(formData => {
      player.sendMessage(`Modal form results: ${formData.formValues}`);
    })
    .catch((error) => {
      player.sendMessage('Failed to show form: ' + error);
      return -1;
    });
}

world.beforeEvents.chatSend.subscribe((ev) => {
  const player = ev.sender;
  const message = ev.message;
  if (message === ".show") {
    ev.cancel = true;
    showExampleModal(player)
  }
})

Error:

[Scripting][error]-ReferenceError: Native function [ModalFormData::show] does not have required privileges.    at showExampleModal (main.js:10)
    at <anonymous> (main.js:25)



#

How to get privilege?

plucky flume
#

Add delay to the form show function using system.runTimeout- remember to import system from mincraft/server
system.runTimeout(() => showExampleModal(player), 100)

harsh salmon
#

Isn't there any other way?

plucky flume
#

No. Before events cannot modify the world at all, and that includes showing forms.

#

Chat event forms need delay regardless, as the chat window overrides any form UIs you wish to show.

harsh salmon
#

After events will do?

plucky flume
#

It wouldn't throw the error, but you cannot cancel the message from being sent, and you cannot see the form, as the open chat window would cancel the form from showing.

#

#add-ons message

harsh salmon
#

:(

plucky flume
#

You can show the UI as soon as the chat window closes. See the message I linked above.

harsh salmon
#

So, have to use system.Timeout no other way :(

#

Anyway, thank you for your help :)

plucky flume
harsh salmon
#

Or I can use items instead.

#

Thank you again :)
Issue resolved I guess.

vale breach
#
function showExampleModal(player) {
  const modalForm = new ModalFormData().title('Example Modal Controls for §o§7ModalFormData§r');

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

  modalForm
    .show(player).then(formData => {
if (formData.cancelationReason == `UserBusy`) return showExampleModal(player)
      player.sendMessage(`Modal form results: ${formData.formValues}`);
    })
    .catch((error) => {
      player.sendMessage('Failed to show form: ' + error);
      return -1;
    });
}