    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.block.BlockCloth;
    008import net.minecraft.block.BlockCrops;
    009import net.minecraft.block.BlockDirectional;
    010import net.minecraft.block.BlockLog;
    011import net.minecraft.block.BlockMushroom;
    012import net.minecraft.block.BlockSapling;
    013import net.minecraft.block.BlockStem;
    014import net.minecraft.creativetab.CreativeTabs;
    015import net.minecraft.entity.EntityLiving;
    016import net.minecraft.entity.passive.EntitySheep;
    017import net.minecraft.entity.player.EntityPlayer;
    018import net.minecraft.util.MathHelper;
    019import net.minecraft.world.World;
    020
    021import net.minecraftforge.common.ForgeHooks;
    022import net.minecraftforge.common.MinecraftForge;
    023import net.minecraftforge.event.Event.Result;
    024import net.minecraftforge.event.entity.player.BonemealEvent;
    025
    026public class ItemDye extends Item
    027{
    028    /** List of dye color names */
    029    public static final String[] dyeColorNames = new String[] {"black", "red", "green", "brown", "blue", "purple", "cyan", "silver", "gray", "pink", "lime", "yellow", "lightBlue", "magenta", "orange", "white"};
    030    public static final int[] dyeColors = new int[] {1973019, 11743532, 3887386, 5320730, 2437522, 8073150, 2651799, 11250603, 4408131, 14188952, 4312372, 14602026, 6719955, 12801229, 15435844, 15790320};
    031
    032    public ItemDye(int par1)
    033    {
    034        super(par1);
    035        this.setHasSubtypes(true);
    036        this.setMaxDamage(0);
    037        this.setCreativeTab(CreativeTabs.tabMaterials);
    038    }
    039
    040    @SideOnly(Side.CLIENT)
    041
    042    /**
    043     * Gets an icon index based on an item's damage value
    044     */
    045    public int getIconFromDamage(int par1)
    046    {
    047        int var2 = MathHelper.clamp_int(par1, 0, 15);
    048        return this.iconIndex + var2 % 8 * 16 + var2 / 8;
    049    }
    050
    051    public String getItemNameIS(ItemStack par1ItemStack)
    052    {
    053        int var2 = MathHelper.clamp_int(par1ItemStack.getItemDamage(), 0, 15);
    054        return super.getItemName() + "." + dyeColorNames[var2];
    055    }
    056
    057    /**
    058     * Callback for item usage. If the item does something special on right clicking, he will have one of those. Return
    059     * True if something happen and false if it don't. This is for ITEMS, not BLOCKS
    060     */
    061    public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
    062    {
    063        if (!par2EntityPlayer.canPlayerEdit(par4, par5, par6, par7, par1ItemStack))
    064        {
    065            return false;
    066        }
    067        else
    068        {
    069            int var11;
    070            int var12;
    071            int var13;
    072
    073            if (par1ItemStack.getItemDamage() == 15)
    074            {
    075                var11 = par3World.getBlockId(par4, par5, par6);
    076
    077                BonemealEvent event = new BonemealEvent(par2EntityPlayer, par3World, var11, par4, par5, par6);
    078                if (MinecraftForge.EVENT_BUS.post(event))
    079                {
    080                    return false;
    081                }
    082
    083                if (event.getResult() == Result.ALLOW)
    084                {
    085                    if (!par3World.isRemote)
    086                    {
    087                        par1ItemStack.stackSize--;
    088                    }
    089                    return true;
    090                }
    091
    092                if (var11 == Block.sapling.blockID)
    093                {
    094                    if (!par3World.isRemote)
    095                    {
    096                        ((BlockSapling)Block.sapling).growTree(par3World, par4, par5, par6, par3World.rand);
    097                        --par1ItemStack.stackSize;
    098                    }
    099
    100                    return true;
    101                }
    102
    103                if (var11 == Block.mushroomBrown.blockID || var11 == Block.mushroomRed.blockID)
    104                {
    105                    if (!par3World.isRemote && ((BlockMushroom)Block.blocksList[var11]).fertilizeMushroom(par3World, par4, par5, par6, par3World.rand))
    106                    {
    107                        --par1ItemStack.stackSize;
    108                    }
    109
    110                    return true;
    111                }
    112
    113                if (var11 == Block.melonStem.blockID || var11 == Block.pumpkinStem.blockID)
    114                {
    115                    if (par3World.getBlockMetadata(par4, par5, par6) == 7)
    116                    {
    117                        return false;
    118                    }
    119
    120                    if (!par3World.isRemote)
    121                    {
    122                        ((BlockStem)Block.blocksList[var11]).fertilizeStem(par3World, par4, par5, par6);
    123                        --par1ItemStack.stackSize;
    124                    }
    125
    126                    return true;
    127                }
    128
    129                if (var11 > 0 && Block.blocksList[var11] instanceof BlockCrops)
    130                {
    131                    if (par3World.getBlockMetadata(par4, par5, par6) == 7)
    132                    {
    133                        return false;
    134                    }
    135
    136                    if (!par3World.isRemote)
    137                    {
    138                        ((BlockCrops)Block.blocksList[var11]).fertilize(par3World, par4, par5, par6);
    139                        --par1ItemStack.stackSize;
    140                    }
    141
    142                    return true;
    143                }
    144
    145                if (var11 == Block.cocoaPlant.blockID)
    146                {
    147                    if (!par3World.isRemote)
    148                    {
    149                        par3World.setBlockMetadataWithNotify(par4, par5, par6, 8 | BlockDirectional.getDirection(par3World.getBlockMetadata(par4, par5, par6)));
    150                        --par1ItemStack.stackSize;
    151                    }
    152
    153                    return true;
    154                }
    155
    156                if (var11 == Block.grass.blockID)
    157                {
    158                    if (!par3World.isRemote)
    159                    {
    160                        --par1ItemStack.stackSize;
    161                        label133:
    162
    163                        for (var12 = 0; var12 < 128; ++var12)
    164                        {
    165                            var13 = par4;
    166                            int var14 = par5 + 1;
    167                            int var15 = par6;
    168
    169                            for (int var16 = 0; var16 < var12 / 16; ++var16)
    170                            {
    171                                var13 += itemRand.nextInt(3) - 1;
    172                                var14 += (itemRand.nextInt(3) - 1) * itemRand.nextInt(3) / 2;
    173                                var15 += itemRand.nextInt(3) - 1;
    174
    175                                if (par3World.getBlockId(var13, var14 - 1, var15) != Block.grass.blockID || par3World.isBlockNormalCube(var13, var14, var15))
    176                                {
    177                                    continue label133;
    178                                }
    179                            }
    180
    181                            if (par3World.getBlockId(var13, var14, var15) == 0)
    182                            {
    183                                if (itemRand.nextInt(10) != 0)
    184                                {
    185                                    if (Block.tallGrass.canBlockStay(par3World, var13, var14, var15))
    186                                    {
    187                                        par3World.setBlockAndMetadataWithNotify(var13, var14, var15, Block.tallGrass.blockID, 1);
    188                                    }
    189                                }
    190                                else
    191                                {
    192                                    ForgeHooks.plantGrass(par3World, var13, var14, var15);
    193                                }
    194                            }
    195                        }
    196                    }
    197
    198                    return true;
    199                }
    200            }
    201            else if (par1ItemStack.getItemDamage() == 3)
    202            {
    203                var11 = par3World.getBlockId(par4, par5, par6);
    204                var12 = par3World.getBlockMetadata(par4, par5, par6);
    205
    206                if (var11 == Block.wood.blockID && BlockLog.limitToValidMetadata(var12) == 3)
    207                {
    208                    if (par7 == 0)
    209                    {
    210                        return false;
    211                    }
    212
    213                    if (par7 == 1)
    214                    {
    215                        return false;
    216                    }
    217
    218                    if (par7 == 2)
    219                    {
    220                        --par6;
    221                    }
    222
    223                    if (par7 == 3)
    224                    {
    225                        ++par6;
    226                    }
    227
    228                    if (par7 == 4)
    229                    {
    230                        --par4;
    231                    }
    232
    233                    if (par7 == 5)
    234                    {
    235                        ++par4;
    236                    }
    237
    238                    if (par3World.isAirBlock(par4, par5, par6))
    239                    {
    240                        var13 = Block.blocksList[Block.cocoaPlant.blockID].onBlockPlaced(par3World, par4, par5, par6, par7, par8, par9, par10, 0);
    241                        par3World.setBlockAndMetadataWithNotify(par4, par5, par6, Block.cocoaPlant.blockID, var13);
    242
    243                        if (!par2EntityPlayer.capabilities.isCreativeMode)
    244                        {
    245                            --par1ItemStack.stackSize;
    246                        }
    247                    }
    248
    249                    return true;
    250                }
    251            }
    252
    253            return false;
    254        }
    255    }
    256
    257    /**
    258     * Called when a player right clicks an entity with an item.
    259     */
    260    public boolean itemInteractionForEntity(ItemStack par1ItemStack, EntityLiving par2EntityLiving)
    261    {
    262        if (par2EntityLiving instanceof EntitySheep)
    263        {
    264            EntitySheep var3 = (EntitySheep)par2EntityLiving;
    265            int var4 = BlockCloth.getBlockFromDye(par1ItemStack.getItemDamage());
    266
    267            if (!var3.getSheared() && var3.getFleeceColor() != var4)
    268            {
    269                var3.setFleeceColor(var4);
    270                --par1ItemStack.stackSize;
    271            }
    272
    273            return true;
    274        }
    275        else
    276        {
    277            return false;
    278        }
    279    }
    280
    281    @SideOnly(Side.CLIENT)
    282
    283    /**
    284     * returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
    285     */
    286    public void getSubItems(int par1, CreativeTabs par2CreativeTabs, List par3List)
    287    {
    288        for (int var4 = 0; var4 < 16; ++var4)
    289        {
    290            par3List.add(new ItemStack(par1, 1, var4));
    291        }
    292    }
    293}