1 package org.bukkit.command.defaults;
5 import org.apache.commons.lang.Validate;
6 import org.bukkit.Bukkit;
7 import org.bukkit.ChatColor;
8 import org.bukkit.Location;
9 import org.bukkit.command.Command;
10 import org.bukkit.command.CommandSender;
11 import org.bukkit.entity.Player;
12 import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
14 import com.google.common.collect.ImmutableList;
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>";
28 if (args.length < 1 || args.length > 4) {
35 if (args.length == 1 || args.length == 3) {
36 if (sender instanceof
Player) {
37 player = (Player) sender;
51 if (args.length < 3) {
54 sender.
sendMessage(
"Can't find player " + args[args.length - 1] +
". No tp.");
59 }
else if (player.
getWorld() != null) {
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]);
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!");
70 playerLocation.
setX(x);
71 playerLocation.
setY(y);
72 playerLocation.
setZ(z);
80 private double getCoordinate(
CommandSender sender,
double current, String input) {
81 return getCoordinate(sender, current, input, MIN_COORD, MAX_COORD);
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;
88 if (!relative || input.length() > 1) {
89 boolean exact = input.contains(
".");
90 if (relative) input = input.substring(1);
92 double testResult =
getDouble(sender, input);
93 if (testResult == MIN_COORD_MINUS_ONE) {
94 return MIN_COORD_MINUS_ONE;
98 if (!exact && !relative) result += 0.5f;
100 if (min != 0 || max != 0) {
102 result = MIN_COORD_MINUS_ONE;
106 result = MIN_COORD_MINUS_ONE;
115 Validate.notNull(sender,
"Sender cannot be null");
116 Validate.notNull(args,
"Arguments cannot be null");
117 Validate.notNull(alias,
"Alias cannot be null");
119 if (args.length == 1 || args.length == 2) {
120 return super.tabComplete(sender, alias, args);
122 return ImmutableList.of();