Solved Powering a piston programmatically

Discussion in 'Spigot Plugin Development' started by SpleshXD, Jun 13, 2021.

Thread Status:
Not open for further replies.
  1. Hey, I know that a few people opened threads similar to this before, but most of them stayed unanswered or didn't show their solution.

    A bukkit thread from 2012 showed how to do it (Kind of, only showing half of the code) using PistonBaseMaterial (Deprecated), and anyway it didn't really work for me, nothing happened.
    Does anyone know how to do it or at least point me to the right direction? Even if it's using PistonBaseMaterial.
     
  2. 1) Get the blockdata of the block, cast it to Piston
    2) use the method setExtended(true) to extend the piston.
    3) Set the blockdata of the block to the new blockdata.
     
  3. Do I need to update the state? Sorry, I've never worked with blockdata before.

    Also, is there a way to set a block to air without it making the breaking sound?
     
  4. No need to apologize. You don't have to set the state, an example would be as following.
    Code (Java):
    final Piston piston = (Piston) block.getBlockData();
    piston.setExtended(true);
    block.setBlockData(piston);
     
  5. The thing is, it doesn't push the block, it only the block it self extended, doesn't create the extended "arm" block nor does it push the blocks next to it.
    [​IMG]
     
  6. I see, my bad.

    You need to set the block in the direction which the piston faces to PISTON_HEAD and set the facing of the piston head equal to the facing of the piston itself. When updating the block in the direction which the piston faces, you need to disable physics to avoid the piston retracting from the update. Here's an example again:
    Code (Java):

    final Piston piston = (Piston) block.getBlockData();
    piston.setExtended(true);
    block.setBlockData(piston);

    final Block head = block.getRelative(piston.getFacing());
    head.setType(Material.PISTON_HEAD,false);
    final PistonHead relativeData = (PistonHead) head.getBlockData();
    relativeData.setFacing(piston.getFacing());
    head.setBlockData(relativeData, false);
     
  7. Wouldn't .setType() just replace the block instead of pushing it?
     
  8. Try using the stopsound packet/command right after changing the block
     
  9. No need, block.setType(Material.AIR) doesn't play a sound?

    Yes it would, if you want to actually push the block you'll probably have to resolve to NMS or power the piston by powering a nearby block.
     
  10. It does for me, might be because I'm breaking the piston base first. I guess I could just break the head first.


    So there isn't a way to do it without NMS? And if I choose to use NMS, how would I go about doing that?
     
  11. By breaking a part of the piston, the piston other part will break naturally. You need to disable block physics while breaking to avoid the second part from breaking before you set it to air.
     
Thread Status:
Not open for further replies.