For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /server/classes/ButtonPushAfterEventSignal.md.

Class: ButtonPushAfterEventSignal

event

Beta

管理与按钮被按下时相关的回调。

Example

buttonPushEvent.ts

import { world, system, BlockPermutation, ButtonPushAfterEvent, DimensionLocation } from '@minecraft/server';
import { MinecraftBlockTypes } from '@minecraft/vanilla-data';

function buttonPushEvent(log: (message: string, status?: number) => void, targetLocation: DimensionLocation) {
  // set up a button on cobblestone
  const cobblestone = targetLocation.dimension.getBlock(targetLocation);
  const button = targetLocation.dimension.getBlock({
    x: targetLocation.x,
    y: targetLocation.y + 1,
    z: targetLocation.z,
  });

  if (cobblestone === undefined || button === undefined) {
    log('Could not find block at location.');
    return -1;
  }

  cobblestone.setPermutation(BlockPermutation.resolve(MinecraftBlockTypes.Cobblestone));
  button.setPermutation(BlockPermutation.resolve(MinecraftBlockTypes.AcaciaButton).withState('facing_direction', 1));

  world.afterEvents.buttonPush.subscribe((buttonPushEvent: ButtonPushAfterEvent) => {
    const eventLoc = buttonPushEvent.block.location;

    if (eventLoc.x === targetLocation.x && eventLoc.y === targetLocation.y + 1 && eventLoc.z === targetLocation.z) {
      log('Button push event at tick ' + system.currentTick);
    }
  });
}

Methods

subscribe()

subscribe(callback): (arg0) => void

World Mutation Early Execution Beta

Parameters

callback

(arg0) => void

Returns

(arg0) => void

Remarks

添加一个将在按钮被按下时调用的回调。

Adds a callback that will be called when a button is pushed.


unsubscribe()

unsubscribe(callback): void

World Mutation Early Execution Beta

Parameters

callback

(arg0) => void

Returns

void

Remarks

移除一个在按钮被按下时调用的回调。

Removes a callback from being called when a button is pushed.

同领域相关