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