    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.BlockCocoa;
    009import net.minecraft.block.BlockCrops;
    010import net.minecraft.block.BlockDirectional;
    011import net.minecraft.block.BlockLog;
    012import net.minecraft.block.BlockMushroom;
    013import net.minecraft.block.BlockSapling;
    014import net.minecraft.block.BlockStem;
    015import net.minecraft.client.renderer.texture.IconRegister;
    016import net.minecraft.creativetab.CreativeTabs;
    017import net.minecraft.entity.EntityLiving;
    018import net.minecraft.entity.passive.EntitySheep;
    019import net.minecraft.entity.player.EntityPlayer;
    020import net.minecraft.util.Icon;
    021import net.minecraft.util.MathHelper;
    022import net.minecraft.world.World;
    023
    024import net.minecraftforge.common.FakePlayerFactory;
    025import net.minecraftforge.common.ForgeHooks;
    026import net.minecraftforge.common.MinecraftForge;
    027import net.minecraftforge.event.Event.Result;
    028import net.minecraftforge.event.entity.player.BonemealEvent;
    029
    030public class ItemDye extends Item
    031{
    032    /** List of dye color names */
    033    public static final String[] dyeColorNames = new String[] {"black", "red", "green", "brown", "blue", "purple", "cyan", "silver", "gray", "pink", "lime", "yellow", "lightBlue", "magenta", "orange", "white"};
    034    public static final String[] field_94595_b = new String[] {"dyePowder_black", "dyePowder_red", "dyePowder_green", "dyePowder_brown", "dyePowder_blue", "dyePowder_purple", "dyePowder_cyan", "dyePowder_silver", "dyePowder_gray", "dyePowder_pink", "dyePowder_lime", "dyePowder_yellow", "dyePowder_lightBlue", "dyePowder_magenta", "dyePowder_orange", "dyePowder_white"};
    035    public static final int[] dyeColors = new int[] {1973019, 11743532, 3887386, 5320730, 2437522, 8073150, 2651799, 11250603, 4408131, 14188952, 4312372, 14602026, 6719955, 12801229, 15435844, 15790320};
    036    @SideOnly(Side.CLIENT)
    037    private Icon[] field_94594_d;
    038
    039    public ItemDye(int par1)
    040    {
    041        super(par1);
    042        this.setHasSubtypes(true);
    043        this.setMaxDamage(0);
    044        this.setCreativeTab(CreativeTabs.tabMaterials);
    045    }
    046
    047    @SideOnly(Side.CLIENT)
    048
    049    /**
    050     * Gets an icon index based on an item's damage value
    051     */
    052    public Icon getIconFromDamage(int par1)
    053    {
    054        int j = MathHelper.clamp_int(par1, 0, 15);
    055        return this.field_94594_d[j];
    056    }
    057
    058    /**
    059     * Returns the unlocalized name of this item. This version accepts an ItemStack so different stacks can have
    060     * different names based on their damage or NBT.
    061     */
    062    public String getUnlocalizedName(ItemStack par1ItemStack)
    063    {
    064        int i = MathHelper.clamp_int(par1ItemStack.getItemDamage(), 0, 15);
    065        return super.getUnlocalizedName() + "." + dyeColorNames[i];
    066    }
    067
    068    /**
    069     * Callback for item usage. If the item does something special on right clicking, he will have one of those. Return
    070     * True if something happen and false if it don't. This is for ITEMS, not BLOCKS
    071     */
    072    public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
    073    {
    074        if (!par2EntityPlayer.canPlayerEdit(par4, par5, par6, par7, par1ItemStack))
    075        {
    076            return false;
    077        }
    078        else
    079        {
    080            if (par1ItemStack.getItemDamage() == 15)
    081            {
    082                if (func_96604_a(par1ItemStack, par3World, par4, par5, par6))
    083                {
    084                    if (!par3World.isRemote)
    085                    {
    086                        par3World.playAuxSFX(2005, par4, par5, par6, 0);
    087                    }
    088
    089                    return true;
    090                }
    091            }
    092            else if (par1ItemStack.getItemDamage() == 3)
    093            {
    094                int i1 = par3World.getBlockId(par4, par5, par6);
    095                int j1 = par3World.getBlockMetadata(par4, par5, par6);
    096
    097                if (i1 == Block.wood.blockID && BlockLog.limitToValidMetadata(j1) == 3)
    098                {
    099                    if (par7 == 0)
    100                    {
    101                        return false;
    102                    }
    103
    104                    if (par7 == 1)
    105                    {
    106                        return false;
    107                    }
    108
    109                    if (par7 == 2)
    110                    {
    111                        --par6;
    112                    }
    113
    114                    if (par7 == 3)
    115                    {
    116                        ++par6;
    117                    }
    118
    119                    if (par7 == 4)
    120                    {
    121                        --par4;
    122                    }
    123
    124                    if (par7 == 5)
    125                    {
    126                        ++par4;
    127                    }
    128
    129                    if (par3World.isAirBlock(par4, par5, par6))
    130                    {
    131                        int k1 = Block.blocksList[Block.cocoaPlant.blockID].onBlockPlaced(par3World, par4, par5, par6, par7, par8, par9, par10, 0);
    132                        par3World.setBlockAndMetadataWithNotify(par4, par5, par6, Block.cocoaPlant.blockID, k1, 2);
    133
    134                        if (!par2EntityPlayer.capabilities.isCreativeMode)
    135                        {
    136                            --par1ItemStack.stackSize;
    137                        }
    138                    }
    139
    140                    return true;
    141                }
    142            }
    143
    144            return false;
    145        }
    146    }
    147
    148    public static boolean func_96604_a(ItemStack par0ItemStack, World par1World, int par2, int par3, int par4)
    149    {
    150        return applyBonemeal(par0ItemStack, par1World, par2, par3, par4, FakePlayerFactory.getMinecraft(par1World));
    151    }
    152
    153    public static boolean applyBonemeal(ItemStack par0ItemStack, World par1World, int par2, int par3, int par4, EntityPlayer player)
    154    {
    155        int l = par1World.getBlockId(par2, par3, par4);
    156
    157        BonemealEvent event = new BonemealEvent(player, par1World, l, par2, par3, par4);
    158        if (MinecraftForge.EVENT_BUS.post(event))
    159        {
    160            return false;
    161        }
    162
    163        if (event.getResult() == Result.ALLOW)
    164        {
    165            if (!par1World.isRemote)
    166            {
    167                par0ItemStack.stackSize--;
    168            }
    169            return true;
    170        }
    171
    172        if (l == Block.sapling.blockID)
    173        {
    174            if (!par1World.isRemote)
    175            {
    176                if ((double)par1World.rand.nextFloat() < 0.45D)
    177                {
    178                    ((BlockSapling)Block.sapling).func_96477_c(par1World, par2, par3, par4, par1World.rand);
    179                }
    180
    181                --par0ItemStack.stackSize;
    182            }
    183
    184            return true;
    185        }
    186        else if (l != Block.mushroomBrown.blockID && l != Block.mushroomRed.blockID)
    187        {
    188            if (l != Block.melonStem.blockID && l != Block.pumpkinStem.blockID)
    189            {
    190                if (l > 0 && Block.blocksList[l] instanceof BlockCrops)
    191                {
    192                    if (par1World.getBlockMetadata(par2, par3, par4) == 7)
    193                    {
    194                        return false;
    195                    }
    196                    else
    197                    {
    198                        if (!par1World.isRemote)
    199                        {
    200                            ((BlockCrops)Block.blocksList[l]).fertilize(par1World, par2, par3, par4);
    201                            --par0ItemStack.stackSize;
    202                        }
    203
    204                        return true;
    205                    }
    206                }
    207                else
    208                {
    209                    int i1;
    210                    int j1;
    211                    int k1;
    212
    213                    if (l == Block.cocoaPlant.blockID)
    214                    {
    215                        i1 = par1World.getBlockMetadata(par2, par3, par4);
    216                        j1 = BlockDirectional.getDirection(i1);
    217                        k1 = BlockCocoa.func_72219_c(i1);
    218
    219                        if (k1 >= 2)
    220                        {
    221                            return false;
    222                        }
    223                        else
    224                        {
    225                            if (!par1World.isRemote)
    226                            {
    227                                ++k1;
    228                                par1World.setBlockMetadataWithNotify(par2, par3, par4, k1 << 2 | j1, 2);
    229                                --par0ItemStack.stackSize;
    230                            }
    231
    232                            return true;
    233                        }
    234                    }
    235                    else if (l != Block.grass.blockID)
    236                    {
    237                        return false;
    238                    }
    239                    else
    240                    {
    241                        if (!par1World.isRemote)
    242                        {
    243                            --par0ItemStack.stackSize;
    244                            label102:
    245
    246                            for (i1 = 0; i1 < 128; ++i1)
    247                            {
    248                                j1 = par2;
    249                                k1 = par3 + 1;
    250                                int l1 = par4;
    251
    252                                for (int i2 = 0; i2 < i1 / 16; ++i2)
    253                                {
    254                                    j1 += itemRand.nextInt(3) - 1;
    255                                    k1 += (itemRand.nextInt(3) - 1) * itemRand.nextInt(3) / 2;
    256                                    l1 += itemRand.nextInt(3) - 1;
    257
    258                                    if (par1World.getBlockId(j1, k1 - 1, l1) != Block.grass.blockID || par1World.isBlockNormalCube(j1, k1, l1))
    259                                    {
    260                                        continue label102;
    261                                    }
    262                                }
    263
    264                                if (par1World.getBlockId(j1, k1, l1) == 0)
    265                                {
    266                                    if (itemRand.nextInt(10) != 0)
    267                                    {
    268                                        if (Block.tallGrass.canBlockStay(par1World, j1, k1, l1))
    269                                        {
    270                                            par1World.setBlockAndMetadataWithNotify(j1, k1, l1, Block.tallGrass.blockID, 1, 3);
    271                                        }
    272                                    }
    273                                    else
    274                                    {
    275                                        ForgeHooks.plantGrass(par1World, j1, k1, l1);
    276                                    }
    277                                }
    278                            }
    279                        }
    280
    281                        return true;
    282                    }
    283                }
    284            }
    285            else if (par1World.getBlockMetadata(par2, par3, par4) == 7)
    286            {
    287                return false;
    288            }
    289            else
    290            {
    291                if (!par1World.isRemote)
    292                {
    293                    ((BlockStem)Block.blocksList[l]).fertilizeStem(par1World, par2, par3, par4);
    294                    --par0ItemStack.stackSize;
    295                }
    296
    297                return true;
    298            }
    299        }
    300        else
    301        {
    302            if (!par1World.isRemote)
    303            {
    304                if ((double)par1World.rand.nextFloat() < 0.4D)
    305                {
    306                    ((BlockMushroom)Block.blocksList[l]).fertilizeMushroom(par1World, par2, par3, par4, par1World.rand);
    307                }
    308
    309                --par0ItemStack.stackSize;
    310            }
    311
    312            return true;
    313        }
    314    }
    315
    316    @SideOnly(Side.CLIENT)
    317    public static void func_96603_a(World par0World, int par1, int par2, int par3, int par4)
    318    {
    319        int i1 = par0World.getBlockId(par1, par2, par3);
    320
    321        if (par4 == 0)
    322        {
    323            par4 = 15;
    324        }
    325
    326        Block block = i1 > 0 && i1 < Block.blocksList.length ? Block.blocksList[i1] : null;
    327
    328        if (block != null)
    329        {
    330            block.setBlockBoundsBasedOnState(par0World, par1, par2, par3);
    331
    332            for (int j1 = 0; j1 < par4; ++j1)
    333            {
    334                double d0 = itemRand.nextGaussian() * 0.02D;
    335                double d1 = itemRand.nextGaussian() * 0.02D;
    336                double d2 = itemRand.nextGaussian() * 0.02D;
    337                par0World.spawnParticle("happyVillager", (double)((float)par1 + itemRand.nextFloat()), (double)par2 + (double)itemRand.nextFloat() * block.getBlockBoundsMaxY(), (double)((float)par3 + itemRand.nextFloat()), d0, d1, d2);
    338            }
    339        }
    340    }
    341
    342    /**
    343     * Called when a player right clicks an entity with an item.
    344     */
    345    public boolean itemInteractionForEntity(ItemStack par1ItemStack, EntityLiving par2EntityLiving)
    346    {
    347        if (par2EntityLiving instanceof EntitySheep)
    348        {
    349            EntitySheep entitysheep = (EntitySheep)par2EntityLiving;
    350            int i = BlockCloth.getBlockFromDye(par1ItemStack.getItemDamage());
    351
    352            if (!entitysheep.getSheared() && entitysheep.getFleeceColor() != i)
    353            {
    354                entitysheep.setFleeceColor(i);
    355                --par1ItemStack.stackSize;
    356            }
    357
    358            return true;
    359        }
    360        else
    361        {
    362            return false;
    363        }
    364    }
    365
    366    @SideOnly(Side.CLIENT)
    367
    368    /**
    369     * returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
    370     */
    371    public void getSubItems(int par1, CreativeTabs par2CreativeTabs, List par3List)
    372    {
    373        for (int j = 0; j < 16; ++j)
    374        {
    375            par3List.add(new ItemStack(par1, 1, j));
    376        }
    377    }
    378
    379    @SideOnly(Side.CLIENT)
    380    public void func_94581_a(IconRegister par1IconRegister)
    381    {
    382        this.field_94594_d = new Icon[field_94595_b.length];
    383
    384        for (int i = 0; i < field_94595_b.length; ++i)
    385        {
    386            this.field_94594_d[i] = par1IconRegister.func_94245_a(field_94595_b[i]);
    387        }
    388    }
    389}