From e9032d1739592c5fc598738817edd18eabfcab30 Mon Sep 17 00:00:00 2001 From: Xephi Date: Wed, 20 Aug 2014 06:05:35 +0200 Subject: [PATCH] Begin rework of Cache --- src/main/java/fr/xephi/authme/AuthMe.java | 4 +-- .../xephi/authme/cache/backup/FileCache.java | 32 ++++++++++++++----- .../xephi/authme/cache/limbo/LimboCache.java | 8 ++--- .../xephi/authme/commands/LogoutCommand.java | 2 +- .../authme/commands/UnregisterCommand.java | 4 +-- .../authme/events/StoreInventoryEvent.java | 4 +-- .../authme/listener/AuthMePlayerListener.java | 10 +++--- .../login/ProcessSyncronousPlayerLogin.java | 4 +-- .../fr/xephi/authme/task/TimeoutTask.java | 4 +-- 9 files changed, 44 insertions(+), 28 deletions(-) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index e07e3d4d4..531718d2d 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -562,8 +562,8 @@ public class AuthMe extends JavaPlugin { this.plugin.getServer().getScheduler() .cancelTask(limbo.getTimeoutTaskId()); LimboCache.getInstance().deleteLimboPlayer(name); - if (this.playerBackup.doesCacheExist(name)) { - this.playerBackup.removeCache(name); + if (this.playerBackup.doesCacheExist(player)) { + this.playerBackup.removeCache(player); } } PlayerCache.getInstance().removePlayer(name); diff --git a/src/main/java/fr/xephi/authme/cache/backup/FileCache.java b/src/main/java/fr/xephi/authme/cache/backup/FileCache.java index 72e225af4..c81a42e97 100644 --- a/src/main/java/fr/xephi/authme/cache/backup/FileCache.java +++ b/src/main/java/fr/xephi/authme/cache/backup/FileCache.java @@ -2,13 +2,13 @@ package fr.xephi.authme.cache.backup; import java.io.File; import java.io.FileWriter; - import java.util.ArrayList; import java.util.List; import java.util.Scanner; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -26,8 +26,9 @@ public class FileCache { } } - public void createCache(String playername, DataFileCache playerData, + public void createCache(Player player, DataFileCache playerData, String group, boolean operator, boolean flying) { + String playername = player.getName(); final File file = new File("cache/" + playername + ".cache"); if (file.exists()) { @@ -132,8 +133,13 @@ public class FileCache { } } - public DataFileCache readCache(String playername) { - final File file = new File("cache/" + playername + ".cache"); + public DataFileCache readCache(Player player) { + String playername = player.getName(); + File file = new File("cache/" + playername + ".cache"); + if (!file.exists()) { + playername = player.getName().toLowerCase(); + file = new File("cache/" + playername + ".cache"); + } ItemStack[] stacki = new ItemStack[36]; ItemStack[] stacka = new ItemStack[4]; @@ -252,16 +258,26 @@ public class FileCache { return new DataFileCache(stacki, stacka, group, op, flying); } - public void removeCache(String playername) { - final File file = new File("cache/" + playername + ".cache"); + public void removeCache(Player player) { + String playername = player.getName(); + File file = new File("cache/" + playername + ".cache"); + if (!file.exists()) { + playername = player.getName().toLowerCase(); + file = new File("cache/" + playername + ".cache"); + } if (file.exists()) { file.delete(); } } - public boolean doesCacheExist(String playername) { - final File file = new File("cache/" + playername + ".cache"); + public boolean doesCacheExist(Player player) { + String playername = player.getName(); + File file = new File("cache/" + playername + ".cache"); + if (!file.exists()) { + playername = player.getName().toLowerCase(); + file = new File("cache/" + playername + ".cache"); + } if (file.exists()) { return true; 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 e099c31a7..c9f57819f 100644 --- a/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java +++ b/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java @@ -37,7 +37,7 @@ public class LimboCache { String playerGroup = ""; boolean flying; - if (playerData.doesCacheExist(name)) { + if (playerData.doesCacheExist(player)) { StoreInventoryEvent event = new StoreInventoryEvent(player, playerData); Bukkit.getServer().getPluginManager().callEvent(event); @@ -49,9 +49,9 @@ public class LimboCache { inv = null; arm = null; } - playerGroup = playerData.readCache(name).getGroup(); - operator = playerData.readCache(name).getOperator(); - flying = playerData.readCache(name).isFlying(); + playerGroup = playerData.readCache(player).getGroup(); + operator = playerData.readCache(player).getOperator(); + flying = playerData.readCache(player).isFlying(); } else { StoreInventoryEvent event = new StoreInventoryEvent(player); Bukkit.getServer().getPluginManager().callEvent(event); diff --git a/src/main/java/fr/xephi/authme/commands/LogoutCommand.java b/src/main/java/fr/xephi/authme/commands/LogoutCommand.java index b0435701d..c32a3e6cc 100644 --- a/src/main/java/fr/xephi/authme/commands/LogoutCommand.java +++ b/src/main/java/fr/xephi/authme/commands/LogoutCommand.java @@ -92,7 +92,7 @@ public class LogoutCommand implements CommandExecutor { DataFileCache playerData = new DataFileCache(LimboCache .getInstance().getLimboPlayer(name).getInventory(), LimboCache.getInstance().getLimboPlayer(name).getArmour()); - playerBackup.createCache(name, playerData, LimboCache.getInstance() + playerBackup.createCache(player, playerData, LimboCache.getInstance() .getLimboPlayer(name).getGroup(), LimboCache.getInstance() .getLimboPlayer(name).getOperator(), LimboCache .getInstance().getLimboPlayer(name).isFlying()); diff --git a/src/main/java/fr/xephi/authme/commands/UnregisterCommand.java b/src/main/java/fr/xephi/authme/commands/UnregisterCommand.java index a2d561f27..96532fe30 100644 --- a/src/main/java/fr/xephi/authme/commands/UnregisterCommand.java +++ b/src/main/java/fr/xephi/authme/commands/UnregisterCommand.java @@ -128,8 +128,8 @@ public class UnregisterCommand implements CommandExecutor { player.getName().toLowerCase()); // check if Player cache File Exist and delete it, preventing // duplication of items - if (playerCache.doesCacheExist(name)) { - playerCache.removeCache(name); + if (playerCache.doesCacheExist(player)) { + playerCache.removeCache(player); } if (Settings.applyBlindEffect) player .addPotionEffect(new PotionEffect( diff --git a/src/main/java/fr/xephi/authme/events/StoreInventoryEvent.java b/src/main/java/fr/xephi/authme/events/StoreInventoryEvent.java index 13cd09427..95024f1b8 100644 --- a/src/main/java/fr/xephi/authme/events/StoreInventoryEvent.java +++ b/src/main/java/fr/xephi/authme/events/StoreInventoryEvent.java @@ -23,9 +23,9 @@ public class StoreInventoryEvent extends CustomEvent { public StoreInventoryEvent(Player player, FileCache fileCache) { this.player = player; - this.inventory = fileCache.readCache(player.getName().toLowerCase()) + this.inventory = fileCache.readCache(player) .getInventory(); - this.armor = fileCache.readCache(player.getName().toLowerCase()) + this.armor = fileCache.readCache(player) .getArmour(); } diff --git a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java index ea9179938..bd0cf755d 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java @@ -666,7 +666,7 @@ public class AuthMePlayerListener implements Listener { DataFileCache dataFile = new DataFileCache(LimboCache.getInstance() .getLimboPlayer(name).getInventory(), LimboCache .getInstance().getLimboPlayer(name).getArmour()); - playerBackup.createCache(name, dataFile, LimboCache.getInstance() + playerBackup.createCache(player, dataFile, LimboCache.getInstance() .getLimboPlayer(name).getGroup(), LimboCache.getInstance() .getLimboPlayer(name).getOperator(), LimboCache .getInstance().getLimboPlayer(name).isFlying()); @@ -848,8 +848,8 @@ public class AuthMePlayerListener implements Listener { this.plugin.getServer().getScheduler() .cancelTask(limbo.getMessageTaskId()); LimboCache.getInstance().deleteLimboPlayer(name); - if (playerBackup.doesCacheExist(name)) { - playerBackup.removeCache(name); + if (playerBackup.doesCacheExist(player)) { + playerBackup.removeCache(player); } } PlayerCache.getInstance().removePlayer(name); @@ -950,8 +950,8 @@ public class AuthMePlayerListener implements Listener { this.plugin.getServer().getScheduler() .cancelTask(limbo.getMessageTaskId()); LimboCache.getInstance().deleteLimboPlayer(name); - if (this.playerBackup.doesCacheExist(name)) { - this.playerBackup.removeCache(name); + if (this.playerBackup.doesCacheExist(player)) { + this.playerBackup.removeCache(player); } } PlayerCache.getInstance().removePlayer(name); diff --git a/src/main/java/fr/xephi/authme/process/login/ProcessSyncronousPlayerLogin.java b/src/main/java/fr/xephi/authme/process/login/ProcessSyncronousPlayerLogin.java index 21b1a31c9..392d96a29 100644 --- a/src/main/java/fr/xephi/authme/process/login/ProcessSyncronousPlayerLogin.java +++ b/src/main/java/fr/xephi/authme/process/login/ProcessSyncronousPlayerLogin.java @@ -179,8 +179,8 @@ public class ProcessSyncronousPlayerLogin implements Runnable { // Cleanup no longer used temporary data LimboCache.getInstance().deleteLimboPlayer(name); - if (playerCache.doesCacheExist(name)) { - playerCache.removeCache(name); + if (playerCache.doesCacheExist(player)) { + playerCache.removeCache(player); } } diff --git a/src/main/java/fr/xephi/authme/task/TimeoutTask.java b/src/main/java/fr/xephi/authme/task/TimeoutTask.java index 159976af7..700e52e82 100644 --- a/src/main/java/fr/xephi/authme/task/TimeoutTask.java +++ b/src/main/java/fr/xephi/authme/task/TimeoutTask.java @@ -41,8 +41,8 @@ public class TimeoutTask implements Runnable { .cancelTask(inv.getMessageTaskId()); player.getServer().getScheduler() .cancelTask(inv.getTimeoutTaskId()); - if (playerCache.doesCacheExist(name)) { - playerCache.removeCache(name); + if (playerCache.doesCacheExist(player)) { + playerCache.removeCache(player); } } GameMode gm = AuthMePlayerListener.gameMode.get(name);