improve spawn location check.

This commit is contained in:
DNx5 2016-02-26 12:00:53 +07:00
parent 8536f85361
commit 1b65b285ac
2 changed files with 26 additions and 20 deletions

View File

@ -82,4 +82,13 @@ public abstract class CustomConfiguration extends YamlConfiguration {
} }
return false; return false;
} }
public boolean containsAll(String... paths) {
for (String path : paths) {
if (!contains(path)) {
return false;
}
}
return true;
}
} }

View File

@ -2,7 +2,6 @@ package fr.xephi.authme.settings;
import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MVWorldManager;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.StringUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -73,34 +72,32 @@ public class Spawn extends CustomConfiguration {
} }
public Location getSpawn() { public Location getSpawn() {
try { if (containsAll("spawn.world", "spawn.x", "spawn.y", "spawn.z", "spawn.yaw", "spawn.pitch")) {
String worldName = getString("spawn.world"); String worldName = getString("spawn.world");
World world = Bukkit.getWorld(worldName); World world = Bukkit.getWorld(worldName);
if (StringUtils.isEmpty(worldName) || world == null) { if (!StringUtils.isEmpty(worldName) && world != null) {
return null; return new Location(
world, getDouble("spawn.x"), getDouble("spawn.y"), getDouble("spawn.z"),
Float.parseFloat(getString("spawn.yaw")), Float.parseFloat(getString("spawn.pitch"))
);
} }
return new Location(world, getDouble("spawn.x"), getDouble("spawn.y"), getDouble("spawn.z"),
Float.parseFloat(getString("spawn.yaw")), Float.parseFloat(getString("spawn.pitch")));
} catch (NumberFormatException e) {
ConsoleLogger.writeStackTrace(e);
return null;
} }
return null;
} }
public Location getFirstSpawn() { public Location getFirstSpawn() {
try { if (containsAll("firstspawn.world", "firstspawn.x", "firstspawn.y",
String worldName; "firstspawn.z", "firstspawn.yaw", "firstspawn.pitch")) {
World world; String worldName = getString("firstspawn.world");
if (StringUtils.isEmpty(worldName = getString("firstspawn.world")) || World world = Bukkit.getWorld(worldName);
(world = Bukkit.getWorld(worldName)) == null) { if (!StringUtils.isEmpty(worldName) && world != null) {
return null; return new Location(
world, getDouble("firstspawn.x"), getDouble("firstspawn.y"), getDouble("firstspawn.z"),
Float.parseFloat(getString("firstspawn.yaw")), Float.parseFloat(getString("firstspawn.pitch"))
);
} }
return new Location(world, getDouble("firstspawn.x"), getDouble("firstspawn.y"), getDouble("firstspawn.z"),
Float.parseFloat(getString("firstspawn.yaw")), Float.parseFloat(getString("firstspawn.pitch")));
} catch (NumberFormatException e) {
ConsoleLogger.writeStackTrace(e);
return null;
} }
return null;
} }
// Return the spawn location of a player // Return the spawn location of a player