-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathSpawnModule.java
More file actions
141 lines (115 loc) · 5.54 KB
/
Copy pathSpawnModule.java
File metadata and controls
141 lines (115 loc) · 5.54 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package fr.maxlego08.essentials.module.modules;
import fr.maxlego08.essentials.ZEssentialsPlugin;
import fr.maxlego08.essentials.api.messages.Message;
import fr.maxlego08.essentials.api.user.User;
import fr.maxlego08.essentials.module.ZModule;
import fr.maxlego08.essentials.storage.ConfigStorage;
import fr.maxlego08.essentials.zutils.utils.FoliaJoinHelper;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.plugin.PluginManager;
import org.spigotmc.event.player.PlayerSpawnLocationEvent;
public class SpawnModule extends ZModule {
private final String respawnListenerPriority = "normal";
private final String spawnJoinListenerPriority = "normal";
private boolean respawnAtAnchor;
private boolean respawnAtHome;
private boolean respawnAtBed;
private boolean teleportAtSpawnOnJoin;
public SpawnModule(ZEssentialsPlugin plugin) {
super(plugin, "spawn");
}
@Override
public void loadConfiguration() {
super.loadConfiguration();
if (!this.isEnable()) return;
PluginManager pluginManager = Bukkit.getServer().getPluginManager();
EventPriority respawnPriority = getPriority(this.respawnListenerPriority);
HandlerList.unregisterAll(this);
if (respawnPriority != null) {
pluginManager.registerEvent(PlayerRespawnEvent.class, this, respawnPriority, (listener, event) -> {
if (listener instanceof SpawnModule spawnModule && event instanceof PlayerRespawnEvent playerRespawnEvent) {
spawnModule.onRespawn(playerRespawnEvent, playerRespawnEvent.getPlayer());
}
}, this.plugin);
}
EventPriority spawnPriority = getPriority(this.spawnJoinListenerPriority);
pluginManager.registerEvent(PlayerSpawnLocationEvent.class, this, spawnPriority, (listener, event) -> {
if (listener instanceof SpawnModule spawnModule && event instanceof PlayerSpawnLocationEvent playerSpawnLocationEvent) {
spawnModule.onSpawnLocation(playerSpawnLocationEvent, playerSpawnLocationEvent.getPlayer());
}
}, this.plugin);
if (this.plugin.isFolia()) {
pluginManager.registerEvent(PlayerDeathEvent.class, this, EventPriority.LOWEST, (listener, event) -> {
if (listener instanceof SpawnModule spawnModule && event instanceof PlayerDeathEvent playerDeathEvent) {
spawnModule.onPlayerDeath(playerDeathEvent, playerDeathEvent.getPlayer());
}
}, this.plugin);
}
if (this.teleportAtSpawnOnJoin) {
pluginManager.registerEvent(PlayerJoinEvent.class, this, EventPriority.LOWEST, (listener, event) -> {
if (listener instanceof SpawnModule spawnModule && event instanceof PlayerJoinEvent playerJoinEvent) {
var player = playerJoinEvent.getPlayer();
if (ConfigStorage.spawnLocation != null && ConfigStorage.spawnLocation.isValid()) {
Location spawnLoc = ConfigStorage.spawnLocation.getLocation();
if (spawnLoc != null) {
FoliaJoinHelper.teleportAfterJoin(spawnModule.plugin, player, spawnLoc, true);
}
}
}
}, this.plugin);
}
}
private void onPlayerDeath(PlayerDeathEvent playerDeathEvent, Player player) {
/*try {
Location respawnLocation = player.getRespawnLocation();
if (this.respawnAtAnchor && respawnLocation != null) return;
} catch (Exception ignored) {
}*/
if (this.respawnAtBed) {
// ToDo
}
if (this.respawnAtHome) {
// ToDo
}
if (ConfigStorage.spawnLocation != null && ConfigStorage.spawnLocation.isValid()) {
player.setRespawnLocation(ConfigStorage.spawnLocation.getLocation(), true);
} else {
message(player, Message.COMMAND_SPAWN_LOCATION_INVALID);
}
}
public void onSpawnLocation(PlayerSpawnLocationEvent event, Player player) {
User user = getUser(player);
if (user != null && user.isFirstJoin()) {
if (ConfigStorage.firstSpawnLocation != null && ConfigStorage.firstSpawnLocation.isValid()) {
event.setSpawnLocation(ConfigStorage.firstSpawnLocation.getLocation());
} else if (ConfigStorage.spawnLocation != null && ConfigStorage.spawnLocation.isValid()) {
event.setSpawnLocation(ConfigStorage.spawnLocation.getLocation());
}
}
}
private void onRespawn(PlayerRespawnEvent event, Player player) {
// If the spawn does not exist, we must do nothing
if (ConfigStorage.spawnLocation == null || !ConfigStorage.spawnLocation.isValid()) return;
if (event.isAnchorSpawn() && respawnAtAnchor) {
return;
}
if (this.respawnAtHome) {
// ToDo - Use home
}
if (this.respawnAtBed && event.isBedSpawn()) {
Location respawnLocation = player.getRespawnLocation();
if (respawnLocation != null) {
event.setRespawnLocation(respawnLocation);
return;
}
}
event.setRespawnLocation(ConfigStorage.spawnLocation.getLocation());
}
}