    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
    058                if (par1ItemStack.getItemDamage() == 15)
    059                {
    060                    var11 = par3World.getBlockId(par4, par5, par6);
    061
    062                    BonemealEvent event = new BonemealEvent(par2EntityPlayer, par3World, var11, par4, par5, par6);
    063                    if (MinecraftForge.EVENT_BUS.post(event))
    064                    {
    065                        return false;
    066                    }
    067
    068                    if (event.getResult() == Result.ALLOW)
    069                    {
    070                        if (!par3World.isRemote)
    071                        {
    072                            par1ItemStack.stackSize--;
    073                        }
    074                        return true;
    075                    }
    076
    077                    if (var11 == Block.sapling.blockID)
    078                    {
    079                        if (!par3World.isRemote)
    080                        {
    081                            ((BlockSapling)Block.sapling).growTree(par3World, par4, par5, par6, par3World.rand);
    082                            --par1ItemStack.stackSize;
    083                        }
    084
    085                        return true;
    086                    }
    087
    088                    if (var11 == Block.mushroomBrown.blockID || var11 == Block.mushroomRed.blockID)
    089                    {
    090                        if (!par3World.isRemote && ((BlockMushroom)Block.blocksList[var11]).fertilizeMushroom(par3World, par4, par5, par6, par3World.rand))
    091                        {
    092                            --par1ItemStack.stackSize;
    093                        }
    094
    095                        return true;
    096                    }
    097
    098                    if (var11 == Block.melonStem.blockID || var11 == Block.pumpkinStem.blockID)
    099                    {
    100                        if (par3World.getBlockMetadata(par4, par5, par6) == 7)
    101                        {
    102                            return false;
    103                        }
    104
    105                        if (!par3World.isRemote)
    106                        {
    107                            ((BlockStem)Block.blocksList[var11]).fertilizeStem(par3World, par4, par5, par6);
    108                            --par1ItemStack.stackSize;
    109                        }
    110
    111                        return true;
    112                    }
    113
    114                    if (var11 > 0 && Block.blocksList[var11] instanceof BlockCrops)
    115                    {
    116                        if (par3World.getBlockMetadata(par4, par5, par6) == 7)
    117                        {
    118                            return false;
    119                        }
    120
    121                        if (!par3World.isRemote)
    122                        {
    123                            ((BlockCrops)Block.blocksList[var11]).fertilize(par3World, par4, par5, par6);
    124                            --par1ItemStack.stackSize;
    125                        }
    126
    127                        return true;
    128                    }
    129
    130                    if (var11 == Block.cocoaPlant.blockID)
    131                    {
    132                        if (!par3World.isRemote)
    133                        {
    134                            par3World.setBlockMetadataWithNotify(par4, par5, par6, 8 | BlockDirectional.getDirection(par3World.getBlockMetadata(par4, par5, par6)));
    135                            --par1ItemStack.stackSize;
    136                        }
    137
    138                        return true;
    139                    }
    140
    141                    if (var11 == Block.grass.blockID)
    142                    {
    143                        if (!par3World.isRemote)
    144                        {
    145                            --par1ItemStack.stackSize;
    146                            label137:
    147
    148                            for (var12 = 0; var12 < 128; ++var12)
    149                            {
    150                                int var13 = par4;
    151                                int var14 = par5 + 1;
    152                                int var15 = par6;
    153
    154                                for (int var16 = 0; var16 < var12 / 16; ++var16)
    155                                {
    156                                    var13 += itemRand.nextInt(3) - 1;
    157                                    var14 += (itemRand.nextInt(3) - 1) * itemRand.nextInt(3) / 2;
    158                                    var15 += itemRand.nextInt(3) - 1;
    159
    160                                    if (par3World.getBlockId(var13, var14 - 1, var15) != Block.grass.blockID || par3World.isBlockNormalCube(var13, var14, var15))
    161                                    {
    162                                        continue label137;
    163                                    }
    164                                }
    165
    166                                if (par3World.getBlockId(var13, var14, var15) == 0)
    167                                {
    168                                    if (itemRand.nextInt(10) != 0)
    169                                    {
    170                                        if (Block.tallGrass.canBlockStay(par3World, var13, var14, var15))
    171                                        {
    172                                            par3World.setBlockAndMetadataWithNotify(var13, var14, var15, Block.tallGrass.blockID, 1);
    173                                        }
    174                                    }
    175                                    else
    176                                    {
    177                                        ForgeHooks.plantGrass(par3World, var13, var14, var15);
    178                                    }
    179                                }
    180                            }
    181                        }
    182
    183                        return true;
    184                    }
    185                }
    186                else if (par1ItemStack.getItemDamage() == 3)
    187                {
    188                    var11 = par3World.getBlockId(par4, par5, par6);
    189                    var12 = par3World.getBlockMetadata(par4, par5, par6);
    190
    191                    if (var11 == Block.wood.blockID && BlockLog.limitToValidMetadata(var12) == 3)
    192                    {
    193                        if (par7 == 0)
    194                        {
    195                            return false;
    196                        }
    197
    198                        if (par7 == 1)
    199                        {
    200                            return false;
    201                        }
    202
    203                        if (par7 == 2)
    204                        {
    205                            --par6;
    206                        }
    207
    208                        if (par7 == 3)
    209                        {
    210                            ++par6;
    211                        }
    212
    213                        if (par7 == 4)
    214                        {
    215                            --par4;
    216                        }
    217
    218                        if (par7 == 5)
    219                        {
    220                            ++par4;
    221                        }
    222
    223                        if (par3World.isAirBlock(par4, par5, par6))
    224                        {
    225                            par3World.setBlockWithNotify(par4, par5, par6, Block.cocoaPlant.blockID);
    226
    227                            if (par3World.getBlockId(par4, par5, par6) == Block.cocoaPlant.blockID)
    228                            {
    229                                Block.blocksList[Block.cocoaPlant.blockID].updateBlockMetadata(par3World, par4, par5, par6, par7, par8, par9, par10);
    230                            }
    231
    232                            if (!par2EntityPlayer.capabilities.isCreativeMode)
    233                            {
    234                                --par1ItemStack.stackSize;
    235                            }
    236                        }
    237
    238                        return true;
    239                    }
    240                }
    241
    242                return false;
    243            }
    244        }
    245
    246        /**
    247         * dye sheep, place saddles, etc ...
    248         */
    249        public boolean itemInteractionForEntity(ItemStack par1ItemStack, EntityLiving par2EntityLiving)
    250        {
    251            if (par2EntityLiving instanceof EntitySheep)
    252            {
    253                EntitySheep var3 = (EntitySheep)par2EntityLiving;
    254                int var4 = BlockCloth.getBlockFromDye(par1ItemStack.getItemDamage());
    255
    256                if (!var3.getSheared() && var3.getFleeceColor() != var4)
    257                {
    258                    var3.setFleeceColor(var4);
    259                    --par1ItemStack.stackSize;
    260                }
    261
    262                return true;
    263            }
    264            else
    265            {
    266                return false;
    267            }
    268        }
    269
    270        @SideOnly(Side.CLIENT)
    271
    272        /**
    273         * returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
    274         */
    275        public void getSubItems(int par1, CreativeTabs par2CreativeTabs, List par3List)
    276        {
    277            for (int var4 = 0; var4 < 16; ++var4)
    278            {
    279                par3List.add(new ItemStack(par1, 1, var4));
    280            }
    281        }
    282    }