From 81f6014e94a5566e781b9c95659aa5cbb818c493 Mon Sep 17 00:00:00 2001 From: benwoo1110 <30431861+benwoo1110@users.noreply.github.com> Date: Mon, 28 Dec 2020 22:06:51 +0800 Subject: [PATCH] Add DestinationFactory#getPlayerAwareDestination to parse cannon dest. --- .../commands/TeleportCommand.java | 25 +---------------- .../destination/DestinationFactory.java | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java index dfb6b093..e254d239 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java @@ -102,8 +102,7 @@ public class TeleportCommand extends MultiverseCommand { @NotNull Player teleportee, @NotNull String destinationName) { - destinationName = parseCannonDestination(teleportee, destinationName); - MVDestination destination = this.plugin.getDestFactory().getDestination(destinationName); + MVDestination destination = this.plugin.getDestFactory().getPlayerAwareDestination(destinationName, teleportee); MVTeleportEvent teleportEvent = new MVTeleportEvent(destination, teleportee, teleporter, true); this.plugin.getServer().getPluginManager().callEvent(teleportEvent); @@ -165,28 +164,6 @@ public class TeleportCommand extends MultiverseCommand { // else: Player was teleported successfully (or the tp event was fired I should say) } - @NotNull - private String parseCannonDestination(@NotNull Player teleportee, - @NotNull String destinationName) { - - if (!destinationName.matches("(?i)cannon-[\\d]+(\\.[\\d]+)?")) { - return 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; - } - private boolean checkSendPermissions(@NotNull CommandSender teleporter, @NotNull Player teleportee, @NotNull MVDestination destination) { diff --git a/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationFactory.java b/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationFactory.java index 7d993c9f..9940fbc4 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationFactory.java +++ b/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationFactory.java @@ -11,8 +11,10 @@ import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVDestination; import com.onarandombox.MultiverseCore.utils.PermissionTools; +import org.bukkit.entity.Player; import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; +import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.HashMap; @@ -34,6 +36,31 @@ public class DestinationFactory { this.permTools = new PermissionTools(plugin); } + public MVDestination getPlayerAwareDestination(String destination, Player player) { + return getDestination(parseCannonDest(player, destination)); + } + + private String parseCannonDest(@NotNull Player teleportee, + @NotNull String destinationName) { + + if (!destinationName.matches("(?i)cannon-[\\d]+(\\.[\\d]+)?")) { + return 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.