Script API - v26.60
    Preparing search index...

    Class ObservableUIRawMessage

    An observable that holds a UIRawMessage value. Listeners are notified whenever the value changes.

    import { Player, system } from "@minecraft/server";
    import { CustomForm, DataDrivenScreenClosedReason, ObservableUIRawMessage } from "@minecraft/server-ui";

    // Run `/scriptevent example:realmwelcome` in chat to open the form.
    system.afterEvents.scriptEventReceive.subscribe(({ id, sourceEntity }) => {
    if (id !== "example:realmwelcome" || !(sourceEntity instanceof Player)) return;

    const welcomeMessage = new ObservableUIRawMessage({
    rawtext: [{ text: "Welcome to " }, { text: "the realm!" }],
    });

    const form = new CustomForm(sourceEntity, welcomeMessage);
    form.label("Thanks for visiting our realm.");

    form.show().then((reason) => {
    if (reason === DataDrivenScreenClosedReason.ServerClosed) {
    sourceEntity.sendMessage("The server closed the welcome form.");
    } else {
    sourceEntity.sendMessage("You closed the welcome form.");
    }
    });
    });
    export class ObservableUIRawMessage {
    constructor(data: UIRawMessage, options?: ObservableOptions);
    getData(): UIRawMessage;
    setData(data: UIRawMessage): void;
    subscribe(callback: (arg0: UIRawMessage) => void): (arg0: UIRawMessage) => void;
    unsubscribe(callback: (arg0: UIRawMessage) => void): boolean;
    }
    Index
    • Returns UIRawMessage

      Returns the current UIRawMessage value held by this observable.

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

      early-execution-allowed - This function can be called in early-execution mode.

      import { Player, system } from "@minecraft/server";
      import { ObservableUIRawMessage } from "@minecraft/server-ui";

      // Run `/scriptevent example:supporterbadge` in chat to try this example.
      system.afterEvents.scriptEventReceive.subscribe(({ id, sourceEntity }) => {
      if (id !== "example:supporterbadge" || !(sourceEntity instanceof Player)) return;

      const badgeMessage = new ObservableUIRawMessage({
      rawtext: [{ text: "Gold Tier Supporter" }],
      });

      const messageData = badgeMessage.getData();
      const text = messageData.rawtext?.[0]?.text;
      if (text) {
      sourceEntity.sendMessage(`Supporter badge: ${text}.`);
      }
      });
    • Parameters

      Returns void

      Updates the UIRawMessage value held by this observable. If the new value differs from the current value, all subscribed listeners are notified with the new value.

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

      early-execution-allowed - This function can be called in early-execution mode.

      import { Player, system } from "@minecraft/server";
      import { ObservableUIRawMessage } from "@minecraft/server-ui";

      // Run `/scriptevent example:missionupdate` in chat to try this example.
      system.afterEvents.scriptEventReceive.subscribe(({ id, sourceEntity }) => {
      if (id !== "example:missionupdate" || !(sourceEntity instanceof Player)) return;

      const missionText = new ObservableUIRawMessage({
      rawtext: [{ text: "Phase 1: Explore Caves" }],
      });
      missionText.subscribe((data) => {
      sourceEntity.sendMessage(`Mission updated: ${data.rawtext?.[0]?.text}`);
      });

      missionText.setData({
      rawtext: [{ text: "Phase 2: Defeat Boss" }],
      });
      });
    • Parameters

      • callback: (arg0: UIRawMessage) => void

        A function that receives the new UIRawMessage value each time the observable changes.

      Returns (arg0: UIRawMessage) => void

      Registers a callback to be invoked whenever the observable's value changes. Returns the callback, which can be passed to unsubscribe to remove the listener.

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

      early-execution-allowed - This function can be called in early-execution mode.

      import { Player, system } from "@minecraft/server";
      import { CustomForm, ObservableUIRawMessage } from "@minecraft/server-ui";

      const alerts = [
      { rawtext: [{ text: "Maintenance in 1 hour." }] },
      { rawtext: [{ text: "Double XP weekend active!" }] },
      { rawtext: [{ text: "New community event started." }] },
      ];

      // Run `/scriptevent example:serveralerts` in chat to open the form.
      system.afterEvents.scriptEventReceive.subscribe(({ id, sourceEntity }) => {
      if (id !== "example:serveralerts" || !(sourceEntity instanceof Player)) return;

      const announcement = new ObservableUIRawMessage({
      rawtext: [{ text: "Welcome to the server!" }],
      });
      // `subscribe` returns the callback. Keep it so `unsubscribe` can remove the same listener.
      const listener = announcement.subscribe((data) => {
      sourceEntity.sendMessage(`Notice: ${data.rawtext?.[0]?.text}`);
      });

      const form = new CustomForm(sourceEntity, "Alerts");
      form.button("Unsubscribe", () => announcement.unsubscribe(listener));
      form.button("Subscribe", () => announcement.subscribe(listener));

      // Change the announcement every 2 seconds (40 ticks) so the listener receives updates.
      const intervalId = system.runInterval(() => {
      announcement.setData(alerts[Math.floor(Math.random() * alerts.length)]);
      }, 40);

      // `show` resolves when the player closes the form. Stop the changes then.
      form.show().then(() => system.clearRun(intervalId));
      });
    • Parameters

      • callback: (arg0: UIRawMessage) => void

        The callback handle previously returned by subscribe.

      Returns boolean

      Removes a previously registered listener from this observable. Returns true if the listener was found and removed, false if it was not found.

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

      early-execution-allowed - This function can be called in early-execution mode.

      import { Player, system } from "@minecraft/server";
      import { CustomForm, ObservableUIRawMessage } from "@minecraft/server-ui";

      const eventNotices = [
      { rawtext: [{ text: "Event: Parkour challenge open!" }] },
      { rawtext: [{ text: "Event: Arena battles starting!" }] },
      { rawtext: [{ text: "Event: Scavenger hunt underway!" }] },
      ];

      // Run `/scriptevent example:eventsfeed` in chat to open the form.
      system.afterEvents.scriptEventReceive.subscribe(({ id, sourceEntity }) => {
      if (id !== "example:eventsfeed" || !(sourceEntity instanceof Player)) return;

      const eventFeed = new ObservableUIRawMessage({
      rawtext: [{ text: "Event: Gathering participants..." }],
      });
      const listener = eventFeed.subscribe((data) => {
      sourceEntity.sendMessage(`Feed: ${data.rawtext?.[0]?.text}`);
      });

      const form = new CustomForm(sourceEntity, "Events Feed");
      // `unsubscribe` removes the listener so it stops receiving changes.
      form.button("Unsubscribe", () => eventFeed.unsubscribe(listener));
      form.button("Subscribe", () => eventFeed.subscribe(listener));

      // Change the notice every 2 seconds (40 ticks) so the listener receives updates while subscribed.
      const intervalId = system.runInterval(() => {
      eventFeed.setData(eventNotices[Math.floor(Math.random() * eventNotices.length)]);
      }, 40);

      // `show` resolves when the player closes the form. Stop the changes then.
      form.show().then(() => system.clearRun(intervalId));
      });