package net.knarcraft.stargate.utility; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.portal.teleporter.EntityTeleporter; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Creature; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.util.Vector; import java.util.ArrayList; import java.util.List; /** * A helper class with methods for various teleportation tasks * *
The teleport helper mainly helps with passengers and leashed creatures
*/ public final class TeleportHelper { private TeleportHelper() { } /** * Checks whether a player has leashed creatures that block the teleportation * * @param playerThe player trying to teleport
* @returnFalse if the player has leashed any creatures that cannot go through the portal
*/ public static boolean noLeashedCreaturesPreventTeleportation(Player player) { //Find any nearby leashed entities to teleport with the player ListThe player to check
* @returnA list of all creatures the player is holding in a leash (lead)
*/ public static ListTeleportation of living vehicles is really buggy if you wait between the teleportation and passenger adding, * but there needs to be a delay between teleporting the vehicle and teleporting and adding the passenger.
* * @param targetVehicleThe entity to add the passenger to
* @param passengerThe passenger to teleport and add
* @param exitDirectionThe direction of any passengers exiting the stargate
*/ public static void teleportAndAddPassenger(Entity targetVehicle, Entity passenger, Vector exitDirection) { Location passengerExit = targetVehicle.getLocation().clone().setDirection(exitDirection); if (!passenger.teleport(passengerExit)) { Stargate.debug("TeleportHelper::handleVehiclePassengers", "Failed to teleport passenger" + passenger); } else { Stargate.debug("TeleportHelper::handleVehiclePassengers", "Teleported " + passenger + " to " + passengerExit); } if (!targetVehicle.addPassenger(passenger)) { Stargate.debug("TeleportHelper::handleVehiclePassengers", "Failed to add passenger" + passenger); } else { Stargate.debug("TeleportHelper::handleVehiclePassengers", "Added passenger " + passenger + " to " + targetVehicle); } } /** * Ejects, teleports and adds all passengers to the target entity * * @param passengersThe passengers to handle
* @param entityThe entity the passengers should be put into
The portal the entity teleported from * @param targetThe portal the entity is teleporting to
* @param exitRotationThe rotation of any passengers exiting the stargate
*/ public static void handleEntityPassengers(ListWill return false if the teleportation should be aborted because the player has leashed creatures that * aren't allowed to be teleported with the player.
* * @param playerThe player which is teleported
* @param originThe portal the player is teleporting from
* @param targetThe portal the player is teleporting to
*/ public static void teleportLeashedCreatures(Player player, Portal origin, Portal target) { //If this feature is disabled, just return if (!Stargate.getGateConfig().handleLeashedCreatures()) { return; } BukkitScheduler scheduler = Bukkit.getScheduler(); //Find any nearby leashed entities to teleport with the player ListThe list of entities to check
* @returnTrue if at least one entity is not a player
*/ public static boolean containsNonPlayer(ListThe list of entities to check
* @returnTrue if at least one player is present among the passengers
*/ public static boolean containsPlayer(ListThe entities to check for players
* @returnThe found players
*/ public static ListThe player trying to teleport
* @param entrancePortalThe portal the player is entering
* @param destinationPortalThe portal the player is to exit from
* @returnTrue if the player is allowed to teleport and is able to pay necessary fees
*/ public static boolean playerCanTeleport(Player player, Portal entrancePortal, Portal destinationPortal) { //Make sure the user can access the portal if (PermissionHelper.cannotAccessPortal(player, entrancePortal, destinationPortal)) { if (!entrancePortal.getOptions().isSilent()) { Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString("denyMsg")); } entrancePortal.getPortalOpener().closePortal(false); return false; } //Check if the player is able to afford the teleport fee int cost = EconomyHelper.getUseCost(player, entrancePortal, destinationPortal); boolean canAffordFee = cost <= 0 || Stargate.getEconomyConfig().canAffordFee(player, cost); if (!canAffordFee) { if (!entrancePortal.getOptions().isSilent()) { Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString("ecoInFunds")); } return false; } return TeleportHelper.noLeashedCreaturesPreventTeleportation(player); } }