mirror of
https://github.com/SunNetservers/MiniGames.git
synced 2025-07-19 06:24:43 +02:00
Parkour implementation safety save 2
This is just a safety save in case the code gets too broken to fix.
This commit is contained in:
@ -0,0 +1,67 @@
|
||||
package net.knarcraft.minigames.util;
|
||||
|
||||
import net.knarcraft.minigames.MiniGames;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
/**
|
||||
* A helper class for teleporting players
|
||||
*/
|
||||
public final class PlayerTeleporter {
|
||||
|
||||
private PlayerTeleporter() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Teleports the given player to the given location
|
||||
*
|
||||
* <p>Forcing teleport should only be used inside an arena, to prevent the player from becoming stuck.</p>
|
||||
*
|
||||
* @param player <p>The player about to teleport</p>
|
||||
* @param location <p>The location the player should be teleported to</p>
|
||||
* @param force <p>Whether to force a player teleport, even in a vehicle or a passenger</p>
|
||||
* @param immediately <p>Whether to to the teleportation immediately, not using any timers</p>
|
||||
* @return <p>True if the player was successfully teleported</p>
|
||||
*/
|
||||
public static boolean teleportPlayer(Player player, Location location, boolean force, boolean immediately) {
|
||||
if (!player.getPassengers().isEmpty()) {
|
||||
if (force) {
|
||||
for (Entity passenger : player.getPassengers()) {
|
||||
passenger.eject();
|
||||
passenger.teleport(location);
|
||||
}
|
||||
} else {
|
||||
player.sendMessage("You cannot be teleported with a passenger!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (player.isInsideVehicle()) {
|
||||
if (force && player.getVehicle() != null) {
|
||||
Entity vehicle = player.getVehicle();
|
||||
player.eject();
|
||||
vehicle.teleport(location);
|
||||
} else {
|
||||
player.sendMessage("You cannot be teleported while in a vehicle");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//Stop the existing player velocity to prevent unevenness between players
|
||||
player.setVelocity(new Vector(0, 0, 0));
|
||||
player.setInvulnerable(true);
|
||||
player.teleport(location);
|
||||
player.setVelocity(new Vector(0, 0, 0));
|
||||
//When teleporting a player out of the arena, sometimes the move listener is slow to react, giving the player
|
||||
// lethal velocity, and causing damage. That's why the player is given 5 ticks of invulnerability
|
||||
if (!immediately) {
|
||||
Bukkit.getScheduler().runTaskLater(MiniGames.getInstance(), () -> player.setInvulnerable(false), 5);
|
||||
} else {
|
||||
player.setInvulnerable(false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user