Add DestinationFactory#getPlayerAwareDestination to parse cannon dest.

This commit is contained in:
benwoo1110 2020-12-28 22:06:51 +08:00
parent fee822a2fe
commit 81f6014e94
2 changed files with 28 additions and 24 deletions

View File

@ -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) {

View File

@ -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.