From 5e330398d390bcc3c939e24b1672ba8925c3b8ec Mon Sep 17 00:00:00 2001 From: DNx5 Date: Mon, 22 Feb 2016 01:01:05 +0700 Subject: [PATCH 1/2] Spawn Location --- src/main/java/fr/xephi/authme/AuthMe.java | 20 +- .../executable/authme/ReloadCommand.java | 2 + .../fr/xephi/authme/settings/Settings.java | 5 +- .../java/fr/xephi/authme/settings/Spawn.java | 187 +++++++++--------- 4 files changed, 105 insertions(+), 109 deletions(-) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 56580cf9..6fbda54e 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -737,25 +737,9 @@ public class AuthMe extends JavaPlugin { } // Return the spawn location of a player + @Deprecated public Location getSpawnLocation(Player player) { - World world = player.getWorld(); - String[] spawnPriority = Settings.spawnPriority.split(","); - Location spawnLoc = world.getSpawnLocation(); - for (int i = spawnPriority.length - 1; i >= 0; i--) { - String s = spawnPriority[i]; - if (s.equalsIgnoreCase("default") && getDefaultSpawn(world) != null) - spawnLoc = getDefaultSpawn(world); - if (s.equalsIgnoreCase("multiverse") && getMultiverseSpawn(world) != null) - spawnLoc = getMultiverseSpawn(world); - if (s.equalsIgnoreCase("essentials") && getEssentialsSpawn() != null) - spawnLoc = getEssentialsSpawn(); - if (s.equalsIgnoreCase("authme") && getAuthMeSpawn(player) != null) - spawnLoc = getAuthMeSpawn(player); - } - if (spawnLoc == null) { - spawnLoc = world.getSpawnLocation(); - } - return spawnLoc; + return Spawn.getInstance().getSpawnLocation(player); } // Return the default spawn point of a world diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/ReloadCommand.java b/src/main/java/fr/xephi/authme/command/executable/authme/ReloadCommand.java index 3dce54e1..0c7adf2f 100644 --- a/src/main/java/fr/xephi/authme/command/executable/authme/ReloadCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/authme/ReloadCommand.java @@ -5,6 +5,7 @@ import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.output.MessageKey; +import fr.xephi.authme.settings.Spawn; import org.bukkit.command.CommandSender; import java.util.List; @@ -20,6 +21,7 @@ public class ReloadCommand implements ExecutableCommand { try { commandService.getSettings().reload(); commandService.reloadMessages(commandService.getSettings().getMessagesFile()); + Spawn.reload(); // TODO #432: We should not reload only certain plugin entities but actually reinitialize all elements, // i.e. here in the future we might not have setupDatabase() but Authme.onEnable(), maybe after // a call to some destructor method diff --git a/src/main/java/fr/xephi/authme/settings/Settings.java b/src/main/java/fr/xephi/authme/settings/Settings.java index 7ba3b3b1..8e5bf2c9 100644 --- a/src/main/java/fr/xephi/authme/settings/Settings.java +++ b/src/main/java/fr/xephi/authme/settings/Settings.java @@ -5,6 +5,7 @@ import fr.xephi.authme.datasource.DataSourceType; import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.settings.domain.Property; import fr.xephi.authme.settings.properties.DatabaseSettings; +import fr.xephi.authme.settings.properties.HooksSettings; import fr.xephi.authme.settings.properties.PluginSettings; import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; @@ -176,7 +177,7 @@ public final class Settings { emailRegistration = configFile.getBoolean("settings.registration.enableEmailRegistrationSystem", false); saltLength = configFile.getInt("settings.security.doubleMD5SaltLength", 8); getmaxRegPerEmail = configFile.getInt("Email.maxRegPerEmail", 1); - multiverse = configFile.getBoolean("Hooks.multiverse", true); + multiverse = load(HooksSettings.MULTIVERSE); bungee = configFile.getBoolean("Hooks.bungeecord", false); getForcedWorlds = configFile.getStringList("settings.restrictions.ForceSpawnOnTheseWorlds"); banUnsafeIp = configFile.getBoolean("settings.restrictions.banUnsafedIP", false); @@ -213,7 +214,7 @@ public final class Settings { broadcastWelcomeMessage = configFile.getBoolean("settings.broadcastWelcomeMessage", false); forceRegKick = configFile.getBoolean("settings.registration.forceKickAfterRegister", false); forceRegLogin = configFile.getBoolean("settings.registration.forceLoginAfterRegister", false); - spawnPriority = configFile.getString("settings.restrictions.spawnPriority", "authme,essentials,multiverse,default"); + spawnPriority = load(RestrictionSettings.SPAWN_PRIORITY); getMaxLoginPerIp = configFile.getInt("settings.restrictions.maxLoginPerIp", 0); getMaxJoinPerIp = configFile.getInt("settings.restrictions.maxJoinPerIp", 0); checkVeryGames = configFile.getBoolean("VeryGames.enableIpCheck", false); diff --git a/src/main/java/fr/xephi/authme/settings/Spawn.java b/src/main/java/fr/xephi/authme/settings/Spawn.java index 4c20b296..fa710c95 100644 --- a/src/main/java/fr/xephi/authme/settings/Spawn.java +++ b/src/main/java/fr/xephi/authme/settings/Spawn.java @@ -1,7 +1,14 @@ package fr.xephi.authme.settings; +import com.onarandombox.MultiverseCore.api.MVWorldManager; +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.ConsoleLogger; +import fr.xephi.authme.cache.auth.PlayerCache; +import fr.xephi.authme.util.StringUtils; import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Player; import java.io.File; @@ -12,13 +19,17 @@ import java.io.File; public class Spawn extends CustomConfiguration { private static Spawn spawn; + private static String[] spawnPriority; - public Spawn() { - super(new File("." + File.separator + "plugins" + File.separator + "AuthMe" + File.separator + "spawn.yml")); - spawn = this; - load(); - save(); - saveDefault(); + private Spawn() { + super(new File(Settings.PLUGIN_FOLDER, "spawn.yml")); + reload(); + } + + public static void reload() { + getInstance().load(); + getInstance().save(); + spawnPriority = Settings.spawnPriority.split(","); } /** @@ -33,111 +44,109 @@ public class Spawn extends CustomConfiguration { return spawn; } - private void saveDefault() { - if (!contains("spawn")) { - set("spawn.world", ""); - set("spawn.x", ""); - set("spawn.y", ""); - set("spawn.z", ""); - set("spawn.yaw", ""); - set("spawn.pitch", ""); - save(); - } - if (!contains("firstspawn")) { - set("firstspawn.world", ""); - set("firstspawn.x", ""); - set("firstspawn.y", ""); - set("firstspawn.z", ""); - set("firstspawn.yaw", ""); - set("firstspawn.pitch", ""); - save(); - } - } - - /** - * Method setSpawn. - * - * @param location Location - * - * @return boolean - */ public boolean setSpawn(Location location) { - try { - set("spawn.world", location.getWorld().getName()); - set("spawn.x", location.getX()); - set("spawn.y", location.getY()); - set("spawn.z", location.getZ()); - set("spawn.yaw", location.getYaw()); - set("spawn.pitch", location.getPitch()); - save(); - return true; - } catch (NullPointerException npe) { + if (location == null || location.getWorld() == null) { return false; } + set("spawn.world", location.getWorld().getName()); + set("spawn.x", location.getX()); + set("spawn.y", location.getY()); + set("spawn.z", location.getZ()); + set("spawn.yaw", location.getYaw()); + set("spawn.pitch", location.getPitch()); + save(); + return true; } - /** - * Method setFirstSpawn. - * - * @param location Location - * - * @return boolean - */ public boolean setFirstSpawn(Location location) { - try { - set("firstspawn.world", location.getWorld().getName()); - set("firstspawn.x", location.getX()); - set("firstspawn.y", location.getY()); - set("firstspawn.z", location.getZ()); - set("firstspawn.yaw", location.getYaw()); - set("firstspawn.pitch", location.getPitch()); - save(); - return true; - } catch (NullPointerException npe) { + if (location == null || location.getWorld() == null) { return false; } + set("firstspawn.world", location.getWorld().getName()); + set("firstspawn.x", location.getX()); + set("firstspawn.y", location.getY()); + set("firstspawn.z", location.getZ()); + set("firstspawn.yaw", location.getYaw()); + set("firstspawn.pitch", location.getPitch()); + save(); + return true; } - /** - * Method getLocation. - * - * @return Location - */ - @Deprecated - public Location getLocation() { - return getSpawn(); - } - - /** - * Method getSpawn. - * - * @return Location - */ public Location getSpawn() { try { - if (this.getString("spawn.world").isEmpty() || this.getString("spawn.world").equals("")) + String worldName; + World world; + if (StringUtils.isEmpty(worldName = getString("spawn.world")) || + (world = Bukkit.getWorld(worldName)) == null) { return null; - Location location = new Location(Bukkit.getWorld(this.getString("spawn.world")), this.getDouble("spawn.x"), this.getDouble("spawn.y"), this.getDouble("spawn.z"), Float.parseFloat(this.getString("spawn.yaw")), Float.parseFloat(this.getString("spawn.pitch"))); - return location; - } catch (NullPointerException | NumberFormatException npe) { + } + 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; } } - /** - * Method getFirstSpawn. - * - * @return Location - */ public Location getFirstSpawn() { try { - if (this.getString("firstspawn.world").isEmpty() || this.getString("firstspawn.world").equals("")) + String worldName; + World world; + if (StringUtils.isEmpty(worldName = getString("firstspawn.world")) || + (world = Bukkit.getWorld(worldName)) == null) { return null; - Location location = new Location(Bukkit.getWorld(this.getString("firstspawn.world")), this.getDouble("firstspawn.x"), this.getDouble("firstspawn.y"), this.getDouble("firstspawn.z"), Float.parseFloat(this.getString("firstspawn.yaw")), Float.parseFloat(this.getString("firstspawn.pitch"))); - return location; - } catch (NullPointerException | NumberFormatException npe) { + } + 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 the spawn location of a player + public Location getSpawnLocation(Player player) { + AuthMe plugin = AuthMe.getInstance(); + World world; + if (plugin == null || player == null || (world = player.getWorld()) == null) { + return null; + } + + Location spawnLoc = null; + for (String priority : spawnPriority) { + switch (priority.toLowerCase()) { + case "default": + if (world.getSpawnLocation() != null) { + spawnLoc = world.getSpawnLocation(); + } + break; + case "multiverse": + if (Settings.multiverse && plugin.multiverse != null) { + MVWorldManager manager = plugin.multiverse.getMVWorldManager(); + if (manager.isMVWorld(world)) { + spawnLoc = manager.getMVWorld(world).getSpawnLocation(); + } + } + break; + case "essentials": + spawnLoc = plugin.essentialsSpawn; + break; + case "authme": + String playerNameLower = player.getName().toLowerCase(); + if (PlayerCache.getInstance().isAuthenticated(playerNameLower)) { + spawnLoc = getSpawn(); + } else if ((getFirstSpawn() != null) && (!player.hasPlayedBefore() || + (!plugin.getDataSource().isAuthAvailable(playerNameLower)))) { + spawnLoc = getFirstSpawn(); + } else { + spawnLoc = getSpawn(); + } + break; + } + if (spawnLoc != null) { + return spawnLoc; + } + } + return world.getSpawnLocation(); // return default location + } } From 5943537c2629c9255c6faccf1c16c70a8376bda0 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Mon, 22 Feb 2016 22:48:02 +0700 Subject: [PATCH 2/2] cleanup It's good now --- src/main/java/fr/xephi/authme/settings/Spawn.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/fr/xephi/authme/settings/Spawn.java b/src/main/java/fr/xephi/authme/settings/Spawn.java index fa710c95..275a8d0e 100644 --- a/src/main/java/fr/xephi/authme/settings/Spawn.java +++ b/src/main/java/fr/xephi/authme/settings/Spawn.java @@ -74,10 +74,9 @@ public class Spawn extends CustomConfiguration { public Location getSpawn() { try { - String worldName; - World world; - if (StringUtils.isEmpty(worldName = getString("spawn.world")) || - (world = Bukkit.getWorld(worldName)) == null) { + String worldName = getString("spawn.world"); + World world = Bukkit.getWorld(worldName); + if (StringUtils.isEmpty(worldName) || world == null) { return null; } return new Location(world, getDouble("spawn.x"), getDouble("spawn.y"), getDouble("spawn.z"), @@ -107,11 +106,11 @@ public class Spawn extends CustomConfiguration { // Return the spawn location of a player public Location getSpawnLocation(Player player) { AuthMe plugin = AuthMe.getInstance(); - World world; - if (plugin == null || player == null || (world = player.getWorld()) == null) { + if (plugin == null || player == null || player.getWorld() == null) { return null; } + World world = player.getWorld(); Location spawnLoc = null; for (String priority : spawnPriority) { switch (priority.toLowerCase()) {