Edit

Share via


TripWireTripAfterEventSignal Class

Manages callbacks that are connected to when a trip wire is tripped.

Methods

subscribe

subscribe(callback: (arg0: TripWireTripAfterEvent) => void): (arg0: TripWireTripAfterEvent) => void

Adds a callback that will be called when a trip wire is tripped.

Parameters

Returns (arg0: TripWireTripAfterEvent) => void

Notes:

  • This function can't be called in read-only mode.
  • This function can be called in early-execution mode.

unsubscribe

unsubscribe(callback: (arg0: TripWireTripAfterEvent) => void): void

Removes a callback from being called when a trip wire is tripped.

Parameters

Notes:

  • This function can't be called in read-only mode.
  • This function can be called in early-execution mode.

Examples

tripWireTripEvent.ts
import { world, system, BlockPermutation, TripWireTripAfterEvent, DimensionLocation } from "@minecraft/server";
import { MinecraftBlockTypes } from "@minecraft/vanilla-data";

function tripWireTripEvent(log: (message: string, status?: number) => void, targetLocation: DimensionLocation) {
  // set up a tripwire
  const redstone = targetLocation.dimension.getBlock({
    x: targetLocation.x,
    y: targetLocation.y - 1,
    z: targetLocation.z,
  });
  const tripwire = targetLocation.dimension.getBlock(targetLocation);

  if (redstone === undefined || tripwire === undefined) {
    log("Could not find block at location.");
    return -1;
  }

  redstone.setPermutation(BlockPermutation.resolve(MinecraftBlockTypes.RedstoneBlock));
  tripwire.setPermutation(BlockPermutation.resolve(MinecraftBlockTypes.TripWire));

  world.afterEvents.tripWireTrip.subscribe((tripWireTripEvent: TripWireTripAfterEvent) => {
    const eventLoc = tripWireTripEvent.block.location;

    if (eventLoc.x === targetLocation.x && eventLoc.y === targetLocation.y && eventLoc.z === targetLocation.z) {
      log(
        "Tripwire trip event at tick " +
          system.currentTick +
          (tripWireTripEvent.sources.length > 0 ? " by entity " + tripWireTripEvent.sources[0].id : "")
      );
    }
  });
}

(preview) Work with this sample on the MCTools.dev code sandbox.