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 186b1b9a0..07221e108 100644 --- a/src/main/java/fr/xephi/authme/cache/backup/FileCache.java +++ b/src/main/java/fr/xephi/authme/cache/backup/FileCache.java @@ -21,6 +21,7 @@ import com.comphenix.attribute.Attributes.AttributeType; import com.comphenix.attribute.Attributes.Operation; import fr.xephi.authme.AuthMe; +import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.api.API; public class FileCache { @@ -101,19 +102,19 @@ public class FileCache { for (Enchantment ench : item.getEnchantments().keySet()) { writer.write("enchant=" + ench.getName() + ":" + item.getEnchantments().get(ench) + API.newline); } - Attributes attributes = new Attributes(item); - if (attributes != null) - while (attributes.values().iterator().hasNext()) { - Attribute a = attributes.values().iterator().next(); - if (a != null) { - try { + try { + Attributes attributes = new Attributes(item); + if (attributes != null) + while (attributes.values().iterator().hasNext()) { + Attribute a = attributes.values().iterator().next(); + if (a != null) { if (a.getName() != null && a.getAttributeType() != null && a.getOperation() != null && a.getUUID() != null) writer.write("attribute=" + a.getName() + ";" + a.getAttributeType().getMinecraftId() + ";" + a.getAmount() + ";" + a.getOperation().getId() + ";" + a.getUUID().toString()); - } catch (Exception e) { - } catch (Error e) {} - } - } + } + } + } catch (Exception e) { + } catch (Error e) {} } else { writer.write("AIR"); } @@ -155,25 +156,25 @@ public class FileCache { for (Enchantment ench : item.getEnchantments().keySet()) { writer.write("enchant=" + ench.getName() + ":" + item.getEnchantments().get(ench) + API.newline); } - Attributes attributes = new Attributes(item); - if (attributes != null) - while (attributes.values().iterator().hasNext()) { - Attribute a = attributes.values().iterator().next(); - if (a != null) { - try { + try { + Attributes attributes = new Attributes(item); + if (attributes != null) + while (attributes.values().iterator().hasNext()) { + Attribute a = attributes.values().iterator().next(); + if (a != null) { if (a.getName() != null && a.getAttributeType() != null && a.getOperation() != null && a.getUUID() != null) writer.write("attribute=" + a.getName() + ";" + a.getAttributeType().getMinecraftId() + ";" + a.getAmount() + ";" + a.getOperation().getId() + ";" + a.getUUID().toString()); - } catch (Exception e) {} - } - } + } + } + } catch (Exception e) {} } else { writer.write("AIR" + API.newline); } writer.close(); } } catch (final Exception e) { - e.printStackTrace(); + ConsoleLogger.showError("Some error on creating file cache..."); } } @@ -186,312 +187,316 @@ public class FileCache { } catch (Error e) { path = player.getName(); } - File file = new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path + File.separator + ".playerdatas.cache"); - String playername = player.getName().toLowerCase(); - if (!file.exists()) { - // OLD METHOD - file = new File("cache/" + playername + ".cache"); - ItemStack[] stacki = new ItemStack[36]; - ItemStack[] stacka = new ItemStack[4]; + try { + File file = new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path + File.separator + ".playerdatas.cache"); + String playername = player.getName().toLowerCase(); if (!file.exists()) { - return new DataFileCache(stacki, stacka); - } - String group = null; - boolean op = false; - boolean flying = false; + // OLD METHOD + file = new File("cache/" + playername + ".cache"); + ItemStack[] stacki = new ItemStack[36]; + ItemStack[] stacka = new ItemStack[4]; + if (!file.exists()) { + return new DataFileCache(stacki, stacka); + } + String group = null; + boolean op = false; + boolean flying = false; - Scanner reader = null; - try { - reader = new Scanner(file); + Scanner reader = null; + try { + reader = new Scanner(file); - int i = 0; - int a = 0; - while (reader.hasNextLine()) { - String line = reader.nextLine(); + int i = 0; + int a = 0; + while (reader.hasNextLine()) { + String line = reader.nextLine(); - if (!line.contains(":")) { - // the fist line represent the player group, operator - // status - // and flying status - final String[] playerInfo = line.split(";"); - group = playerInfo[0]; + if (!line.contains(":")) { + // the fist line represent the player group, operator + // status + // and flying status + final String[] playerInfo = line.split(";"); + group = playerInfo[0]; - if (Integer.parseInt(playerInfo[1]) == 1) { - op = true; - } else op = false; - if (playerInfo.length > 2) { - if (Integer.parseInt(playerInfo[2]) == 1) - flying = true; - else flying = false; + if (Integer.parseInt(playerInfo[1]) == 1) { + op = true; + } else op = false; + if (playerInfo.length > 2) { + if (Integer.parseInt(playerInfo[2]) == 1) + flying = true; + else flying = false; + } + + continue; } - continue; - } - - if (!line.startsWith("i") && !line.startsWith("w")) { - continue; - } - String lores = ""; - String name = ""; - if (line.split("\\*").length > 1) { - lores = line.split("\\*")[1]; - line = line.split("\\*")[0]; - } - if (line.split(";").length > 1) { - name = line.split(";")[1]; - line = line.split(";")[0]; - } - final String[] in = line.split(":"); - // can enchant item? size ofstring in file - 4 all / 2 = - // number - // of enchant - if (in[0].equals("i")) { - stacki[i] = new ItemStack(Material.getMaterial(in[1]), Integer.parseInt(in[2]), Short.parseShort((in[3]))); - if (in.length > 4 && !in[4].isEmpty()) { - for (int k = 4; k < in.length - 1; k++) { - stacki[i].addUnsafeEnchantment(Enchantment.getByName(in[k]), Integer.parseInt(in[k + 1])); - k++; - } + if (!line.startsWith("i") && !line.startsWith("w")) { + continue; } - try { - ItemMeta meta = plugin.getServer().getItemFactory().getItemMeta(stacki[i].getType()); - if (!name.isEmpty()) { - meta.setDisplayName(name); - } - if (!lores.isEmpty()) { - List loreList = new ArrayList(); - for (String s : lores.split("%newline%")) { - loreList.add(s); + String lores = ""; + String name = ""; + if (line.split("\\*").length > 1) { + lores = line.split("\\*")[1]; + line = line.split("\\*")[0]; + } + if (line.split(";").length > 1) { + name = line.split(";")[1]; + line = line.split(";")[0]; + } + final String[] in = line.split(":"); + // can enchant item? size ofstring in file - 4 all / 2 = + // number + // of enchant + if (in[0].equals("i")) { + stacki[i] = new ItemStack(Material.getMaterial(in[1]), Integer.parseInt(in[2]), Short.parseShort((in[3]))); + if (in.length > 4 && !in[4].isEmpty()) { + for (int k = 4; k < in.length - 1; k++) { + stacki[i].addUnsafeEnchantment(Enchantment.getByName(in[k]), Integer.parseInt(in[k + 1])); + k++; } - meta.setLore(loreList); } - if (meta != null) - stacki[i].setItemMeta(meta); - } catch (Exception e) { - } - i++; - } else { - stacka[a] = new ItemStack(Material.getMaterial(in[1]), Integer.parseInt(in[2]), Short.parseShort((in[3]))); - if (in.length > 4 && !in[4].isEmpty()) { - for (int k = 4; k < in.length - 1; k++) { - stacka[a].addUnsafeEnchantment(Enchantment.getByName(in[k]), Integer.parseInt(in[k + 1])); - k++; - } - } - try { - ItemMeta meta = plugin.getServer().getItemFactory().getItemMeta(stacka[a].getType()); - if (!name.isEmpty()) - meta.setDisplayName(name); - if (!lores.isEmpty()) { - List loreList = new ArrayList(); - for (String s : lores.split("%newline%")) { - loreList.add(s); + try { + ItemMeta meta = plugin.getServer().getItemFactory().getItemMeta(stacki[i].getType()); + if (!name.isEmpty()) { + meta.setDisplayName(name); } - meta.setLore(loreList); + if (!lores.isEmpty()) { + List loreList = new ArrayList(); + for (String s : lores.split("%newline%")) { + loreList.add(s); + } + meta.setLore(loreList); + } + if (meta != null) + stacki[i].setItemMeta(meta); + } catch (Exception e) { } - if (meta != null) - stacki[i].setItemMeta(meta); - } catch (Exception e) { + i++; + } else { + stacka[a] = new ItemStack(Material.getMaterial(in[1]), Integer.parseInt(in[2]), Short.parseShort((in[3]))); + if (in.length > 4 && !in[4].isEmpty()) { + for (int k = 4; k < in.length - 1; k++) { + stacka[a].addUnsafeEnchantment(Enchantment.getByName(in[k]), Integer.parseInt(in[k + 1])); + k++; + } + } + try { + ItemMeta meta = plugin.getServer().getItemFactory().getItemMeta(stacka[a].getType()); + if (!name.isEmpty()) + meta.setDisplayName(name); + if (!lores.isEmpty()) { + List loreList = new ArrayList(); + for (String s : lores.split("%newline%")) { + loreList.add(s); + } + meta.setLore(loreList); + } + if (meta != null) + stacki[i].setItemMeta(meta); + } catch (Exception e) { + } + a++; } - a++; + } + } catch (final Exception e) { + ConsoleLogger.showError("Error on creating a file cache for " + player.getName() + ", maybe wipe inventory..."); + } finally { + if (reader != null) { + reader.close(); } } - } catch (final Exception e) { - e.printStackTrace(); - } finally { - if (reader != null) { - reader.close(); - } - } - return new DataFileCache(stacki, stacka, group, op, flying); - } else { - // NEW METHOD - ItemStack[] inv = new ItemStack[36]; - ItemStack[] armours = new ItemStack[4]; - String group = null; - boolean op = false; - boolean flying = false; + return new DataFileCache(stacki, stacka, group, op, flying); + } else { + // NEW METHOD + ItemStack[] inv = new ItemStack[36]; + ItemStack[] armours = new ItemStack[4]; + String group = null; + boolean op = false; + boolean flying = false; - Scanner reader = null; - try { - reader = new Scanner(file); + Scanner reader = null; + try { + reader = new Scanner(file); - int count = 1; - while (reader.hasNextLine()) { - String line = reader.nextLine(); - switch (count) { - case 1: - group = line; - break; - case 2: - op = Boolean.parseBoolean(line); - break; - case 3: - flying = Boolean.parseBoolean(line); - break; - default: - break; - } - count++; - } - if (reader != null) - reader.close(); - for (int i = 0; i < inv.length; i++) { - reader = new Scanner(new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path + File.separator + "inventory" + File.separator + i + ".cache")); - ItemStack item = new ItemStack(Material.AIR); - ItemMeta meta = null; - Attributes attributes = null; - count = 1; - boolean v = true; - while (reader.hasNextLine() && v == true) { + int count = 1; + while (reader.hasNextLine()) { String line = reader.nextLine(); switch (count) { case 1: - item.setType(Material.getMaterial(line)); - if (item.getType() == Material.AIR) - v = false; - continue; + group = line; + break; case 2: - item.setDurability(Short.parseShort(line)); - continue; + op = Boolean.parseBoolean(line); + break; case 3: - item.setAmount(Integer.parseInt(line)); - continue; - case 4: - meta = Bukkit.getItemFactory().getItemMeta(item.getType()); + flying = Boolean.parseBoolean(line); break; default: break; } - if (line.startsWith("name=")) { - line = line.substring(5); - meta.setDisplayName(line); - item.setItemMeta(meta); - continue; - } - if (line.startsWith("lore=")) { - line = line.substring(5); - List lore = new ArrayList(); - for (String s : line.split("%newline%")) - lore.add(s); - meta.setLore(lore); - item.setItemMeta(meta); - continue; - } - if (line.startsWith("enchant=")) { - line = line.substring(8); - item.addEnchantment(Enchantment.getByName(line.split(":")[0]), Integer.parseInt(line.split(":")[1])); - } - if (line.startsWith("attribute=")) { - if (attributes == null) - attributes = new Attributes(item); - try { - line = line.substring(10); - String[] args = line.split(";"); - if (args.length != 5) - continue; - String name = args[0]; - AttributeType type = AttributeType.fromId(args[1]); - double amount = Double.parseDouble(args[2]); - Operation operation = Operation.fromId(Integer.parseInt(args[3])); - UUID uuid = UUID.fromString(args[4]); - Attribute attribute = new Attribute(new Builder(amount, operation, type, name, uuid)); - attributes.add(attribute); - } catch (Exception e) { - } - } count++; } if (reader != null) reader.close(); - if (attributes != null) - inv[i] = attributes.getStack(); - else - inv[i] = item; - } - for (int i = 0; i < armours.length; i++) { - reader = new Scanner(new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path + File.separator + "armours" + File.separator + i + ".cache")); - ItemStack item = new ItemStack(Material.AIR); - ItemMeta meta = null; - Attributes attributes = null; - count = 1; - boolean v = true; - while (reader.hasNextLine() && v == true) { - String line = reader.nextLine(); - switch (count) { - case 1: - item.setType(Material.getMaterial(line)); - if (item.getType() == Material.AIR) - v = false; - continue; - case 2: - item.setDurability(Short.parseShort(line)); - continue; - case 3: - item.setAmount(Integer.parseInt(line)); - continue; - case 4: - meta = Bukkit.getItemFactory().getItemMeta(item.getType()); - break; - default: - break; - } - if (line.startsWith("name=")) { - line = line.substring(5); - meta.setDisplayName(line); - item.setItemMeta(meta); - continue; - } - if (line.startsWith("lore=")) { - line = line.substring(5); - List lore = new ArrayList(); - for (String s : line.split("%newline%")) - lore.add(s); - meta.setLore(lore); - item.setItemMeta(meta); - continue; - } - if (line.startsWith("enchant=")) { - line = line.substring(8); - item.addEnchantment(Enchantment.getByName(line.split(":")[0]), Integer.parseInt(line.split(":")[1])); - } - if (line.startsWith("attribute=")) { - if (attributes == null) - attributes = new Attributes(item); - try { - line = line.substring(10); - String[] args = line.split(";"); - if (args.length != 5) + for (int i = 0; i < inv.length; i++) { + reader = new Scanner(new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path + File.separator + "inventory" + File.separator + i + ".cache")); + ItemStack item = new ItemStack(Material.AIR); + ItemMeta meta = null; + Attributes attributes = null; + count = 1; + boolean v = true; + while (reader.hasNextLine() && v == true) { + String line = reader.nextLine(); + switch (count) { + case 1: + item.setType(Material.getMaterial(line)); + if (item.getType() == Material.AIR) + v = false; continue; - String name = args[0]; - AttributeType type = AttributeType.fromId(args[1]); - double amount = Double.parseDouble(args[2]); - Operation operation = Operation.fromId(Integer.parseInt(args[3])); - UUID uuid = UUID.fromString(args[4]); - Attribute attribute = new Attribute(new Builder(amount, operation, type, name, uuid)); - attributes.add(attribute); - } catch (Exception e) { + case 2: + item.setDurability(Short.parseShort(line)); + continue; + case 3: + item.setAmount(Integer.parseInt(line)); + continue; + case 4: + meta = Bukkit.getItemFactory().getItemMeta(item.getType()); + break; + default: + break; } + if (line.startsWith("name=")) { + line = line.substring(5); + meta.setDisplayName(line); + item.setItemMeta(meta); + continue; + } + if (line.startsWith("lore=")) { + line = line.substring(5); + List lore = new ArrayList(); + for (String s : line.split("%newline%")) + lore.add(s); + meta.setLore(lore); + item.setItemMeta(meta); + continue; + } + if (line.startsWith("enchant=")) { + line = line.substring(8); + item.addEnchantment(Enchantment.getByName(line.split(":")[0]), Integer.parseInt(line.split(":")[1])); + } + if (line.startsWith("attribute=")) { + if (attributes == null) + attributes = new Attributes(item); + try { + line = line.substring(10); + String[] args = line.split(";"); + if (args.length != 5) + continue; + String name = args[0]; + AttributeType type = AttributeType.fromId(args[1]); + double amount = Double.parseDouble(args[2]); + Operation operation = Operation.fromId(Integer.parseInt(args[3])); + UUID uuid = UUID.fromString(args[4]); + Attribute attribute = new Attribute(new Builder(amount, operation, type, name, uuid)); + attributes.add(attribute); + } catch (Exception e) { + } + } + count++; } - count++; + if (reader != null) + reader.close(); + if (attributes != null) + inv[i] = attributes.getStack(); + else + inv[i] = item; } + for (int i = 0; i < armours.length; i++) { + reader = new Scanner(new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path + File.separator + "armours" + File.separator + i + ".cache")); + ItemStack item = new ItemStack(Material.AIR); + ItemMeta meta = null; + Attributes attributes = null; + count = 1; + boolean v = true; + while (reader.hasNextLine() && v == true) { + String line = reader.nextLine(); + switch (count) { + case 1: + item.setType(Material.getMaterial(line)); + if (item.getType() == Material.AIR) + v = false; + continue; + case 2: + item.setDurability(Short.parseShort(line)); + continue; + case 3: + item.setAmount(Integer.parseInt(line)); + continue; + case 4: + meta = Bukkit.getItemFactory().getItemMeta(item.getType()); + break; + default: + break; + } + if (line.startsWith("name=")) { + line = line.substring(5); + meta.setDisplayName(line); + item.setItemMeta(meta); + continue; + } + if (line.startsWith("lore=")) { + line = line.substring(5); + List lore = new ArrayList(); + for (String s : line.split("%newline%")) + lore.add(s); + meta.setLore(lore); + item.setItemMeta(meta); + continue; + } + if (line.startsWith("enchant=")) { + line = line.substring(8); + item.addEnchantment(Enchantment.getByName(line.split(":")[0]), Integer.parseInt(line.split(":")[1])); + } + if (line.startsWith("attribute=")) { + if (attributes == null) + attributes = new Attributes(item); + try { + line = line.substring(10); + String[] args = line.split(";"); + if (args.length != 5) + continue; + String name = args[0]; + AttributeType type = AttributeType.fromId(args[1]); + double amount = Double.parseDouble(args[2]); + Operation operation = Operation.fromId(Integer.parseInt(args[3])); + UUID uuid = UUID.fromString(args[4]); + Attribute attribute = new Attribute(new Builder(amount, operation, type, name, uuid)); + attributes.add(attribute); + } catch (Exception e) { + } + } + count++; + } + if (reader != null) + reader.close(); + if (attributes != null) + armours[i] = attributes.getStack(); + else + armours[i] = item; + } + + } catch (final Exception e) { + ConsoleLogger.showError("Error while reading file for " + player.getName() + ", some wipe inventory incoming..."); + } finally { if (reader != null) reader.close(); - if (attributes != null) - armours[i] = attributes.getStack(); - else - armours[i] = item; } - - } catch (final Exception e) { - e.printStackTrace(); - } finally { - if (reader != null) - reader.close(); + return new DataFileCache(inv, armours, group, op, flying); } - return new DataFileCache(inv, armours, group, op, flying); + } catch (Exception e) { + ConsoleLogger.showError("Error while reading file for " + player.getName() + ", some wipe inventory incoming..."); + return null; } - } public void removeCache(Player player) { @@ -503,23 +508,26 @@ public class FileCache { } catch (Error e) { path = player.getName(); } - File file = new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path); - if (!file.exists()) { - file = new File("cache/" + player.getName().toLowerCase() + ".cache"); - } - - if (file.exists()) { - if (file.isDirectory()) { - for (File f : file.listFiles()) { - if (f.isDirectory()) { - for (File a : f.listFiles()) { - a.delete(); - } - f.delete(); - } else f.delete(); - } - file.delete(); - } else file.delete(); + try { + File file = new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path); + if (!file.exists()) { + file = new File("cache/" + player.getName().toLowerCase() + ".cache"); + } + if (file.exists()) { + if (file.isDirectory()) { + for (File f : file.listFiles()) { + if (f.isDirectory()) { + for (File a : f.listFiles()) { + a.delete(); + } + f.delete(); + } else f.delete(); + } + file.delete(); + } else file.delete(); + } + } catch (Exception e) { + ConsoleLogger.showError("File cannot be removed correctly :/"); } } diff --git a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java index bb189feed..d101b8693 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java @@ -651,8 +651,13 @@ public class AuthMePlayerListener implements Listener { } placePlayerSafely(player, spawnLoc); LimboCache.getInstance().updateLimboPlayer(player); - DataFileCache dataFile = new DataFileCache(LimboCache.getInstance().getLimboPlayer(name).getInventory(), LimboCache.getInstance().getLimboPlayer(name).getArmour()); - playerBackup.createCache(player, dataFile, LimboCache.getInstance().getLimboPlayer(name).getGroup(), LimboCache.getInstance().getLimboPlayer(name).getOperator(), LimboCache.getInstance().getLimboPlayer(name).isFlying()); + try { + DataFileCache dataFile = new DataFileCache(LimboCache.getInstance().getLimboPlayer(name).getInventory(), LimboCache.getInstance().getLimboPlayer(name).getArmour()); + playerBackup.createCache(player, dataFile, LimboCache.getInstance().getLimboPlayer(name).getGroup(), LimboCache.getInstance().getLimboPlayer(name).getOperator(), LimboCache.getInstance().getLimboPlayer(name).isFlying()); + } catch (Exception e) + { + ConsoleLogger.showError("Error on creating an inventory cache for " + name + ", maybe inventory wipe in preparation..."); + } } else { if (Settings.isForceSurvivalModeEnabled && !Settings.forceOnlyAfterLogin) { causeByAuthMe.put(name, true);