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.field_94336_cN)); 095 } 096 097 @SideOnly(Side.CLIENT) 098 public void func_94332_a(IconRegister par1IconRegister) 099 { 100 this.field_94336_cN = par1IconRegister.func_94245_a("cake_side"); 101 this.field_94382_c = par1IconRegister.func_94245_a("cake_inner"); 102 this.field_94383_a = par1IconRegister.func_94245_a("cake_top"); 103 this.field_94381_b = par1IconRegister.func_94245_a("cake_bottom"); 104 } 105 106 /** 107 * Is this block (a) opaque and (b) a full 1m cube? This determines whether or not to render the shared face of two 108 * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block. 109 */ 110 public boolean isOpaqueCube() 111 { 112 return false; 113 } 114 115 /** 116 * Called upon block activation (right click on the block.) 117 */ 118 public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9) 119 { 120 this.eatCakeSlice(par1World, par2, par3, par4, par5EntityPlayer); 121 return true; 122 } 123 124 /** 125 * Called when the block is clicked by a player. Args: x, y, z, entityPlayer 126 */ 127 public void onBlockClicked(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer) 128 { 129 this.eatCakeSlice(par1World, par2, par3, par4, par5EntityPlayer); 130 } 131 132 /** 133 * Heals the player and removes a slice from the cake. 134 */ 135 private void eatCakeSlice(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer) 136 { 137 if (par5EntityPlayer.canEat(false)) 138 { 139 par5EntityPlayer.getFoodStats().addStats(2, 0.1F); 140 int l = par1World.getBlockMetadata(par2, par3, par4) + 1; 141 142 if (l >= 6) 143 { 144 par1World.func_94571_i(par2, par3, par4); 145 } 146 else 147 { 148 par1World.setBlockMetadataWithNotify(par2, par3, par4, l, 2); 149 } 150 } 151 } 152 153 /** 154 * Checks to see if its valid to put this block at the specified coordinates. Args: world, x, y, z 155 */ 156 public boolean canPlaceBlockAt(World par1World, int par2, int par3, int par4) 157 { 158 return !super.canPlaceBlockAt(par1World, par2, par3, par4) ? false : this.canBlockStay(par1World, par2, par3, par4); 159 } 160 161 /** 162 * Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are 163 * their own) Args: x, y, z, neighbor blockID 164 */ 165 public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5) 166 { 167 if (!this.canBlockStay(par1World, par2, par3, par4)) 168 { 169 par1World.func_94571_i(par2, par3, par4); 170 } 171 } 172 173 /** 174 * Can this block stay at this position. Similar to canPlaceBlockAt except gets checked often with plants. 175 */ 176 public boolean canBlockStay(World par1World, int par2, int par3, int par4) 177 { 178 return par1World.getBlockMaterial(par2, par3 - 1, par4).isSolid(); 179 } 180 181 /** 182 * Returns the quantity of items to drop on block destruction. 183 */ 184 public int quantityDropped(Random par1Random) 185 { 186 return 0; 187 } 188 189 /** 190 * Returns the ID of the items to drop on destruction. 191 */ 192 public int idDropped(int par1, Random par2Random, int par3) 193 { 194 return 0; 195 } 196 197 @SideOnly(Side.CLIENT) 198 199 /** 200 * only called by clickMiddleMouseButton , and passed to inventory.setCurrentItem (along with isCreative) 201 */ 202 public int idPicked(World par1World, int par2, int par3, int par4) 203 { 204 return Item.cake.itemID; 205 } 206}