diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java index b2f8bd03..acb51fbc 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java @@ -29,7 +29,6 @@ import org.bukkit.permissions.PermissionDefault; import java.util.ArrayList; import java.util.List; -import java.util.logging.Level; /** * Used to teleport players. @@ -76,22 +75,9 @@ public class TeleportCommand extends MultiverseCommand { } teleportee = (Player) sender; } - // Special case for cannons: - if (destinationName.matches("(?i)cannon-[\\d]+(\\.[\\d]+)?")) { - String[] cannonSpeed = destinationName.split("-"); - try { - double speed = Double.parseDouble(cannonSpeed[1]); - destinationName = "ca:" + teleportee.getWorld().getName() + ":" + teleportee.getLocation().getX() - + "," + teleportee.getLocation().getY() + "," + teleportee.getLocation().getZ() + ":" - + teleportee.getLocation().getPitch() + ":" + teleportee.getLocation().getYaw() + ":" + speed; - } catch (Exception e) { - destinationName = "i:invalid"; - } - } DestinationFactory df = this.plugin.getDestFactory(); - MVDestination d = df.getDestination(destinationName); - + MVDestination d = df.getPlayerAwareDestination(teleportee, destinationName); MVTeleportEvent teleportEvent = new MVTeleportEvent(d, teleportee, teleporter, true); this.plugin.getServer().getPluginManager().callEvent(teleportEvent); diff --git a/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationFactory.java b/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationFactory.java index 2b80e153..27d756a6 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationFactory.java +++ b/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationFactory.java @@ -11,16 +11,26 @@ import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVDestination; import com.onarandombox.MultiverseCore.commands.TeleportCommand; import com.onarandombox.MultiverseCore.utils.PermissionTools; +import com.onarandombox.MultiverseCore.utils.PlayerFinder; import com.pneumaticraft.commandhandler.Command; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.regex.Pattern; /** A factory class that will create destinations from specific strings. */ public class DestinationFactory { + + private static final Pattern CANNON_PATTERN = Pattern.compile("(?i)cannon-[\\d]+(\\.[\\d]+)?"); + private MultiverseCore plugin; private Map> destList; private Command teleportCommand; @@ -36,6 +46,59 @@ public class DestinationFactory { } } + /** + * Parse a destination that has relation to sender, such as a cannon or player destination. + * + * @param teleportee The player that is going to be teleported. + * @param destinationName The destination to parse. + * @return A non-null MVDestination + */ + @NotNull + public MVDestination getPlayerAwareDestination(@NotNull Player teleportee, + @NotNull String destinationName) { + + // Prioritise world, in the event that a world is named after a player online. + if (Bukkit.getWorld(destinationName) != null) { + return getDestination(destinationName); + } + + Player targetPlayer = PlayerFinder.get(destinationName, teleportee); + if (targetPlayer != null) { + return getDestination("pl:" + targetPlayer.getName()); + } + + if (CANNON_PATTERN.matcher(destinationName).matches()) { + return getDestination(parseCannonDest(teleportee, destinationName)); + } + + return getDestination(destinationName); + } + + /** + * Parses a cannon destination. + * + * @param teleportee The player that is going to be teleported. + * @param destinationName The destination to parse. + * @return A destination string. + */ + @NotNull + private String parseCannonDest(@NotNull Player teleportee, + @NotNull String destinationName) { + + String[] cannonSpeed = destinationName.split("-"); + try { + double speed = Double.parseDouble(cannonSpeed[1]); + destinationName = "ca:" + teleportee.getWorld().getName() + ":" + teleportee.getLocation().getX() + + "," + teleportee.getLocation().getY() + "," + teleportee.getLocation().getZ() + ":" + + teleportee.getLocation().getPitch() + ":" + teleportee.getLocation().getYaw() + ":" + speed; + } + catch (Exception e) { + destinationName = "i:invalid"; + } + + return destinationName; + } + /** * Gets a new destination from a string. * Returns a new InvalidDestination if the string could not be parsed.