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