    001package net.minecraft.block;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.util.Random;
    006import net.minecraft.block.material.Material;
    007import net.minecraft.client.renderer.texture.IconRegister;
    008import net.minecraft.entity.player.EntityPlayer;
    009import net.minecraft.tileentity.TileEntity;
    010import net.minecraft.tileentity.TileEntityPiston;
    011import net.minecraft.util.AxisAlignedBB;
    012import net.minecraft.util.Facing;
    013import net.minecraft.world.IBlockAccess;
    014import net.minecraft.world.World;
    015
    016public class BlockPistonMoving extends BlockContainer
    017{
    018    public BlockPistonMoving(int par1)
    019    {
    020        super(par1, Material.piston);
    021        this.setHardness(-1.0F);
    022    }
    023
    024    /**
    025     * Returns a new instance of a block's tile entity class. Called on placing the block.
    026     */
    027    public TileEntity createNewTileEntity(World par1World)
    028    {
    029        return null;
    030    }
    031
    032    /**
    033     * Called whenever the block is added into the world. Args: world, x, y, z
    034     */
    035    public void onBlockAdded(World par1World, int par2, int par3, int par4) {}
    036
    037    /**
    038     * ejects contained items into the world, and notifies neighbours of an update, as appropriate
    039     */
    040    public void breakBlock(World par1World, int par2, int par3, int par4, int par5, int par6)
    041    {
    042        TileEntity tileentity = par1World.getBlockTileEntity(par2, par3, par4);
    043
    044        if (tileentity instanceof TileEntityPiston)
    045        {
    046            ((TileEntityPiston)tileentity).clearPistonTileEntity();
    047        }
    048        else
    049        {
    050            super.breakBlock(par1World, par2, par3, par4, par5, par6);
    051        }
    052    }
    053
    054    /**
    055     * Checks to see if its valid to put this block at the specified coordinates. Args: world, x, y, z
    056     */
    057    public boolean canPlaceBlockAt(World par1World, int par2, int par3, int par4)
    058    {
    059        return false;
    060    }
    061
    062    /**
    063     * checks to see if you can place this block can be placed on that side of a block: BlockLever overrides
    064     */
    065    public boolean canPlaceBlockOnSide(World par1World, int par2, int par3, int par4, int par5)
    066    {
    067        return false;
    068    }
    069
    070    /**
    071     * The type of render function that is called for this block
    072     */
    073    public int getRenderType()
    074    {
    075        return -1;
    076    }
    077
    078    /**
    079     * Is this block (a) opaque and (b) a full 1m cube?  This determines whether or not to render the shared face of two
    080     * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
    081     */
    082    public boolean isOpaqueCube()
    083    {
    084        return false;
    085    }
    086
    087    /**
    088     * If this block doesn't render as an ordinary block it will return False (examples: signs, buttons, stairs, etc)
    089     */
    090    public boolean renderAsNormalBlock()
    091    {
    092        return false;
    093    }
    094
    095    /**
    096     * Called upon block activation (right click on the block.)
    097     */
    098    public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
    099    {
    100        if (!par1World.isRemote && par1World.getBlockTileEntity(par2, par3, par4) == null)
    101        {
    102            par1World.setBlockToAir(par2, par3, par4);
    103            return true;
    104        }
    105        else
    106        {
    107            return false;
    108        }
    109    }
    110
    111    /**
    112     * Returns the ID of the items to drop on destruction.
    113     */
    114    public int idDropped(int par1, Random par2Random, int par3)
    115    {
    116        return 0;
    117    }
    118
    119    /**
    120     * Drops the block items with a specified chance of dropping the specified items
    121     */
    122    public void dropBlockAsItemWithChance(World par1World, int par2, int par3, int par4, int par5, float par6, int par7)
    123    {
    124        if (!par1World.isRemote)
    125        {
    126            TileEntityPiston tileentitypiston = this.getTileEntityAtLocation(par1World, par2, par3, par4);
    127
    128            if (tileentitypiston != null)
    129            {
    130                Block.blocksList[tileentitypiston.getStoredBlockID()].dropBlockAsItem(par1World, par2, par3, par4, tileentitypiston.getBlockMetadata(), 0);
    131            }
    132        }
    133    }
    134
    135    /**
    136     * Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are
    137     * their own) Args: x, y, z, neighbor blockID
    138     */
    139    public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5)
    140    {
    141        if (!par1World.isRemote && par1World.getBlockTileEntity(par2, par3, par4) == null)
    142        {
    143            ;
    144        }
    145    }
    146
    147    /**
    148     * gets a new TileEntityPiston created with the arguments provided.
    149     */
    150    public static TileEntity getTileEntity(int par0, int par1, int par2, boolean par3, boolean par4)
    151    {
    152        return new TileEntityPiston(par0, par1, par2, par3, par4);
    153    }
    154
    155    /**
    156     * Returns a bounding box from the pool of bounding boxes (this means this box can change after the pool has been
    157     * cleared to be reused)
    158     */
    159    public AxisAlignedBB getCollisionBoundingBoxFromPool(World par1World, int par2, int par3, int par4)
    160    {
    161        TileEntityPiston tileentitypiston = this.getTileEntityAtLocation(par1World, par2, par3, par4);
    162
    163        if (tileentitypiston == null)
    164        {
    165            return null;
    166        }
    167        else
    168        {
    169            float f = tileentitypiston.getProgress(0.0F);
    170
    171            if (tileentitypiston.isExtending())
    172            {
    173                f = 1.0F - f;
    174            }
    175
    176            return this.getAxisAlignedBB(par1World, par2, par3, par4, tileentitypiston.getStoredBlockID(), f, tileentitypiston.getPistonOrientation());
    177        }
    178    }
    179
    180    /**
    181     * Updates the blocks bounds based on its current state. Args: world, x, y, z
    182     */
    183    public void setBlockBoundsBasedOnState(IBlockAccess par1IBlockAccess, int par2, int par3, int par4)
    184    {
    185        TileEntityPiston tileentitypiston = this.getTileEntityAtLocation(par1IBlockAccess, par2, par3, par4);
    186
    187        if (tileentitypiston != null)
    188        {
    189            Block block = Block.blocksList[tileentitypiston.getStoredBlockID()];
    190
    191            if (block == null || block == this)
    192            {
    193                return;
    194            }
    195
    196            block.setBlockBoundsBasedOnState(par1IBlockAccess, par2, par3, par4);
    197            float f = tileentitypiston.getProgress(0.0F);
    198
    199            if (tileentitypiston.isExtending())
    200            {
    201                f = 1.0F - f;
    202            }
    203
    204            int l = tileentitypiston.getPistonOrientation();
    205            this.minX = block.getBlockBoundsMinX() - (double)((float)Facing.offsetsXForSide[l] * f);
    206            this.minY = block.getBlockBoundsMinY() - (double)((float)Facing.offsetsYForSide[l] * f);
    207            this.minZ = block.getBlockBoundsMinZ() - (double)((float)Facing.offsetsZForSide[l] * f);
    208            this.maxX = block.getBlockBoundsMaxX() - (double)((float)Facing.offsetsXForSide[l] * f);
    209            this.maxY = block.getBlockBoundsMaxY() - (double)((float)Facing.offsetsYForSide[l] * f);
    210            this.maxZ = block.getBlockBoundsMaxZ() - (double)((float)Facing.offsetsZForSide[l] * f);
    211        }
    212    }
    213
    214    public AxisAlignedBB getAxisAlignedBB(World par1World, int par2, int par3, int par4, int par5, float par6, int par7)
    215    {
    216        if (par5 != 0 && par5 != this.blockID)
    217        {
    218            AxisAlignedBB axisalignedbb = Block.blocksList[par5].getCollisionBoundingBoxFromPool(par1World, par2, par3, par4);
    219
    220            if (axisalignedbb == null)
    221            {
    222                return null;
    223            }
    224            else
    225            {
    226                if (Facing.offsetsXForSide[par7] < 0)
    227                {
    228                    axisalignedbb.minX -= (double)((float)Facing.offsetsXForSide[par7] * par6);
    229                }
    230                else
    231                {
    232                    axisalignedbb.maxX -= (double)((float)Facing.offsetsXForSide[par7] * par6);
    233                }
    234
    235                if (Facing.offsetsYForSide[par7] < 0)
    236                {
    237                    axisalignedbb.minY -= (double)((float)Facing.offsetsYForSide[par7] * par6);
    238                }
    239                else
    240                {
    241                    axisalignedbb.maxY -= (double)((float)Facing.offsetsYForSide[par7] * par6);
    242                }
    243
    244                if (Facing.offsetsZForSide[par7] < 0)
    245                {
    246                    axisalignedbb.minZ -= (double)((float)Facing.offsetsZForSide[par7] * par6);
    247                }
    248                else
    249                {
    250                    axisalignedbb.maxZ -= (double)((float)Facing.offsetsZForSide[par7] * par6);
    251                }
    252
    253                return axisalignedbb;
    254            }
    255        }
    256        else
    257        {
    258            return null;
    259        }
    260    }
    261
    262    /**
    263     * gets the piston tile entity at the specified location
    264     */
    265    private TileEntityPiston getTileEntityAtLocation(IBlockAccess par1IBlockAccess, int par2, int par3, int par4)
    266    {
    267        TileEntity tileentity = par1IBlockAccess.getBlockTileEntity(par2, par3, par4);
    268        return tileentity instanceof TileEntityPiston ? (TileEntityPiston)tileentity : null;
    269    }
    270
    271    @SideOnly(Side.CLIENT)
    272
    273    /**
    274     * only called by clickMiddleMouseButton , and passed to inventory.setCurrentItem (along with isCreative)
    275     */
    276    public int idPicked(World par1World, int par2, int par3, int par4)
    277    {
    278        return 0;
    279    }
    280
    281    @SideOnly(Side.CLIENT)
    282
    283    /**
    284     * When this method is called, your block should register all the icons it needs with the given IconRegister. This
    285     * is the only chance you get to register icons.
    286     */
    287    public void registerIcons(IconRegister par1IconRegister)
    288    {
    289        this.blockIcon = par1IconRegister.registerIcon("piston_top");
    290    }
    291}