-
Notifications
You must be signed in to change notification settings - Fork 557
Expand file tree
/
Copy pathSpawnLoader.java
More file actions
424 lines (389 loc) · 15.7 KB
/
SpawnLoader.java
File metadata and controls
424 lines (389 loc) · 15.7 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package fr.xephi.authme.settings;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.initialization.Reloadable;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.service.PluginHookService;
import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.util.FileUtils;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
/**
* Manager for spawn points. It loads spawn definitions from AuthMe and third-party plugins
* and is responsible for returning the correct spawn point as per the settings.
* <p>
* The spawn priority setting defines from which sources and in which order the spawn point
* should be taken from. In AuthMe, we can distinguish between the regular spawn and a "first spawn",
* to which players will be teleported who have joined for the first time.
*/
public class SpawnLoader implements Reloadable {
private final ConsoleLogger logger = ConsoleLoggerFactory.get(SpawnLoader.class);
private final File authMeConfigurationFile;
private final Settings settings;
private final PluginHookService pluginHookService;
private FileConfiguration authMeConfiguration;
private String[] spawnPriority;
private Location essentialsSpawn;
private Location cmiSpawn;
/**
* Constructor.
*
* @param pluginFolder The AuthMe data folder
* @param settings The setting instance
* @param pluginHookService The plugin hooks instance
*/
@Inject
SpawnLoader(@DataFolder File pluginFolder, Settings settings, PluginHookService pluginHookService) {
File spawnFile = new File(pluginFolder, "spawn.yml");
FileUtils.copyFileFromResource(spawnFile, "spawn.yml");
this.authMeConfigurationFile = spawnFile;
this.settings = settings;
this.pluginHookService = pluginHookService;
reload();
}
/**
* (Re)loads the spawn file and relevant settings.
*/
@Override
public void reload() {
spawnPriority = settings.getProperty(RestrictionSettings.SPAWN_PRIORITY).split(",");
authMeConfiguration = YamlConfiguration.loadConfiguration(authMeConfigurationFile);
loadEssentialsSpawn();
}
/**
* Return the AuthMe spawn location.
*
* @return The location of the regular AuthMe spawn point
*/
public Location getSpawn() {
return getLocationFromConfiguration(authMeConfiguration, "spawn");
}
/**
* Set the AuthMe spawn point.
*
* @param location The location to use
*
* @return True upon success, false otherwise
*/
public boolean setSpawn(Location location) {
return setLocation("spawn", location);
}
/**
* Return the AuthMe first spawn location.
*
* @return The location of the AuthMe spawn point for first timers
*/
public Location getFirstSpawn() {
return getLocationFromConfiguration(authMeConfiguration, "firstspawn");
}
/**
* Set the AuthMe first spawn location.
*
* @param location The location to use
*
* @return True upon success, false otherwise
*/
public boolean setFirstSpawn(Location location) {
return setLocation("firstspawn", location);
}
/**
* Load the spawn point defined in EssentialsSpawn.
*/
public void loadEssentialsSpawn() {
// EssentialsSpawn cannot run without Essentials, so it's fine to get the Essentials data folder
File essentialsFolder = pluginHookService.getEssentialsDataFolder();
if (essentialsFolder == null) {
return;
}
File essentialsSpawnFile = new File(essentialsFolder, "spawn.yml");
if (essentialsSpawnFile.exists()) {
essentialsSpawn = getLocationFromConfiguration(
YamlConfiguration.loadConfiguration(essentialsSpawnFile), "spawns.default");
} else {
essentialsSpawn = null;
logger.info("Essentials spawn file not found: '" + essentialsSpawnFile.getAbsolutePath() + "'");
}
}
/**
* Retrieves the last logout location of a player from their Essentials data file.
*
* This function fetches the player's data file from the Essentials data folder,
* reads the 'logoutlocation' section, and constructs a Location object from it.
* If the file or the location data is not found, it returns null.
*
* @param player The player whose logout location is to be retrieved.
* @return The last known logout location of the player, or null if not found.
*/
public Location getEssentialsQuitLocation(Player player) {
File essentialsFolder = pluginHookService.getEssentialsDataFolder();
if (essentialsFolder == null) {
return null;
}
String path = "userdata/" + player.getUniqueId() + ".yml";
File essentialsUserdataFile = new File(essentialsFolder, path);
if (essentialsUserdataFile.exists()) {
YamlConfiguration config = YamlConfiguration.loadConfiguration(essentialsUserdataFile);
if (config.contains("logoutlocation")) {
String worldName = config.getString("logoutlocation.world-name");
World world = Bukkit.getWorld(worldName);
if (world == null) {
return null;
}
double x = config.getDouble("logoutlocation.x");
double y = config.getDouble("logoutlocation.y");
double z = config.getDouble("logoutlocation.z");
float yaw = (float) config.getDouble("logoutlocation.yaw");
float pitch = (float) config.getDouble("logoutlocation.pitch");
Location location = new Location(world, x, y, z, yaw, pitch);
return location;
} else {
logger.debug("logoutlocation not found in userdata file: " + essentialsUserdataFile.getAbsolutePath());
return null;
}
} else {
logger.debug("Essentials userdata file not found: '" + essentialsUserdataFile.getAbsolutePath() + "'");
return null;
}
}
/**
* Unset the spawn point defined in EssentialsSpawn.
*/
public void unloadEssentialsSpawn() {
essentialsSpawn = null;
}
/**
* Load the spawn point defined in CMI.
*/
public void loadCmiSpawn() {
File cmiFolder = pluginHookService.getCmiDataFolder();
if (cmiFolder == null) {
return;
}
File cmiConfig = new File(cmiFolder, "config.yml");
if (cmiConfig.exists()) {
cmiSpawn = getLocationFromCmiConfiguration(YamlConfiguration.loadConfiguration(cmiConfig));
} else {
cmiSpawn = null;
logger.info("CMI config file not found: '" + cmiConfig.getAbsolutePath() + "'");
}
}
/**
* Unset the spawn point defined in CMI.
*/
public void unloadCmiSpawn() {
cmiSpawn = null;
}
/**
* Return the spawn location for the given player. The source of the spawn location varies
* depending on the spawn priority setting.
*
* @param player The player to retrieve the spawn point for
*
* @return The spawn location, or the default spawn location upon failure
*
* @see RestrictionSettings#SPAWN_PRIORITY
*/
public Location getSpawnLocation(Player player) {
if (player == null || player.getWorld() == null) {
return null;
}
World world = player.getWorld();
Location spawnLoc = null;
for (String priority : spawnPriority) {
switch (priority.toLowerCase(Locale.ROOT).trim()) {
case "default":
if (world.getSpawnLocation() != null) {
if (!isValidSpawnPoint(world.getSpawnLocation())) {
for (World spawnWorld : Bukkit.getWorlds()) {
if (isValidSpawnPoint(spawnWorld.getSpawnLocation())) {
world = spawnWorld;
break;
}
}
logger.warning("Seems like AuthMe is unable to find a proper spawn location. "
+ "Set a location with the command '/authme setspawn'");
}
spawnLoc = world.getSpawnLocation();
}
break;
case "multiverse":
if (settings.getProperty(HooksSettings.MULTIVERSE)) {
spawnLoc = pluginHookService.getMultiverseSpawn(world);
}
break;
case "essentials":
spawnLoc = essentialsSpawn;
break;
case "cmi":
spawnLoc = cmiSpawn;
break;
case "authme":
spawnLoc = getSpawn();
break;
default:
// ignore
}
if (spawnLoc != null) {
logger.debug("Spawn location determined as `{0}` for world `{1}`", spawnLoc, world.getName());
return spawnLoc;
}
}
logger.debug("Fall back to default world spawn location. World: `{0}`", world.getName());
return world.getSpawnLocation(); // return default location
}
/**
* Checks if a given location is a valid spawn point [!= (0,0,0)].
*
* @param location The location to check
*
* @return True upon success, false otherwise
*/
private boolean isValidSpawnPoint(Location location) {
if (location.getX() == 0 && location.getY() == 0 && location.getZ() == 0) {
return false;
}
return true;
}
/**
* Save the location under the given prefix.
*
* @param prefix The prefix to save the spawn under
* @param location The location to persist
*
* @return True upon success, false otherwise
*/
private boolean setLocation(String prefix, Location location) {
if (location != null && location.getWorld() != null) {
authMeConfiguration.set(prefix + ".world", location.getWorld().getName());
authMeConfiguration.set(prefix + ".x", location.getX());
authMeConfiguration.set(prefix + ".y", location.getY());
authMeConfiguration.set(prefix + ".z", location.getZ());
authMeConfiguration.set(prefix + ".yaw", location.getYaw());
authMeConfiguration.set(prefix + ".pitch", location.getPitch());
return saveAuthMeConfig();
}
return false;
}
private boolean saveAuthMeConfig() {
try {
authMeConfiguration.save(authMeConfigurationFile);
return true;
} catch (IOException e) {
logger.logException("Could not save spawn config (" + authMeConfigurationFile + ")", e);
}
return false;
}
/**
* Return player's location if player is alive, or player's spawn location if dead.
*
* @param player player to retrieve
*
* @return location of the given player if alive, spawn location if dead.
*/
public Location getPlayerLocationOrSpawn(Player player) {
if (player.getHealth() <= 0.0) {
return getSpawnLocation(player);
}
return player.getLocation();
}
/**
* Build a {@link Location} object from the given path in the file configuration.
*
* @param configuration The file configuration to read from
* @param pathPrefix The path to get the spawn point from
*
* @return Location corresponding to the values in the path
*/
private static Location getLocationFromConfiguration(FileConfiguration configuration, String pathPrefix) {
if (containsAllSpawnFields(configuration, pathPrefix)) {
String prefix = pathPrefix + ".";
String worldName = configuration.getString(prefix + "world");
World world = Bukkit.getWorld(worldName);
if (!StringUtils.isBlank(worldName) && world != null) {
return new Location(world, configuration.getDouble(prefix + "x"),
configuration.getDouble(prefix + "y"), configuration.getDouble(prefix + "z"),
getFloat(configuration, prefix + "yaw"), getFloat(configuration, prefix + "pitch"));
}
}
return null;
}
/**
* Build a {@link Location} object based on the CMI configuration.
*
* @param configuration The CMI file configuration to read from
*
* @return Location corresponding to the values in the path
*/
private static Location getLocationFromCmiConfiguration(FileConfiguration configuration) {
final String pathPrefix = "Spawn.Main";
if (isLocationCompleteInCmiConfig(configuration, pathPrefix)) {
String prefix = pathPrefix + ".";
String worldName = configuration.getString(prefix + "World");
World world = Bukkit.getWorld(worldName);
if (!StringUtils.isBlank(worldName) && world != null) {
return new Location(world, configuration.getDouble(prefix + "X"),
configuration.getDouble(prefix + "Y"), configuration.getDouble(prefix + "Z"),
getFloat(configuration, prefix + "Yaw"), getFloat(configuration, prefix + "Pitch"));
}
}
return null;
}
/**
* Return whether the file configuration contains all fields necessary to define a spawn
* under the given path.
*
* @param configuration The file configuration to use
* @param pathPrefix The path to verify
*
* @return True if all spawn fields are present, false otherwise
*/
private static boolean containsAllSpawnFields(FileConfiguration configuration, String pathPrefix) {
String[] fields = {"world", "x", "y", "z", "yaw", "pitch"};
for (String field : fields) {
if (!configuration.contains(pathPrefix + "." + field)) {
return false;
}
}
return true;
}
/**
* Return whether the CMI file configuration contains all spawn fields under the given path.
*
* @param cmiConfiguration The file configuration from CMI
* @param pathPrefix The path to verify
*
* @return True if all spawn fields are present, false otherwise
*/
private static boolean isLocationCompleteInCmiConfig(FileConfiguration cmiConfiguration, String pathPrefix) {
String[] fields = {"World", "X", "Y", "Z", "Yaw", "Pitch"};
for (String field : fields) {
if (!cmiConfiguration.contains(pathPrefix + "." + field)) {
return false;
}
}
return true;
}
/**
* Retrieve a property as a float from the given file configuration.
*
* @param configuration The file configuration to use
* @param path The path of the property to retrieve
*
* @return The float
*/
private static float getFloat(FileConfiguration configuration, String path) {
Object value = configuration.get(path);
// This behavior is consistent with FileConfiguration#getDouble
return (value instanceof Number) ? ((Number) value).floatValue() : 0;
}
}