    001package net.minecraft.command;
    002
    003import java.util.List;
    004import net.minecraft.entity.Entity;
    005import net.minecraft.entity.player.EntityPlayerMP;
    006import net.minecraft.server.MinecraftServer;
    007
    008public class CommandServerTp extends CommandBase
    009{
    010    public String getCommandName()
    011    {
    012        return "tp";
    013    }
    014
    015    /**
    016     * Return the required permission level for this command.
    017     */
    018    public int getRequiredPermissionLevel()
    019    {
    020        return 2;
    021    }
    022
    023    public String getCommandUsage(ICommandSender par1ICommandSender)
    024    {
    025        return par1ICommandSender.translateString("commands.tp.usage", new Object[0]);
    026    }
    027
    028    public void processCommand(ICommandSender par1ICommandSender, String[] par2ArrayOfStr)
    029    {
    030        if (par2ArrayOfStr.length < 1)
    031        {
    032            throw new WrongUsageException("commands.tp.usage", new Object[0]);
    033        }
    034        else
    035        {
    036            EntityPlayerMP entityplayermp;
    037
    038            if (par2ArrayOfStr.length != 2 && par2ArrayOfStr.length != 4)
    039            {
    040                entityplayermp = getCommandSenderAsPlayer(par1ICommandSender);
    041            }
    042            else
    043            {
    044                entityplayermp = func_82359_c(par1ICommandSender, par2ArrayOfStr[0]);
    045
    046                if (entityplayermp == null)
    047                {
    048                    throw new PlayerNotFoundException();
    049                }
    050            }
    051
    052            if (par2ArrayOfStr.length != 3 && par2ArrayOfStr.length != 4)
    053            {
    054                if (par2ArrayOfStr.length == 1 || par2ArrayOfStr.length == 2)
    055                {
    056                    EntityPlayerMP entityplayermp1 = func_82359_c(par1ICommandSender, par2ArrayOfStr[par2ArrayOfStr.length - 1]);
    057
    058                    if (entityplayermp1 == null)
    059                    {
    060                        throw new PlayerNotFoundException();
    061                    }
    062
    063                    if (entityplayermp1.worldObj != entityplayermp.worldObj)
    064                    {
    065                        notifyAdmins(par1ICommandSender, "commands.tp.notSameDimension", new Object[0]);
    066                        return;
    067                    }
    068
    069                    entityplayermp.mountEntity((Entity)null);
    070                    entityplayermp.playerNetServerHandler.setPlayerLocation(entityplayermp1.posX, entityplayermp1.posY, entityplayermp1.posZ, entityplayermp1.rotationYaw, entityplayermp1.rotationPitch);
    071                    notifyAdmins(par1ICommandSender, "commands.tp.success", new Object[] {entityplayermp.getEntityName(), entityplayermp1.getEntityName()});
    072                }
    073            }
    074            else if (entityplayermp.worldObj != null)
    075            {
    076                int i = par2ArrayOfStr.length - 3;
    077                double d0 = this.func_82368_a(par1ICommandSender, entityplayermp.posX, par2ArrayOfStr[i++]);
    078                double d1 = this.func_82367_a(par1ICommandSender, entityplayermp.posY, par2ArrayOfStr[i++], 0, 0);
    079                double d2 = this.func_82368_a(par1ICommandSender, entityplayermp.posZ, par2ArrayOfStr[i++]);
    080                entityplayermp.mountEntity((Entity)null);
    081                entityplayermp.setPositionAndUpdate(d0, d1, d2);
    082                notifyAdmins(par1ICommandSender, "commands.tp.success.coordinates", new Object[] {entityplayermp.getEntityName(), Double.valueOf(d0), Double.valueOf(d1), Double.valueOf(d2)});
    083            }
    084        }
    085    }
    086
    087    private double func_82368_a(ICommandSender par1ICommandSender, double par2, String par4Str)
    088    {
    089        return this.func_82367_a(par1ICommandSender, par2, par4Str, -30000000, 30000000);
    090    }
    091
    092    private double func_82367_a(ICommandSender par1ICommandSender, double par2, String par4Str, int par5, int par6)
    093    {
    094        boolean flag = par4Str.startsWith("~");
    095        double d1 = flag ? par2 : 0.0D;
    096
    097        if (!flag || par4Str.length() > 1)
    098        {
    099            boolean flag1 = par4Str.contains(".");
    100
    101            if (flag)
    102            {
    103                par4Str = par4Str.substring(1);
    104            }
    105
    106            d1 += func_82363_b(par1ICommandSender, par4Str);
    107
    108            if (!flag1 && !flag)
    109            {
    110                d1 += 0.5D;
    111            }
    112        }
    113
    114        if (par5 != 0 || par6 != 0)
    115        {
    116            if (d1 < (double)par5)
    117            {
    118                throw new NumberInvalidException("commands.generic.double.tooSmall", new Object[] {Double.valueOf(d1), Integer.valueOf(par5)});
    119            }
    120
    121            if (d1 > (double)par6)
    122            {
    123                throw new NumberInvalidException("commands.generic.double.tooBig", new Object[] {Double.valueOf(d1), Integer.valueOf(par6)});
    124            }
    125        }
    126
    127        return d1;
    128    }
    129
    130    /**
    131     * Adds the strings available in this command to the given list of tab completion options.
    132     */
    133    public List addTabCompletionOptions(ICommandSender par1ICommandSender, String[] par2ArrayOfStr)
    134    {
    135        return par2ArrayOfStr.length != 1 && par2ArrayOfStr.length != 2 ? null : getListOfStringsMatchingLastWord(par2ArrayOfStr, MinecraftServer.getServer().getAllUsernames());
    136    }
    137
    138    /**
    139     * Return whether the specified command parameter index is a username parameter.
    140     */
    141    public boolean isUsernameIndex(String[] par1ArrayOfStr, int par2)
    142    {
    143        return par2 == 0;
    144    }
    145}