Script API - v1.26.10.23
    Preparing search index...

    Class WorldBeforeEvents

    Index

    Constructors

    Properties

    This property can be read in early-execution mode.

    import { Player, world } from "@minecraft/server";

    world.beforeEvents.effectAdd.subscribe((event) => {
    if (event.entity instanceof Player) {
    // Cancel any effects that apply to players.
    event.cancel = true;

    // Message player that effect is not added
    event.entity.sendMessage("You can't get the following effect:");
    // For some reason, the effect type is translated based on server's language.
    event.entity.sendMessage("Effect Type: " + event.effectType);
    event.entity.sendMessage("Effect Duration: " + event.duration);
    }
    });

    This property can be read in early-execution mode.

    This property can be read in early-execution mode.

    This property can be read in early-execution mode.

    This property can be read in early-execution mode.

    import { world } from "@minecraft/server";

    // Subscribe to entityRemove event
    world.beforeEvents.entityRemove.subscribe((data) => {
    world.sendMessage("Entity Removed: " + data.removedEntity.typeId);
    // Spawn lightning when an entity is removed.
    data.removedEntity.runCommand("summon lightning_bolt ~ ~10 ~");
    });

    This property can be read in early-execution mode.

    import { world } from "@minecraft/server";

    world.beforeEvents.explosion.subscribe((data) => {
    world.sendMessage("Explosion at " + data.dimension.id + " dimension.");
    data.cancel = true;
    });
    import { world } from "@minecraft/server";

    world.beforeEvents.explosion.subscribe((data) => {
    // Force the explosion to not destroy blocks, but it can impact entities still.
    data.setImpactedBlocks([]);
    });

    This property can be read in early-execution mode.

    This property can be read in early-execution mode.

    import { world } from "@minecraft/server";

    // Disable the ability of a player from breaking a block.
    world.beforeEvents.playerBreakBlock.subscribe((event) => {
    event.cancel = true;
    event.player.sendMessage("You can't break blocks!");
    });

    This property can be read in early-execution mode.

    import { world } from "@minecraft/server";

    // Disable the ability of any players without admin tag from changing gamemode.
    world.beforeEvents.playerGameModeChange.subscribe((event) => {
    if (!event.player.hasTag("admin")) {
    event.cancel = true;
    event.player.sendMessage("You can't change gamemode!");
    }
    });

    This property can be read in early-execution mode.

    import { system, world } from "@minecraft/server";
    import { MessageFormData } from "@minecraft/server-ui";

    // Subscribe to playerInteractWithBlock event to detect if a player interacts with a block
    world.beforeEvents.playerInteractWithBlock.subscribe((event) => {
    // Check if player interacts with a crafter whilst holding diamonds
    if (
    event.block.typeId === "minecraft:crafter" &&
    event.itemStack &&
    event.itemStack.typeId === "minecraft:diamond"
    ) {
    // Cancel interaction
    event.cancel = true;

    // Use system.run to queue for later in the current tick to bypass read-only state
    system.run(() => {
    // Show the player a message form
    new MessageFormData().title("Crafter").body("This is a crafter!").button1("Close").show(event.player);
    });
    }
    });

    // Please note that playerInteractWithBlock does not fire when player interacts with air.

    This property can be read in early-execution mode.

    import { system, world } from "@minecraft/server";
    import { MessageFormData } from "@minecraft/server-ui";

    // Subscribe to playerInteractWithEntity event to detect if a player interacts with a block
    world.beforeEvents.playerInteractWithEntity.subscribe((event) => {
    // Check if player interacts with a villager whilst holding diamonds
    if (event.target.typeId === "minecraft:villager" || event.target.typeId === "minecraft:villager_v2") {
    // Cancel interaction
    event.cancel = true;

    // Use system.run to queue for later in the current tick to bypass read-only state
    system.run(() => {
    // Show the player a message form
    new MessageFormData().title("villager").body("This is a villager!").button1("Close").show(event.player);
    });
    }
    });

    This property can be read in early-execution mode.

    This property can be read in early-execution mode.