Search in sources :

Example 1 with BlockPistonExtendEvent

use of org.bukkit.event.block.BlockPistonExtendEvent 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);
    }
}
Also used : BlockPistonExtendEvent(org.bukkit.event.block.BlockPistonExtendEvent) GlowBlock(net.glowstone.block.GlowBlock) PistonBaseMaterial(org.bukkit.material.PistonBaseMaterial) Getter(lombok.Getter) ItemTable(net.glowstone.block.ItemTable) GlowPlayer(net.glowstone.entity.GlowPlayer) BlockFace(org.bukkit.block.BlockFace) MaterialValueManager(net.glowstone.block.MaterialValueManager) ArrayList(java.util.ArrayList) Block(org.bukkit.block.Block) BlockPistonRetractEvent(org.bukkit.event.block.BlockPistonRetractEvent) Location(org.bukkit.Location) GameRules(net.glowstone.constants.GameRules) World(org.bukkit.World) BlockActionMessage(net.glowstone.net.message.play.game.BlockActionMessage) GlowWorld(net.glowstone.GlowWorld) Material(org.bukkit.Material) SoundCategory(org.bukkit.SoundCategory) EventFactory(net.glowstone.EventFactory) GlowChunk(net.glowstone.chunk.GlowChunk) Collection(java.util.Collection) Sound(org.bukkit.Sound) ItemStack(org.bukkit.inventory.ItemStack) List(java.util.List) PistonMoveBehavior(net.glowstone.block.PistonMoveBehavior) BlockFace(org.bukkit.block.BlockFace) ArrayList(java.util.ArrayList) BlockPistonRetractEvent(org.bukkit.event.block.BlockPistonRetractEvent) BlockPistonExtendEvent(org.bukkit.event.block.BlockPistonExtendEvent) GlowWorld(net.glowstone.GlowWorld) GlowBlock(net.glowstone.block.GlowBlock) Block(org.bukkit.block.Block) PistonBaseMaterial(org.bukkit.material.PistonBaseMaterial) BlockActionMessage(net.glowstone.net.message.play.game.BlockActionMessage) GlowChunk(net.glowstone.chunk.GlowChunk)

Aggregations

ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 Getter (lombok.Getter)1 EventFactory (net.glowstone.EventFactory)1 GlowWorld (net.glowstone.GlowWorld)1 GlowBlock (net.glowstone.block.GlowBlock)1 ItemTable (net.glowstone.block.ItemTable)1 MaterialValueManager (net.glowstone.block.MaterialValueManager)1 PistonMoveBehavior (net.glowstone.block.PistonMoveBehavior)1 GlowChunk (net.glowstone.chunk.GlowChunk)1 GameRules (net.glowstone.constants.GameRules)1 GlowPlayer (net.glowstone.entity.GlowPlayer)1 BlockActionMessage (net.glowstone.net.message.play.game.BlockActionMessage)1 Location (org.bukkit.Location)1 Material (org.bukkit.Material)1 Sound (org.bukkit.Sound)1 SoundCategory (org.bukkit.SoundCategory)1 World (org.bukkit.World)1 Block (org.bukkit.block.Block)1