001    package net.minecraft.src;
002    
003    public class BlockFenceGate extends BlockDirectional
004    {
005        public BlockFenceGate(int par1, int par2)
006        {
007            super(par1, par2, Material.wood);
008            this.setCreativeTab(CreativeTabs.tabRedstone);
009        }
010    
011        /**
012         * Checks to see if its valid to put this block at the specified coordinates. Args: world, x, y, z
013         */
014        public boolean canPlaceBlockAt(World par1World, int par2, int par3, int par4)
015        {
016            return !par1World.getBlockMaterial(par2, par3 - 1, par4).isSolid() ? false : super.canPlaceBlockAt(par1World, par2, par3, par4);
017        }
018    
019        /**
020         * Returns a bounding box from the pool of bounding boxes (this means this box can change after the pool has been
021         * cleared to be reused)
022         */
023        public AxisAlignedBB getCollisionBoundingBoxFromPool(World par1World, int par2, int par3, int par4)
024        {
025            int var5 = par1World.getBlockMetadata(par2, par3, par4);
026            return isFenceGateOpen(var5) ? null : (var5 != 2 && var5 != 0 ? AxisAlignedBB.getAABBPool().addOrModifyAABBInPool((double)((float)par2 + 0.375F), (double)par3, (double)par4, (double)((float)par2 + 0.625F), (double)((float)par3 + 1.5F), (double)(par4 + 1)) : AxisAlignedBB.getAABBPool().addOrModifyAABBInPool((double)par2, (double)par3, (double)((float)par4 + 0.375F), (double)(par2 + 1), (double)((float)par3 + 1.5F), (double)((float)par4 + 0.625F)));
027        }
028    
029        /**
030         * Updates the blocks bounds based on its current state. Args: world, x, y, z
031         */
032        public void setBlockBoundsBasedOnState(IBlockAccess par1IBlockAccess, int par2, int par3, int par4)
033        {
034            int var5 = getDirection(par1IBlockAccess.getBlockMetadata(par2, par3, par4));
035    
036            if (var5 != 2 && var5 != 0)
037            {
038                this.setBlockBounds(0.375F, 0.0F, 0.0F, 0.625F, 1.0F, 1.0F);
039            }
040            else
041            {
042                this.setBlockBounds(0.0F, 0.0F, 0.375F, 1.0F, 1.0F, 0.625F);
043            }
044        }
045    
046        /**
047         * Is this block (a) opaque and (b) a full 1m cube?  This determines whether or not to render the shared face of two
048         * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
049         */
050        public boolean isOpaqueCube()
051        {
052            return false;
053        }
054    
055        /**
056         * If this block doesn't render as an ordinary block it will return False (examples: signs, buttons, stairs, etc)
057         */
058        public boolean renderAsNormalBlock()
059        {
060            return false;
061        }
062    
063        public boolean getBlocksMovement(IBlockAccess par1IBlockAccess, int par2, int par3, int par4)
064        {
065            return isFenceGateOpen(par1IBlockAccess.getBlockMetadata(par2, par3, par4));
066        }
067    
068        /**
069         * The type of render function that is called for this block
070         */
071        public int getRenderType()
072        {
073            return 21;
074        }
075    
076        /**
077         * Called when the block is placed in the world.
078         */
079        public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLiving par5EntityLiving)
080        {
081            int var6 = (MathHelper.floor_double((double)(par5EntityLiving.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3) % 4;
082            par1World.setBlockMetadataWithNotify(par2, par3, par4, var6);
083        }
084    
085        /**
086         * Called upon block activation (right click on the block.)
087         */
088        public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
089        {
090            int var10 = par1World.getBlockMetadata(par2, par3, par4);
091    
092            if (isFenceGateOpen(var10))
093            {
094                par1World.setBlockMetadataWithNotify(par2, par3, par4, var10 & -5);
095            }
096            else
097            {
098                int var11 = (MathHelper.floor_double((double)(par5EntityPlayer.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3) % 4;
099                int var12 = getDirection(var10);
100    
101                if (var12 == (var11 + 2) % 4)
102                {
103                    var10 = var11;
104                }
105    
106                par1World.setBlockMetadataWithNotify(par2, par3, par4, var10 | 4);
107            }
108    
109            par1World.playAuxSFXAtEntity(par5EntityPlayer, 1003, par2, par3, par4, 0);
110            return true;
111        }
112    
113        /**
114         * Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are
115         * their own) Args: x, y, z, neighbor blockID
116         */
117        public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5)
118        {
119            if (!par1World.isRemote)
120            {
121                int var6 = par1World.getBlockMetadata(par2, par3, par4);
122                boolean var7 = par1World.isBlockIndirectlyGettingPowered(par2, par3, par4);
123    
124                if (var7 || par5 > 0 && Block.blocksList[par5].canProvidePower() || par5 == 0)
125                {
126                    if (var7 && !isFenceGateOpen(var6))
127                    {
128                        par1World.setBlockMetadataWithNotify(par2, par3, par4, var6 | 4);
129                        par1World.playAuxSFXAtEntity((EntityPlayer)null, 1003, par2, par3, par4, 0);
130                    }
131                    else if (!var7 && isFenceGateOpen(var6))
132                    {
133                        par1World.setBlockMetadataWithNotify(par2, par3, par4, var6 & -5);
134                        par1World.playAuxSFXAtEntity((EntityPlayer)null, 1003, par2, par3, par4, 0);
135                    }
136                }
137            }
138        }
139    
140        /**
141         * Returns if the fence gate is open according to its metadata.
142         */
143        public static boolean isFenceGateOpen(int par0)
144        {
145            return (par0 & 4) != 0;
146        }
147    }