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