-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathFoliaJoinHelper.java
More file actions
63 lines (52 loc) · 2.31 KB
/
Copy pathFoliaJoinHelper.java
File metadata and controls
63 lines (52 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package fr.maxlego08.essentials.zutils.utils;
import fr.maxlego08.essentials.api.EssentialsPlugin;
import fr.maxlego08.essentials.storage.ConfigStorage;
import org.bukkit.Location;
import org.bukkit.entity.Player;
/**
* Folia-safe helpers for player join flows.
* <p>
* Teleports must not run during {@link org.bukkit.event.player.PlayerJoinEvent}
* or {@link org.bukkit.event.player.PlayerLoginEvent}; the player is still being placed in the world.
*/
public final class FoliaJoinHelper {
private static final double SPAWN_TOLERANCE_SQUARED = 1.0;
private static final long JOIN_TELEPORT_DELAY_TICKS = 1L;
public static final long SAFE_LOGIN_DELAY_TICKS = 5L;
private FoliaJoinHelper() {
}
public static Location resolveFirstSpawnLocation() {
if (ConfigStorage.firstSpawnLocation != null && ConfigStorage.firstSpawnLocation.isValid()) {
return ConfigStorage.firstSpawnLocation.getLocation();
}
if (ConfigStorage.spawnLocation != null && ConfigStorage.spawnLocation.isValid()) {
return ConfigStorage.spawnLocation.getLocation();
}
return null;
}
public static void teleportFirstSpawnAfterJoin(EssentialsPlugin plugin, Player player) {
teleportAfterJoin(plugin, player, resolveFirstSpawnLocation(), true);
}
public static void teleportAfterJoin(EssentialsPlugin plugin, Player player, Location target) {
teleportAfterJoin(plugin, player, target, false);
}
public static void teleportAfterJoin(EssentialsPlugin plugin, Player player, Location target, boolean skipWhenAlreadyThere) {
if (player == null || target == null || target.getWorld() == null) {
return;
}
Location destination = target;
plugin.getScheduler().runAtLocationLater(player.getLocation(), () -> {
if (!player.isOnline()) {
return;
}
if (skipWhenAlreadyThere) {
Location current = player.getLocation();
if (current.getWorld().equals(destination.getWorld())
&& current.distanceSquared(destination) < SPAWN_TOLERANCE_SQUARED) {
return;
}
}
plugin.getScheduler().teleportAsync(player, destination);
}, JOIN_TELEPORT_DELAY_TICKS);
}
}