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