    001    package net.minecraft.src;
    002
    003    import net.minecraftforge.common.MinecraftForge;
    004    import net.minecraftforge.event.Event;
    005    import net.minecraftforge.event.entity.player.FillBucketEvent;
    006
    007    public class ItemBucket extends Item
    008    {
    009        /** field for checking if the bucket has been filled. */
    010        private int isFull;
    011
    012        public ItemBucket(int par1, int par2)
    013        {
    014            super(par1);
    015            this.maxStackSize = 1;
    016            this.isFull = par2;
    017            this.setCreativeTab(CreativeTabs.tabMisc);
    018        }
    019
    020        /**
    021         * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
    022         */
    023        public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    024        {
    025            float var4 = 1.0F;
    026            double var5 = par3EntityPlayer.prevPosX + (par3EntityPlayer.posX - par3EntityPlayer.prevPosX) * (double)var4;
    027            double var7 = par3EntityPlayer.prevPosY + (par3EntityPlayer.posY - par3EntityPlayer.prevPosY) * (double)var4 + 1.62D - (double)par3EntityPlayer.yOffset;
    028            double var9 = par3EntityPlayer.prevPosZ + (par3EntityPlayer.posZ - par3EntityPlayer.prevPosZ) * (double)var4;
    029            boolean var11 = this.isFull == 0;
    030            MovingObjectPosition var12 = this.getMovingObjectPositionFromPlayer(par2World, par3EntityPlayer, var11);
    031
    032            if (var12 == null)
    033            {
    034                return par1ItemStack;
    035            }
    036            else
    037            {
    038                FillBucketEvent event = new FillBucketEvent(par3EntityPlayer, par1ItemStack, par2World, var12);
    039                if (MinecraftForge.EVENT_BUS.post(event))
    040                {
    041                    return par1ItemStack;
    042                }
    043
    044                if (event.getResult() == Event.Result.ALLOW)
    045                {
    046                    if (par3EntityPlayer.capabilities.isCreativeMode)
    047                    {
    048                        return par1ItemStack;
    049                    }
    050
    051                    if (--par1ItemStack.stackSize <= 0)
    052                    {
    053                        return event.result;
    054                    }
    055
    056                    if (!par3EntityPlayer.inventory.addItemStackToInventory(event.result))
    057                    {
    058                        par3EntityPlayer.dropPlayerItem(event.result);
    059                    }
    060
    061                    return par1ItemStack;
    062                }
    063
    064                if (var12.typeOfHit == EnumMovingObjectType.TILE)
    065                {
    066                    int var13 = var12.blockX;
    067                    int var14 = var12.blockY;
    068                    int var15 = var12.blockZ;
    069
    070                    if (!par2World.canMineBlock(par3EntityPlayer, var13, var14, var15))
    071                    {
    072                        return par1ItemStack;
    073                    }
    074
    075                    if (this.isFull == 0)
    076                    {
    077                        if (!par3EntityPlayer.func_82247_a(var13, var14, var15, var12.sideHit, par1ItemStack))
    078                        {
    079                            return par1ItemStack;
    080                        }
    081
    082                        if (par2World.getBlockMaterial(var13, var14, var15) == Material.water && par2World.getBlockMetadata(var13, var14, var15) == 0)
    083                        {
    084                            par2World.setBlockWithNotify(var13, var14, var15, 0);
    085
    086                            if (par3EntityPlayer.capabilities.isCreativeMode)
    087                            {
    088                                return par1ItemStack;
    089                            }
    090
    091                            if (--par1ItemStack.stackSize <= 0)
    092                            {
    093                                return new ItemStack(Item.bucketWater);
    094                            }
    095
    096                            if (!par3EntityPlayer.inventory.addItemStackToInventory(new ItemStack(Item.bucketWater)))
    097                            {
    098                                par3EntityPlayer.dropPlayerItem(new ItemStack(Item.bucketWater.shiftedIndex, 1, 0));
    099                            }
    100
    101                            return par1ItemStack;
    102                        }
    103
    104                        if (par2World.getBlockMaterial(var13, var14, var15) == Material.lava && par2World.getBlockMetadata(var13, var14, var15) == 0)
    105                        {
    106                            par2World.setBlockWithNotify(var13, var14, var15, 0);
    107
    108                            if (par3EntityPlayer.capabilities.isCreativeMode)
    109                            {
    110                                return par1ItemStack;
    111                            }
    112
    113                            if (--par1ItemStack.stackSize <= 0)
    114                            {
    115                                return new ItemStack(Item.bucketLava);
    116                            }
    117
    118                            if (!par3EntityPlayer.inventory.addItemStackToInventory(new ItemStack(Item.bucketLava)))
    119                            {
    120                                par3EntityPlayer.dropPlayerItem(new ItemStack(Item.bucketLava.shiftedIndex, 1, 0));
    121                            }
    122
    123                            return par1ItemStack;
    124                        }
    125                    }
    126                    else
    127                    {
    128                        if (this.isFull < 0)
    129                        {
    130                            return new ItemStack(Item.bucketEmpty);
    131                        }
    132
    133                        if (var12.sideHit == 0)
    134                        {
    135                            --var14;
    136                        }
    137
    138                        if (var12.sideHit == 1)
    139                        {
    140                            ++var14;
    141                        }
    142
    143                        if (var12.sideHit == 2)
    144                        {
    145                            --var15;
    146                        }
    147
    148                        if (var12.sideHit == 3)
    149                        {
    150                            ++var15;
    151                        }
    152
    153                        if (var12.sideHit == 4)
    154                        {
    155                            --var13;
    156                        }
    157
    158                        if (var12.sideHit == 5)
    159                        {
    160                            ++var13;
    161                        }
    162
    163                        if (!par3EntityPlayer.func_82247_a(var13, var14, var15, var12.sideHit, par1ItemStack))
    164                        {
    165                            return par1ItemStack;
    166                        }
    167
    168                        if (this.tryPlaceContainedLiquid(par2World, var5, var7, var9, var13, var14, var15) && !par3EntityPlayer.capabilities.isCreativeMode)
    169                        {
    170                            return new ItemStack(Item.bucketEmpty);
    171                        }
    172                    }
    173                }
    174                else if (this.isFull == 0 && var12.entityHit instanceof EntityCow)
    175                {
    176                    return new ItemStack(Item.bucketMilk);
    177                }
    178
    179                return par1ItemStack;
    180            }
    181        }
    182
    183        /**
    184         * Attempts to place the liquid contained inside the bucket.
    185         */
    186        public boolean tryPlaceContainedLiquid(World par1World, double par2, double par4, double par6, int par8, int par9, int par10)
    187        {
    188            if (this.isFull <= 0)
    189            {
    190                return false;
    191            }
    192            else if (!par1World.isAirBlock(par8, par9, par10) && par1World.getBlockMaterial(par8, par9, par10).isSolid())
    193            {
    194                return false;
    195            }
    196            else
    197            {
    198                if (par1World.provider.isHellWorld && this.isFull == Block.waterMoving.blockID)
    199                {
    200                    par1World.playSoundEffect(par2 + 0.5D, par4 + 0.5D, par6 + 0.5D, "random.fizz", 0.5F, 2.6F + (par1World.rand.nextFloat() - par1World.rand.nextFloat()) * 0.8F);
    201
    202                    for (int var11 = 0; var11 < 8; ++var11)
    203                    {
    204                        par1World.spawnParticle("largesmoke", (double)par8 + Math.random(), (double)par9 + Math.random(), (double)par10 + Math.random(), 0.0D, 0.0D, 0.0D);
    205                    }
    206                }
    207                else
    208                {
    209                    par1World.setBlockAndMetadataWithNotify(par8, par9, par10, this.isFull, 0);
    210                }
    211
    212                return true;
    213            }
    214        }
    215    }