MiniGames/src/main/java/net/knarcraft/dropper/util/PlayerTeleporter.java
EpicKnarvik97 9a7d3ca360 Implements #5 and stuff
Uses a player entry state to store and restore a player's state such as game-mode and fly-mode.
Removes the restriction of fly mode and game-mode, and sets all arena players to adventure mode.
Force-quits all sessions when exiting the plugin.
Prevents usage of any commands other than the leave command when playing in an arena.
2023-03-27 14:23:55 +02:00

68 lines
2.5 KiB
Java

package net.knarcraft.dropper.util;
import net.knarcraft.dropper.Dropper;
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(Dropper.getInstance(), () -> player.setInvulnerable(false), 5);
} else {
player.setInvulnerable(false);
}
return true;
}
}