001 package net.minecraft.src; 002 003 import cpw.mods.fml.common.Side; 004 import cpw.mods.fml.common.asm.SideOnly; 005 import java.util.List; 006 007 public class ItemBlock extends Item 008 { 009 /** The block ID of the Block associated with this ItemBlock */ 010 private int blockID; 011 012 public ItemBlock(int par1) 013 { 014 super(par1); 015 this.blockID = par1 + 256; 016 this.setIconIndex(Block.blocksList[par1 + 256].getBlockTextureFromSide(2)); 017 isDefaultTexture = Block.blocksList[par1 + 256].isDefaultTexture; 018 } 019 020 /** 021 * Returns the blockID for this Item 022 */ 023 public int getBlockID() 024 { 025 return this.blockID; 026 } 027 028 /** 029 * Callback for item usage. If the item does something special on right clicking, he will have one of those. Return 030 * True if something happen and false if it don't. This is for ITEMS, not BLOCKS 031 */ 032 public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10) 033 { 034 int var11 = par3World.getBlockId(par4, par5, par6); 035 036 if (var11 == Block.snow.blockID) 037 { 038 par7 = 1; 039 } 040 else if (var11 != Block.vine.blockID && var11 != Block.tallGrass.blockID && var11 != Block.deadBush.blockID 041 && (Block.blocksList[var11] == null || !Block.blocksList[var11].isBlockReplaceable(par3World, par4, par5, par6))) 042 { 043 if (par7 == 0) 044 { 045 --par5; 046 } 047 048 if (par7 == 1) 049 { 050 ++par5; 051 } 052 053 if (par7 == 2) 054 { 055 --par6; 056 } 057 058 if (par7 == 3) 059 { 060 ++par6; 061 } 062 063 if (par7 == 4) 064 { 065 --par4; 066 } 067 068 if (par7 == 5) 069 { 070 ++par4; 071 } 072 } 073 074 if (par1ItemStack.stackSize == 0) 075 { 076 return false; 077 } 078 else if (!par2EntityPlayer.canPlayerEdit(par4, par5, par6)) 079 { 080 return false; 081 } 082 else if (par5 == 255 && Block.blocksList[this.blockID].blockMaterial.isSolid()) 083 { 084 return false; 085 } 086 else if (par3World.canPlaceEntityOnSide(this.blockID, par4, par5, par6, false, par7, par2EntityPlayer)) 087 { 088 Block var12 = Block.blocksList[this.blockID]; 089 090 if (placeBlockAt(par1ItemStack, par2EntityPlayer, par3World, par4, par5, par6, par7, par8, par9, par10)) 091 { 092 par3World.playSoundEffect((double)((float)par4 + 0.5F), (double)((float)par5 + 0.5F), (double)((float)par6 + 0.5F), var12.stepSound.getStepSound(), (var12.stepSound.getVolume() + 1.0F) / 2.0F, var12.stepSound.getPitch() * 0.8F); 093 --par1ItemStack.stackSize; 094 } 095 096 return true; 097 } 098 else 099 { 100 return false; 101 } 102 } 103 104 @SideOnly(Side.CLIENT) 105 106 /** 107 * Returns true if the given ItemBlock can be placed on the given side of the given block position. 108 */ 109 public boolean canPlaceItemBlockOnSide(World par1World, int par2, int par3, int par4, int par5, EntityPlayer par6EntityPlayer, ItemStack par7ItemStack) 110 { 111 int var8 = par1World.getBlockId(par2, par3, par4); 112 113 if (var8 == Block.snow.blockID) 114 { 115 par5 = 1; 116 } 117 else if (var8 != Block.vine.blockID && var8 != Block.tallGrass.blockID && var8 != Block.deadBush.blockID 118 && (Block.blocksList[var8] == null || !Block.blocksList[var8].isBlockReplaceable(par1World, par2, par3, par4))) 119 { 120 if (par5 == 0) 121 { 122 --par3; 123 } 124 125 if (par5 == 1) 126 { 127 ++par3; 128 } 129 130 if (par5 == 2) 131 { 132 --par4; 133 } 134 135 if (par5 == 3) 136 { 137 ++par4; 138 } 139 140 if (par5 == 4) 141 { 142 --par2; 143 } 144 145 if (par5 == 5) 146 { 147 ++par2; 148 } 149 } 150 151 return par1World.canPlaceEntityOnSide(this.getBlockID(), par2, par3, par4, false, par5, (Entity)null); 152 } 153 154 public String getItemNameIS(ItemStack par1ItemStack) 155 { 156 return Block.blocksList[this.blockID].getBlockName(); 157 } 158 159 public String getItemName() 160 { 161 return Block.blocksList[this.blockID].getBlockName(); 162 } 163 164 @SideOnly(Side.CLIENT) 165 166 /** 167 * gets the CreativeTab this item is displayed on 168 */ 169 public CreativeTabs getCreativeTab() 170 { 171 return Block.blocksList[this.blockID].getCreativeTabToDisplayOn(); 172 } 173 174 @SideOnly(Side.CLIENT) 175 176 /** 177 * returns a list of items with the same ID, but different meta (eg: dye returns 16 items) 178 */ 179 public void getSubItems(int par1, CreativeTabs par2CreativeTabs, List par3List) 180 { 181 Block.blocksList[this.blockID].getSubBlocks(par1, par2CreativeTabs, par3List); 182 } 183 184 /** 185 * Called to actually place the block, after the location is determined 186 * and all permission checks have been made. 187 * 188 * @param stack The item stack that was used to place the block. This can be changed inside the method. 189 * @param player The player who is placing the block. Can be null if the block is not being placed by a player. 190 * @param side The side the player (or machine) right-clicked on. 191 */ 192 public boolean placeBlockAt(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ) 193 { 194 if (!world.setBlockAndMetadataWithNotify(x, y, z, this.blockID, this.getMetadata(stack.getItemDamage()))) 195 { 196 return false; 197 } 198 199 if (world.getBlockId(x, y, z) == this.blockID) 200 { 201 Block.blocksList[this.blockID].updateBlockMetadata(world, x, y, z, side, hitX, hitY, hitZ); 202 Block.blocksList[this.blockID].onBlockPlacedBy(world, x, y, z, player); 203 } 204 205 return true; 206 } 207 }