Actually make /tp <x, y, z> work

This commit is contained in:
Spencer 2018-03-07 14:10:35 -05:00 committed by Alexander Meech
parent 42a0da0d3e
commit b71538b700

View File

@ -30,6 +30,32 @@ public class TeleportCommand extends BaseCommand
return String.format(COORDINATE_FORMAT, in); return String.format(COORDINATE_FORMAT, in);
} }
private static Double parseCoordinate(String in, double beginning)
{
try
{
if (in.startsWith("~"))
{
if (in.length() == 1)
{
return beginning;
}
String relativeIn = in.substring(1);
double relative = Double.parseDouble(relativeIn);
return beginning + relative;
}
return Double.parseDouble(in);
}
catch (NumberFormatException ex)
{
return null;
}
}
@Override @Override
public boolean execute(Player player, String alias, String[] args) public boolean execute(Player player, String alias, String[] args)
{ {
@ -82,30 +108,30 @@ public class TeleportCommand extends BaseCommand
// Teleport to coordinates... // Teleport to coordinates...
else if (args.length == 3) else if (args.length == 3)
{ {
List<Float> coordinates = new ArrayList<>(); List<Double> coordinates = new ArrayList<>();
try coordinates.add(parseCoordinate(args[0], player.getLocation().getX()));
coordinates.add(parseCoordinate(args[1], player.getLocation().getY()));
coordinates.add(parseCoordinate(args[2], player.getLocation().getZ()));
for (Double coordinate : coordinates)
{ {
for (String arg : args) if (coordinate == null)
{
coordinates.add(Float.parseFloat(arg));
}
}
catch (NumberFormatException ex)
{ {
message(player, "Hmm, those coordinates don't look quite right."); message(player, "Hmm, those coordinates don't look quite right.");
return true; return true;
} }
}
Location destination = player.getLocation().clone(); Location destination = player.getLocation().clone();
destination.setX(coordinates.get(0)); destination.setX(coordinates.get(0));
destination.setX(coordinates.get(1)); destination.setY(coordinates.get(1));
destination.setX(coordinates.get(2)); destination.setZ(coordinates.get(2));
message(player, "You teleported to coordinates (" message(player, "You teleported to ("
+ F.name(formatCoordinate(destination.getZ())) + F.name(formatCoordinate(destination.getX()))
+ C.mBody + ", " + C.mBody + ", "
+ F.name(formatCoordinate(destination.getZ())) + F.name(formatCoordinate(destination.getY()))
+ C.mBody + ", " + C.mBody + ", "
+ F.name(formatCoordinate(destination.getZ())) + F.name(formatCoordinate(destination.getZ()))
+ C.mBody + ")."); + C.mBody + ").");