From 145747505f82d004f8f04347575f7774248876a3 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Tue, 28 Jun 2016 21:36:58 +0700 Subject: [PATCH] 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 181e0da34..5ce8ba348 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 d97354915..90cb013d1 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 a41b8aa95..000000000 --- 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 97670a88e..71482c815 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 6029e6dad..f8e84e3bc 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 980f3ccb8..05b393771 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 b1f5573a9..8788be9db 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 c7557fc24..a33cb7c5e 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 221e1e8c7..894c3a485 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 5551b9b8f..139d80bc7 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 18e7e8e4d..8de8c0e85 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 783a4ed36..f1e68cfd1 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 fc81017d4..4924b480c 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 a157287bf..459452d82 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 c1382c2c3..8ed3edbfe 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 524d6b1c4..9e20d49b6 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 d34c41f86..bd9c7fb9c 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(); + } + } }