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

    Class WorldBeforeEvents

    A set of events that fire before an actual action occurs. In most cases, you can potentially cancel or modify the impending event. Note that in before events any APIs that modify gameplay state will not function and will throw an error. (e.g., dimension.spawnEntity)

    Index

    Constructors

    Properties

    This event is triggered after an event has been added to an entity.

    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);
    }
    });

    Fires before an entity is removed from the world (for example, unloaded or removed after being killed.)

    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.runCommandAsync("summon lightning_bolt ~ ~10 ~");
    });

    This event is fired after an explosion occurs.

    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 event fires when an item is successfully used by a player.

    This property can be read in early-execution mode.

    This event fires before a block is broken by a player.

    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!");
    }
    });

    Fires before a player interacts with a block.

    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.

    Fires before a player interacts with an entity.

    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);
    });
    }
    });

    Fires when a player leaves the game.

    This property can be read in early-execution mode.

    This property can be read in early-execution mode.