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

    Class ObservableBoolean

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

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

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

    const nightVision = new ObservableBoolean(false, { clientWritable: true });
    nightVision.subscribe((enabled) => {
    sourceEntity.sendMessage(`Night Vision set to ${enabled}.`);
    });

    const form = new CustomForm(sourceEntity, "Visual Perks");
    form.toggle("Night Vision", nightVision);

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

      • data: boolean

        The initial boolean value for this observable.

      • Optionaloptions: ObservableOptions

        Optional configuration for the observable, such as whether the value can be written by the client.

      Returns ObservableBoolean

      Creates a new ObservableBoolean with the provided initial boolean value.

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

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

    • Returns boolean

      Returns the current boolean 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 { ObservableBoolean } from "@minecraft/server-ui";

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

      const autoJump = new ObservableBoolean(true);
      const isEnabled = autoJump.getData();
      sourceEntity.sendMessage(`Auto jump is currently ${isEnabled ? "enabled" : "disabled"}.`);
      });
    • Parameters

      • data: boolean

        The new boolean value to set.

      Returns void

      Updates the boolean 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 { ObservableBoolean } from "@minecraft/server-ui";

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

      const notifications = new ObservableBoolean(false);
      notifications.subscribe((enabled) => {
      sourceEntity.sendMessage(`Notification status changed to ${enabled}.`);
      });

      notifications.setData(true);
      });
    • Parameters

      • callback: (arg0: boolean) => void

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

      Returns (arg0: boolean) => 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, ObservableBoolean } from "@minecraft/server-ui";

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

      const flightMode = new ObservableBoolean(false);
      // `subscribe` returns the callback. Keep it so `unsubscribe` can remove the same listener.
      const listener = flightMode.subscribe((value) => {
      sourceEntity.sendMessage(`Flight mode changed to ${value}.`);
      });

      const form = new CustomForm(sourceEntity, "Flight Monitor");
      form.button("Unsubscribe", () => flightMode.unsubscribe(listener));
      form.button("Subscribe", () => flightMode.subscribe(listener));

      // Toggle the boolean every 2 seconds (40 ticks) so the listener receives changes.
      const intervalId = system.runInterval(() => {
      flightMode.setData(!flightMode.getData());
      }, 40);

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

      • callback: (arg0: boolean) => 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, ObservableBoolean } from "@minecraft/server-ui";

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

      const shieldActive = new ObservableBoolean(true);
      const listener = shieldActive.subscribe((value) => {
      sourceEntity.sendMessage(`Shield active: ${value}`);
      });

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

      // Toggle the boolean every 2 seconds (40 ticks) so the listener receives changes while subscribed.
      const intervalId = system.runInterval(() => {
      shieldActive.setData(!shieldActive.getData());
      }, 40);

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