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