diff --git a/src/main/java/fr/xephi/authme/Utils.java b/src/main/java/fr/xephi/authme/Utils.java index 4caee45..510a710 100644 --- a/src/main/java/fr/xephi/authme/Utils.java +++ b/src/main/java/fr/xephi/authme/Utils.java @@ -204,12 +204,7 @@ public class Utils { public void run() { final AuthMeTeleportEvent tpEvent = new AuthMeTeleportEvent(pl, locat); plugin.getServer().getPluginManager().callEvent(tpEvent); - if (!tpEvent.isCancelled()) { - if (!tpEvent.getTo().getChunk().isLoaded()) { - tpEvent.getTo().getChunk().load(); - } - pl.teleport(tpEvent.getTo()); - } + Utils.safeTP(pl, locat); } }); } @@ -232,6 +227,13 @@ public class Utils { } } + public static void safeTP(final Player p, final Location loc) { + if (!loc.getWorld().getChunkAt(loc).isLoaded()) { + loc.getWorld().getChunkAt(loc).load(); + } + p.teleport(loc); + } + public static void setGroup(final Player player, final GroupType group) { if (!Settings.isPermissionCheckEnabled) { return; diff --git a/src/main/java/fr/xephi/authme/commands/UnregisterCommand.java b/src/main/java/fr/xephi/authme/commands/UnregisterCommand.java index d0cb299..02940de 100644 --- a/src/main/java/fr/xephi/authme/commands/UnregisterCommand.java +++ b/src/main/java/fr/xephi/authme/commands/UnregisterCommand.java @@ -29,16 +29,16 @@ import fr.xephi.authme.task.TimeoutTask; public class UnregisterCommand implements CommandExecutor { public AuthMe plugin; - private Messages m = Messages.getInstance(); - private JsonCache playerCache; + private final Messages m = Messages.getInstance(); + private final JsonCache playerCache; - public UnregisterCommand(AuthMe plugin) { + public UnregisterCommand(final AuthMe plugin) { this.plugin = plugin; this.playerCache = new JsonCache(); } @Override - public boolean onCommand(final CommandSender sender, Command cmnd, String label, final String[] args) { + public boolean onCommand(final CommandSender sender, final Command cmnd, final String label, final String[] args) { if (!(sender instanceof Player)) { return true; } @@ -71,8 +71,8 @@ public class UnregisterCommand implements CommandExecutor { } if (Settings.isForcedRegistrationEnabled) { if (Settings.isTeleportToSpawnEnabled && !Settings.noTeleport) { - Location spawn = plugin.getSpawnLocation(player); - SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(player, player.getLocation(), spawn, false); + final Location spawn = plugin.getSpawnLocation(player); + final SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(player, player.getLocation(), spawn, false); plugin.getServer().getPluginManager().callEvent(tpEvent); if (!tpEvent.isCancelled()) { player.teleport(tpEvent.getTo()); @@ -81,14 +81,15 @@ public class UnregisterCommand implements CommandExecutor { player.saveData(); PlayerCache.getInstance().removePlayer(player.getName().toLowerCase()); - if (!Settings.getRegisteredGroup.isEmpty()) + if (!Settings.getRegisteredGroup.isEmpty()) { Utils.setGroup(player, GroupType.UNREGISTERED); + } LimboCache.getInstance().addLimboPlayer(player); - int delay = Settings.getRegistrationTimeout * 20; - int interval = Settings.getWarnMessageInterval; - BukkitScheduler sched = sender.getServer().getScheduler(); + final int delay = Settings.getRegistrationTimeout * 20; + final int interval = Settings.getWarnMessageInterval; + final BukkitScheduler sched = sender.getServer().getScheduler(); if (delay != 0) { - BukkitTask id = sched.runTaskLaterAsynchronously(plugin, new TimeoutTask(plugin, name, player), delay); + final BukkitTask id = sched.runTaskLaterAsynchronously(plugin, new TimeoutTask(plugin, name, player), delay); LimboCache.getInstance().getLimboPlayer(name).setTimeoutTaskId(id); } LimboCache.getInstance().getLimboPlayer(name).setMessageTaskId(sched.runTaskAsynchronously(plugin, new MessageTask(plugin, name, m.send("reg_msg"), interval))); @@ -105,8 +106,9 @@ public class UnregisterCommand implements CommandExecutor { if (playerCache.doesCacheExist(player)) { playerCache.removeCache(player); } - if (Settings.applyBlindEffect) + if (Settings.applyBlindEffect) { player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, Settings.getRegistrationTimeout * 20, 2)); + } if (!Settings.isMovementAllowed && Settings.isRemoveSpeedEnabled) { player.setWalkSpeed(0.0f); player.setFlySpeed(0.0f); @@ -114,21 +116,16 @@ public class UnregisterCommand implements CommandExecutor { m.send(player, "unregistered"); ConsoleLogger.info(player.getDisplayName() + " unregistered himself"); if (Settings.isTeleportToSpawnEnabled && !Settings.noTeleport) { - Location spawn = plugin.getSpawnLocation(player); - SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(player, player.getLocation(), spawn, false); + final Location spawn = plugin.getSpawnLocation(player); + final SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(player, player.getLocation(), spawn, false); plugin.getServer().getPluginManager().callEvent(tpEvent); - if (!tpEvent.isCancelled()) { - if (!tpEvent.getTo().getWorld().getChunkAt(tpEvent.getTo()).isLoaded()) { - tpEvent.getTo().getWorld().getChunkAt(tpEvent.getTo()).load(); - } - player.teleport(tpEvent.getTo()); - } + Utils.safeTP(player, spawn); } return; } else { m.send(player, "wrong_pwd"); } - } catch (NoSuchAlgorithmException ex) { + } catch (final NoSuchAlgorithmException ex) { ConsoleLogger.showError(ex.getMessage()); sender.sendMessage("Internal Error please read the server log"); } diff --git a/src/main/java/fr/xephi/authme/process/login/ProcessSyncronousPlayerLogin.java b/src/main/java/fr/xephi/authme/process/login/ProcessSyncronousPlayerLogin.java index 9b473cf..7c67231 100644 --- a/src/main/java/fr/xephi/authme/process/login/ProcessSyncronousPlayerLogin.java +++ b/src/main/java/fr/xephi/authme/process/login/ProcessSyncronousPlayerLogin.java @@ -24,16 +24,16 @@ import fr.xephi.authme.settings.Settings; public class ProcessSyncronousPlayerLogin implements Runnable { - private PlayerAuth auth; - private DataSource database; - private LimboPlayer limbo; - private String name; - private Player player; - private JsonCache playerCache; - private AuthMe plugin; - private PluginManager pm; + private final PlayerAuth auth; + private final DataSource database; + private final LimboPlayer limbo; + private final String name; + private final Player player; + private final JsonCache playerCache; + private final AuthMe plugin; + private final PluginManager pm; - public ProcessSyncronousPlayerLogin(Player player, AuthMe plugin, DataSource data) { + public ProcessSyncronousPlayerLogin(final Player player, final AuthMe plugin, final DataSource data) { this.plugin = plugin; this.database = data; this.pm = plugin.getServer().getPluginManager(); @@ -91,8 +91,9 @@ public class ProcessSyncronousPlayerLogin implements Runnable { // Re-Force Survival GameMode if we need due to world change // specification - if (Settings.isForceSurvivalModeEnabled) + if (Settings.isForceSurvivalModeEnabled) { Utils.forceGM(player); + } // Restore Permission Group Utils.setGroup(player, GroupType.LOGGEDIN); @@ -106,15 +107,17 @@ public class ProcessSyncronousPlayerLogin implements Runnable { // We can now display the join message if (AuthMePlayerListener.joinMessage.containsKey(name) && AuthMePlayerListener.joinMessage.get(name) != null && !AuthMePlayerListener.joinMessage.get(name).isEmpty()) { - for (Player p : Utils.getOnlinePlayers()) { - if (p.isOnline()) + for (final Player p : Utils.getOnlinePlayers()) { + if (p.isOnline()) { p.sendMessage(AuthMePlayerListener.joinMessage.get(name)); + } } AuthMePlayerListener.joinMessage.remove(name); } - if (Settings.applyBlindEffect) + if (Settings.applyBlindEffect) { player.removePotionEffect(PotionEffectType.BLINDNESS); + } if (!Settings.isMovementAllowed && Settings.isRemoveSpeedEnabled) { player.setWalkSpeed(0.2f); player.setFlySpeed(0.1f); @@ -125,29 +128,30 @@ public class ProcessSyncronousPlayerLogin implements Runnable { player.saveData(); // Login is finish, display welcome message - if (Settings.useWelcomeMessage) + if (Settings.useWelcomeMessage) { if (Settings.broadcastWelcomeMessage) { - for (String s : Settings.welcomeMsg) { + for (final String s : Settings.welcomeMsg) { Bukkit.getServer().broadcastMessage(plugin.replaceAllInfos(s, player)); } } else { - for (String s : Settings.welcomeMsg) { + for (final String s : Settings.welcomeMsg) { player.sendMessage(plugin.replaceAllInfos(s, player)); } } + } // Login is now finish , we can force all commands forceCommands(); } protected void forceCommands() { - for (String command : Settings.forceCommands) { + for (final String command : Settings.forceCommands) { try { player.performCommand(command.replace("%p", player.getName())); - } catch (Exception ignored) { + } catch (final Exception ignored) { } } - for (String command : Settings.forceCommandsAsConsole) { + for (final String command : Settings.forceCommandsAsConsole) { Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), command.replace("%p", player.getName())); } } @@ -157,7 +161,7 @@ public class ProcessSyncronousPlayerLogin implements Runnable { } protected void restoreInventory() { - RestoreInventoryEvent event = new RestoreInventoryEvent(player); + final RestoreInventoryEvent event = new RestoreInventoryEvent(player); Bukkit.getServer().getPluginManager().callEvent(event); if (!event.isCancelled()) { plugin.inventoryProtector.sendInventoryPacket(player); @@ -178,27 +182,21 @@ public class ProcessSyncronousPlayerLogin implements Runnable { } protected void teleportBackFromSpawn() { - AuthMeTeleportEvent tpEvent = new AuthMeTeleportEvent(player, limbo.getLoc()); + final AuthMeTeleportEvent tpEvent = new AuthMeTeleportEvent(player, limbo.getLoc()); pm.callEvent(tpEvent); if (!tpEvent.isCancelled()) { - Location fLoc = tpEvent.getTo(); - if (!fLoc.getChunk().isLoaded()) { - fLoc.getChunk().load(); - } - player.teleport(fLoc); + final Location fLoc = tpEvent.getTo(); + Utils.safeTP(player, fLoc); } } protected void teleportToSpawn() { - Location spawnL = plugin.getSpawnLocation(player); - SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(player, player.getLocation(), spawnL, true); + final Location spawnL = plugin.getSpawnLocation(player); + final SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(player, player.getLocation(), spawnL, true); pm.callEvent(tpEvent); if (!tpEvent.isCancelled()) { - Location fLoc = tpEvent.getTo(); - if (!fLoc.getChunk().isLoaded()) { - fLoc.getChunk().load(); - } - player.teleport(fLoc); + final Location fLoc = tpEvent.getTo(); + Utils.safeTP(player, fLoc); } } diff --git a/src/main/java/fr/xephi/authme/process/register/ProcessSyncronousPasswordRegister.java b/src/main/java/fr/xephi/authme/process/register/ProcessSyncronousPasswordRegister.java index 2143d74..efe42b4 100644 --- a/src/main/java/fr/xephi/authme/process/register/ProcessSyncronousPasswordRegister.java +++ b/src/main/java/fr/xephi/authme/process/register/ProcessSyncronousPasswordRegister.java @@ -24,12 +24,12 @@ import fr.xephi.authme.task.TimeoutTask; public class ProcessSyncronousPasswordRegister implements Runnable { - private Messages m = Messages.getInstance(); - private AuthMe plugin; + private final Messages m = Messages.getInstance(); + private final AuthMe plugin; protected String name; protected Player player; - public ProcessSyncronousPasswordRegister(Player player, AuthMe plugin) { + public ProcessSyncronousPasswordRegister(final Player player, final AuthMe plugin) { this.player = player; this.name = player.getName().toLowerCase(); this.plugin = plugin; @@ -37,23 +37,20 @@ public class ProcessSyncronousPasswordRegister implements Runnable { @Override public void run() { - LimboPlayer limbo = LimboCache.getInstance().getLimboPlayer(name); + final LimboPlayer limbo = LimboCache.getInstance().getLimboPlayer(name); if (limbo != null) { player.setGameMode(limbo.getGameMode()); if (Settings.isTeleportToSpawnEnabled && !Settings.noTeleport) { - Location loca = plugin.getSpawnLocation(player); - RegisterTeleportEvent tpEvent = new RegisterTeleportEvent(player, loca); + final Location loca = plugin.getSpawnLocation(player); + final RegisterTeleportEvent tpEvent = new RegisterTeleportEvent(player, loca); plugin.getServer().getPluginManager().callEvent(tpEvent); if (!tpEvent.isCancelled()) { - if (!tpEvent.getTo().getWorld().getChunkAt(tpEvent.getTo()).isLoaded()) { - tpEvent.getTo().getWorld().getChunkAt(tpEvent.getTo()).load(); - } - player.teleport(tpEvent.getTo()); + Utils.safeTP(player, tpEvent.getTo()); } } if (Settings.protectInventoryBeforeLogInEnabled && plugin.inventoryProtector != null) { - RestoreInventoryEvent event = new RestoreInventoryEvent(player); + final RestoreInventoryEvent event = new RestoreInventoryEvent(player); Bukkit.getPluginManager().callEvent(event); if (!event.isCancelled()) { plugin.inventoryProtector.sendInventoryPacket(player); @@ -69,14 +66,16 @@ public class ProcessSyncronousPasswordRegister implements Runnable { Utils.setGroup(player, Utils.GroupType.REGISTERED); } m.send(player, "registered"); - if (!Settings.getmailAccount.isEmpty()) + if (!Settings.getmailAccount.isEmpty()) { m.send(player, "add_email"); + } if (player.getGameMode() != GameMode.CREATIVE && !Settings.isMovementAllowed) { player.setAllowFlight(false); player.setFlying(false); } - if (Settings.applyBlindEffect) + if (Settings.applyBlindEffect) { player.removePotionEffect(PotionEffectType.BLINDNESS); + } if (!Settings.isMovementAllowed && Settings.isRemoveSpeedEnabled) { player.setWalkSpeed(0.2f); player.setFlySpeed(0.1f); @@ -85,8 +84,9 @@ public class ProcessSyncronousPasswordRegister implements Runnable { plugin.getServer().getPluginManager().callEvent(new LoginEvent(player, true)); player.saveData(); - if (!Settings.noConsoleSpam) + if (!Settings.noConsoleSpam) { ConsoleLogger.info(player.getName() + " registered " + plugin.getIP(player)); + } // Kick Player after Registration is enabled, kick the player if (Settings.forceRegKick) { @@ -101,56 +101,55 @@ public class ProcessSyncronousPasswordRegister implements Runnable { } // Register is finish and player is logged, display welcome message - if (Settings.useWelcomeMessage) + if (Settings.useWelcomeMessage) { if (Settings.broadcastWelcomeMessage) { - for (String s : Settings.welcomeMsg) { + for (final String s : Settings.welcomeMsg) { plugin.getServer().broadcastMessage(plugin.replaceAllInfos(s, player)); } } else { - for (String s : Settings.welcomeMsg) { + for (final String s : Settings.welcomeMsg) { player.sendMessage(plugin.replaceAllInfos(s, player)); } } + } // Register is now finish , we can force all commands forceCommands(); } protected void forceCommands() { - for (String command : Settings.forceRegisterCommands) { + for (final String command : Settings.forceRegisterCommands) { try { player.performCommand(command.replace("%p", player.getName())); - } catch (Exception ignored) { + } catch (final Exception ignored) { } } - for (String command : Settings.forceRegisterCommandsAsConsole) { + for (final String command : Settings.forceRegisterCommandsAsConsole) { Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), command.replace("%p", player.getName())); } } - protected void forceLogin(Player player) { + protected void forceLogin(final Player player) { if (Settings.isTeleportToSpawnEnabled && !Settings.noTeleport) { - Location spawnLoc = plugin.getSpawnLocation(player); - AuthMeTeleportEvent tpEvent = new AuthMeTeleportEvent(player, spawnLoc); + final Location spawnLoc = plugin.getSpawnLocation(player); + final AuthMeTeleportEvent tpEvent = new AuthMeTeleportEvent(player, spawnLoc); plugin.getServer().getPluginManager().callEvent(tpEvent); if (!tpEvent.isCancelled()) { - if (!tpEvent.getTo().getWorld().getChunkAt(tpEvent.getTo()).isLoaded()) { - tpEvent.getTo().getWorld().getChunkAt(tpEvent.getTo()).load(); - } - player.teleport(tpEvent.getTo()); + Utils.safeTP(player, tpEvent.getTo()); } } - if (LimboCache.getInstance().hasLimboPlayer(name)) + if (LimboCache.getInstance().hasLimboPlayer(name)) { LimboCache.getInstance().deleteLimboPlayer(name); + } LimboCache.getInstance().addLimboPlayer(player); - int delay = Settings.getRegistrationTimeout * 20; - int interval = Settings.getWarnMessageInterval; - BukkitScheduler sched = plugin.getServer().getScheduler(); + final int delay = Settings.getRegistrationTimeout * 20; + final int interval = Settings.getWarnMessageInterval; + final BukkitScheduler sched = plugin.getServer().getScheduler(); if (delay != 0) { - BukkitTask id = sched.runTaskLaterAsynchronously(plugin, new TimeoutTask(plugin, name, player), delay); + final BukkitTask id = sched.runTaskLaterAsynchronously(plugin, new TimeoutTask(plugin, name, player), delay); LimboCache.getInstance().getLimboPlayer(name).setTimeoutTaskId(id); } - BukkitTask msgT = sched.runTaskAsynchronously(plugin, new MessageTask(plugin, name, m.send("login_msg"), interval)); + final BukkitTask msgT = sched.runTaskAsynchronously(plugin, new MessageTask(plugin, name, m.send("login_msg"), interval)); LimboCache.getInstance().getLimboPlayer(name).setMessageTaskId(msgT); if (player.isInsideVehicle() && player.getVehicle() != null) { player.getVehicle().eject();