Problem while getting the prevoius location of a ChangeBlockEvent caused by a piston

I first tried to use MoveBlockEvent but it wasn’t thrown when using pistons, so i used ChangeBlockEvent.
Here’s my code:

   @Listener
   public void onBlockMove(ChangeBlockEvent event) {
      if (event.getCause().first(Piston.class).isPresent()) {
         for(Transaction<BlockSnapshot> transaction: event.getTransactions()) {
            if (!transaction.getOriginal().getLocation().isPresent() |!
                   transaction.getFinal().getLocation().isPresent()) {
                 continue;
            }
            Location<World> oldLocation = transaction.getOriginal().getLocation().get();
            Location<World> newLocation = transaction.getFinal().getLocation)().get();
         }
      }
   }

The problem is, that oldLocation and newLocation are the same. How can I get the original location?

Remember, each transaction in ChangeBlockEvent occurs over only one block position.
The event itself may contain multiple transactions and therefore you can determine whether a block moved from A to B.

If I position a piston with a stone block in front of it, then activate the piston, a bunch of events fire.
Lets say piston is at 100, the stone at 101 and air at 102
From inspecting the transactions, it looks like the piston movement is as follows:

  1. stone changes to piston_extension at 101 with the piston base blocksnapshot as the cause (100)
  2. air changes to piston_extension at 102 (not sure why but that’s what I got)
  3. piston_extension changes to stone at 102 with the piston tile entity as the cause (100)
  4. piston_extension changes to piston_head at 101 with the piston tile entity as the cause (100)

There were a bunch of intermediate transactions but these were the ones of interest.

I suppose number 3 would be the most interesting one here since you can tell that the block is moved by the piston.
Note that for some reason these seem to occur mostly in separate events so it might be a bit difficult to follow the trace exactly.
I feel the tracking may be a little bit broken but maybe @blood would know if that is the case.

1 Like

the air changes to piston extension as piston_extension is a general technical block used when the blocks are still being moved. http://minecraft.gamepedia.com/Piston#Piston_extension