use of org.bukkit.event.block.BlockPistonRetractEvent in project Glowstone by GlowstoneMC.
the class BlockPiston method onRedstoneUpdate.
@Override
public void onRedstoneUpdate(GlowBlock me) {
PistonBaseMaterial piston = (PistonBaseMaterial) me.getState().getData();
BlockFace pistonBlockFace = piston.getFacing();
int rawFace = BlockDirectional.getRawFace(pistonBlockFace);
BlockActionMessage message = new BlockActionMessage(me.getX(), me.getY(), me.getZ(), me.isBlockIndirectlyPowered() ? 0 : 1, rawFace, me.getType().getId());
GlowChunk chunk = me.getChunk();
GlowChunk.Key chunkKey = GlowChunk.Key.of(chunk.getX(), chunk.getZ());
GlowWorld world = me.getWorld();
if (me.isBlockIndirectlyPowered() && !isPistonExtended(me)) {
List<Block> blocksToMove = new ArrayList<>();
List<Block> blocksToBreak = new ArrayList<>();
boolean allowMovement = addBlock(me, pistonBlockFace, me.getRelative(pistonBlockFace), pistonBlockFace.getOppositeFace(), blocksToMove, blocksToBreak);
if (!allowMovement) {
return;
}
BlockPistonExtendEvent event = EventFactory.getInstance().callEvent(new BlockPistonExtendEvent(me, blocksToMove, pistonBlockFace));
if (event.isCancelled()) {
return;
}
world.getRawPlayers().stream().filter(player -> player.canSeeChunk(chunkKey)).forEach(player -> player.getSession().send(message));
world.playSound(me.getLocation(), Sound.BLOCK_PISTON_EXTEND, SoundCategory.BLOCKS, 0.5f, 0.75f);
// extended state for piston base
me.setData((byte) (me.getData() | 0x08));
performMovement(pistonBlockFace, blocksToMove, blocksToBreak);
// set piston head block when extended
setType(me.getRelative(pistonBlockFace), Material.MOVING_PISTON, sticky ? me.getData() | 0x08 : rawFace);
return;
}
if (!isPistonExtended(me)) {
return;
}
List<Block> blocksToMove = new ArrayList<>();
List<Block> blocksToBreak = new ArrayList<>();
if (sticky) {
addBlock(me, pistonBlockFace.getOppositeFace(), me.getRelative(pistonBlockFace, 2), pistonBlockFace.getOppositeFace(), blocksToMove, blocksToBreak);
}
BlockPistonRetractEvent event = EventFactory.getInstance().callEvent(new BlockPistonRetractEvent(me, blocksToMove, pistonBlockFace));
if (event.isCancelled()) {
return;
}
world.getRawPlayers().stream().filter(player -> player.canSeeChunk(chunkKey)).forEach(player -> player.getSession().send(message));
world.playSound(me.getLocation(), Sound.BLOCK_PISTON_CONTRACT, SoundCategory.BLOCKS, 0.5f, 0.75f);
// normal state for piston
setType(me, me.getType(), me.getData() & ~0x08);
if (sticky && blocksToMove.size() > 0) {
performMovement(pistonBlockFace.getOppositeFace(), blocksToMove, blocksToBreak);
} else {
// remove piston head
me.getRelative(pistonBlockFace).setTypeIdAndData(0, (byte) 0, true);
}
}
Aggregations