Some Fix on FileCache

This commit is contained in:
Xephi 2015-04-21 17:41:46 +02:00
parent 44bbd30fd2
commit 5548004fd0
2 changed files with 319 additions and 306 deletions

View File

@ -21,6 +21,7 @@ import com.comphenix.attribute.Attributes.AttributeType;
import com.comphenix.attribute.Attributes.Operation; import com.comphenix.attribute.Attributes.Operation;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.api.API; import fr.xephi.authme.api.API;
public class FileCache { public class FileCache {
@ -101,19 +102,19 @@ public class FileCache {
for (Enchantment ench : item.getEnchantments().keySet()) { for (Enchantment ench : item.getEnchantments().keySet()) {
writer.write("enchant=" + ench.getName() + ":" + item.getEnchantments().get(ench) + API.newline); writer.write("enchant=" + ench.getName() + ":" + item.getEnchantments().get(ench) + API.newline);
} }
Attributes attributes = new Attributes(item); try {
if (attributes != null) Attributes attributes = new Attributes(item);
while (attributes.values().iterator().hasNext()) { if (attributes != null)
Attribute a = attributes.values().iterator().next(); while (attributes.values().iterator().hasNext()) {
if (a != null) { Attribute a = attributes.values().iterator().next();
try { if (a != null) {
if (a.getName() != null && a.getAttributeType() != null if (a.getName() != null && a.getAttributeType() != null
&& a.getOperation() != null && a.getUUID() != null) && a.getOperation() != null && a.getUUID() != null)
writer.write("attribute=" + a.getName() + ";" + a.getAttributeType().getMinecraftId() + ";" + a.getAmount() + ";" + a.getOperation().getId() + ";" + a.getUUID().toString()); 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 { } else {
writer.write("AIR"); writer.write("AIR");
} }
@ -155,25 +156,25 @@ public class FileCache {
for (Enchantment ench : item.getEnchantments().keySet()) { for (Enchantment ench : item.getEnchantments().keySet()) {
writer.write("enchant=" + ench.getName() + ":" + item.getEnchantments().get(ench) + API.newline); writer.write("enchant=" + ench.getName() + ":" + item.getEnchantments().get(ench) + API.newline);
} }
Attributes attributes = new Attributes(item); try {
if (attributes != null) Attributes attributes = new Attributes(item);
while (attributes.values().iterator().hasNext()) { if (attributes != null)
Attribute a = attributes.values().iterator().next(); while (attributes.values().iterator().hasNext()) {
if (a != null) { Attribute a = attributes.values().iterator().next();
try { if (a != null) {
if (a.getName() != null && a.getAttributeType() != null if (a.getName() != null && a.getAttributeType() != null
&& a.getOperation() != null && a.getUUID() != null) && a.getOperation() != null && a.getUUID() != null)
writer.write("attribute=" + a.getName() + ";" + a.getAttributeType().getMinecraftId() + ";" + a.getAmount() + ";" + a.getOperation().getId() + ";" + a.getUUID().toString()); writer.write("attribute=" + a.getName() + ";" + a.getAttributeType().getMinecraftId() + ";" + a.getAmount() + ";" + a.getOperation().getId() + ";" + a.getUUID().toString());
} catch (Exception e) {} }
} }
} } catch (Exception e) {}
} else { } else {
writer.write("AIR" + API.newline); writer.write("AIR" + API.newline);
} }
writer.close(); writer.close();
} }
} catch (final Exception e) { } catch (final Exception e) {
e.printStackTrace(); ConsoleLogger.showError("Some error on creating file cache...");
} }
} }
@ -186,312 +187,316 @@ public class FileCache {
} catch (Error e) { } catch (Error e) {
path = player.getName(); path = player.getName();
} }
File file = new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path + File.separator + ".playerdatas.cache"); try {
String playername = player.getName().toLowerCase(); File file = new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path + File.separator + ".playerdatas.cache");
if (!file.exists()) { String playername = player.getName().toLowerCase();
// OLD METHOD
file = new File("cache/" + playername + ".cache");
ItemStack[] stacki = new ItemStack[36];
ItemStack[] stacka = new ItemStack[4];
if (!file.exists()) { if (!file.exists()) {
return new DataFileCache(stacki, stacka); // OLD METHOD
} file = new File("cache/" + playername + ".cache");
String group = null; ItemStack[] stacki = new ItemStack[36];
boolean op = false; ItemStack[] stacka = new ItemStack[4];
boolean flying = false; if (!file.exists()) {
return new DataFileCache(stacki, stacka);
}
String group = null;
boolean op = false;
boolean flying = false;
Scanner reader = null; Scanner reader = null;
try { try {
reader = new Scanner(file); reader = new Scanner(file);
int i = 0; int i = 0;
int a = 0; int a = 0;
while (reader.hasNextLine()) { while (reader.hasNextLine()) {
String line = reader.nextLine(); String line = reader.nextLine();
if (!line.contains(":")) { if (!line.contains(":")) {
// the fist line represent the player group, operator // the fist line represent the player group, operator
// status // status
// and flying status // and flying status
final String[] playerInfo = line.split(";"); final String[] playerInfo = line.split(";");
group = playerInfo[0]; group = playerInfo[0];
if (Integer.parseInt(playerInfo[1]) == 1) { if (Integer.parseInt(playerInfo[1]) == 1) {
op = true; op = true;
} else op = false; } else op = false;
if (playerInfo.length > 2) { if (playerInfo.length > 2) {
if (Integer.parseInt(playerInfo[2]) == 1) if (Integer.parseInt(playerInfo[2]) == 1)
flying = true; flying = true;
else flying = false; else flying = false;
}
continue;
} }
continue; if (!line.startsWith("i") && !line.startsWith("w")) {
} 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++;
}
} }
try { String lores = "";
ItemMeta meta = plugin.getServer().getItemFactory().getItemMeta(stacki[i].getType()); String name = "";
if (!name.isEmpty()) { if (line.split("\\*").length > 1) {
meta.setDisplayName(name); lores = line.split("\\*")[1];
} line = line.split("\\*")[0];
if (!lores.isEmpty()) { }
List<String> loreList = new ArrayList<String>(); if (line.split(";").length > 1) {
for (String s : lores.split("%newline%")) { name = line.split(";")[1];
loreList.add(s); 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) try {
stacki[i].setItemMeta(meta); ItemMeta meta = plugin.getServer().getItemFactory().getItemMeta(stacki[i].getType());
} catch (Exception e) { if (!name.isEmpty()) {
} meta.setDisplayName(name);
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<String> loreList = new ArrayList<String>();
for (String s : lores.split("%newline%")) {
loreList.add(s);
} }
meta.setLore(loreList); if (!lores.isEmpty()) {
List<String> loreList = new ArrayList<String>();
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) i++;
stacki[i].setItemMeta(meta); } else {
} catch (Exception e) { 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<String> loreList = new ArrayList<String>();
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) { return new DataFileCache(stacki, stacka, group, op, flying);
e.printStackTrace(); } else {
} finally { // NEW METHOD
if (reader != null) { ItemStack[] inv = new ItemStack[36];
reader.close(); ItemStack[] armours = new ItemStack[4];
} String group = null;
} boolean op = false;
return new DataFileCache(stacki, stacka, group, op, flying); boolean flying = false;
} 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; Scanner reader = null;
try { try {
reader = new Scanner(file); reader = new Scanner(file);
int count = 1; int count = 1;
while (reader.hasNextLine()) { 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) {
String line = reader.nextLine(); String line = reader.nextLine();
switch (count) { switch (count) {
case 1: case 1:
item.setType(Material.getMaterial(line)); group = line;
if (item.getType() == Material.AIR) break;
v = false;
continue;
case 2: case 2:
item.setDurability(Short.parseShort(line)); op = Boolean.parseBoolean(line);
continue; break;
case 3: case 3:
item.setAmount(Integer.parseInt(line)); flying = Boolean.parseBoolean(line);
continue;
case 4:
meta = Bukkit.getItemFactory().getItemMeta(item.getType());
break; break;
default: default:
break; 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<String> lore = new ArrayList<String>();
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) if (reader != null)
reader.close(); reader.close();
if (attributes != null) for (int i = 0; i < inv.length; i++) {
inv[i] = attributes.getStack(); reader = new Scanner(new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path + File.separator + "inventory" + File.separator + i + ".cache"));
else ItemStack item = new ItemStack(Material.AIR);
inv[i] = item; ItemMeta meta = null;
} Attributes attributes = null;
for (int i = 0; i < armours.length; i++) { count = 1;
reader = new Scanner(new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path + File.separator + "armours" + File.separator + i + ".cache")); boolean v = true;
ItemStack item = new ItemStack(Material.AIR); while (reader.hasNextLine() && v == true) {
ItemMeta meta = null; String line = reader.nextLine();
Attributes attributes = null; switch (count) {
count = 1; case 1:
boolean v = true; item.setType(Material.getMaterial(line));
while (reader.hasNextLine() && v == true) { if (item.getType() == Material.AIR)
String line = reader.nextLine(); v = false;
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<String> lore = new ArrayList<String>();
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; continue;
String name = args[0]; case 2:
AttributeType type = AttributeType.fromId(args[1]); item.setDurability(Short.parseShort(line));
double amount = Double.parseDouble(args[2]); continue;
Operation operation = Operation.fromId(Integer.parseInt(args[3])); case 3:
UUID uuid = UUID.fromString(args[4]); item.setAmount(Integer.parseInt(line));
Attribute attribute = new Attribute(new Builder(amount, operation, type, name, uuid)); continue;
attributes.add(attribute); case 4:
} catch (Exception e) { 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<String> lore = new ArrayList<String>();
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<String> lore = new ArrayList<String>();
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) if (reader != null)
reader.close(); reader.close();
if (attributes != null)
armours[i] = attributes.getStack();
else
armours[i] = item;
} }
return new DataFileCache(inv, armours, group, op, flying);
} catch (final Exception e) {
e.printStackTrace();
} finally {
if (reader != null)
reader.close();
} }
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) { public void removeCache(Player player) {
@ -503,23 +508,26 @@ public class FileCache {
} catch (Error e) { } catch (Error e) {
path = player.getName(); path = player.getName();
} }
File file = new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path); try {
if (!file.exists()) { File file = new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path);
file = new File("cache/" + player.getName().toLowerCase() + ".cache"); if (!file.exists()) {
} file = new File("cache/" + player.getName().toLowerCase() + ".cache");
}
if (file.exists()) { if (file.exists()) {
if (file.isDirectory()) { if (file.isDirectory()) {
for (File f : file.listFiles()) { for (File f : file.listFiles()) {
if (f.isDirectory()) { if (f.isDirectory()) {
for (File a : f.listFiles()) { for (File a : f.listFiles()) {
a.delete(); a.delete();
} }
f.delete(); f.delete();
} else f.delete(); } else f.delete();
} }
file.delete(); file.delete();
} else file.delete(); } else file.delete();
}
} catch (Exception e) {
ConsoleLogger.showError("File cannot be removed correctly :/");
} }
} }

View File

@ -651,8 +651,13 @@ public class AuthMePlayerListener implements Listener {
} }
placePlayerSafely(player, spawnLoc); placePlayerSafely(player, spawnLoc);
LimboCache.getInstance().updateLimboPlayer(player); LimboCache.getInstance().updateLimboPlayer(player);
DataFileCache dataFile = new DataFileCache(LimboCache.getInstance().getLimboPlayer(name).getInventory(), LimboCache.getInstance().getLimboPlayer(name).getArmour()); try {
playerBackup.createCache(player, dataFile, LimboCache.getInstance().getLimboPlayer(name).getGroup(), LimboCache.getInstance().getLimboPlayer(name).getOperator(), LimboCache.getInstance().getLimboPlayer(name).isFlying()); 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 { } else {
if (Settings.isForceSurvivalModeEnabled && !Settings.forceOnlyAfterLogin) { if (Settings.isForceSurvivalModeEnabled && !Settings.forceOnlyAfterLogin) {
causeByAuthMe.put(name, true); causeByAuthMe.put(name, true);