Initiates a delayed teleport for a player which will be cancelled if the player moves. The messages relevant to this will be sent automatically. @param player The player to teleport @param loc The location to teleport the player to after the delay @param ticks The delay for the teleport, in tic
(Player player, Location loc, int ticks, Consumer<Boolean> result)
| 135 | * @param result A lambda to handle the result, given true if the teleport succeeded, false otherwise |
| 136 | */ |
| 137 | public static void delayedTeleport(Player player, Location loc, int ticks, Consumer<Boolean> result) { |
| 138 | double seconds = ticks / 20d; |
| 139 | player.sendMessage(RedLib.msg("teleportDelay").replace("%seconds%", timeFormat.format(seconds))); |
| 140 | Location start = player.getLocation(); |
| 141 | Task[] task = {null}; |
| 142 | EventListener<?> listener = new EventListener<>(RedLib.getInstance(), PlayerMoveEvent.class, (l, e) -> { |
| 143 | if (!e.getPlayer().equals(player)) { |
| 144 | return; |
| 145 | } |
| 146 | if (!start.getWorld().equals(e.getTo().getWorld()) || start.distanceSquared(e.getTo()) > 0.125) { |
| 147 | player.sendMessage(RedLib.msg("teleportCancelled")); |
| 148 | task[0].cancel(); |
| 149 | l.unregister(); |
| 150 | result.accept(false); |
| 151 | } |
| 152 | }); |
| 153 | task[0] = Task.syncDelayed(RedLib.getInstance(), () -> { |
| 154 | player.teleport(loc); |
| 155 | listener.unregister(); |
| 156 | result.accept(true); |
| 157 | }, ticks); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Initiates a delayed teleport for a player which will be cancelled if the player moves. The messages |
nothing calls this directly
no test coverage detected