    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 var11 = par3World.getBlockId(par4, par5, par6);
    029        int var12 = par3World.getBlockMetadata(par4, par5, par6);
    030
    031        if (par2EntityPlayer.canPlayerEdit(par4, par5, par6, par7, par1ItemStack) && var11 == Block.endPortalFrame.blockID && !BlockEndPortalFrame.isEnderEyeInserted(var12))
    032        {
    033            if (par3World.isRemote)
    034            {
    035                return true;
    036            }
    037            else
    038            {
    039                par3World.setBlockMetadataWithNotify(par4, par5, par6, var12 + 4);
    040                --par1ItemStack.stackSize;
    041                int var13;
    042
    043                for (var13 = 0; var13 < 16; ++var13)
    044                {
    045                    double var14 = (double)((float)par4 + (5.0F + itemRand.nextFloat() * 6.0F) / 16.0F);
    046                    double var16 = (double)((float)par5 + 0.8125F);
    047                    double var18 = (double)((float)par6 + (5.0F + itemRand.nextFloat() * 6.0F) / 16.0F);
    048                    double var20 = 0.0D;
    049                    double var22 = 0.0D;
    050                    double var24 = 0.0D;
    051                    par3World.spawnParticle("smoke", var14, var16, var18, var20, var22, var24);
    052                }
    053
    054                var13 = var12 & 3;
    055                int var26 = 0;
    056                int var15 = 0;
    057                boolean var27 = false;
    058                boolean var17 = true;
    059                int var28 = Direction.enderEyeMetaToDirection[var13];
    060                int var19;
    061                int var21;
    062                int var23;
    063                int var29;
    064                int var30;
    065
    066                for (var19 = -2; var19 <= 2; ++var19)
    067                {
    068                    var29 = par4 + Direction.offsetX[var28] * var19;
    069                    var21 = par6 + Direction.offsetZ[var28] * var19;
    070                    var30 = par3World.getBlockId(var29, par5, var21);
    071
    072                    if (var30 == Block.endPortalFrame.blockID)
    073                    {
    074                        var23 = par3World.getBlockMetadata(var29, par5, var21);
    075
    076                        if (!BlockEndPortalFrame.isEnderEyeInserted(var23))
    077                        {
    078                            var17 = false;
    079                            break;
    080                        }
    081
    082                        var15 = var19;
    083
    084                        if (!var27)
    085                        {
    086                            var26 = var19;
    087                            var27 = true;
    088                        }
    089                    }
    090                }
    091
    092                if (var17 && var15 == var26 + 2)
    093                {
    094                    for (var19 = var26; var19 <= var15; ++var19)
    095                    {
    096                        var29 = par4 + Direction.offsetX[var28] * var19;
    097                        var21 = par6 + Direction.offsetZ[var28] * var19;
    098                        var29 += Direction.offsetX[var13] * 4;
    099                        var21 += Direction.offsetZ[var13] * 4;
    100                        var30 = par3World.getBlockId(var29, par5, var21);
    101                        var23 = par3World.getBlockMetadata(var29, par5, var21);
    102
    103                        if (var30 != Block.endPortalFrame.blockID || !BlockEndPortalFrame.isEnderEyeInserted(var23))
    104                        {
    105                            var17 = false;
    106                            break;
    107                        }
    108                    }
    109
    110                    for (var19 = var26 - 1; var19 <= var15 + 1; var19 += 4)
    111                    {
    112                        for (var29 = 1; var29 <= 3; ++var29)
    113                        {
    114                            var21 = par4 + Direction.offsetX[var28] * var19;
    115                            var30 = par6 + Direction.offsetZ[var28] * var19;
    116                            var21 += Direction.offsetX[var13] * var29;
    117                            var30 += Direction.offsetZ[var13] * var29;
    118                            var23 = par3World.getBlockId(var21, par5, var30);
    119                            int var31 = par3World.getBlockMetadata(var21, par5, var30);
    120
    121                            if (var23 != Block.endPortalFrame.blockID || !BlockEndPortalFrame.isEnderEyeInserted(var31))
    122                            {
    123                                var17 = false;
    124                                break;
    125                            }
    126                        }
    127                    }
    128
    129                    if (var17)
    130                    {
    131                        for (var19 = var26; var19 <= var15; ++var19)
    132                        {
    133                            for (var29 = 1; var29 <= 3; ++var29)
    134                            {
    135                                var21 = par4 + Direction.offsetX[var28] * var19;
    136                                var30 = par6 + Direction.offsetZ[var28] * var19;
    137                                var21 += Direction.offsetX[var13] * var29;
    138                                var30 += Direction.offsetZ[var13] * var29;
    139                                par3World.setBlockWithNotify(var21, par5, var30, Block.endPortal.blockID);
    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 var4 = this.getMovingObjectPositionFromPlayer(par2World, par3EntityPlayer, false);
    160
    161        if (var4 != null && var4.typeOfHit == EnumMovingObjectType.TILE)
    162        {
    163            int var5 = par2World.getBlockId(var4.blockX, var4.blockY, var4.blockZ);
    164
    165            if (var5 == Block.endPortalFrame.blockID)
    166            {
    167                return par1ItemStack;
    168            }
    169        }
    170
    171        if (!par2World.isRemote)
    172        {
    173            ChunkPosition var7 = par2World.findClosestStructure("Stronghold", (int)par3EntityPlayer.posX, (int)par3EntityPlayer.posY, (int)par3EntityPlayer.posZ);
    174
    175            if (var7 != null)
    176            {
    177                EntityEnderEye var6 = new EntityEnderEye(par2World, par3EntityPlayer.posX, par3EntityPlayer.posY + 1.62D - (double)par3EntityPlayer.yOffset, par3EntityPlayer.posZ);
    178                var6.moveTowards((double)var7.x, var7.y, (double)var7.z);
    179                par2World.spawnEntityInWorld(var6);
    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}