001package net.minecraft.item;
002
003import net.minecraft.block.Block;
004import net.minecraft.block.BlockEndPortalFrame;
005import net.minecraft.creativetab.CreativeTabs;
006import net.minecraft.entity.item.EntityEnderEye;
007import net.minecraft.entity.player.EntityPlayer;
008import net.minecraft.util.Direction;
009import net.minecraft.util.EnumMovingObjectType;
010import net.minecraft.util.MovingObjectPosition;
011import net.minecraft.world.ChunkPosition;
012import net.minecraft.world.World;
013
014public class ItemEnderEye extends Item
015{
016    public ItemEnderEye(int par1)
017    {
018        super(par1);
019        this.setCreativeTab(CreativeTabs.tabMisc);
020    }
021
022    /**
023     * Callback for item usage. If the item does something special on right clicking, he will have one of those. Return
024     * True if something happen and false if it don't. This is for ITEMS, not BLOCKS
025     */
026    public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
027    {
028        int i1 = par3World.getBlockId(par4, par5, par6);
029        int j1 = par3World.getBlockMetadata(par4, par5, par6);
030
031        if (par2EntityPlayer.canPlayerEdit(par4, par5, par6, par7, par1ItemStack) && i1 == Block.endPortalFrame.blockID && !BlockEndPortalFrame.isEnderEyeInserted(j1))
032        {
033            if (par3World.isRemote)
034            {
035                return true;
036            }
037            else
038            {
039                par3World.setBlockMetadataWithNotify(par4, par5, par6, j1 + 4, 2);
040                --par1ItemStack.stackSize;
041                int k1;
042
043                for (k1 = 0; k1 < 16; ++k1)
044                {
045                    double d0 = (double)((float)par4 + (5.0F + itemRand.nextFloat() * 6.0F) / 16.0F);
046                    double d1 = (double)((float)par5 + 0.8125F);
047                    double d2 = (double)((float)par6 + (5.0F + itemRand.nextFloat() * 6.0F) / 16.0F);
048                    double d3 = 0.0D;
049                    double d4 = 0.0D;
050                    double d5 = 0.0D;
051                    par3World.spawnParticle("smoke", d0, d1, d2, d3, d4, d5);
052                }
053
054                k1 = j1 & 3;
055                int l1 = 0;
056                int i2 = 0;
057                boolean flag = false;
058                boolean flag1 = true;
059                int j2 = Direction.rotateRight[k1];
060                int k2;
061                int l2;
062                int i3;
063                int j3;
064                int k3;
065
066                for (k2 = -2; k2 <= 2; ++k2)
067                {
068                    j3 = par4 + Direction.offsetX[j2] * k2;
069                    l2 = par6 + Direction.offsetZ[j2] * k2;
070                    k3 = par3World.getBlockId(j3, par5, l2);
071
072                    if (k3 == Block.endPortalFrame.blockID)
073                    {
074                        i3 = par3World.getBlockMetadata(j3, par5, l2);
075
076                        if (!BlockEndPortalFrame.isEnderEyeInserted(i3))
077                        {
078                            flag1 = false;
079                            break;
080                        }
081
082                        i2 = k2;
083
084                        if (!flag)
085                        {
086                            l1 = k2;
087                            flag = true;
088                        }
089                    }
090                }
091
092                if (flag1 && i2 == l1 + 2)
093                {
094                    for (k2 = l1; k2 <= i2; ++k2)
095                    {
096                        j3 = par4 + Direction.offsetX[j2] * k2;
097                        l2 = par6 + Direction.offsetZ[j2] * k2;
098                        j3 += Direction.offsetX[k1] * 4;
099                        l2 += Direction.offsetZ[k1] * 4;
100                        k3 = par3World.getBlockId(j3, par5, l2);
101                        i3 = par3World.getBlockMetadata(j3, par5, l2);
102
103                        if (k3 != Block.endPortalFrame.blockID || !BlockEndPortalFrame.isEnderEyeInserted(i3))
104                        {
105                            flag1 = false;
106                            break;
107                        }
108                    }
109
110                    for (k2 = l1 - 1; k2 <= i2 + 1; k2 += 4)
111                    {
112                        for (j3 = 1; j3 <= 3; ++j3)
113                        {
114                            l2 = par4 + Direction.offsetX[j2] * k2;
115                            k3 = par6 + Direction.offsetZ[j2] * k2;
116                            l2 += Direction.offsetX[k1] * j3;
117                            k3 += Direction.offsetZ[k1] * j3;
118                            i3 = par3World.getBlockId(l2, par5, k3);
119                            int l3 = par3World.getBlockMetadata(l2, par5, k3);
120
121                            if (i3 != Block.endPortalFrame.blockID || !BlockEndPortalFrame.isEnderEyeInserted(l3))
122                            {
123                                flag1 = false;
124                                break;
125                            }
126                        }
127                    }
128
129                    if (flag1)
130                    {
131                        for (k2 = l1; k2 <= i2; ++k2)
132                        {
133                            for (j3 = 1; j3 <= 3; ++j3)
134                            {
135                                l2 = par4 + Direction.offsetX[j2] * k2;
136                                k3 = par6 + Direction.offsetZ[j2] * k2;
137                                l2 += Direction.offsetX[k1] * j3;
138                                k3 += Direction.offsetZ[k1] * j3;
139                                par3World.setBlock(l2, par5, k3, Block.endPortal.blockID, 0, 2);
140                            }
141                        }
142                    }
143                }
144
145                return true;
146            }
147        }
148        else
149        {
150            return false;
151        }
152    }
153
154    /**
155     * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
156     */
157    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
158    {
159        MovingObjectPosition movingobjectposition = this.getMovingObjectPositionFromPlayer(par2World, par3EntityPlayer, false);
160
161        if (movingobjectposition != null && movingobjectposition.typeOfHit == EnumMovingObjectType.TILE)
162        {
163            int i = par2World.getBlockId(movingobjectposition.blockX, movingobjectposition.blockY, movingobjectposition.blockZ);
164
165            if (i == Block.endPortalFrame.blockID)
166            {
167                return par1ItemStack;
168            }
169        }
170
171        if (!par2World.isRemote)
172        {
173            ChunkPosition chunkposition = par2World.findClosestStructure("Stronghold", (int)par3EntityPlayer.posX, (int)par3EntityPlayer.posY, (int)par3EntityPlayer.posZ);
174
175            if (chunkposition != null)
176            {
177                EntityEnderEye entityendereye = new EntityEnderEye(par2World, par3EntityPlayer.posX, par3EntityPlayer.posY + 1.62D - (double)par3EntityPlayer.yOffset, par3EntityPlayer.posZ);
178                entityendereye.moveTowards((double)chunkposition.x, chunkposition.y, (double)chunkposition.z);
179                par2World.spawnEntityInWorld(entityendereye);
180                par2World.playSoundAtEntity(par3EntityPlayer, "random.bow", 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));
181                par2World.playAuxSFXAtEntity((EntityPlayer)null, 1002, (int)par3EntityPlayer.posX, (int)par3EntityPlayer.posY, (int)par3EntityPlayer.posZ, 0);
182
183                if (!par3EntityPlayer.capabilities.isCreativeMode)
184                {
185                    --par1ItemStack.stackSize;
186                }
187            }
188        }
189
190        return par1ItemStack;
191    }
192}