#pistonActivate afterEvent

1 messages · Page 1 of 1 (latest)

stuck dust
#

world.afterEvents.pistonActivate.subscribe((eventData) => {
    // If the purge is active, allow pistons to act normally
    if (isPurgeActive) {
       // console.warn("Purge is active - pistons can affect all blocks.");
        return; // Exit early, do not cancel piston action
    }

    // If the purge is not active, proceed with checking claims
    const pistonBlock = eventData.block;
    const pistonLocation = pistonBlock.location;
    const pistonClaim = checkClaimedChunks(pistonLocation);

    const attachedBlocks = eventData.piston.getAttachedBlocks();
    console.warn(JSON.stringify(attachedBlocks.typeId))
    attachedBlocks.forEach(blockLoc => {
        const adjacentLocations = getAdjacentLocations(blockLoc);
        for (const adjacentLoc of adjacentLocations) {
            const adjacentClaim = checkClaimedChunks(adjacentLoc);

            if (pistonClaim !== adjacentClaim || (pistonClaim && adjacentClaim && pistonClaim.owner !== adjacentClaim.owner)) {
                // Spawn the piston item at its location
                const pistonItem = pistonBlock.getItemStack(1); // Get an ItemStack representing the piston
                system.run(() => {
                    world.getDimension('overworld').spawnItem(pistonItem, pistonLocation);
                    
                    // Set the piston block to air
                    pistonBlock.setType("minecraft:air");
                });
                return;
            }
        }
    });

    if (!eventData.cancel) {
        //console.warn(`Piston action allowed`);
    }
});```

im having an issue where the attachedBlocks are returning undefined and im not sure why
#

it returns the location of the block before it moved, but the block is now in a new position right? so is it thinking its air so it returns undefined?

steady pasture
#

Shouldn't it be empty array?

stuck dust
#

so its thinking its air but its the piston rod

#

ohhh

#

yeah i might have to adjust it a little bit

steady pasture
#

It only returns what's been removed

stuck dust
#

for the afterevent as im tryin to remove the blocks pushed

steady pasture
#

Then use afterevent...

stuck dust
#

thats what im doing

#

its just not grabbing the attachedblocks

#

just keeps saying undefined

steady pasture
#

attachedblocks is array...

stuck dust
#

so iterate? is that the issue?

steady pasture
#

perhaps map(b=>b.typeId)

stuck dust
#

hmm okay!

#

oh lol it grabbed the piston arm

#

as the attachedBlocks

#

didn't say the block it pushed was one

#

weird

steady pasture
#

Hmmm

stuck dust
#

without anything empty array

#

with block \

#

it pushes but only gets the psiton arm

#

so its grabbing after of course, so maybe i need to delay it?

#

for the check

#

of array

steady pasture
#

I'd just check whatever property there is

stuck dust
#

ok

stuck dust
#

can i get the direction a piston is facing, i didn't see anything about facing for them