When present, this block has piston-like behavior. Contains additional properties for discovering block piston state.

Hierarchy

Constructors

Properties

block: Block

Remarks

Block instance that this component pertains to.

isMoving: boolean

Remarks

Whether the piston is in the process of expanding or retracting.

Throws

This property can throw when used.

Remarks

The current state of the piston.

Throws

This property can throw when used.

typeId: string

Remarks

Identifier of the component.

componentId: "minecraft:piston" = 'minecraft:piston'

Methods

  • Returns Block[]

    Remarks

    Retrieves a set of blocks that this piston is connected with.

    Throws

    This function can throw errors.

    Example

    randomize.js

    // Script by WavePlayz

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

    // Get all available block types in the game
    const blockTypes = BlockTypes.getAll();

    // Function to get a random block type from the list of block types
    const getRandomBlockType = () =>
    blockTypes[Math.floor(blockTypes.length * Math.random())];

    // Subscribe to the event that triggers when a piston is activated
    world.afterEvents.pistonActivate.subscribe((eventData) => {
    // Destructure the dimension and piston objects from the event data
    const { dimension, piston } = eventData;

    // Get the blocks attached to the piston
    const attachedBlocks = piston.getAttachedBlocks();

    // Get the locations of the blocks attached to the piston
    const attachedBlocksLocations = piston.getAttachedBlocksLocations();

    // Loop through each attached block location
    attachedBlocksLocations.forEach((location, index) => {
    // Set the type of each attached block to a random block type
    attachedBlocks[index].setType(getRandomBlockType());
    });
    });

  • Returns Vector3[]

    Remarks

    Retrieves a set of block locations that this piston is connected with.

    Throws

    This function can throw errors.

    Example

    randomize.js

    // Script by WavePlayz

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

    // Get all available block types in the game
    const blockTypes = BlockTypes.getAll();

    // Function to get a random block type from the list of block types
    const getRandomBlockType = () =>
    blockTypes[Math.floor(blockTypes.length * Math.random())];

    // Subscribe to the event that triggers when a piston is activated
    world.afterEvents.pistonActivate.subscribe((eventData) => {
    // Destructure the dimension and piston objects from the event data
    const { dimension, piston } = eventData;

    // Get the blocks attached to the piston
    const attachedBlocks = piston.getAttachedBlocks();

    // Get the locations of the blocks attached to the piston
    const attachedBlocksLocations = piston.getAttachedBlocksLocations();

    // Loop through each attached block location
    attachedBlocksLocations.forEach((location, index) => {
    // Set the type of each attached block to a random block type
    attachedBlocks[index].setType(getRandomBlockType());
    });
    });

  • Returns boolean

    Whether the component is valid.

    Remarks

    Returns whether the component is valid. A component is considered valid if its owner is valid, in addition to any addition to any additional validation required by the component.