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        public boolean tryPlaceIntoWorld(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
029        {
030            int var11 = par3World.getBlockId(par4, par5, par6);
031    
032            if (var11 == Block.snow.blockID)
033            {
034                par7 = 1;
035            }
036            else if (var11 != Block.vine.blockID && var11 != Block.tallGrass.blockID && var11 != Block.deadBush.blockID
037                    && (Block.blocksList[var11] == null || !Block.blocksList[var11].isBlockReplaceable(par3World, par4, par5, par6)))
038            {
039                if (par7 == 0)
040                {
041                    --par5;
042                }
043    
044                if (par7 == 1)
045                {
046                    ++par5;
047                }
048    
049                if (par7 == 2)
050                {
051                    --par6;
052                }
053    
054                if (par7 == 3)
055                {
056                    ++par6;
057                }
058    
059                if (par7 == 4)
060                {
061                    --par4;
062                }
063    
064                if (par7 == 5)
065                {
066                    ++par4;
067                }
068            }
069    
070            if (par1ItemStack.stackSize == 0)
071            {
072                return false;
073            }
074            else if (!par2EntityPlayer.canPlayerEdit(par4, par5, par6))
075            {
076                return false;
077            }
078            else if (par5 == 255 && Block.blocksList[this.blockID].blockMaterial.isSolid())
079            {
080                return false;
081            }
082            else if (par3World.canPlaceEntityOnSide(this.blockID, par4, par5, par6, false, par7, par2EntityPlayer))
083            {
084                Block var12 = Block.blocksList[this.blockID];
085    
086                if (placeBlockAt(par1ItemStack, par2EntityPlayer, par3World, par4, par5, par6, par7, par8, par9, par10))
087                {
088                    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);
089                    --par1ItemStack.stackSize;
090                }
091    
092                return true;
093            }
094            else
095            {
096                return false;
097            }
098        }
099    
100        @SideOnly(Side.CLIENT)
101    
102        /**
103         * Returns true if the given ItemBlock can be placed on the given side of the given block position.
104         */
105        public boolean canPlaceItemBlockOnSide(World par1World, int par2, int par3, int par4, int par5, EntityPlayer par6EntityPlayer, ItemStack par7ItemStack)
106        {
107            int var8 = par1World.getBlockId(par2, par3, par4);
108    
109            if (var8 == Block.snow.blockID)
110            {
111                par5 = 1;
112            }
113            else if (var8 != Block.vine.blockID && var8 != Block.tallGrass.blockID && var8 != Block.deadBush.blockID
114                    && (Block.blocksList[var8] == null || !Block.blocksList[var8].isBlockReplaceable(par1World, par2, par3, par4)))
115            {
116                if (par5 == 0)
117                {
118                    --par3;
119                }
120    
121                if (par5 == 1)
122                {
123                    ++par3;
124                }
125    
126                if (par5 == 2)
127                {
128                    --par4;
129                }
130    
131                if (par5 == 3)
132                {
133                    ++par4;
134                }
135    
136                if (par5 == 4)
137                {
138                    --par2;
139                }
140    
141                if (par5 == 5)
142                {
143                    ++par2;
144                }
145            }
146    
147            return par1World.canPlaceEntityOnSide(this.getBlockID(), par2, par3, par4, false, par5, (Entity)null);
148        }
149    
150        public String getItemNameIS(ItemStack par1ItemStack)
151        {
152            return Block.blocksList[this.blockID].getBlockName();
153        }
154    
155        public String getItemName()
156        {
157            return Block.blocksList[this.blockID].getBlockName();
158        }
159    
160        @SideOnly(Side.CLIENT)
161    
162        /**
163         * gets the CreativeTab this item is displayed on
164         */
165        public CreativeTabs getCreativeTab()
166        {
167            return Block.blocksList[this.blockID].getCreativeTabToDisplayOn();
168        }
169    
170        @SideOnly(Side.CLIENT)
171    
172        /**
173         * returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
174         */
175        public void getSubItems(int par1, CreativeTabs par2CreativeTabs, List par3List)
176        {
177            Block.blocksList[this.blockID].getSubBlocks(par1, par2CreativeTabs, par3List);
178        }
179    
180        /**
181         * Called to actually place the block, after the location is determined
182         * and all permission checks have been made.
183         * 
184         * @param stack The item stack that was used to place the block. This can be changed inside the method.
185         * @param player The player who is placing the block. Can be null if the block is not being placed by a player.
186         * @param side The side the player (or machine) right-clicked on.
187         */
188        public boolean placeBlockAt(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ)
189        {
190           if (!world.setBlockAndMetadataWithNotify(x, y, z, this.blockID, this.getMetadata(stack.getItemDamage())))
191           {
192                   return false;
193           }
194    
195           if (world.getBlockId(x, y, z) == this.blockID)
196           {
197               Block.blocksList[this.blockID].updateBlockMetadata(world, x, y, z, side, hitX, hitY, hitZ);
198               Block.blocksList[this.blockID].onBlockPlacedBy(world, x, y, z, player);
199           }
200    
201           return true;
202        }
203    }