From 145747505f82d004f8f04347575f7774248876a3 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Tue, 28 Jun 2016 21:36:58 +0700 Subject: [PATCH 01/20] Use JsonCache correctly, couldn't list all changes. --- src/main/java/fr/xephi/authme/AuthMe.java | 7 +- .../xephi/authme/cache/backup/JsonCache.java | 140 ++++++++++++------ .../xephi/authme/cache/backup/PlayerData.java | 26 ---- .../xephi/authme/cache/limbo/LimboCache.java | 25 ++-- .../xephi/authme/cache/limbo/LimboPlayer.java | 16 +- .../AuthMeInventoryPacketAdapter.java | 4 - .../protocollib/ProtocolLibService.java | 13 -- .../authme/permission/AuthGroupHandler.java | 7 +- .../authme/process/SyncProcessManager.java | 4 +- .../authme/process/join/AsynchronousJoin.java | 2 +- .../process/login/AsynchronousLogin.java | 7 +- .../process/login/ProcessSyncPlayerLogin.java | 18 +-- .../process/logout/AsynchronousLogout.java | 2 +- .../authme/process/quit/AsynchronousQuit.java | 16 +- .../quit/ProcessSyncronousPlayerQuit.java | 22 ++- .../register/ProcessSyncPasswordRegister.java | 4 +- .../java/fr/xephi/authme/util/FileUtils.java | 24 ++- 17 files changed, 185 insertions(+), 152 deletions(-) delete mode 100644 src/main/java/fr/xephi/authme/cache/backup/PlayerData.java diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 181e0da3..5ce8ba34 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -580,11 +580,12 @@ public class AuthMe extends JavaPlugin { if (!Settings.noTeleport) { player.teleport(limbo.getLoc()); } - Utils.addNormal(player, limbo.getGroup()); player.setOp(limbo.isOperator()); - limbo.getTimeoutTask().cancel(); - limboCache.deleteLimboPlayer(name); + player.setAllowFlight(limbo.isCanFly()); + player.setWalkSpeed(limbo.getWalkSpeed()); + limbo.clearTasks(); + limboCache.deleteLimboPlayer(player); } PlayerCache.getInstance().removePlayer(name); } diff --git a/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java b/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java index d9735491..90cb013d 100644 --- a/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java +++ b/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java @@ -12,8 +12,17 @@ import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; +import fr.xephi.authme.cache.limbo.LimboPlayer; +import fr.xephi.authme.permission.PermissionsManager; +import fr.xephi.authme.settings.SpawnLoader; +import fr.xephi.authme.util.BukkitService; +import fr.xephi.authme.util.FileUtils; +import fr.xephi.authme.util.Utils; +import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.entity.Player; +import javax.inject.Inject; import java.io.File; import java.io.IOException; import java.lang.reflect.Type; @@ -22,110 +31,145 @@ public class JsonCache { private final Gson gson; private final File cacheDir; + @Inject + private AuthMe plugin; + @Inject + private PermissionsManager permissionsManager; + @Inject + private SpawnLoader spawnLoader; + @Inject + private BukkitService bukkitService; public JsonCache() { - cacheDir = new File(AuthMe.getInstance().getDataFolder(), "cache"); + cacheDir = new File(plugin.getDataFolder(), "cache"); if (!cacheDir.exists() && !cacheDir.isDirectory() && !cacheDir.mkdir()) { ConsoleLogger.showError("Failed to create cache directory."); } gson = new GsonBuilder() - .registerTypeAdapter(PlayerData.class, new PlayerDataSerializer()) - .registerTypeAdapter(PlayerData.class, new PlayerDataDeserializer()) + .registerTypeAdapter(LimboPlayer.class, new LimboPlayerSerializer()) + .registerTypeAdapter(LimboPlayer.class, new LimboPlayerDeserializer()) .setPrettyPrinting() .create(); } - public PlayerData readCache(Player player) { - String name = player.getName().toLowerCase(); - File file = new File(cacheDir, name + File.separator + "cache.json"); + public LimboPlayer readCache(Player player) { + String id = Utils.getUUIDorName(player); + File file = new File(cacheDir, id + File.separator + "cache.json"); if (!file.exists()) { return null; } try { String str = Files.toString(file, Charsets.UTF_8); - return gson.fromJson(str, PlayerData.class); + return gson.fromJson(str, LimboPlayer.class); } catch (IOException e) { ConsoleLogger.writeStackTrace(e); return null; } } - public void removeCache(Player player) { + public void writeCache(Player player) { + String id = Utils.getUUIDorName(player); String name = player.getName().toLowerCase(); - File file = new File(cacheDir, name); + Location location = player.isDead() ? spawnLoader.getSpawnLocation(player) : player.getLocation(); + String group = permissionsManager.getPrimaryGroup(player); + boolean operator = player.isOp(); + boolean canFly = player.getAllowFlight(); + float walkSpeed = player.getWalkSpeed(); + LimboPlayer limboPlayer = new LimboPlayer(name, location, operator, group, canFly, walkSpeed); + try { + File file = new File(cacheDir, id + File.separator + "cache.json"); + Files.write(gson.toJson(limboPlayer), file, Charsets.UTF_8); + } catch (IOException e) { + ConsoleLogger.logException("Failed to write " + player.getName() + " cache.", e); + } + } + + public void removeCache(Player player) { + String id = Utils.getUUIDorName(player); + File file = new File(cacheDir, id); if (file.exists()) { - purgeDirectory(file); + FileUtils.purgeDirectory(file); if (!file.delete()) { - ConsoleLogger.showError("Failed to remove" + player.getName() + "cache."); + ConsoleLogger.showError("Failed to remove " + player.getName() + " cache."); } } } public boolean doesCacheExist(Player player) { - String name = player.getName().toLowerCase(); - File file = new File(cacheDir, name + File.separator + "cache.json"); + String id = Utils.getUUIDorName(player); + File file = new File(cacheDir, id + File.separator + "cache.json"); return file.exists(); } - private class PlayerDataDeserializer implements JsonDeserializer { + private class LimboPlayerDeserializer implements JsonDeserializer { @Override - public PlayerData deserialize(JsonElement jsonElement, Type type, - JsonDeserializationContext context) { + public LimboPlayer deserialize(JsonElement jsonElement, Type type, + JsonDeserializationContext context) { JsonObject jsonObject = jsonElement.getAsJsonObject(); if (jsonObject == null) { return null; } - String group = null; + + Location loc = null; + String group = ""; boolean operator = false; - boolean fly = false; + boolean canFly = false; + float walkSpeed = 0.2f; JsonElement e; + if ((e = jsonObject.getAsJsonObject("location")) != null) { + JsonObject obj = e.getAsJsonObject(); + World world = bukkitService.getWorld(obj.get("world").getAsString()); + if (world != null) { + double x = obj.get("x").getAsDouble(); + double y = obj.get("y").getAsDouble(); + double z = obj.get("z").getAsDouble(); + float yaw = obj.get("yaw").getAsFloat(); + float pitch = obj.get("pitch").getAsFloat(); + loc = new Location(world, x, y, z, yaw, pitch); + } + } if ((e = jsonObject.get("group")) != null) { group = e.getAsString(); } if ((e = jsonObject.get("operator")) != null) { operator = e.getAsBoolean(); } - if ((e = jsonObject.get("fly")) != null) { - fly = e.getAsBoolean(); + if ((e = jsonObject.get("can-fly")) != null) { + canFly = e.getAsBoolean(); + } + if ((e = jsonObject.get("walk-speed")) != null) { + walkSpeed = e.getAsFloat(); } - return new PlayerData(group, operator, fly); + return new LimboPlayer("", loc, operator, group, canFly, walkSpeed); } } - private class PlayerDataSerializer implements JsonSerializer { + private class LimboPlayerSerializer implements JsonSerializer { @Override - public JsonElement serialize(PlayerData playerData, Type type, + public JsonElement serialize(LimboPlayer limboPlayer, Type type, JsonSerializationContext context) { - JsonObject jsonObject = new JsonObject(); - jsonObject.addProperty("group", playerData.getGroup()); - jsonObject.addProperty("operator", playerData.getOperator()); - jsonObject.addProperty("fly", playerData.isFlyEnabled()); - return jsonObject; + JsonObject obj = new JsonObject(); + obj.addProperty("group", limboPlayer.getGroup()); + + Location loc = limboPlayer.getLoc(); + JsonObject obj2 = new JsonObject(); + obj2.addProperty("world", loc.getWorld().getName()); + obj2.addProperty("x", loc.getX()); + obj2.addProperty("y", loc.getY()); + obj2.addProperty("z", loc.getZ()); + obj2.addProperty("yaw", loc.getYaw()); + obj2.addProperty("pitch", loc.getPitch()); + obj.add("location", obj2); + + obj.addProperty("operator", limboPlayer.isOperator()); + obj.addProperty("can-fly", limboPlayer.isCanFly()); + obj.addProperty("walk-speed", limboPlayer.getWalkSpeed()); + return obj; } } - /** - * Delete a given directory and all its content. - * - * @param directory The directory to remove - */ - private static void purgeDirectory(File directory) { - if (!directory.isDirectory()) { - return; - } - File[] files = directory.listFiles(); - if (files == null) { - return; - } - for (File target : files) { - if (target.isDirectory()) { - purgeDirectory(target); - } - target.delete(); - } - } } diff --git a/src/main/java/fr/xephi/authme/cache/backup/PlayerData.java b/src/main/java/fr/xephi/authme/cache/backup/PlayerData.java deleted file mode 100644 index a41b8aa9..00000000 --- a/src/main/java/fr/xephi/authme/cache/backup/PlayerData.java +++ /dev/null @@ -1,26 +0,0 @@ -package fr.xephi.authme.cache.backup; - -public class PlayerData { - - private final String group; - private final boolean operator; - private final boolean flyEnabled; - - public PlayerData(String group, boolean operator, boolean flyEnabled) { - this.group = group; - this.operator = operator; - this.flyEnabled = flyEnabled; - } - - public String getGroup() { - return group; - } - - public boolean getOperator() { - return operator; - } - - public boolean isFlyEnabled() { - return flyEnabled; - } -} diff --git a/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java b/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java index 97670a88..71482c81 100644 --- a/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java +++ b/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java @@ -1,7 +1,6 @@ package fr.xephi.authme.cache.limbo; import fr.xephi.authme.cache.backup.JsonCache; -import fr.xephi.authme.cache.backup.PlayerData; import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.settings.SpawnLoader; import org.bukkit.Location; @@ -41,36 +40,40 @@ public class LimboCache { Location location = player.isDead() ? spawnLoader.getSpawnLocation(player) : player.getLocation(); boolean operator = player.isOp(); boolean flyEnabled = player.getAllowFlight(); + float walkSpeed = player.getWalkSpeed(); String playerGroup = ""; if (permissionsManager.hasGroupSupport()) { playerGroup = permissionsManager.getPrimaryGroup(player); } if (jsonCache.doesCacheExist(player)) { - PlayerData cache = jsonCache.readCache(player); + LimboPlayer cache = jsonCache.readCache(player); if (cache != null) { + location = cache.getLoc(); playerGroup = cache.getGroup(); - operator = cache.getOperator(); - flyEnabled = cache.isFlyEnabled(); + operator = cache.isOperator(); + flyEnabled = cache.isCanFly(); + walkSpeed = cache.getWalkSpeed(); } + } else { + jsonCache.writeCache(player); } - - cache.put(name, new LimboPlayer(name, location, operator, playerGroup, flyEnabled)); + cache.put(name, new LimboPlayer(name, location, operator, playerGroup, flyEnabled, walkSpeed)); } /** * Method deleteLimboPlayer. * - * @param name String + * @param player Player player to remove. */ - public void deleteLimboPlayer(String name) { - checkNotNull(name); - name = name.toLowerCase(); + public void deleteLimboPlayer(Player player) { + String name = player.getName().toLowerCase(); LimboPlayer cachedPlayer = cache.remove(name); if (cachedPlayer != null) { cachedPlayer.clearTasks(); } + jsonCache.removeCache(player); } /** @@ -104,7 +107,7 @@ public class LimboCache { */ public void updateLimboPlayer(Player player) { checkNotNull(player); - deleteLimboPlayer(player.getName().toLowerCase()); + deleteLimboPlayer(player); addLimboPlayer(player); } diff --git a/src/main/java/fr/xephi/authme/cache/limbo/LimboPlayer.java b/src/main/java/fr/xephi/authme/cache/limbo/LimboPlayer.java index 6029e6da..f8e84e3b 100644 --- a/src/main/java/fr/xephi/authme/cache/limbo/LimboPlayer.java +++ b/src/main/java/fr/xephi/authme/cache/limbo/LimboPlayer.java @@ -10,20 +10,22 @@ import org.bukkit.scheduler.BukkitTask; public class LimboPlayer { private final String name; - private final boolean fly; + private final boolean canFly; private final boolean operator; private final String group; private final Location loc; + private final float walkSpeed; private BukkitTask timeoutTask = null; private BukkitTask messageTask = null; public LimboPlayer(String name, Location loc, boolean operator, - String group, boolean fly) { + String group, boolean fly, float walkSpeed) { this.name = name; this.loc = loc; this.operator = operator; this.group = group; - this.fly = fly; + this.canFly = fly; + this.walkSpeed = walkSpeed; } /** @@ -62,8 +64,12 @@ public class LimboPlayer { return group; } - public boolean isFly() { - return fly; + public boolean isCanFly() { + return canFly; + } + + public float getWalkSpeed() { + return walkSpeed; } /** diff --git a/src/main/java/fr/xephi/authme/listener/protocollib/AuthMeInventoryPacketAdapter.java b/src/main/java/fr/xephi/authme/listener/protocollib/AuthMeInventoryPacketAdapter.java index 980f3ccb..05b39377 100644 --- a/src/main/java/fr/xephi/authme/listener/protocollib/AuthMeInventoryPacketAdapter.java +++ b/src/main/java/fr/xephi/authme/listener/protocollib/AuthMeInventoryPacketAdapter.java @@ -73,10 +73,6 @@ public class AuthMeInventoryPacketAdapter extends PacketAdapter { ProtocolLibrary.getProtocolManager().removePacketListener(this); } - public void sendInventoryPacket(Player player) { - player.updateInventory(); - } - public void sendBlankInventoryPacket(Player player) { ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager(); PacketContainer inventoryPacket = protocolManager.createPacket(PacketType.Play.Server.WINDOW_ITEMS); diff --git a/src/main/java/fr/xephi/authme/listener/protocollib/ProtocolLibService.java b/src/main/java/fr/xephi/authme/listener/protocollib/ProtocolLibService.java index b1f5573a..8788be9d 100644 --- a/src/main/java/fr/xephi/authme/listener/protocollib/ProtocolLibService.java +++ b/src/main/java/fr/xephi/authme/listener/protocollib/ProtocolLibService.java @@ -98,19 +98,6 @@ public class ProtocolLibService implements SettingsDependent { } } - /** - * Send a packet to the player to give them an inventory. - * - * @param player The player to send the packet to. - */ - public void sendInventoryPacket(Player player) { - if (!isEnabled || inventoryPacketAdapter == null) { - return; - } - - inventoryPacketAdapter.sendInventoryPacket(player); - } - /** * Send a packet to the player to give them a blank inventory. * diff --git a/src/main/java/fr/xephi/authme/permission/AuthGroupHandler.java b/src/main/java/fr/xephi/authme/permission/AuthGroupHandler.java index c7557fc2..a33cb7c5 100644 --- a/src/main/java/fr/xephi/authme/permission/AuthGroupHandler.java +++ b/src/main/java/fr/xephi/authme/permission/AuthGroupHandler.java @@ -67,14 +67,17 @@ public class AuthGroupHandler { case LOGGED_IN: // Get the limbo player data LimboPlayer limbo = limboCache.getLimboPlayer(player.getName().toLowerCase()); - if (limbo == null) + if (limbo == null) { return false; + } // Get the players group String realGroup = limbo.getGroup(); // Remove the other group types groups, set the real group - permissionsManager.removeGroups(player, Arrays.asList(Settings.unRegisteredGroup, Settings.getRegisteredGroup, Settings.getUnloggedinGroup)); + permissionsManager.removeGroups(player, + Arrays.asList(Settings.unRegisteredGroup, Settings.getRegisteredGroup, Settings.getUnloggedinGroup) + ); return permissionsManager.addGroup(player, realGroup); default: return false; diff --git a/src/main/java/fr/xephi/authme/process/SyncProcessManager.java b/src/main/java/fr/xephi/authme/process/SyncProcessManager.java index 221e1e8c..894c3a48 100644 --- a/src/main/java/fr/xephi/authme/process/SyncProcessManager.java +++ b/src/main/java/fr/xephi/authme/process/SyncProcessManager.java @@ -71,11 +71,11 @@ public class SyncProcessManager { }); } - public void processSyncPlayerQuit(final Player player, final boolean isOp, final boolean needToChange) { + public void processSyncPlayerQuit(final Player player) { runTask(new Runnable() { @Override public void run() { - processSyncronousPlayerQuit.processSyncQuit(player, isOp, needToChange); + processSyncronousPlayerQuit.processSyncQuit(player); } }); } diff --git a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java index 5551b9b8..139d80bc 100644 --- a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java +++ b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java @@ -134,7 +134,7 @@ public class AsynchronousJoin implements AsynchronousProcess { ProtectInventoryEvent ev = new ProtectInventoryEvent(player); bukkitService.callEvent(ev); if (ev.isCancelled()) { - protocolLibService.sendInventoryPacket(player); + player.updateInventory(); if (!service.getProperty(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE)) { ConsoleLogger.info("ProtectInventoryEvent has been cancelled for " + player.getName() + "..."); } diff --git a/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java b/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java index 18e7e8e4..8de8c0e8 100644 --- a/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java +++ b/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java @@ -199,12 +199,7 @@ public class AsynchronousLogin implements AsynchronousProcess { // processed in other order. LimboPlayer limboPlayer = limboCache.getLimboPlayer(name); if (limboPlayer != null) { - if (limboPlayer.getTimeoutTask() != null) { - limboPlayer.getTimeoutTask().cancel(); - } - if (limboPlayer.getMessageTask() != null) { - limboPlayer.getMessageTask().cancel(); - } + limboPlayer.clearTasks(); } syncProcessManager.processSyncPlayerLogin(player); } else if (player.isOnline()) { diff --git a/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java b/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java index 783a4ed3..f1e68cfd 100644 --- a/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java +++ b/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java @@ -2,7 +2,6 @@ package fr.xephi.authme.process.login; import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; - import fr.xephi.authme.AuthMe; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.limbo.LimboCache; @@ -20,7 +19,6 @@ import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.util.BukkitService; import fr.xephi.authme.util.TeleportationService; - import org.apache.commons.lang.reflect.MethodUtils; import org.bukkit.Bukkit; import org.bukkit.entity.LivingEntity; @@ -77,7 +75,7 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess { RestoreInventoryEvent event = new RestoreInventoryEvent(player); pluginManager.callEvent(event); if (!event.isCancelled()) { - protocolLibService.sendInventoryPacket(player); + player.updateInventory(); } } @@ -99,8 +97,13 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess { if (limbo != null) { // Restore Op state and Permission Group - restoreOpState(player, limbo); + player.setOp(limbo.isOperator()); + // Restore primary group service.setGroup(player, AuthGroupType.LOGGED_IN); + // Restore can-fly state + player.setAllowFlight(limbo.isCanFly()); + // Restore walk speed + player.setWalkSpeed(limbo.getWalkSpeed()); teleportationService.teleportOnLogin(player, auth, limbo); @@ -117,7 +120,7 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess { } // Clean up no longer used temporary data - limboCache.deleteLimboPlayer(name); + limboCache.deleteLimboPlayer(player); } // We can now display the join message (if delayed) @@ -142,6 +145,7 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess { bukkitService.callEvent(new LoginEvent(player)); player.saveData(); sendBungeeMessage(player); + // Login is done, display welcome message if (service.getProperty(RegistrationSettings.USE_WELCOME_MESSAGE)) { if (service.getProperty(RegistrationSettings.BROADCAST_WELCOME_MESSAGE)) { @@ -161,10 +165,6 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess { sendTo(player); } - private void restoreOpState(Player player, LimboPlayer limboPlayer) { - player.setOp(limboPlayer.isOperator()); - } - private void sendTo(Player player) { if(!service.getProperty(HooksSettings.BUNGEECORD)) { return; diff --git a/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java b/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java index fc81017d..4924b480 100644 --- a/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java +++ b/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java @@ -60,7 +60,7 @@ public class AsynchronousLogout implements AsynchronousProcess { } }); if (limboCache.hasLimboPlayer(name)) { - limboCache.deleteLimboPlayer(name); + limboCache.deleteLimboPlayer(player); } limboCache.addLimboPlayer(player); service.setGroup(player, AuthGroupType.NOT_LOGGED_IN); diff --git a/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java b/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java index a157287b..459452d8 100644 --- a/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java +++ b/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java @@ -5,7 +5,6 @@ import fr.xephi.authme.cache.SessionManager; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.limbo.LimboCache; -import fr.xephi.authme.cache.limbo.LimboPlayer; import fr.xephi.authme.datasource.CacheDataSource; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.process.AsynchronousProcess; @@ -13,7 +12,6 @@ import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.SyncProcessManager; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.RestrictionSettings; -import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.Utils; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -74,18 +72,6 @@ public class AsynchronousQuit implements AsynchronousProcess { database.updateSession(auth); } - boolean needToChange = false; - boolean isOp = false; - - LimboPlayer limbo = limboCache.getLimboPlayer(name); - if (limbo != null) { - if (!StringUtils.isEmpty(limbo.getGroup())) { - Utils.addNormal(player, limbo.getGroup()); - } - needToChange = true; - isOp = limbo.isOperator(); - limboCache.deleteLimboPlayer(name); - } if (!isKick) { if (plugin.isEnabled()) { BukkitTask task = plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, new Runnable() { @@ -108,7 +94,7 @@ public class AsynchronousQuit implements AsynchronousProcess { } if (plugin.isEnabled()) { - syncProcessManager.processSyncPlayerQuit(player, isOp, needToChange); + syncProcessManager.processSyncPlayerQuit(player); } // remove player from cache if (database instanceof CacheDataSource) { diff --git a/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java b/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java index c1382c2c..8ed3edbf 100644 --- a/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java +++ b/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java @@ -1,14 +1,30 @@ package fr.xephi.authme.process.quit; +import fr.xephi.authme.cache.limbo.LimboCache; +import fr.xephi.authme.cache.limbo.LimboPlayer; import fr.xephi.authme.process.SynchronousProcess; +import fr.xephi.authme.util.StringUtils; +import fr.xephi.authme.util.Utils; import org.bukkit.entity.Player; +import javax.inject.Inject; + public class ProcessSyncronousPlayerQuit implements SynchronousProcess { - public void processSyncQuit(Player player, boolean isOp, boolean needToChange) { - if (needToChange) { - player.setOp(isOp); + @Inject + private LimboCache limboCache; + + public void processSyncQuit(Player player) { + LimboPlayer limbo = limboCache.getLimboPlayer(player.getName().toLowerCase()); + if (limbo != null) { + if (!StringUtils.isEmpty(limbo.getGroup())) { + Utils.addNormal(player, limbo.getGroup()); + } + player.setOp(limbo.isOperator()); + player.setAllowFlight(limbo.isCanFly()); + player.setWalkSpeed(limbo.getWalkSpeed()); + limboCache.deleteLimboPlayer(player); } player.leaveVehicle(); } diff --git a/src/main/java/fr/xephi/authme/process/register/ProcessSyncPasswordRegister.java b/src/main/java/fr/xephi/authme/process/register/ProcessSyncPasswordRegister.java index 524d6b1c..9e20d49b 100644 --- a/src/main/java/fr/xephi/authme/process/register/ProcessSyncPasswordRegister.java +++ b/src/main/java/fr/xephi/authme/process/register/ProcessSyncPasswordRegister.java @@ -106,11 +106,11 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess { RestoreInventoryEvent event = new RestoreInventoryEvent(player); bukkitService.callEvent(event); if (!event.isCancelled()) { - protocolLibService.sendInventoryPacket(player); + player.updateInventory(); } } - limboCache.deleteLimboPlayer(name); + limboCache.deleteLimboPlayer(player); } if (!Settings.getRegisteredGroup.isEmpty()) { diff --git a/src/main/java/fr/xephi/authme/util/FileUtils.java b/src/main/java/fr/xephi/authme/util/FileUtils.java index d34c41f8..bd9c7fb9 100644 --- a/src/main/java/fr/xephi/authme/util/FileUtils.java +++ b/src/main/java/fr/xephi/authme/util/FileUtils.java @@ -22,7 +22,8 @@ public class FileUtils { * Copy a resource file (from the JAR) to the given file if it doesn't exist. * * @param destinationFile The file to check and copy to (outside of JAR) - * @param resourcePath Absolute path to the resource file (path to file within JAR) + * @param resourcePath Absolute path to the resource file (path to file within JAR) + * * @return False if the file does not exist and could not be copied, true otherwise */ public static boolean copyFileFromResource(File destinationFile, String resourcePath) { @@ -49,4 +50,25 @@ public class FileUtils { } return false; } + + /** + * Delete a given directory and all its content. + * + * @param directory The directory to remove + */ + public static void purgeDirectory(File directory) { + if (!directory.isDirectory()) { + return; + } + File[] files = directory.listFiles(); + if (files == null) { + return; + } + for (File target : files) { + if (target.isDirectory()) { + purgeDirectory(target); + } + target.delete(); + } + } } From 45d8e24350a8d4ec91d68185815687679075cc92 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Wed, 29 Jun 2016 00:34:31 +0700 Subject: [PATCH 02/20] - Fix fly speed not get restored after login. - Attempt to fix #592 --- src/main/java/fr/xephi/authme/AuthMe.java | 50 ++++++++++++------- .../xephi/authme/cache/limbo/LimboCache.java | 5 +- .../process/login/ProcessSyncPlayerLogin.java | 19 +++---- .../process/logout/AsynchronousLogout.java | 7 +-- .../ProcessSynchronousPlayerLogout.java | 19 +++---- 5 files changed, 53 insertions(+), 47 deletions(-) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 5ce8ba34..1224aba4 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -293,16 +293,16 @@ public class AuthMe extends JavaPlugin { // Some statically injected things initializer.register(PlayerCache.class, PlayerCache.getInstance()); - messages = initializer.get(Messages.class); - permsMan = initializer.get(PermissionsManager.class); - bukkitService = initializer.get(BukkitService.class); - pluginHooks = initializer.get(PluginHooks.class); + messages = initializer.get(Messages.class); + permsMan = initializer.get(PermissionsManager.class); + bukkitService = initializer.get(BukkitService.class); + pluginHooks = initializer.get(PluginHooks.class); passwordSecurity = initializer.get(PasswordSecurity.class); - spawnLoader = initializer.get(SpawnLoader.class); - commandHandler = initializer.get(CommandHandler.class); - api = initializer.get(NewAPI.class); - management = initializer.get(Management.class); - geoLiteApi = initializer.get(GeoLiteAPI.class); + spawnLoader = initializer.get(SpawnLoader.class); + commandHandler = initializer.get(CommandHandler.class); + api = initializer.get(NewAPI.class); + management = initializer.get(Management.class); + geoLiteApi = initializer.get(GeoLiteAPI.class); initializer.get(API.class); } @@ -341,7 +341,7 @@ public class AuthMe extends JavaPlugin { // Register event listeners pluginManager.registerEvents(initializer.get(AuthMePlayerListener.class), this); - pluginManager.registerEvents(initializer.get(AuthMeBlockListener.class), this); + pluginManager.registerEvents(initializer.get(AuthMeBlockListener.class), this); pluginManager.registerEvents(initializer.get(AuthMeEntityListener.class), this); pluginManager.registerEvents(initializer.get(AuthMeServerListener.class), this); @@ -568,16 +568,9 @@ public class AuthMe extends JavaPlugin { return; } String name = player.getName().toLowerCase(); - if (PlayerCache.getInstance().isAuthenticated(name) && !player.isDead() && Settings.isSaveQuitLocationEnabled) { - final PlayerAuth auth = PlayerAuth.builder() - .name(player.getName().toLowerCase()) - .realName(player.getName()) - .location(player.getLocation()).build(); - database.updateQuitLoc(auth); - } if (limboCache.hasLimboPlayer(name)) { LimboPlayer limbo = limboCache.getLimboPlayer(name); - if (!Settings.noTeleport) { + if (!newSettings.getProperty(RestrictionSettings.NO_TELEPORT)) { player.teleport(limbo.getLoc()); } Utils.addNormal(player, limbo.getGroup()); @@ -587,6 +580,21 @@ public class AuthMe extends JavaPlugin { limbo.clearTasks(); limboCache.deleteLimboPlayer(player); } + if (PlayerCache.getInstance().isAuthenticated(name) && !player.isDead()) { + if (Settings.isSaveQuitLocationEnabled) { + + final PlayerAuth auth = PlayerAuth.builder() + .name(player.getName().toLowerCase()) + .realName(player.getName()) + .location(player.getLocation()).build(); + database.updateQuitLoc(auth); + } + if (newSettings.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN) + && !newSettings.getProperty(RestrictionSettings.NO_TELEPORT)) { + limboCache.getJsonCache().writeCache(player); + player.teleport(spawnLoader.getSpawnLocation(player)); + } + } PlayerCache.getInstance().removePlayer(name); } @@ -638,7 +646,6 @@ public class AuthMe extends JavaPlugin { } - /** * Handle Bukkit commands. * @@ -678,6 +685,7 @@ public class AuthMe extends JavaPlugin { /** * @return permission manager + * * @deprecated should be used in API classes only (temporarily) */ @Deprecated @@ -687,6 +695,7 @@ public class AuthMe extends JavaPlugin { /** * @return process manager + * * @deprecated should be used in API classes only (temporarily) */ @Deprecated @@ -696,6 +705,7 @@ public class AuthMe extends JavaPlugin { /** * @return the datasource + * * @deprecated should be used in API classes only (temporarily) */ @Deprecated @@ -705,6 +715,7 @@ public class AuthMe extends JavaPlugin { /** * @return password manager + * * @deprecated should be used in API classes only (temporarily) */ @Deprecated @@ -714,6 +725,7 @@ public class AuthMe extends JavaPlugin { /** * @return plugin hooks + * * @deprecated should be used in API classes only (temporarily) */ @Deprecated diff --git a/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java b/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java index 71482c81..2bc39ded 100644 --- a/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java +++ b/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java @@ -30,6 +30,10 @@ public class LimboCache { this.spawnLoader = spawnLoader; } + public JsonCache getJsonCache() { + return jsonCache; + } + /** * Add a limbo player. * @@ -110,5 +114,4 @@ public class LimboCache { deleteLimboPlayer(player); addLimboPlayer(player); } - } diff --git a/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java b/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java index f1e68cfd..7f6e598c 100644 --- a/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java +++ b/src/main/java/fr/xephi/authme/process/login/ProcessSyncPlayerLogin.java @@ -62,15 +62,6 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess { ProcessSyncPlayerLogin() { } - - private void restoreSpeedEffects(Player player) { - if (!service.getProperty(RestrictionSettings.ALLOW_UNAUTHED_MOVEMENT) - && service.getProperty(RestrictionSettings.REMOVE_SPEED)) { - player.setWalkSpeed(0.2F); - player.setFlySpeed(0.1F); - } - } - private void restoreInventory(Player player) { RestoreInventoryEvent event = new RestoreInventoryEvent(player); pluginManager.callEvent(event); @@ -102,8 +93,13 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess { service.setGroup(player, AuthGroupType.LOGGED_IN); // Restore can-fly state player.setAllowFlight(limbo.isCanFly()); - // Restore walk speed - player.setWalkSpeed(limbo.getWalkSpeed()); + + // Restore speed + if (!service.getProperty(RestrictionSettings.ALLOW_UNAUTHED_MOVEMENT) + && service.getProperty(RestrictionSettings.REMOVE_SPEED)) { + player.setWalkSpeed(limbo.getWalkSpeed()); + player.setFlySpeed(0.2F); + } teleportationService.teleportOnLogin(player, auth, limbo); @@ -136,7 +132,6 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess { AuthMePlayerListener.joinMessage.remove(name); } - restoreSpeedEffects(player); if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) { player.removePotionEffect(PotionEffectType.BLINDNESS); } diff --git a/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java b/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java index 4924b480..25842af0 100644 --- a/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java +++ b/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java @@ -5,7 +5,6 @@ import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.output.MessageKey; -import fr.xephi.authme.permission.AuthGroupType; import fr.xephi.authme.process.AsynchronousProcess; import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.SyncProcessManager; @@ -59,11 +58,7 @@ public class AsynchronousLogout implements AsynchronousProcess { Utils.teleportToSpawn(player); } }); - if (limboCache.hasLimboPlayer(name)) { - limboCache.deleteLimboPlayer(player); - } - limboCache.addLimboPlayer(player); - service.setGroup(player, AuthGroupType.NOT_LOGGED_IN); + limboCache.updateLimboPlayer(player); syncProcessManager.processSyncPlayerLogout(player); } } diff --git a/src/main/java/fr/xephi/authme/process/logout/ProcessSynchronousPlayerLogout.java b/src/main/java/fr/xephi/authme/process/logout/ProcessSynchronousPlayerLogout.java index 6a36ab44..c8336262 100644 --- a/src/main/java/fr/xephi/authme/process/logout/ProcessSynchronousPlayerLogout.java +++ b/src/main/java/fr/xephi/authme/process/logout/ProcessSynchronousPlayerLogout.java @@ -8,6 +8,7 @@ import fr.xephi.authme.cache.SessionManager; import fr.xephi.authme.events.LogoutEvent; import fr.xephi.authme.listener.protocollib.ProtocolLibService; import fr.xephi.authme.output.MessageKey; +import fr.xephi.authme.permission.AuthGroupType; import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.SynchronousProcess; import fr.xephi.authme.settings.properties.HooksSettings; @@ -56,14 +57,6 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess { player.sendPluginMessage(plugin, "BungeeCord", out.toByteArray()); } - private void restoreSpeedEffect(Player player) { - if (!service.getProperty(RestrictionSettings.ALLOW_UNAUTHED_MOVEMENT) - && service.getProperty(RestrictionSettings.REMOVE_SPEED)) { - player.setFlySpeed(0.0f); - player.setWalkSpeed(0.0f); - } - } - public void processSyncLogout(Player player) { final String name = player.getName().toLowerCase(); if (sessionManager.hasSession(name)) { @@ -83,8 +76,16 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess { if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) { player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, timeout, 2)); } + + service.setGroup(player, AuthGroupType.NOT_LOGGED_IN); player.setOp(false); - restoreSpeedEffect(player); + // Remove speed + if (!service.getProperty(RestrictionSettings.ALLOW_UNAUTHED_MOVEMENT) + && service.getProperty(RestrictionSettings.REMOVE_SPEED)) { + player.setFlySpeed(0.0f); + player.setWalkSpeed(0.0f); + } + // Player is now logout... Time to fire event ! bukkitService.callEvent(new LogoutEvent(player)); if (service.getProperty(HooksSettings.BUNGEECORD)) { From ad068ffba0fd0cf229f6670c0da769e4490ea393 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Wed, 29 Jun 2016 04:49:10 +0700 Subject: [PATCH 03/20] - Make JsonCache injectable - Only inject needed field --- src/main/java/fr/xephi/authme/AuthMe.java | 8 ++++++-- .../java/fr/xephi/authme/cache/backup/JsonCache.java | 9 ++++----- .../java/fr/xephi/authme/cache/limbo/LimboCache.java | 9 ++++----- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 1224aba4..ddd0df7e 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -5,6 +5,7 @@ import fr.xephi.authme.api.API; import fr.xephi.authme.api.NewAPI; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; +import fr.xephi.authme.cache.backup.JsonCache; import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.cache.limbo.LimboPlayer; import fr.xephi.authme.command.CommandHandler; @@ -591,8 +592,11 @@ public class AuthMe extends JavaPlugin { } if (newSettings.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN) && !newSettings.getProperty(RestrictionSettings.NO_TELEPORT)) { - limboCache.getJsonCache().writeCache(player); - player.teleport(spawnLoader.getSpawnLocation(player)); + JsonCache jsonCache = initializer.getIfAvailable(JsonCache.class); + if (jsonCache != null) { + jsonCache.writeCache(player); + player.teleport(spawnLoader.getSpawnLocation(player)); + } } } PlayerCache.getInstance().removePlayer(name); diff --git a/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java b/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java index 90cb013d..6f56e2c4 100644 --- a/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java +++ b/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java @@ -10,9 +10,9 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; -import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.limbo.LimboPlayer; +import fr.xephi.authme.initialization.DataFolder; import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.settings.SpawnLoader; import fr.xephi.authme.util.BukkitService; @@ -32,16 +32,15 @@ public class JsonCache { private final Gson gson; private final File cacheDir; @Inject - private AuthMe plugin; - @Inject private PermissionsManager permissionsManager; @Inject private SpawnLoader spawnLoader; @Inject private BukkitService bukkitService; - public JsonCache() { - cacheDir = new File(plugin.getDataFolder(), "cache"); + @Inject + public JsonCache(@DataFolder File dataFolder) { + cacheDir = new File(dataFolder, "cache"); if (!cacheDir.exists() && !cacheDir.isDirectory() && !cacheDir.mkdir()) { ConsoleLogger.showError("Failed to create cache directory."); } diff --git a/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java b/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java index 2bc39ded..2bd4c669 100644 --- a/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java +++ b/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java @@ -17,10 +17,13 @@ import static com.google.common.base.Preconditions.checkNotNull; public class LimboCache { private final ConcurrentHashMap cache = new ConcurrentHashMap<>(); - private final JsonCache jsonCache = new JsonCache(); + + @Inject + private JsonCache jsonCache; @Inject private PermissionsManager permissionsManager; + @Inject private SpawnLoader spawnLoader; @@ -30,10 +33,6 @@ public class LimboCache { this.spawnLoader = spawnLoader; } - public JsonCache getJsonCache() { - return jsonCache; - } - /** * Add a limbo player. * From 392b8ac19e8349d123cb1a907c449921531b15e0 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Wed, 29 Jun 2016 05:17:56 +0700 Subject: [PATCH 04/20] Sync with master. --- .../xephi/authme/process/quit/AsynchronousQuit.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java b/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java index 57e6552e..cad276a0 100644 --- a/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java +++ b/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java @@ -44,7 +44,8 @@ public class AsynchronousQuit implements AsynchronousProcess { @Inject private SessionManager sessionManager; - AsynchronousQuit() { } + AsynchronousQuit() { + } public void processQuit(Player player, boolean isKick) { @@ -72,9 +73,11 @@ public class AsynchronousQuit implements AsynchronousProcess { database.updateSession(auth); } - if (!isKick) { - if (plugin.isEnabled()) { - BukkitTask task = plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, new Runnable() { + //always unauthenticate the player - use session only for auto logins on the same ip + playerCache.removePlayer(name); + + if (plugin.isEnabled() && service.getProperty(PluginSettings.SESSIONS_ENABLED)) { + BukkitTask task = plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, new Runnable() { @Override public void run() { From b455e20811de71916bc4562f41e77f334c87c36e Mon Sep 17 00:00:00 2001 From: DNx5 Date: Wed, 29 Jun 2016 06:06:54 +0700 Subject: [PATCH 05/20] - Fix NPE in JsonCache. --- src/main/java/fr/xephi/authme/cache/backup/JsonCache.java | 7 ++++++- src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java b/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java index 6f56e2c4..6de1ec9c 100644 --- a/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java +++ b/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java @@ -39,7 +39,12 @@ public class JsonCache { private BukkitService bukkitService; @Inject - public JsonCache(@DataFolder File dataFolder) { + public JsonCache(@DataFolder File dataFolder, PermissionsManager permsMan, + SpawnLoader spawnLoader, BukkitService bukkitService) { + this.permissionsManager = permsMan; + this.spawnLoader = spawnLoader; + this.bukkitService = bukkitService; + cacheDir = new File(dataFolder, "cache"); if (!cacheDir.exists() && !cacheDir.isDirectory() && !cacheDir.mkdir()) { ConsoleLogger.showError("Failed to create cache directory."); diff --git a/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java b/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java index 2bd4c669..902270d2 100644 --- a/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java +++ b/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java @@ -28,9 +28,10 @@ public class LimboCache { private SpawnLoader spawnLoader; @Inject - LimboCache(PermissionsManager permissionsManager, SpawnLoader spawnLoader) { + LimboCache(PermissionsManager permissionsManager, SpawnLoader spawnLoader, JsonCache jsonCache) { this.permissionsManager = permissionsManager; this.spawnLoader = spawnLoader; + this.jsonCache = jsonCache; } /** From 609b14815732950e74cd0eecaae656d98561d2c6 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Wed, 29 Jun 2016 06:19:49 +0700 Subject: [PATCH 06/20] Touch the file, it works fine now. --- src/main/java/fr/xephi/authme/cache/backup/JsonCache.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java b/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java index 6de1ec9c..57bf7051 100644 --- a/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java +++ b/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java @@ -83,6 +83,8 @@ public class JsonCache { LimboPlayer limboPlayer = new LimboPlayer(name, location, operator, group, canFly, walkSpeed); try { File file = new File(cacheDir, id + File.separator + "cache.json"); + Files.createParentDirs(file); + Files.touch(file); Files.write(gson.toJson(limboPlayer), file, Charsets.UTF_8); } catch (IOException e) { ConsoleLogger.logException("Failed to write " + player.getName() + " cache.", e); From 22a4ef93bf2d21369ea9b4ec28d027e4206c34bc Mon Sep 17 00:00:00 2001 From: DNx5 Date: Wed, 29 Jun 2016 20:25:01 +0700 Subject: [PATCH 07/20] - Remove unnecessary @Inject on field. - Teleport player to spawn immediately on PlayerLoginEvent. - Only save authenticated player's location on quit. - Fix player's last location get reset if fail to login. --- src/main/java/fr/xephi/authme/AuthMe.java | 36 ++++++++-------- .../xephi/authme/cache/backup/JsonCache.java | 10 ++--- .../xephi/authme/cache/limbo/LimboCache.java | 20 +++++---- .../authme/listener/AuthMePlayerListener.java | 4 ++ .../authme/process/join/AsynchronousJoin.java | 28 +++++-------- .../authme/process/quit/AsynchronousQuit.java | 11 +++-- .../quit/ProcessSyncronousPlayerQuit.java | 41 +++++++++++++++---- 7 files changed, 88 insertions(+), 62 deletions(-) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 64595834..ca5df4fa 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -293,15 +293,15 @@ public class AuthMe extends JavaPlugin { // Some statically injected things initializer.register(PlayerCache.class, PlayerCache.getInstance()); - messages = initializer.get(Messages.class); - permsMan = initializer.get(PermissionsManager.class); - bukkitService = initializer.get(BukkitService.class); - pluginHooks = initializer.get(PluginHooks.class); + messages = initializer.get(Messages.class); + permsMan = initializer.get(PermissionsManager.class); + bukkitService = initializer.get(BukkitService.class); + pluginHooks = initializer.get(PluginHooks.class); passwordSecurity = initializer.get(PasswordSecurity.class); - spawnLoader = initializer.get(SpawnLoader.class); - commandHandler = initializer.get(CommandHandler.class); - management = initializer.get(Management.class); - geoLiteApi = initializer.get(GeoLiteAPI.class); + spawnLoader = initializer.get(SpawnLoader.class); + commandHandler = initializer.get(CommandHandler.class); + management = initializer.get(Management.class); + geoLiteApi = initializer.get(GeoLiteAPI.class); initializer.get(NewAPI.class); initializer.get(API.class); } @@ -577,24 +577,26 @@ public class AuthMe extends JavaPlugin { player.setOp(limbo.isOperator()); player.setAllowFlight(limbo.isCanFly()); player.setWalkSpeed(limbo.getWalkSpeed()); - limbo.clearTasks(); - limboCache.deleteLimboPlayer(player); - } - if (PlayerCache.getInstance().isAuthenticated(name) && !player.isDead()) { - if (Settings.isSaveQuitLocationEnabled) { - + if (newSettings.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN)) { + limboCache.removeLimboPlayer(player); + } else { + limboCache.deleteLimboPlayer(player); + } + } else { + if (newSettings.getProperty(RestrictionSettings.SAVE_QUIT_LOCATION)) { + Location loc = + player.isOnline() && player.isDead() ? spawnLoader.getSpawnLocation(player) : player.getLocation(); final PlayerAuth auth = PlayerAuth.builder() .name(player.getName().toLowerCase()) .realName(player.getName()) - .location(player.getLocation()).build(); + .location(loc).build(); database.updateQuitLoc(auth); } if (newSettings.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN) && !newSettings.getProperty(RestrictionSettings.NO_TELEPORT)) { JsonCache jsonCache = initializer.getIfAvailable(JsonCache.class); - if (jsonCache != null) { + if (jsonCache != null && !jsonCache.doesCacheExist(player)) { jsonCache.writeCache(player); - player.teleport(spawnLoader.getSpawnLocation(player)); } } } diff --git a/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java b/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java index 57bf7051..39b2d48f 100644 --- a/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java +++ b/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java @@ -31,16 +31,13 @@ public class JsonCache { private final Gson gson; private final File cacheDir; - @Inject private PermissionsManager permissionsManager; - @Inject private SpawnLoader spawnLoader; - @Inject private BukkitService bukkitService; @Inject - public JsonCache(@DataFolder File dataFolder, PermissionsManager permsMan, - SpawnLoader spawnLoader, BukkitService bukkitService) { + JsonCache(@DataFolder File dataFolder, PermissionsManager permsMan, + SpawnLoader spawnLoader, BukkitService bukkitService) { this.permissionsManager = permsMan; this.spawnLoader = spawnLoader; this.bukkitService = bukkitService; @@ -75,7 +72,8 @@ public class JsonCache { public void writeCache(Player player) { String id = Utils.getUUIDorName(player); String name = player.getName().toLowerCase(); - Location location = player.isDead() ? spawnLoader.getSpawnLocation(player) : player.getLocation(); + Location location = + player.isOnline() && player.isDead() ? spawnLoader.getSpawnLocation(player) : player.getLocation(); String group = permissionsManager.getPrimaryGroup(player); boolean operator = player.isOp(); boolean canFly = player.getAllowFlight(); diff --git a/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java b/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java index 902270d2..7553630e 100644 --- a/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java +++ b/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java @@ -18,13 +18,8 @@ public class LimboCache { private final ConcurrentHashMap cache = new ConcurrentHashMap<>(); - @Inject private JsonCache jsonCache; - - @Inject private PermissionsManager permissionsManager; - - @Inject private SpawnLoader spawnLoader; @Inject @@ -67,17 +62,26 @@ public class LimboCache { } /** - * Method deleteLimboPlayer. + * Remove LimboPlayer and delete cache.json from disk. * * @param player Player player to remove. */ public void deleteLimboPlayer(Player player) { + removeLimboPlayer(player); + jsonCache.removeCache(player); + } + + /** + * Remove LimboPlayer from cache, without deleting cache.json file. + * + * @param player Player player to remove. + */ + public void removeLimboPlayer(Player player) { String name = player.getName().toLowerCase(); LimboPlayer cachedPlayer = cache.remove(name); if (cachedPlayer != null) { cachedPlayer.clearTasks(); } - jsonCache.removeCache(player); } /** @@ -111,7 +115,7 @@ public class LimboCache { */ public void updateLimboPlayer(Player player) { checkNotNull(player); - deleteLimboPlayer(player); + removeLimboPlayer(player); addLimboPlayer(player); } } diff --git a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java index fba9e6fc..5320a72a 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java @@ -12,6 +12,7 @@ import fr.xephi.authme.settings.properties.HooksSettings; import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.util.BukkitService; +import fr.xephi.authme.util.TeleportationService; import fr.xephi.authme.util.Utils; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -72,6 +73,8 @@ public class AuthMePlayerListener implements Listener { private OnJoinVerifier onJoinVerifier; @Inject private ListenerService listenerService; + @Inject + private TeleportationService teleportationService; @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { @@ -229,6 +232,7 @@ public class AuthMePlayerListener implements Listener { } antiBot.handlePlayerJoin(player); + teleportationService.teleportOnJoin(player); } @EventHandler(priority = EventPriority.HIGHEST) diff --git a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java index 0e3540db..f63973e3 100644 --- a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java +++ b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java @@ -22,7 +22,6 @@ import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.settings.properties.SecuritySettings; import fr.xephi.authme.task.LimboPlayerTaskManager; import fr.xephi.authme.util.BukkitService; -import fr.xephi.authme.util.TeleportationService; import fr.xephi.authme.util.Utils; import org.apache.commons.lang.reflect.MethodUtils; import org.bukkit.GameMode; @@ -63,9 +62,6 @@ public class AsynchronousJoin implements AsynchronousProcess { @Inject private PluginHooks pluginHooks; - @Inject - private TeleportationService teleportationService; - @Inject private BukkitService bukkitService; @@ -75,7 +71,8 @@ public class AsynchronousJoin implements AsynchronousProcess { @Inject private LimboPlayerTaskManager limboPlayerTaskManager; - AsynchronousJoin() { } + AsynchronousJoin() { + } public void processJoin(final Player player) { @@ -122,12 +119,12 @@ public class AsynchronousJoin implements AsynchronousProcess { return; } + limboCache.updateLimboPlayer(player); + final boolean isAuthAvailable = database.isAuthAvailable(name); if (isAuthAvailable) { service.setGroup(player, AuthGroupType.NOT_LOGGED_IN); - teleportationService.teleportOnJoin(player); - limboCache.updateLimboPlayer(player); // Protect inventory if (service.getProperty(PROTECT_INVENTORY_BEFORE_LOGIN)) { @@ -166,13 +163,6 @@ public class AsynchronousJoin implements AsynchronousProcess { if (!service.getProperty(RegistrationSettings.FORCE)) { return; } - - teleportationService.teleportOnJoin(player); - } - // The user is not logged in - - if (!limboCache.hasLimboPlayer(name)) { - limboCache.addLimboPlayer(player); } final int registrationTimeout = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND; @@ -211,11 +201,12 @@ public class AsynchronousJoin implements AsynchronousProcess { /** * Returns whether the name is restricted based on the restriction settings. * - * @param name The name to check - * @param ip The IP address of the player + * @param name The name to check + * @param ip The IP address of the player * @param domain The hostname of the IP address + * * @return True if the name is restricted (IP/domain is not allowed for the given name), - * false if the restrictions are met or if the name has no restrictions to it + * false if the restrictions are met or if the name has no restrictions to it */ private boolean isNameRestricted(String name, String ip, String domain) { if (!service.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)) { @@ -242,7 +233,8 @@ public class AsynchronousJoin implements AsynchronousProcess { * settings and permissions). If this is the case, the player is kicked. * * @param player the player to verify - * @param ip the ip address of the player + * @param ip the ip address of the player + * * @return true if the verification is OK (no infraction), false if player has been kicked */ private boolean validatePlayerCountForIp(final Player player, String ip) { diff --git a/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java b/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java index cad276a0..726fc1a2 100644 --- a/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java +++ b/src/main/java/fr/xephi/authme/process/quit/AsynchronousQuit.java @@ -4,12 +4,12 @@ import fr.xephi.authme.AuthMe; import fr.xephi.authme.cache.SessionManager; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; -import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.datasource.CacheDataSource; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.process.AsynchronousProcess; import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.SyncProcessManager; +import fr.xephi.authme.settings.SpawnLoader; import fr.xephi.authme.settings.properties.PluginSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.util.Utils; @@ -35,15 +35,15 @@ public class AsynchronousQuit implements AsynchronousProcess { @Inject private PlayerCache playerCache; - @Inject - private LimboCache limboCache; - @Inject private SyncProcessManager syncProcessManager; @Inject private SessionManager sessionManager; + @Inject + private SpawnLoader spawnLoader; + AsynchronousQuit() { } @@ -55,10 +55,9 @@ public class AsynchronousQuit implements AsynchronousProcess { final String name = player.getName().toLowerCase(); String ip = Utils.getPlayerIp(player); - if (playerCache.isAuthenticated(name)) { if (service.getProperty(RestrictionSettings.SAVE_QUIT_LOCATION)) { - Location loc = player.getLocation(); + Location loc = player.isDead() ? spawnLoader.getSpawnLocation(player) : player.getLocation(); PlayerAuth auth = PlayerAuth.builder() .name(name).location(loc) .realName(player.getName()).build(); diff --git a/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java b/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java index 8ed3edbf..2ed9bb1f 100644 --- a/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java +++ b/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java @@ -1,8 +1,12 @@ package fr.xephi.authme.process.quit; +import fr.xephi.authme.cache.backup.JsonCache; import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.cache.limbo.LimboPlayer; +import fr.xephi.authme.datasource.DataSource; +import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.SynchronousProcess; +import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.Utils; import org.bukkit.entity.Player; @@ -12,20 +16,43 @@ import javax.inject.Inject; public class ProcessSyncronousPlayerQuit implements SynchronousProcess { + @Inject + private JsonCache jsonCache; + + @Inject + private DataSource database; + + @Inject + private ProcessService service; + @Inject private LimboCache limboCache; public void processSyncQuit(Player player) { LimboPlayer limbo = limboCache.getLimboPlayer(player.getName().toLowerCase()); - if (limbo != null) { - if (!StringUtils.isEmpty(limbo.getGroup())) { - Utils.addNormal(player, limbo.getGroup()); + if (limbo != null) { // it mean player is not authenticated + // Only delete if we don't need player's last location + if (service.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN)) { + limboCache.removeLimboPlayer(player); + } else { + // Restore data if its about to delete LimboPlayer + if (!StringUtils.isEmpty(limbo.getGroup())) { + Utils.addNormal(player, limbo.getGroup()); + } + player.setOp(limbo.isOperator()); + player.setAllowFlight(limbo.isCanFly()); + player.setWalkSpeed(limbo.getWalkSpeed()); + limboCache.deleteLimboPlayer(player); + } + } else { + // Write player's location, so we could retrieve it later on player next join + if (service.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN)) { + if (!jsonCache.doesCacheExist(player)) { + jsonCache.writeCache(player); + } } - player.setOp(limbo.isOperator()); - player.setAllowFlight(limbo.isCanFly()); - player.setWalkSpeed(limbo.getWalkSpeed()); - limboCache.deleteLimboPlayer(player); } + player.leaveVehicle(); } } From b1b56f2d515f76a2e528a7ba06b7e614812873b4 Mon Sep 17 00:00:00 2001 From: Gabriele C Date: Wed, 29 Jun 2016 15:56:55 +0200 Subject: [PATCH 08/20] Cleanup, updated some settings --- .../protocollib/AuthMeInventoryPacketAdapter.java | 4 +--- .../fr/xephi/authme/process/join/AsynchronousJoin.java | 3 --- .../authme/process/quit/ProcessSyncronousPlayerQuit.java | 3 --- src/main/java/fr/xephi/authme/settings/Settings.java | 8 -------- src/test/java/fr/xephi/authme/task/PurgeServiceTest.java | 1 + 5 files changed, 2 insertions(+), 17 deletions(-) diff --git a/src/main/java/fr/xephi/authme/listener/protocollib/AuthMeInventoryPacketAdapter.java b/src/main/java/fr/xephi/authme/listener/protocollib/AuthMeInventoryPacketAdapter.java index 50c6cdfb..88d83f61 100644 --- a/src/main/java/fr/xephi/authme/listener/protocollib/AuthMeInventoryPacketAdapter.java +++ b/src/main/java/fr/xephi/authme/listener/protocollib/AuthMeInventoryPacketAdapter.java @@ -24,7 +24,6 @@ import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketEvent; import fr.xephi.authme.AuthMe; import fr.xephi.authme.cache.auth.PlayerCache; -import fr.xephi.authme.settings.Settings; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -52,8 +51,7 @@ public class AuthMeInventoryPacketAdapter extends PacketAdapter { PacketContainer packet = packetEvent.getPacket(); byte windowId = packet.getIntegers().read(0).byteValue(); - if (windowId == PLAYER_INVENTORY && Settings.protectInventoryBeforeLogInEnabled - && !PlayerCache.getInstance().isAuthenticated(player.getName())) { + if (windowId == PLAYER_INVENTORY && !PlayerCache.getInstance().isAuthenticated(player.getName())) { packetEvent.setCancelled(true); } } diff --git a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java index f63973e3..7c83ec28 100644 --- a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java +++ b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java @@ -65,9 +65,6 @@ public class AsynchronousJoin implements AsynchronousProcess { @Inject private BukkitService bukkitService; - @Inject - private ProtocolLibService protocolLibService; - @Inject private LimboPlayerTaskManager limboPlayerTaskManager; diff --git a/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java b/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java index 2ed9bb1f..13a6a7b3 100644 --- a/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java +++ b/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java @@ -19,9 +19,6 @@ public class ProcessSyncronousPlayerQuit implements SynchronousProcess { @Inject private JsonCache jsonCache; - @Inject - private DataSource database; - @Inject private ProcessService service; diff --git a/src/main/java/fr/xephi/authme/settings/Settings.java b/src/main/java/fr/xephi/authme/settings/Settings.java index 6e843001..7c128d99 100644 --- a/src/main/java/fr/xephi/authme/settings/Settings.java +++ b/src/main/java/fr/xephi/authme/settings/Settings.java @@ -18,10 +18,7 @@ public final class Settings { public static List getUnrestrictedName; public static boolean isPermissionCheckEnabled; public static boolean isTeleportToSpawnEnabled; - public static boolean isSessionsEnabled; public static boolean isAllowRestrictedIp; - public static boolean isSaveQuitLocationEnabled; - public static boolean protectInventoryBeforeLogInEnabled; public static boolean isStopEnabled; public static boolean reloadSupport; public static boolean noTeleport; @@ -31,7 +28,6 @@ public final class Settings { public static String getRegisteredGroup; public static String defaultWorld; public static String crazyloginFileName; - public static int getSessionTimeout; public static int getNonActivatedGroup; private static FileConfiguration configFile; @@ -48,17 +44,13 @@ public final class Settings { private static void loadVariables() { isPermissionCheckEnabled = load(PluginSettings.ENABLE_PERMISSION_CHECK); isTeleportToSpawnEnabled = load(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN); - isSessionsEnabled = load(PluginSettings.SESSIONS_ENABLED); - getSessionTimeout = configFile.getInt("settings.sessions.timeout", 10); isAllowRestrictedIp = load(RestrictionSettings.ENABLE_RESTRICTED_USERS); isRemoveSpeedEnabled = load(RestrictionSettings.REMOVE_SPEED); - isSaveQuitLocationEnabled = load(RestrictionSettings.SAVE_QUIT_LOCATION); getUnloggedinGroup = load(SecuritySettings.UNLOGGEDIN_GROUP); getNonActivatedGroup = configFile.getInt("ExternalBoardOptions.nonActivedUserGroup", -1); unRegisteredGroup = configFile.getString("GroupOptions.UnregisteredPlayerGroup", ""); getUnrestrictedName = load(RestrictionSettings.UNRESTRICTED_NAMES); getRegisteredGroup = configFile.getString("GroupOptions.RegisteredPlayerGroup", ""); - protectInventoryBeforeLogInEnabled = load(RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN); isStopEnabled = configFile.getBoolean("Security.SQLProblem.stopServer", true); reloadSupport = configFile.getBoolean("Security.ReloadCommand.useReloadCommandSupport", true); defaultWorld = configFile.getString("Purge.defaultWorld", "world"); diff --git a/src/test/java/fr/xephi/authme/task/PurgeServiceTest.java b/src/test/java/fr/xephi/authme/task/PurgeServiceTest.java index 3eb4bac4..a631476e 100644 --- a/src/test/java/fr/xephi/authme/task/PurgeServiceTest.java +++ b/src/test/java/fr/xephi/authme/task/PurgeServiceTest.java @@ -126,6 +126,7 @@ public class PurgeServiceTest { verifyScheduledPurgeTask(null, "alpha", "charlie"); } + @SuppressWarnings("unchecked") @Test public void shouldRecognizeNoPlayersToPurge() { // given From 56d6fd81b41d8499b6441c66d52b8e304df8b6b6 Mon Sep 17 00:00:00 2001 From: Gabriele C Date: Wed, 29 Jun 2016 16:18:12 +0200 Subject: [PATCH 09/20] Cleanup --- .../java/fr/xephi/authme/hooks/BungeeCordMessage.java | 1 - .../fr/xephi/authme/permission/AuthGroupHandler.java | 10 ++++++---- .../fr/xephi/authme/process/join/AsynchronousJoin.java | 1 - .../process/quit/ProcessSyncronousPlayerQuit.java | 1 - 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/fr/xephi/authme/hooks/BungeeCordMessage.java b/src/main/java/fr/xephi/authme/hooks/BungeeCordMessage.java index 1fda06ba..758d0446 100644 --- a/src/main/java/fr/xephi/authme/hooks/BungeeCordMessage.java +++ b/src/main/java/fr/xephi/authme/hooks/BungeeCordMessage.java @@ -4,7 +4,6 @@ import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteStreams; import fr.xephi.authme.ConsoleLogger; -import fr.xephi.authme.cache.SessionManager; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.datasource.DataSource; diff --git a/src/main/java/fr/xephi/authme/permission/AuthGroupHandler.java b/src/main/java/fr/xephi/authme/permission/AuthGroupHandler.java index a33cb7c5..bbad6f0f 100644 --- a/src/main/java/fr/xephi/authme/permission/AuthGroupHandler.java +++ b/src/main/java/fr/xephi/authme/permission/AuthGroupHandler.java @@ -6,6 +6,8 @@ import fr.xephi.authme.cache.limbo.LimboPlayer; import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.PluginSettings; +import fr.xephi.authme.settings.properties.SecuritySettings; + import org.bukkit.entity.Player; import javax.inject.Inject; @@ -51,18 +53,18 @@ public class AuthGroupHandler { switch (group) { case UNREGISTERED: // Remove the other group type groups, set the current group - permissionsManager.removeGroups(player, Arrays.asList(Settings.getRegisteredGroup, Settings.getUnloggedinGroup)); + permissionsManager.removeGroups(player, Arrays.asList(Settings.getRegisteredGroup, settings.getProperty(SecuritySettings.UNLOGGEDIN_GROUP))); return permissionsManager.addGroup(player, Settings.unRegisteredGroup); case REGISTERED: // Remove the other group type groups, set the current group - permissionsManager.removeGroups(player, Arrays.asList(Settings.unRegisteredGroup, Settings.getUnloggedinGroup)); + permissionsManager.removeGroups(player, Arrays.asList(Settings.unRegisteredGroup, settings.getProperty(SecuritySettings.UNLOGGEDIN_GROUP))); return permissionsManager.addGroup(player, Settings.getRegisteredGroup); case NOT_LOGGED_IN: // Remove the other group type groups, set the current group permissionsManager.removeGroups(player, Arrays.asList(Settings.unRegisteredGroup, Settings.getRegisteredGroup)); - return permissionsManager.addGroup(player, Settings.getUnloggedinGroup); + return permissionsManager.addGroup(player, settings.getProperty(SecuritySettings.UNLOGGEDIN_GROUP)); case LOGGED_IN: // Get the limbo player data @@ -76,7 +78,7 @@ public class AuthGroupHandler { // Remove the other group types groups, set the real group permissionsManager.removeGroups(player, - Arrays.asList(Settings.unRegisteredGroup, Settings.getRegisteredGroup, Settings.getUnloggedinGroup) + Arrays.asList(Settings.unRegisteredGroup, Settings.getRegisteredGroup, settings.getProperty(SecuritySettings.UNLOGGEDIN_GROUP)) ); return permissionsManager.addGroup(player, realGroup); default: diff --git a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java index 7c83ec28..390a3f9b 100644 --- a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java +++ b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java @@ -9,7 +9,6 @@ import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.events.ProtectInventoryEvent; import fr.xephi.authme.hooks.PluginHooks; -import fr.xephi.authme.listener.protocollib.ProtocolLibService; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.permission.AuthGroupType; import fr.xephi.authme.permission.PlayerStatePermission; diff --git a/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java b/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java index 13a6a7b3..88153345 100644 --- a/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java +++ b/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java @@ -3,7 +3,6 @@ package fr.xephi.authme.process.quit; import fr.xephi.authme.cache.backup.JsonCache; import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.cache.limbo.LimboPlayer; -import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.SynchronousProcess; import fr.xephi.authme.settings.properties.RestrictionSettings; From 5726b0d326c3b7a6e225b1af45cf6f2c37cc6f40 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Wed, 29 Jun 2016 22:02:45 +0700 Subject: [PATCH 10/20] Fix player always teleport to spawn on join. --- .../authme/listener/AuthMePlayerListener.java | 3 +- .../process/logout/AsynchronousLogout.java | 30 ++++++-------- .../ProcessSynchronousPlayerLogout.java | 41 +++++++++++-------- .../authme/util/TeleportationService.java | 23 +++++++---- 4 files changed, 53 insertions(+), 44 deletions(-) diff --git a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java index 5320a72a..fc92e1e8 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java @@ -194,6 +194,7 @@ public class AuthMePlayerListener implements Listener { @EventHandler(priority = EventPriority.LOW) public void onPlayerJoin(PlayerJoinEvent event) { final Player player = event.getPlayer(); + teleportationService.teleportOnJoin(player); management.performJoin(player); } @@ -232,7 +233,7 @@ public class AuthMePlayerListener implements Listener { } antiBot.handlePlayerJoin(player); - teleportationService.teleportOnJoin(player); + teleportationService.teleportOnLoginEvent(player); } @EventHandler(priority = EventPriority.HIGHEST) diff --git a/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java b/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java index 25842af0..f58cd359 100644 --- a/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java +++ b/src/main/java/fr/xephi/authme/process/logout/AsynchronousLogout.java @@ -8,8 +8,7 @@ import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.process.AsynchronousProcess; import fr.xephi.authme.process.ProcessService; import fr.xephi.authme.process.SyncProcessManager; -import fr.xephi.authme.util.BukkitService; -import fr.xephi.authme.util.Utils; +import fr.xephi.authme.settings.properties.RestrictionSettings; import org.bukkit.entity.Player; import javax.inject.Inject; @@ -31,10 +30,8 @@ public class AsynchronousLogout implements AsynchronousProcess { @Inject private SyncProcessManager syncProcessManager; - @Inject - private BukkitService bukkitService; - - AsynchronousLogout() { } + AsynchronousLogout() { + } public void logout(final Player player) { final String name = player.getName().toLowerCase(); @@ -42,23 +39,20 @@ public class AsynchronousLogout implements AsynchronousProcess { service.send(player, MessageKey.NOT_LOGGED_IN); return; } + PlayerAuth auth = playerCache.getAuth(name); database.updateSession(auth); - auth.setQuitLocX(player.getLocation().getX()); - auth.setQuitLocY(player.getLocation().getY()); - auth.setQuitLocZ(player.getLocation().getZ()); - auth.setWorld(player.getWorld().getName()); - database.updateQuitLoc(auth); + if (service.getProperty(RestrictionSettings.SAVE_QUIT_LOCATION)) { + auth.setQuitLocX(player.getLocation().getX()); + auth.setQuitLocY(player.getLocation().getY()); + auth.setQuitLocZ(player.getLocation().getZ()); + auth.setWorld(player.getWorld().getName()); + database.updateQuitLoc(auth); + } + limboCache.updateLimboPlayer(player); playerCache.removePlayer(name); database.setUnlogged(name); - bukkitService.scheduleSyncDelayedTask(new Runnable() { - @Override - public void run() { - Utils.teleportToSpawn(player); - } - }); - limboCache.updateLimboPlayer(player); syncProcessManager.processSyncPlayerLogout(player); } } diff --git a/src/main/java/fr/xephi/authme/process/logout/ProcessSynchronousPlayerLogout.java b/src/main/java/fr/xephi/authme/process/logout/ProcessSynchronousPlayerLogout.java index c8336262..4c168a6b 100644 --- a/src/main/java/fr/xephi/authme/process/logout/ProcessSynchronousPlayerLogout.java +++ b/src/main/java/fr/xephi/authme/process/logout/ProcessSynchronousPlayerLogout.java @@ -69,22 +69,7 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess { limboPlayerTaskManager.registerTimeoutTask(player); limboPlayerTaskManager.registerMessageTask(name, true); - if (player.isInsideVehicle() && player.getVehicle() != null) { - player.getVehicle().eject(); - } - final int timeout = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND; - if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) { - player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, timeout, 2)); - } - - service.setGroup(player, AuthGroupType.NOT_LOGGED_IN); - player.setOp(false); - // Remove speed - if (!service.getProperty(RestrictionSettings.ALLOW_UNAUTHED_MOVEMENT) - && service.getProperty(RestrictionSettings.REMOVE_SPEED)) { - player.setFlySpeed(0.0f); - player.setWalkSpeed(0.0f); - } + applyLogoutEffect(player); // Player is now logout... Time to fire event ! bukkitService.callEvent(new LogoutEvent(player)); @@ -95,4 +80,28 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess { ConsoleLogger.info(player.getName() + " logged out"); } + private void applyLogoutEffect(Player player) { + // dismount player + if (player.isInsideVehicle() && player.getVehicle() != null) { + player.getVehicle().eject(); + } + + // Apply Blindness effect + final int timeout = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND; + if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) { + player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, timeout, 2)); + } + + // Set player's data to unauthenticated + service.setGroup(player, AuthGroupType.NOT_LOGGED_IN); + player.setOp(false); + player.setAllowFlight(false); + // Remove speed + if (!service.getProperty(RestrictionSettings.ALLOW_UNAUTHED_MOVEMENT) + && service.getProperty(RestrictionSettings.REMOVE_SPEED)) { + player.setFlySpeed(0.0f); + player.setWalkSpeed(0.0f); + } + } + } diff --git a/src/main/java/fr/xephi/authme/util/TeleportationService.java b/src/main/java/fr/xephi/authme/util/TeleportationService.java index 06c7558d..53e77421 100644 --- a/src/main/java/fr/xephi/authme/util/TeleportationService.java +++ b/src/main/java/fr/xephi/authme/util/TeleportationService.java @@ -41,8 +41,12 @@ public class TeleportationService implements Reloadable { private Set spawnOnLoginWorlds; - TeleportationService() { } + TeleportationService() { + } + private static boolean isEventValid(AbstractTeleportEvent event) { + return !event.isCancelled() && event.getTo() != null && event.getTo().getWorld() != null; + } @PostConstruct @Override @@ -51,11 +55,9 @@ public class TeleportationService implements Reloadable { spawnOnLoginWorlds = new HashSet<>(settings.getProperty(RestrictionSettings.FORCE_SPAWN_ON_WORLDS)); } - public void teleportOnJoin(final Player player) { + public void teleportOnLoginEvent(final Player player) { if (settings.getProperty(RestrictionSettings.NO_TELEPORT)) { return; - } else if (teleportToFirstSpawn(player)) { - return; } if (settings.getProperty(TELEPORT_UNAUTHED_TO_SPAWN) || mustForceSpawnAfterLogin(player.getWorld().getName())) { @@ -63,6 +65,13 @@ public class TeleportationService implements Reloadable { } } + public void teleportOnJoin(final Player player) { + if (settings.getProperty(RestrictionSettings.NO_TELEPORT)) { + return; + } + teleportToFirstSpawn(player); + } + public void teleportOnLogin(final Player player, PlayerAuth auth, LimboPlayer limbo) { if (settings.getProperty(RestrictionSettings.NO_TELEPORT)) { return; @@ -122,7 +131,7 @@ public class TeleportationService implements Reloadable { * by external listeners). Note that not teleportation is performed if the event's location is empty. * * @param player the player to teleport - * @param event the event to emit and according to which to teleport + * @param event the event to emit and according to which to teleport */ private void performTeleportation(final Player player, final AbstractTeleportEvent event) { bukkitService.scheduleSyncDelayedTask(new Runnable() { @@ -135,8 +144,4 @@ public class TeleportationService implements Reloadable { } }); } - - private static boolean isEventValid(AbstractTeleportEvent event) { - return !event.isCancelled() && event.getTo() != null && event.getTo().getWorld() != null; - } } From abf6645620e92539c8c70533d1ea01a3002b5c94 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Wed, 29 Jun 2016 22:31:10 +0700 Subject: [PATCH 11/20] Fix test --- .../authme/util/TeleportationServiceTest.java | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/test/java/fr/xephi/authme/util/TeleportationServiceTest.java b/src/test/java/fr/xephi/authme/util/TeleportationServiceTest.java index ad9d9620..de8d286f 100644 --- a/src/test/java/fr/xephi/authme/util/TeleportationServiceTest.java +++ b/src/test/java/fr/xephi/authme/util/TeleportationServiceTest.java @@ -38,6 +38,7 @@ import static org.mockito.Mockito.verifyZeroInteractions; /** * Test for {@link TeleportationService}. */ +// TODO: Correct me! @RunWith(MockitoJUnitRunner.class) public class TeleportationServiceTest { @@ -56,6 +57,20 @@ public class TeleportationServiceTest { @Mock private PlayerCache playerCache; + // We check that the World in Location is set, this method creates a mock World in Location for us + private static Location mockLocation() { + Location location = mock(Location.class); + given(location.getWorld()).willReturn(mock(World.class)); + return location; + } + + private static PlayerAuth createAuthWithLocation() { + return PlayerAuth.builder() + .name("bobby") + .locX(123.45).locY(23.4).locZ(-4.567) + .build(); + } + @Before public void setUpForcedWorlds() { given(settings.getProperty(RestrictionSettings.FORCE_SPAWN_ON_WORLDS)) @@ -113,7 +128,7 @@ public class TeleportationServiceTest { given(spawnLoader.getSpawnLocation(player)).willReturn(spawn); // when - teleportationService.teleportOnJoin(player); + teleportationService.teleportOnLoginEvent(player); runSyncDelayedTask(bukkitService); // then @@ -135,6 +150,7 @@ public class TeleportationServiceTest { given(spawnLoader.getFirstSpawn()).willReturn(null); // when + teleportationService.teleportOnLoginEvent(player); teleportationService.teleportOnJoin(player); // then @@ -161,6 +177,7 @@ public class TeleportationServiceTest { given(spawnLoader.getSpawnLocation(player)).willReturn(spawn); // when + teleportationService.teleportOnLoginEvent(player); teleportationService.teleportOnJoin(player); runSyncDelayedTask(bukkitService); @@ -189,6 +206,7 @@ public class TeleportationServiceTest { }).when(bukkitService).callEvent(any(SpawnTeleportEvent.class)); // when + teleportationService.teleportOnLoginEvent(player); teleportationService.teleportOnJoin(player); runSyncDelayedTask(bukkitService); @@ -216,6 +234,7 @@ public class TeleportationServiceTest { }).when(bukkitService).callEvent(any(SpawnTeleportEvent.class)); // when + teleportationService.teleportOnLoginEvent(player); teleportationService.teleportOnJoin(player); runSyncDelayedTask(bukkitService); @@ -224,7 +243,6 @@ public class TeleportationServiceTest { verify(player, never()).teleport(any(Location.class)); } - // --------- // LOGIN // --------- @@ -397,21 +415,6 @@ public class TeleportationServiceTest { verify(player).teleport(location); } - - // We check that the World in Location is set, this method creates a mock World in Location for us - private static Location mockLocation() { - Location location = mock(Location.class); - given(location.getWorld()).willReturn(mock(World.class)); - return location; - } - - private static PlayerAuth createAuthWithLocation() { - return PlayerAuth.builder() - .name("bobby") - .locX(123.45).locY(23.4).locZ(-4.567) - .build(); - } - private void assertCorrectLocation(Location location, PlayerAuth auth, World world) { assertThat(location.getX(), equalTo(auth.getQuitLocX())); assertThat(location.getY(), equalTo(auth.getQuitLocY())); From 6585b68749b0cb843b57f81517e08aae3ebdd65b Mon Sep 17 00:00:00 2001 From: ljacqu Date: Thu, 30 Jun 2016 22:38:36 +0200 Subject: [PATCH 12/20] Fix TeleportationService tests + rename methods - Fix and supplement unit tests for TeleportationService - Rename methods as to avoid confusion (login vs. LoginEvent when player joins) - Add javadoc with note about Player#hasPlayedBefore always being false --- .../authme/listener/AuthMePlayerListener.java | 4 +- .../authme/util/TeleportationService.java | 57 ++++++++------ .../authme/util/TeleportationServiceTest.java | 75 ++++++++++++------- 3 files changed, 86 insertions(+), 50 deletions(-) diff --git a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java index fc92e1e8..0e5915e1 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java @@ -194,7 +194,7 @@ public class AuthMePlayerListener implements Listener { @EventHandler(priority = EventPriority.LOW) public void onPlayerJoin(PlayerJoinEvent event) { final Player player = event.getPlayer(); - teleportationService.teleportOnJoin(player); + teleportationService.teleportNewPlayerToFirstSpawn(player); management.performJoin(player); } @@ -233,7 +233,7 @@ public class AuthMePlayerListener implements Listener { } antiBot.handlePlayerJoin(player); - teleportationService.teleportOnLoginEvent(player); + teleportationService.teleportOnJoin(player); } @EventHandler(priority = EventPriority.HIGHEST) diff --git a/src/main/java/fr/xephi/authme/util/TeleportationService.java b/src/main/java/fr/xephi/authme/util/TeleportationService.java index 53e77421..a7cffb35 100644 --- a/src/main/java/fr/xephi/authme/util/TeleportationService.java +++ b/src/main/java/fr/xephi/authme/util/TeleportationService.java @@ -44,10 +44,6 @@ public class TeleportationService implements Reloadable { TeleportationService() { } - private static boolean isEventValid(AbstractTeleportEvent event) { - return !event.isCancelled() && event.getTo() != null && event.getTo().getWorld() != null; - } - @PostConstruct @Override public void reload() { @@ -55,7 +51,18 @@ public class TeleportationService implements Reloadable { spawnOnLoginWorlds = new HashSet<>(settings.getProperty(RestrictionSettings.FORCE_SPAWN_ON_WORLDS)); } - public void teleportOnLoginEvent(final Player player) { + /** + * Teleports the player according to the settings when he joins. + *

+ * Note: this is triggered by Bukkit's PlayerLoginEvent, during which you cannot use + * {@link Player#hasPlayedBefore()}: it always returns {@code false}. We trigger teleportation + * from the PlayerLoginEvent and not the PlayerJoinEvent to ensure that the location is overridden + * as fast as possible (cf. AuthMe #682). + * + * @param player the player to process + * @see BUKKIT-3521: Player.hasPlayedBefore() always false + */ + public void teleportOnJoin(final Player player) { if (settings.getProperty(RestrictionSettings.NO_TELEPORT)) { return; } @@ -65,13 +72,30 @@ public class TeleportationService implements Reloadable { } } - public void teleportOnJoin(final Player player) { - if (settings.getProperty(RestrictionSettings.NO_TELEPORT)) { + /** + * Teleports the player to the first spawn if he is new and the first spawn is configured. + * + * @param player the player to process + */ + public void teleportNewPlayerToFirstSpawn(final Player player) { + if (settings.getProperty(RestrictionSettings.NO_TELEPORT) || player.hasPlayedBefore()) { return; } - teleportToFirstSpawn(player); + Location firstSpawn = spawnLoader.getFirstSpawn(); + if (firstSpawn == null) { + return; + } + + performTeleportation(player, new FirstSpawnTeleportEvent(player, firstSpawn)); } + /** + * Teleports the player according to the settings after having successfully logged in. + * + * @param player the player + * @param auth corresponding PlayerAuth object + * @param limbo corresponding LimboPlayer object + */ public void teleportOnLogin(final Player player, PlayerAuth auth, LimboPlayer limbo) { if (settings.getProperty(RestrictionSettings.NO_TELEPORT)) { return; @@ -104,19 +128,6 @@ public class TeleportationService implements Reloadable { return new Location(world, auth.getQuitLocX(), auth.getQuitLocY(), auth.getQuitLocZ()); } - private boolean teleportToFirstSpawn(final Player player) { - if (player.hasPlayedBefore()) { - return false; - } - Location firstSpawn = spawnLoader.getFirstSpawn(); - if (firstSpawn == null) { - return false; - } - - performTeleportation(player, new FirstSpawnTeleportEvent(player, firstSpawn)); - return true; - } - private void teleportBackFromSpawn(final Player player, final Location location) { performTeleportation(player, new AuthMeTeleportEvent(player, location)); } @@ -144,4 +155,8 @@ public class TeleportationService implements Reloadable { } }); } + + private static boolean isEventValid(AbstractTeleportEvent event) { + return !event.isCancelled() && event.getTo() != null && event.getTo().getWorld() != null; + } } diff --git a/src/test/java/fr/xephi/authme/util/TeleportationServiceTest.java b/src/test/java/fr/xephi/authme/util/TeleportationServiceTest.java index de8d286f..84e1a1de 100644 --- a/src/test/java/fr/xephi/authme/util/TeleportationServiceTest.java +++ b/src/test/java/fr/xephi/authme/util/TeleportationServiceTest.java @@ -38,7 +38,6 @@ import static org.mockito.Mockito.verifyZeroInteractions; /** * Test for {@link TeleportationService}. */ -// TODO: Correct me! @RunWith(MockitoJUnitRunner.class) public class TeleportationServiceTest { @@ -57,20 +56,6 @@ public class TeleportationServiceTest { @Mock private PlayerCache playerCache; - // We check that the World in Location is set, this method creates a mock World in Location for us - private static Location mockLocation() { - Location location = mock(Location.class); - given(location.getWorld()).willReturn(mock(World.class)); - return location; - } - - private static PlayerAuth createAuthWithLocation() { - return PlayerAuth.builder() - .name("bobby") - .locX(123.45).locY(23.4).locZ(-4.567) - .build(); - } - @Before public void setUpForcedWorlds() { given(settings.getProperty(RestrictionSettings.FORCE_SPAWN_ON_WORLDS)) @@ -107,7 +92,7 @@ public class TeleportationServiceTest { given(spawnLoader.getFirstSpawn()).willReturn(firstSpawn); // when - teleportationService.teleportOnJoin(player); + teleportationService.teleportNewPlayerToFirstSpawn(player); runSyncDelayedTask(bukkitService); // then @@ -122,13 +107,12 @@ public class TeleportationServiceTest { // given given(settings.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN)).willReturn(true); Player player = mock(Player.class); - given(player.hasPlayedBefore()).willReturn(true); given(player.isOnline()).willReturn(true); Location spawn = mockLocation(); given(spawnLoader.getSpawnLocation(player)).willReturn(spawn); // when - teleportationService.teleportOnLoginEvent(player); + teleportationService.teleportOnJoin(player); runSyncDelayedTask(bukkitService); // then @@ -150,8 +134,7 @@ public class TeleportationServiceTest { given(spawnLoader.getFirstSpawn()).willReturn(null); // when - teleportationService.teleportOnLoginEvent(player); - teleportationService.teleportOnJoin(player); + teleportationService.teleportNewPlayerToFirstSpawn(player); // then verify(player, never()).teleport(any(Location.class)); @@ -161,10 +144,39 @@ public class TeleportationServiceTest { } @Test - public void shouldTeleportPlayerDueToForcedWorld() { + public void shouldNotTeleportPlayerToFirstSpawnIfNoTeleportEnabled() { + // given + Player player = mock(Player.class); + given(player.hasPlayedBefore()).willReturn(false); + given(settings.getProperty(RestrictionSettings.NO_TELEPORT)).willReturn(true); + + // when + teleportationService.teleportNewPlayerToFirstSpawn(player); + + // then + verify(player, never()).teleport(any(Location.class)); + verifyZeroInteractions(bukkitService); + } + + @Test + public void shouldNotTeleportNotNewPlayerToFirstSpawn() { // given Player player = mock(Player.class); given(player.hasPlayedBefore()).willReturn(true); + given(settings.getProperty(RestrictionSettings.NO_TELEPORT)).willReturn(false); + + // when + teleportationService.teleportNewPlayerToFirstSpawn(player); + + // then + verify(player, never()).teleport(any(Location.class)); + verifyZeroInteractions(bukkitService); + } + + @Test + public void shouldTeleportPlayerDueToForcedWorld() { + // given + Player player = mock(Player.class); given(player.isOnline()).willReturn(true); World playerWorld = mock(World.class); @@ -177,7 +189,6 @@ public class TeleportationServiceTest { given(spawnLoader.getSpawnLocation(player)).willReturn(spawn); // when - teleportationService.teleportOnLoginEvent(player); teleportationService.teleportOnJoin(player); runSyncDelayedTask(bukkitService); @@ -191,7 +202,6 @@ public class TeleportationServiceTest { public void shouldNotTeleportPlayerForRemovedLocationInEvent() { // given final Player player = mock(Player.class); - given(player.hasPlayedBefore()).willReturn(true); Location spawn = mockLocation(); given(spawnLoader.getSpawnLocation(player)).willReturn(spawn); given(settings.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN)).willReturn(true); @@ -206,7 +216,6 @@ public class TeleportationServiceTest { }).when(bukkitService).callEvent(any(SpawnTeleportEvent.class)); // when - teleportationService.teleportOnLoginEvent(player); teleportationService.teleportOnJoin(player); runSyncDelayedTask(bukkitService); @@ -219,7 +228,6 @@ public class TeleportationServiceTest { public void shouldNotTeleportPlayerForCanceledEvent() { // given final Player player = mock(Player.class); - given(player.hasPlayedBefore()).willReturn(true); Location spawn = mockLocation(); given(spawnLoader.getSpawnLocation(player)).willReturn(spawn); given(settings.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN)).willReturn(true); @@ -234,7 +242,6 @@ public class TeleportationServiceTest { }).when(bukkitService).callEvent(any(SpawnTeleportEvent.class)); // when - teleportationService.teleportOnLoginEvent(player); teleportationService.teleportOnJoin(player); runSyncDelayedTask(bukkitService); @@ -415,11 +422,25 @@ public class TeleportationServiceTest { verify(player).teleport(location); } - private void assertCorrectLocation(Location location, PlayerAuth auth, World world) { + private static void assertCorrectLocation(Location location, PlayerAuth auth, World world) { assertThat(location.getX(), equalTo(auth.getQuitLocX())); assertThat(location.getY(), equalTo(auth.getQuitLocY())); assertThat(location.getZ(), equalTo(auth.getQuitLocZ())); assertThat(location.getWorld(), equalTo(world)); } + // We check that the World in Location is set, this method creates a mock World in Location for us + private static Location mockLocation() { + Location location = mock(Location.class); + given(location.getWorld()).willReturn(mock(World.class)); + return location; + } + + private static PlayerAuth createAuthWithLocation() { + return PlayerAuth.builder() + .name("bobby") + .locX(123.45).locY(23.4).locZ(-4.567) + .build(); + } + } From deffcb3e2b75b88ea568d5ae4d081d3d1fdbf2b8 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Sun, 3 Jul 2016 21:52:46 +0700 Subject: [PATCH 13/20] - Renamed JsonCache to PlayerDataStorage * the methods inside it renamed to fit with class name * cache folder changed into playerdata - Renamed LimboPlayer to PlayerData - Added fly speed to PlayerData - Removed player's name from PlayerData object - Added getPlayerLocationOrSpawn method in spawn loader. --- .idea/codeStyleSettings.xml | 275 +++++++++++++++++- src/main/java/fr/xephi/authme/AuthMe.java | 21 +- .../xephi/authme/cache/auth/PlayerCache.java | 7 +- ...{JsonCache.java => PlayerDataStorage.java} | 98 ++++--- .../xephi/authme/cache/limbo/LimboCache.java | 58 ++-- .../{LimboPlayer.java => PlayerData.java} | 23 +- .../authme/UnregisterAdminCommand.java | 10 +- .../authme/permission/AuthGroupHandler.java | 5 +- .../authme/process/join/AsynchronousJoin.java | 10 +- .../process/login/AsynchronousLogin.java | 14 +- .../process/login/ProcessSyncPlayerLogin.java | 6 +- .../process/logout/AsynchronousLogout.java | 2 +- .../ProcessSynchronousPlayerLogout.java | 8 +- .../authme/process/quit/AsynchronousQuit.java | 2 +- .../quit/ProcessSyncronousPlayerQuit.java | 18 +- .../register/ProcessSyncEmailRegister.java | 8 +- .../register/ProcessSyncPasswordRegister.java | 16 +- .../unregister/AsynchronousUnregister.java | 10 +- .../fr/xephi/authme/settings/Settings.java | 4 - .../fr/xephi/authme/settings/SpawnLoader.java | 36 ++- .../fr/xephi/authme/task/MessageTask.java | 4 +- ...anager.java => PlayerDataTaskManager.java} | 29 +- .../authme/util/TeleportationService.java | 8 +- src/main/java/fr/xephi/authme/util/Utils.java | 16 + ...st.java => PlayerDataTaskManagerTest.java} | 70 ++--- .../authme/util/TeleportationServiceTest.java | 16 +- 26 files changed, 554 insertions(+), 220 deletions(-) rename src/main/java/fr/xephi/authme/cache/backup/{JsonCache.java => PlayerDataStorage.java} (61%) rename src/main/java/fr/xephi/authme/cache/limbo/{LimboPlayer.java => PlayerData.java} (89%) rename src/main/java/fr/xephi/authme/task/{LimboPlayerTaskManager.java => PlayerDataTaskManager.java} (79%) rename src/test/java/fr/xephi/authme/task/{LimboPlayerTaskManagerTest.java => PlayerDataTaskManagerTest.java} (72%) diff --git a/.idea/codeStyleSettings.xml b/.idea/codeStyleSettings.xml index 26493260..91baa3db 100644 --- a/.idea/codeStyleSettings.xml +++ b/.idea/codeStyleSettings.xml @@ -12,9 +12,280 @@ + + + +

+ + + + true + true + true + true + + + +
+
+ + + + true + true + true + true + + + +
+
+ + + + true + true + true + true + + + +
+
+ + + + true + true + true + true + + + +
+
+ + + + true + true + true + + + +
+
+ + + + true + true + true + + + +
+
+ + + + true + true + true + + + +
+
+ + + + true + true + true + + + +
+
+ + + + true + true + + + +
+
+ + + + true + true + true + + + +
+
+ + + + true + true + true + + + +
+
+ + + + true + true + true + + + +
+
+ + + + true + true + true + + + +
+
+ + + + true + true + + + +
+
+ + + + true + true + + + +
+
+ + + + true + true + + + +
+
+ + + + true + true + + + +
+
+ + + true + + +
+
+ + + true + + +
+
+ + + true + + +
+
+ + + + true + true + + + +
+
+ + + true + + +
+
+ + + + true + true + true + + + +
+
+ + + true + + +
+
+ + + true + + +
+
+ + + + true + true + + + +
+
+ + + true + + +
+ + +