001 package org.bukkit.command.defaults; 002 003 import java.util.List; 004 005 import org.apache.commons.lang.Validate; 006 import org.bukkit.Bukkit; 007 import org.bukkit.ChatColor; 008 import org.bukkit.Location; 009 import org.bukkit.command.Command; 010 import org.bukkit.command.CommandSender; 011 import org.bukkit.entity.Player; 012 import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; 013 014 import com.google.common.collect.ImmutableList; 015 016 public class TeleportCommand extends VanillaCommand { 017 018 public TeleportCommand() { 019 super("tp"); 020 this.description = "Teleports the given player (or yourself) to another player or coordinates"; 021 this.usageMessage = "/tp [player] <target> and/or <x> <y> <z>"; 022 this.setPermission("bukkit.command.teleport"); 023 } 024 025 @Override 026 public boolean execute(CommandSender sender, String currentAlias, String[] args) { 027 if (!testPermission(sender)) return true; 028 if (args.length < 1 || args.length > 4) { 029 sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); 030 return false; 031 } 032 033 Player player; 034 035 if (args.length == 1 || args.length == 3) { 036 if (sender instanceof Player) { 037 player = (Player) sender; 038 } else { 039 sender.sendMessage("Please provide a player!"); 040 return true; 041 } 042 } else { 043 player = Bukkit.getPlayerExact(args[0]); 044 } 045 046 if (player == null) { 047 sender.sendMessage("Player not found: " + args[0]); 048 return true; 049 } 050 051 if (args.length < 3) { 052 Player target = Bukkit.getPlayerExact(args[args.length - 1]); 053 if (target == null) { 054 sender.sendMessage("Can't find player " + args[args.length - 1] + ". No tp."); 055 return true; 056 } 057 player.teleport(target, TeleportCause.COMMAND); 058 Command.broadcastCommandMessage(sender, "Teleported " + player.getDisplayName() + " to " + target.getDisplayName()); 059 } else if (player.getWorld() != null) { 060 Location playerLocation = player.getLocation(); 061 double x = getCoordinate(sender, playerLocation.getX(), args[args.length - 3]); 062 double y = getCoordinate(sender, playerLocation.getY(), args[args.length - 2], 0, 0); 063 double z = getCoordinate(sender, playerLocation.getZ(), args[args.length - 1]); 064 065 if (x == MIN_COORD_MINUS_ONE || y == MIN_COORD_MINUS_ONE || z == MIN_COORD_MINUS_ONE) { 066 sender.sendMessage("Please provide a valid location!"); 067 return true; 068 } 069 070 playerLocation.setX(x); 071 playerLocation.setY(y); 072 playerLocation.setZ(z); 073 074 player.teleport(playerLocation, TeleportCause.COMMAND); 075 Command.broadcastCommandMessage(sender, String.format("Teleported %s to %.2f, %.2f, %.2f", player.getDisplayName(), x, y, z)); 076 } 077 return true; 078 } 079 080 private double getCoordinate(CommandSender sender, double current, String input) { 081 return getCoordinate(sender, current, input, MIN_COORD, MAX_COORD); 082 } 083 084 private double getCoordinate(CommandSender sender, double current, String input, int min, int max) { 085 boolean relative = input.startsWith("~"); 086 double result = relative ? current : 0; 087 088 if (!relative || input.length() > 1) { 089 boolean exact = input.contains("."); 090 if (relative) input = input.substring(1); 091 092 double testResult = getDouble(sender, input); 093 if (testResult == MIN_COORD_MINUS_ONE) { 094 return MIN_COORD_MINUS_ONE; 095 } 096 result += testResult; 097 098 if (!exact && !relative) result += 0.5f; 099 } 100 if (min != 0 || max != 0) { 101 if (result < min) { 102 result = MIN_COORD_MINUS_ONE; 103 } 104 105 if (result > max) { 106 result = MIN_COORD_MINUS_ONE; 107 } 108 } 109 110 return result; 111 } 112 113 @Override 114 public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException { 115 Validate.notNull(sender, "Sender cannot be null"); 116 Validate.notNull(args, "Arguments cannot be null"); 117 Validate.notNull(alias, "Alias cannot be null"); 118 119 if (args.length == 1 || args.length == 2) { 120 return super.tabComplete(sender, alias, args); 121 } 122 return ImmutableList.of(); 123 } 124 }