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 f = 1.0F;
035        double d0 = par3EntityPlayer.prevPosX + (par3EntityPlayer.posX - par3EntityPlayer.prevPosX) * (double)f;
036        double d1 = par3EntityPlayer.prevPosY + (par3EntityPlayer.posY - par3EntityPlayer.prevPosY) * (double)f + 1.62D - (double)par3EntityPlayer.yOffset;
037        double d2 = par3EntityPlayer.prevPosZ + (par3EntityPlayer.posZ - par3EntityPlayer.prevPosZ) * (double)f;
038        boolean flag = this.isFull == 0;
039        MovingObjectPosition movingobjectposition = this.getMovingObjectPositionFromPlayer(par2World, par3EntityPlayer, flag);
040
041        if (movingobjectposition == null)
042        {
043            return par1ItemStack;
044        }
045        else
046        {
047            FillBucketEvent event = new FillBucketEvent(par3EntityPlayer, par1ItemStack, par2World, movingobjectposition);
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 (movingobjectposition.typeOfHit == EnumMovingObjectType.TILE)
074            {
075                int i = movingobjectposition.blockX;
076                int j = movingobjectposition.blockY;
077                int k = movingobjectposition.blockZ;
078
079                if (!par2World.canMineBlock(par3EntityPlayer, i, j, k))
080                {
081                    return par1ItemStack;
082                }
083
084                if (this.isFull == 0)
085                {
086                    if (!par3EntityPlayer.canPlayerEdit(i, j, k, movingobjectposition.sideHit, par1ItemStack))
087                    {
088                        return par1ItemStack;
089                    }
090
091                    if (par2World.getBlockMaterial(i, j, k) == Material.water && par2World.getBlockMetadata(i, j, k) == 0)
092                    {
093                        par2World.setBlockToAir(i, j, k);
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(i, j, k) == Material.lava && par2World.getBlockMetadata(i, j, k) == 0)
114                    {
115                        par2World.setBlockToAir(i, j, k);
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 (movingobjectposition.sideHit == 0)
143                    {
144                        --j;
145                    }
146
147                    if (movingobjectposition.sideHit == 1)
148                    {
149                        ++j;
150                    }
151
152                    if (movingobjectposition.sideHit == 2)
153                    {
154                        --k;
155                    }
156
157                    if (movingobjectposition.sideHit == 3)
158                    {
159                        ++k;
160                    }
161
162                    if (movingobjectposition.sideHit == 4)
163                    {
164                        --i;
165                    }
166
167                    if (movingobjectposition.sideHit == 5)
168                    {
169                        ++i;
170                    }
171
172                    if (!par3EntityPlayer.canPlayerEdit(i, j, k, movingobjectposition.sideHit, par1ItemStack))
173                    {
174                        return par1ItemStack;
175                    }
176
177                    if (this.tryPlaceContainedLiquid(par2World, d0, d1, d2, i, j, k) && !par3EntityPlayer.capabilities.isCreativeMode)
178                    {
179                        return new ItemStack(Item.bucketEmpty);
180                    }
181                }
182            }
183            else if (this.isFull == 0 && movingobjectposition.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 l = 0; l < 8; ++l)
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.setBlock(par8, par9, par10, this.isFull, 0, 3);
219            }
220
221            return true;
222        }
223    }
224}