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