Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
TeleportCommand.java
1 package org.bukkit.command.defaults;
2 
3 import java.util.List;
4 
5 import org.apache.commons.lang.Validate;
6 import org.bukkit.Bukkit;
7 import org.bukkit.ChatColor;
8 import org.bukkit.Location;
11 import org.bukkit.entity.Player;
13 
14 import com.google.common.collect.ImmutableList;
15 
16 public class TeleportCommand extends VanillaCommand {
17 
18  public TeleportCommand() {
19  super("tp");
20  this.description = "Teleports the given player (or yourself) to another player or coordinates";
21  this.usageMessage = "/tp [player] <target> and/or <x> <y> <z>";
22  this.setPermission("bukkit.command.teleport");
23  }
24 
25  @Override
26  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
27  if (!testPermission(sender)) return true;
28  if (args.length < 1 || args.length > 4) {
29  sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
30  return false;
31  }
32 
33  Player player;
34 
35  if (args.length == 1 || args.length == 3) {
36  if (sender instanceof Player) {
37  player = (Player) sender;
38  } else {
39  sender.sendMessage("Please provide a player!");
40  return true;
41  }
42  } else {
43  player = Bukkit.getPlayerExact(args[0]);
44  }
45 
46  if (player == null) {
47  sender.sendMessage("Player not found: " + args[0]);
48  return true;
49  }
50 
51  if (args.length < 3) {
52  Player target = Bukkit.getPlayerExact(args[args.length - 1]);
53  if (target == null) {
54  sender.sendMessage("Can't find player " + args[args.length - 1] + ". No tp.");
55  return true;
56  }
57  player.teleport(target, TeleportCause.COMMAND);
58  Command.broadcastCommandMessage(sender, "Teleported " + player.getDisplayName() + " to " + target.getDisplayName());
59  } else if (player.getWorld() != null) {
60  Location playerLocation = player.getLocation();
61  double x = getCoordinate(sender, playerLocation.getX(), args[args.length - 3]);
62  double y = getCoordinate(sender, playerLocation.getY(), args[args.length - 2], 0, 0);
63  double z = getCoordinate(sender, playerLocation.getZ(), args[args.length - 1]);
64 
65  if (x == MIN_COORD_MINUS_ONE || y == MIN_COORD_MINUS_ONE || z == MIN_COORD_MINUS_ONE) {
66  sender.sendMessage("Please provide a valid location!");
67  return true;
68  }
69 
70  playerLocation.setX(x);
71  playerLocation.setY(y);
72  playerLocation.setZ(z);
73 
74  player.teleport(playerLocation, TeleportCause.COMMAND);
75  Command.broadcastCommandMessage(sender, String.format("Teleported %s to %.2f, %.2f, %.2f", player.getDisplayName(), x, y, z));
76  }
77  return true;
78  }
79 
80  private double getCoordinate(CommandSender sender, double current, String input) {
81  return getCoordinate(sender, current, input, MIN_COORD, MAX_COORD);
82  }
83 
84  private double getCoordinate(CommandSender sender, double current, String input, int min, int max) {
85  boolean relative = input.startsWith("~");
86  double result = relative ? current : 0;
87 
88  if (!relative || input.length() > 1) {
89  boolean exact = input.contains(".");
90  if (relative) input = input.substring(1);
91 
92  double testResult = getDouble(sender, input);
93  if (testResult == MIN_COORD_MINUS_ONE) {
94  return MIN_COORD_MINUS_ONE;
95  }
96  result += testResult;
97 
98  if (!exact && !relative) result += 0.5f;
99  }
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 }
boolean testPermission(CommandSender target)
boolean teleport(Location location)
void setX(double x)
Definition: Location.java:90
void sendMessage(String message)
void setPermission(String permission)
void setY(double y)
Definition: Location.java:118
static Player getPlayerExact(String name)
Definition: Bukkit.java:436
void setZ(double z)
Definition: Location.java:146