001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package net.tridentsdk.event.block;
019
020import net.tridentsdk.base.Block;
021import net.tridentsdk.base.BlockDirection;
022import net.tridentsdk.base.Substance;
023import net.tridentsdk.event.Cancellable;
024
025/**
026 * Called whenever a piston extends or retracts
027 *
028 * <p>This is an umbrella event, do not listen to it</p>
029 *
030 * @author The TridentSDK Team
031 * @since 0.3-alpha-DP
032 */
033public abstract class BlockPistonEvent extends BlockEvent implements Cancellable {
034    private final BlockDirection direction;
035    private final boolean retract;
036    private final Block influenced;
037    private boolean cancelled;
038
039    public BlockPistonEvent(Block block, BlockDirection direction, boolean retract, Block influenced) {
040        super(block);
041        this.direction = direction;
042        this.retract = retract;
043        this.influenced = influenced;
044    }
045
046    /**
047     * Returns the direction that the piston is facing, for example if the piston head face of a block is on the north
048     * end of a block, then the Direction that this event returns will be north, does not change depending on whether
049     * this piston is extending or retracting, so a block may actually be moving south if this is returns north
050     *
051     * @return Orientation
052     */
053    public BlockDirection direction() {
054        return this.direction;
055    }
056
057    /**
058     * Returns true if this piston is retracting
059     *
060     * @return Boolean
061     */
062    public boolean isRetracting() {
063        return this.retract;
064    }
065
066    /**
067     * Returns true if this piston is extending, convenience for !isRetracting()
068     *
069     * @return Boolean
070     */
071    public boolean isExtending() {
072        return !this.retract;
073    }
074
075    /**
076     * Gets the block that is being moved by this piston, if any
077     *
078     * <p>If this is a piston extend event, this will return the first block in the series of blocks being pushed.</p>
079     *
080     * @return the block being moved, may be null if air, or retracting from a block without this piston being sticky
081     */
082    public Block influencedBlock() {
083        return this.influenced;
084    }
085
086    public boolean isSticky() {
087        return this.block().substance() == Substance.PISTON_STICKY_BASE;
088    }
089
090    @Override
091    public boolean isIgnored() {
092        return cancelled;
093    }
094
095    @Override
096    public void cancel(boolean cancelled) {
097        this.cancelled = cancelled;
098    }
099}