001package net.minecraft.block; 002 003import cpw.mods.fml.relauncher.Side; 004import cpw.mods.fml.relauncher.SideOnly; 005import net.minecraft.block.material.Material; 006import net.minecraft.client.renderer.texture.IconRegister; 007import net.minecraft.creativetab.CreativeTabs; 008import net.minecraft.entity.EntityLiving; 009import net.minecraft.entity.player.EntityPlayer; 010import net.minecraft.item.ItemStack; 011import net.minecraft.util.AxisAlignedBB; 012import net.minecraft.util.Icon; 013import net.minecraft.util.MathHelper; 014import net.minecraft.world.IBlockAccess; 015import net.minecraft.world.World; 016 017public class BlockFenceGate extends BlockDirectional 018{ 019 public BlockFenceGate(int par1) 020 { 021 super(par1, Material.wood); 022 this.setCreativeTab(CreativeTabs.tabRedstone); 023 } 024 025 @SideOnly(Side.CLIENT) 026 027 /** 028 * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata 029 */ 030 public Icon getIcon(int par1, int par2) 031 { 032 return Block.planks.getBlockTextureFromSide(par1); 033 } 034 035 /** 036 * Checks to see if its valid to put this block at the specified coordinates. Args: world, x, y, z 037 */ 038 public boolean canPlaceBlockAt(World par1World, int par2, int par3, int par4) 039 { 040 return !par1World.getBlockMaterial(par2, par3 - 1, par4).isSolid() ? false : super.canPlaceBlockAt(par1World, par2, par3, par4); 041 } 042 043 /** 044 * Returns a bounding box from the pool of bounding boxes (this means this box can change after the pool has been 045 * cleared to be reused) 046 */ 047 public AxisAlignedBB getCollisionBoundingBoxFromPool(World par1World, int par2, int par3, int par4) 048 { 049 int l = par1World.getBlockMetadata(par2, par3, par4); 050 return isFenceGateOpen(l) ? null : (l != 2 && l != 0 ? AxisAlignedBB.getAABBPool().getAABB((double)((float)par2 + 0.375F), (double)par3, (double)par4, (double)((float)par2 + 0.625F), (double)((float)par3 + 1.5F), (double)(par4 + 1)) : AxisAlignedBB.getAABBPool().getAABB((double)par2, (double)par3, (double)((float)par4 + 0.375F), (double)(par2 + 1), (double)((float)par3 + 1.5F), (double)((float)par4 + 0.625F))); 051 } 052 053 /** 054 * Updates the blocks bounds based on its current state. Args: world, x, y, z 055 */ 056 public void setBlockBoundsBasedOnState(IBlockAccess par1IBlockAccess, int par2, int par3, int par4) 057 { 058 int l = getDirection(par1IBlockAccess.getBlockMetadata(par2, par3, par4)); 059 060 if (l != 2 && l != 0) 061 { 062 this.setBlockBounds(0.375F, 0.0F, 0.0F, 0.625F, 1.0F, 1.0F); 063 } 064 else 065 { 066 this.setBlockBounds(0.0F, 0.0F, 0.375F, 1.0F, 1.0F, 0.625F); 067 } 068 } 069 070 /** 071 * Is this block (a) opaque and (b) a full 1m cube? This determines whether or not to render the shared face of two 072 * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block. 073 */ 074 public boolean isOpaqueCube() 075 { 076 return false; 077 } 078 079 /** 080 * If this block doesn't render as an ordinary block it will return False (examples: signs, buttons, stairs, etc) 081 */ 082 public boolean renderAsNormalBlock() 083 { 084 return false; 085 } 086 087 public boolean getBlocksMovement(IBlockAccess par1IBlockAccess, int par2, int par3, int par4) 088 { 089 return isFenceGateOpen(par1IBlockAccess.getBlockMetadata(par2, par3, par4)); 090 } 091 092 /** 093 * The type of render function that is called for this block 094 */ 095 public int getRenderType() 096 { 097 return 21; 098 } 099 100 /** 101 * Called when the block is placed in the world. 102 */ 103 public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLiving par5EntityLiving, ItemStack par6ItemStack) 104 { 105 int l = (MathHelper.floor_double((double)(par5EntityLiving.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3) % 4; 106 par1World.setBlockMetadataWithNotify(par2, par3, par4, l, 2); 107 } 108 109 /** 110 * Called upon block activation (right click on the block.) 111 */ 112 public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9) 113 { 114 int i1 = par1World.getBlockMetadata(par2, par3, par4); 115 116 if (isFenceGateOpen(i1)) 117 { 118 par1World.setBlockMetadataWithNotify(par2, par3, par4, i1 & -5, 2); 119 } 120 else 121 { 122 int j1 = (MathHelper.floor_double((double)(par5EntityPlayer.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3) % 4; 123 int k1 = getDirection(i1); 124 125 if (k1 == (j1 + 2) % 4) 126 { 127 i1 = j1; 128 } 129 130 par1World.setBlockMetadataWithNotify(par2, par3, par4, i1 | 4, 2); 131 } 132 133 par1World.playAuxSFXAtEntity(par5EntityPlayer, 1003, par2, par3, par4, 0); 134 return true; 135 } 136 137 /** 138 * Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are 139 * their own) Args: x, y, z, neighbor blockID 140 */ 141 public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5) 142 { 143 if (!par1World.isRemote) 144 { 145 int i1 = par1World.getBlockMetadata(par2, par3, par4); 146 boolean flag = par1World.isBlockIndirectlyGettingPowered(par2, par3, par4); 147 148 if (flag || par5 > 0 && Block.blocksList[par5].canProvidePower()) 149 { 150 if (flag && !isFenceGateOpen(i1)) 151 { 152 par1World.setBlockMetadataWithNotify(par2, par3, par4, i1 | 4, 2); 153 par1World.playAuxSFXAtEntity((EntityPlayer)null, 1003, par2, par3, par4, 0); 154 } 155 else if (!flag && isFenceGateOpen(i1)) 156 { 157 par1World.setBlockMetadataWithNotify(par2, par3, par4, i1 & -5, 2); 158 par1World.playAuxSFXAtEntity((EntityPlayer)null, 1003, par2, par3, par4, 0); 159 } 160 } 161 } 162 } 163 164 /** 165 * Returns if the fence gate is open according to its metadata. 166 */ 167 public static boolean isFenceGateOpen(int par0) 168 { 169 return (par0 & 4) != 0; 170 } 171 172 @SideOnly(Side.CLIENT) 173 174 /** 175 * Returns true if the given side of this block type should be rendered, if the adjacent block is at the given 176 * coordinates. Args: blockAccess, x, y, z, side 177 */ 178 public boolean shouldSideBeRendered(IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5) 179 { 180 return true; 181 } 182 183 @SideOnly(Side.CLIENT) 184 185 /** 186 * When this method is called, your block should register all the icons it needs with the given IconRegister. This 187 * is the only chance you get to register icons. 188 */ 189 public void registerIcons(IconRegister par1IconRegister) {} 190}