    001package net.minecraft.block;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.util.List;
    006import java.util.Random;
    007import net.minecraft.block.material.Material;
    008import net.minecraft.client.renderer.texture.IconRegister;
    009import net.minecraft.entity.Entity;
    010import net.minecraft.util.AxisAlignedBB;
    011import net.minecraft.util.Facing;
    012import net.minecraft.util.Icon;
    013import net.minecraft.world.IBlockAccess;
    014import net.minecraft.world.World;
    015
    016public class BlockPistonExtension extends Block
    017{
    018    /** The texture for the 'head' of the piston. Sticky or normal. */
    019    private Icon headTexture = null;
    020
    021    public BlockPistonExtension(int par1)
    022    {
    023        super(par1, Material.piston);
    024        this.setStepSound(soundStoneFootstep);
    025        this.setHardness(0.5F);
    026    }
    027
    028    @SideOnly(Side.CLIENT)
    029    public void setHeadTexture(Icon par1Icon)
    030    {
    031        this.headTexture = par1Icon;
    032    }
    033
    034    /**
    035     * ejects contained items into the world, and notifies neighbours of an update, as appropriate
    036     */
    037    public void breakBlock(World par1World, int par2, int par3, int par4, int par5, int par6)
    038    {
    039        super.breakBlock(par1World, par2, par3, par4, par5, par6);
    040        int j1 = Facing.oppositeSide[getDirectionMeta(par6)];
    041        par2 += Facing.offsetsXForSide[j1];
    042        par3 += Facing.offsetsYForSide[j1];
    043        par4 += Facing.offsetsZForSide[j1];
    044        int k1 = par1World.getBlockId(par2, par3, par4);
    045
    046        if (k1 == Block.pistonBase.blockID || k1 == Block.pistonStickyBase.blockID)
    047        {
    048            par6 = par1World.getBlockMetadata(par2, par3, par4);
    049
    050            if (BlockPistonBase.isExtended(par6))
    051            {
    052                Block.blocksList[k1].dropBlockAsItem(par1World, par2, par3, par4, par6, 0);
    053                par1World.setBlockToAir(par2, par3, par4);
    054            }
    055        }
    056    }
    057
    058    @SideOnly(Side.CLIENT)
    059    public void clearHeadTexture()
    060    {
    061        this.headTexture = null;
    062    }
    063
    064    @SideOnly(Side.CLIENT)
    065
    066    /**
    067     * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
    068     */
    069    public Icon getIcon(int par1, int par2)
    070    {
    071        int k = getDirectionMeta(par2);
    072        return par1 == k ? (this.headTexture != null ? this.headTexture : ((par2 & 8) != 0 ? BlockPistonBase.func_94496_b("piston_top_sticky") : BlockPistonBase.func_94496_b("piston_top"))) : (k < 6 && par1 == Facing.oppositeSide[k] ? BlockPistonBase.func_94496_b("piston_top") : BlockPistonBase.func_94496_b("piston_side"));
    073    }
    074
    075    @SideOnly(Side.CLIENT)
    076
    077    /**
    078     * When this method is called, your block should register all the icons it needs with the given IconRegister. This
    079     * is the only chance you get to register icons.
    080     */
    081    public void registerIcons(IconRegister par1IconRegister) {}
    082
    083    /**
    084     * The type of render function that is called for this block
    085     */
    086    public int getRenderType()
    087    {
    088        return 17;
    089    }
    090
    091    /**
    092     * Is this block (a) opaque and (b) a full 1m cube?  This determines whether or not to render the shared face of two
    093     * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
    094     */
    095    public boolean isOpaqueCube()
    096    {
    097        return false;
    098    }
    099
    100    /**
    101     * If this block doesn't render as an ordinary block it will return False (examples: signs, buttons, stairs, etc)
    102     */
    103    public boolean renderAsNormalBlock()
    104    {
    105        return false;
    106    }
    107
    108    /**
    109     * Checks to see if its valid to put this block at the specified coordinates. Args: world, x, y, z
    110     */
    111    public boolean canPlaceBlockAt(World par1World, int par2, int par3, int par4)
    112    {
    113        return false;
    114    }
    115
    116    /**
    117     * checks to see if you can place this block can be placed on that side of a block: BlockLever overrides
    118     */
    119    public boolean canPlaceBlockOnSide(World par1World, int par2, int par3, int par4, int par5)
    120    {
    121        return false;
    122    }
    123
    124    /**
    125     * Returns the quantity of items to drop on block destruction.
    126     */
    127    public int quantityDropped(Random par1Random)
    128    {
    129        return 0;
    130    }
    131
    132    /**
    133     * Adds all intersecting collision boxes to a list. (Be sure to only add boxes to the list if they intersect the
    134     * mask.) Parameters: World, X, Y, Z, mask, list, colliding entity
    135     */
    136    public void addCollisionBoxesToList(World par1World, int par2, int par3, int par4, AxisAlignedBB par5AxisAlignedBB, List par6List, Entity par7Entity)
    137    {
    138        int l = par1World.getBlockMetadata(par2, par3, par4);
    139
    140        switch (getDirectionMeta(l))
    141        {
    142            case 0:
    143                this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.25F, 1.0F);
    144                super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
    145                this.setBlockBounds(0.375F, 0.25F, 0.375F, 0.625F, 1.0F, 0.625F);
    146                super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
    147                break;
    148            case 1:
    149                this.setBlockBounds(0.0F, 0.75F, 0.0F, 1.0F, 1.0F, 1.0F);
    150                super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
    151                this.setBlockBounds(0.375F, 0.0F, 0.375F, 0.625F, 0.75F, 0.625F);
    152                super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
    153                break;
    154            case 2:
    155                this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 0.25F);
    156                super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
    157                this.setBlockBounds(0.25F, 0.375F, 0.25F, 0.75F, 0.625F, 1.0F);
    158                super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
    159                break;
    160            case 3:
    161                this.setBlockBounds(0.0F, 0.0F, 0.75F, 1.0F, 1.0F, 1.0F);
    162                super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
    163                this.setBlockBounds(0.25F, 0.375F, 0.0F, 0.75F, 0.625F, 0.75F);
    164                super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
    165                break;
    166            case 4:
    167                this.setBlockBounds(0.0F, 0.0F, 0.0F, 0.25F, 1.0F, 1.0F);
    168                super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
    169                this.setBlockBounds(0.375F, 0.25F, 0.25F, 0.625F, 0.75F, 1.0F);
    170                super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
    171                break;
    172            case 5:
    173                this.setBlockBounds(0.75F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
    174                super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
    175                this.setBlockBounds(0.0F, 0.375F, 0.25F, 0.75F, 0.625F, 0.75F);
    176                super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
    177        }
    178
    179        this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
    180    }
    181
    182    /**
    183     * Updates the blocks bounds based on its current state. Args: world, x, y, z
    184     */
    185    public void setBlockBoundsBasedOnState(IBlockAccess par1IBlockAccess, int par2, int par3, int par4)
    186    {
    187        int l = par1IBlockAccess.getBlockMetadata(par2, par3, par4);
    188
    189        switch (getDirectionMeta(l))
    190        {
    191            case 0:
    192                this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.25F, 1.0F);
    193                break;
    194            case 1:
    195                this.setBlockBounds(0.0F, 0.75F, 0.0F, 1.0F, 1.0F, 1.0F);
    196                break;
    197            case 2:
    198                this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 0.25F);
    199                break;
    200            case 3:
    201                this.setBlockBounds(0.0F, 0.0F, 0.75F, 1.0F, 1.0F, 1.0F);
    202                break;
    203            case 4:
    204                this.setBlockBounds(0.0F, 0.0F, 0.0F, 0.25F, 1.0F, 1.0F);
    205                break;
    206            case 5:
    207                this.setBlockBounds(0.75F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
    208        }
    209    }
    210
    211    /**
    212     * Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are
    213     * their own) Args: x, y, z, neighbor blockID
    214     */
    215    public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5)
    216    {
    217        int i1 = getDirectionMeta(par1World.getBlockMetadata(par2, par3, par4));
    218        int j1 = par1World.getBlockId(par2 - Facing.offsetsXForSide[i1], par3 - Facing.offsetsYForSide[i1], par4 - Facing.offsetsZForSide[i1]);
    219
    220        if (j1 != Block.pistonBase.blockID && j1 != Block.pistonStickyBase.blockID)
    221        {
    222            par1World.setBlockToAir(par2, par3, par4);
    223        }
    224        else
    225        {
    226            Block.blocksList[j1].onNeighborBlockChange(par1World, par2 - Facing.offsetsXForSide[i1], par3 - Facing.offsetsYForSide[i1], par4 - Facing.offsetsZForSide[i1], par5);
    227        }
    228    }
    229
    230    public static int getDirectionMeta(int par0)
    231    {
    232        return par0 & 7;
    233    }
    234
    235    @SideOnly(Side.CLIENT)
    236
    237    /**
    238     * only called by clickMiddleMouseButton , and passed to inventory.setCurrentItem (along with isCreative)
    239     */
    240    public int idPicked(World par1World, int par2, int par3, int par4)
    241    {
    242        return 0;
    243    }
    244}