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.item.Item; 010import net.minecraft.util.AxisAlignedBB; 011import net.minecraft.util.Icon; 012import net.minecraft.world.IBlockAccess; 013import net.minecraft.world.World; 014 015public class BlockCake extends Block 016{ 017 @SideOnly(Side.CLIENT) 018 private Icon field_94383_a; 019 @SideOnly(Side.CLIENT) 020 private Icon field_94381_b; 021 @SideOnly(Side.CLIENT) 022 private Icon field_94382_c; 023 024 protected BlockCake(int par1) 025 { 026 super(par1, Material.cake); 027 this.setTickRandomly(true); 028 } 029 030 /** 031 * Updates the blocks bounds based on its current state. Args: world, x, y, z 032 */ 033 public void setBlockBoundsBasedOnState(IBlockAccess par1IBlockAccess, int par2, int par3, int par4) 034 { 035 int l = par1IBlockAccess.getBlockMetadata(par2, par3, par4); 036 float f = 0.0625F; 037 float f1 = (float)(1 + l * 2) / 16.0F; 038 float f2 = 0.5F; 039 this.setBlockBounds(f1, 0.0F, f, 1.0F - f, f2, 1.0F - f); 040 } 041 042 /** 043 * Sets the block's bounds for rendering it as an item 044 */ 045 public void setBlockBoundsForItemRender() 046 { 047 float f = 0.0625F; 048 float f1 = 0.5F; 049 this.setBlockBounds(f, 0.0F, f, 1.0F - f, f1, 1.0F - f); 050 } 051 052 /** 053 * Returns a bounding box from the pool of bounding boxes (this means this box can change after the pool has been 054 * cleared to be reused) 055 */ 056 public AxisAlignedBB getCollisionBoundingBoxFromPool(World par1World, int par2, int par3, int par4) 057 { 058 int l = par1World.getBlockMetadata(par2, par3, par4); 059 float f = 0.0625F; 060 float f1 = (float)(1 + l * 2) / 16.0F; 061 float f2 = 0.5F; 062 return AxisAlignedBB.getAABBPool().getAABB((double)((float)par2 + f1), (double)par3, (double)((float)par4 + f), (double)((float)(par2 + 1) - f), (double)((float)par3 + f2 - f), (double)((float)(par4 + 1) - f)); 063 } 064 065 /** 066 * If this block doesn't render as an ordinary block it will return False (examples: signs, buttons, stairs, etc) 067 */ 068 public boolean renderAsNormalBlock() 069 { 070 return false; 071 } 072 073 @SideOnly(Side.CLIENT) 074 075 /** 076 * Returns the bounding box of the wired rectangular prism to render. 077 */ 078 public AxisAlignedBB getSelectedBoundingBoxFromPool(World par1World, int par2, int par3, int par4) 079 { 080 int l = par1World.getBlockMetadata(par2, par3, par4); 081 float f = 0.0625F; 082 float f1 = (float)(1 + l * 2) / 16.0F; 083 float f2 = 0.5F; 084 return AxisAlignedBB.getAABBPool().getAABB((double)((float)par2 + f1), (double)par3, (double)((float)par4 + f), (double)((float)(par2 + 1) - f), (double)((float)par3 + f2), (double)((float)(par4 + 1) - f)); 085 } 086 087 @SideOnly(Side.CLIENT) 088 089 /** 090 * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata 091 */ 092 public Icon getBlockTextureFromSideAndMetadata(int par1, int par2) 093 { 094 return par1 == 1 ? this.field_94383_a : (par1 == 0 ? this.field_94381_b : (par2 > 0 && par1 == 4 ? this.field_94382_c : this.blockIcon)); 095 } 096 097 @SideOnly(Side.CLIENT) 098 099 /** 100 * When this method is called, your block should register all the icons it needs with the given IconRegister. This 101 * is the only chance you get to register icons. 102 */ 103 public void registerIcons(IconRegister par1IconRegister) 104 { 105 this.blockIcon = par1IconRegister.registerIcon("cake_side"); 106 this.field_94382_c = par1IconRegister.registerIcon("cake_inner"); 107 this.field_94383_a = par1IconRegister.registerIcon("cake_top"); 108 this.field_94381_b = par1IconRegister.registerIcon("cake_bottom"); 109 } 110 111 /** 112 * Is this block (a) opaque and (b) a full 1m cube? This determines whether or not to render the shared face of two 113 * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block. 114 */ 115 public boolean isOpaqueCube() 116 { 117 return false; 118 } 119 120 /** 121 * Called upon block activation (right click on the block.) 122 */ 123 public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9) 124 { 125 this.eatCakeSlice(par1World, par2, par3, par4, par5EntityPlayer); 126 return true; 127 } 128 129 /** 130 * Called when the block is clicked by a player. Args: x, y, z, entityPlayer 131 */ 132 public void onBlockClicked(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer) 133 { 134 this.eatCakeSlice(par1World, par2, par3, par4, par5EntityPlayer); 135 } 136 137 /** 138 * Heals the player and removes a slice from the cake. 139 */ 140 private void eatCakeSlice(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer) 141 { 142 if (par5EntityPlayer.canEat(false)) 143 { 144 par5EntityPlayer.getFoodStats().addStats(2, 0.1F); 145 int l = par1World.getBlockMetadata(par2, par3, par4) + 1; 146 147 if (l >= 6) 148 { 149 par1World.setBlockToAir(par2, par3, par4); 150 } 151 else 152 { 153 par1World.setBlockMetadataWithNotify(par2, par3, par4, l, 2); 154 } 155 } 156 } 157 158 /** 159 * Checks to see if its valid to put this block at the specified coordinates. Args: world, x, y, z 160 */ 161 public boolean canPlaceBlockAt(World par1World, int par2, int par3, int par4) 162 { 163 return !super.canPlaceBlockAt(par1World, par2, par3, par4) ? false : this.canBlockStay(par1World, par2, par3, par4); 164 } 165 166 /** 167 * Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are 168 * their own) Args: x, y, z, neighbor blockID 169 */ 170 public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5) 171 { 172 if (!this.canBlockStay(par1World, par2, par3, par4)) 173 { 174 par1World.setBlockToAir(par2, par3, par4); 175 } 176 } 177 178 /** 179 * Can this block stay at this position. Similar to canPlaceBlockAt except gets checked often with plants. 180 */ 181 public boolean canBlockStay(World par1World, int par2, int par3, int par4) 182 { 183 return par1World.getBlockMaterial(par2, par3 - 1, par4).isSolid(); 184 } 185 186 /** 187 * Returns the quantity of items to drop on block destruction. 188 */ 189 public int quantityDropped(Random par1Random) 190 { 191 return 0; 192 } 193 194 /** 195 * Returns the ID of the items to drop on destruction. 196 */ 197 public int idDropped(int par1, Random par2Random, int par3) 198 { 199 return 0; 200 } 201 202 @SideOnly(Side.CLIENT) 203 204 /** 205 * only called by clickMiddleMouseButton , and passed to inventory.setCurrentItem (along with isCreative) 206 */ 207 public int idPicked(World par1World, int par2, int par3, int par4) 208 { 209 return Item.cake.itemID; 210 } 211}