Merge pull request #43 from DNx5/master

improve inventory cache method  (EXPERIMENTAL)
This commit is contained in:
Gabriele C. 2015-09-10 12:11:10 +02:00
commit 639a0482c2
4 changed files with 214 additions and 482 deletions

View File

@ -1,498 +1,233 @@
package fr.xephi.authme.cache.backup; package fr.xephi.authme.cache.backup;
import java.io.File; import com.google.common.io.BaseEncoding;
import java.io.FileWriter;
import java.util.*;
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;
import com.comphenix.attribute.Attributes;
import com.comphenix.attribute.Attributes.Attribute;
import com.comphenix.attribute.Attributes.Attribute.Builder;
import com.comphenix.attribute.Attributes.AttributeType;
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.ConsoleLogger;
import fr.xephi.authme.Utils; import fr.xephi.authme.Utils;
import fr.xephi.authme.api.API; import fr.xephi.authme.api.API;
import fr.xephi.authme.settings.Settings; import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.util.io.BukkitObjectInputStream;
import org.bukkit.util.io.BukkitObjectOutputStream;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
public class FileCache { public class FileCache {
private final File cacheDir;
private AuthMe plugin; private AuthMe plugin;
public FileCache(AuthMe plugin) { public FileCache(AuthMe plugin) {
this.plugin = plugin; this.plugin = plugin;
final File file = new File(plugin.getDataFolder() + File.separator + "cache"); cacheDir = new File(plugin.getDataFolder() + File.separator + "cache");
if (!file.exists()) if (!cacheDir.exists() && !cacheDir.isDirectory() && !cacheDir.mkdir()) {
file.mkdir(); ConsoleLogger.showError("Failed to create cache directory.");
}
} }
public void createCache(Player player, DataFileCache playerData, public void createCache(Player player, DataFileCache playerData,
String group, boolean operator, boolean flying) { String group, boolean operator, boolean flying) {
String path = ""; if (player == null) {
if (player == null)
return; return;
}
String path;
try { try {
path = player.getUniqueId().toString(); path = player.getUniqueId().toString();
} catch (Exception | Error e) { } catch (Exception | Error e) {
path = player.getName().toLowerCase(); path = player.getName().toLowerCase();
} }
File file = new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path + File.separator + "playerdatas.cache");
if (!file.getParentFile().exists()) File playerDir = new File(cacheDir, path);
file.getParentFile().mkdirs(); if (!playerDir.exists() && !playerDir.isDirectory() && !playerDir.mkdir()) {
if (file.exists()) {
return; return;
} }
FileWriter writer = null; File datafile = new File(playerDir, "playerdatas.cache");
try { if (datafile.exists()) {
file.createNewFile(); return;
}
writer = new FileWriter(file); FileWriter writer;
try {
datafile.createNewFile();
writer = new FileWriter(datafile);
writer.write(group + API.newline); writer.write(group + API.newline);
writer.write(String.valueOf(operator) + API.newline); writer.write(String.valueOf(operator) + API.newline);
writer.write(String.valueOf(flying) + API.newline); writer.write(String.valueOf(flying) + API.newline);
writer.close(); writer.close();
} catch (IOException e) {
file = new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path + File.separator + "inventory"); e.printStackTrace();
file.mkdirs();
ItemStack[] inv = playerData.getInventory();
for (int i = 0; i < inv.length; i++) {
ItemStack item = inv[i];
file = new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path + File.separator + "inventory" + File.separator + i + ".cache");
file.createNewFile();
writer = new FileWriter(file);
if (item != null) {
if (item.getType() == Material.AIR) {
writer.write("AIR");
writer.close();
continue;
}
writer.write(item.getType().name() + API.newline);
writer.write(item.getDurability() + API.newline);
writer.write(item.getAmount() + API.newline);
writer.flush();
if (item.hasItemMeta()) {
ItemMeta meta = item.getItemMeta();
if (meta.hasDisplayName())
writer.write("name=" + meta.getDisplayName() + API.newline);
if (meta.hasLore()) {
String lores = "";
for (String lore : meta.getLore())
lores = lore + "%newline%";
writer.write("lore=" + lores + API.newline);
}
if (meta.hasEnchants()) {
for (Enchantment ench : meta.getEnchants().keySet()) {
writer.write("metaenchant=" + ench.getName() + ":" + meta.getEnchants().get(ench) + API.newline);
}
}
writer.flush();
}
for (Enchantment ench : item.getEnchantments().keySet()) {
writer.write("enchant=" + ench.getName() + ":" + item.getEnchantments().get(ench) + API.newline);
}
if (Settings.customAttributes) {
try {
Attributes attributes = new Attributes(item);
Iterator<Attribute> iter = attributes.values().iterator();
Attribute a = null;
while (iter.hasNext()) {
Attribute b = iter.next();
if (a != null && a == b)
break;
a = b;
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 | Error e) {
}
}
} else {
writer.write("AIR");
}
writer.close();
}
file = new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path + File.separator + "armours");
if (!file.getParentFile().exists())
file.getParentFile().mkdirs();
file.mkdirs();
ItemStack[] armors = playerData.getArmour();
for (int i = 0; i < armors.length; i++) {
ItemStack item = armors[i];
file = new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path + File.separator + "armours" + File.separator + i + ".cache");
file.createNewFile();
writer = new FileWriter(file);
if (item != null) {
if (item.getType() == Material.AIR) {
writer.write("AIR");
writer.close();
continue;
}
writer.write(item.getType().name() + API.newline);
writer.write(item.getDurability() + API.newline);
writer.write(item.getAmount() + API.newline);
writer.flush();
if (item.hasItemMeta()) {
ItemMeta meta = item.getItemMeta();
if (meta.hasDisplayName())
writer.write("name=" + meta.getDisplayName() + API.newline);
if (meta.hasLore()) {
String lores = "";
for (String lore : meta.getLore())
lores = lore + "%newline%";
writer.write("lore=" + lores + API.newline);
}
writer.flush();
}
for (Enchantment ench : item.getEnchantments().keySet()) {
writer.write("enchant=" + ench.getName() + ":" + item.getEnchantments().get(ench) + API.newline);
}
if (Settings.customAttributes) {
try {
Attributes attributes = new Attributes(item);
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 && a.getAttributeType().getMinecraftId() != null)
writer.write("attribute=" + a.getName() + ";" + a.getAttributeType().getMinecraftId() + ";" + a.getAmount() + ";" + a.getOperation().getId() + ";" + a.getUUID().toString());
}
}
} catch (Exception e) {
}
}
} else {
writer.write("AIR" + API.newline);
}
writer.close();
}
} catch (final Exception e) {
ConsoleLogger.showError("Some error on creating file cache...");
} }
BaseEncoding base64 = BaseEncoding.base64();
File invDir = new File(playerDir, "inventory");
if (!invDir.isDirectory() && !invDir.mkdir())
return;
ItemStack[] inv = playerData.getInventory();
for (int i = 0; i < inv.length; i++) {
ItemStack item = inv[i];
if (item == null) {
item = new ItemStack(Material.AIR);
}
if (item.getType() == Material.SKULL_ITEM) {
SkullMeta meta = (SkullMeta) item.getItemMeta();
if (meta.hasOwner() && (meta.getOwner() == null || meta.getOwner().isEmpty())) {
item.setItemMeta(plugin.getServer().getItemFactory().getItemMeta(Material.SKULL_ITEM));
}
}
try {
File cacheFile = new File(invDir, i + ".cache");
if (!cacheFile.isFile() && !cacheFile.createNewFile()) {
continue;
}
writer = new FileWriter(cacheFile);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BukkitObjectOutputStream objectOut = new BukkitObjectOutputStream(baos);
objectOut.writeObject(item);
objectOut.close();
writer.write(base64.encode(baos.toByteArray()));
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
File armourDir = new File(cacheDir, path + File.separator + "armours");
if (!armourDir.isDirectory() && !armourDir.mkdir())
return;
ItemStack[] armors = playerData.getArmour();
for (int i = 0; i < armors.length; i++) {
ItemStack item = armors[i];
if (item == null) {
item = new ItemStack(Material.AIR);
}
if (item.getType() == Material.SKULL_ITEM) {
SkullMeta meta = (SkullMeta) item.getItemMeta();
if (meta.hasOwner() && (meta.getOwner() == null || meta.getOwner().isEmpty())) {
item.setItemMeta(plugin.getServer().getItemFactory().getItemMeta(Material.SKULL_ITEM));
}
}
try {
File cacheFile = new File(armourDir, i + ".cache");
if (!cacheFile.isFile() && !cacheFile.createNewFile()) {
continue;
}
writer = new FileWriter(cacheFile);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BukkitObjectOutputStream objectOut = new BukkitObjectOutputStream(baos);
objectOut.writeObject(item);
objectOut.close();
writer.write(base64.encode(baos.toByteArray()));
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} }
public DataFileCache readCache(Player player) { public DataFileCache readCache(Player player) {
String path = ""; if (player == null) {
return null;
}
String path;
try { try {
path = player.getUniqueId().toString(); path = player.getUniqueId().toString();
} catch (Exception | Error e) { } catch (Exception | Error e) {
path = player.getName().toLowerCase(); path = player.getName().toLowerCase();
} }
File playerDir = new File(cacheDir, path);
if (!playerDir.exists() && !playerDir.isDirectory()) {
return null;
}
try { try {
File file = new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path + File.separator + "playerdatas.cache"); File datafile = new File(playerDir, "playerdatas.cache");
String playername = player.getName().toLowerCase(); if (!datafile.exists() || !datafile.isFile()) {
if (!file.exists()) { return null;
// OLD METHOD }
file = new File("cache/" + playername + ".cache"); ItemStack[] inv = new ItemStack[36];
ItemStack[] stacki = new ItemStack[36]; ItemStack[] armours = new ItemStack[4];
ItemStack[] stacka = new ItemStack[4]; String group = null;
if (!file.exists()) { boolean op = false;
return new DataFileCache(stacki, stacka); boolean flying = false;
}
String group = null;
boolean op = false;
boolean flying = false;
Scanner reader = new Scanner(file); Scanner reader;
try {
int i = 0; reader = new Scanner(datafile);
int a = 0; int count = 1;
while (reader.hasNextLine()) { while (reader.hasNextLine()) {
String line = reader.nextLine(); String line = reader.nextLine();
switch (count) {
if (!line.contains(":")) { case 1:
// the fist line represent the player group, group = line;
// operator break;
// status case 2:
// and flying status op = Boolean.parseBoolean(line);
final String[] playerInfo = line.split(";"); break;
group = playerInfo[0]; case 3:
flying = Boolean.parseBoolean(line);
op = Integer.parseInt(playerInfo[1]) == 1; break;
if (playerInfo.length > 2) { default:
flying = Integer.parseInt(playerInfo[2]) == 1; break;
}
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++;
}
}
ItemMeta meta = stacki[i].getItemMeta();
if (!name.isEmpty()) {
meta.setDisplayName(name);
}
if (!lores.isEmpty()) {
List<String> loreList = new ArrayList<String>();
Collections.addAll(loreList, lores.split("%newline%"));
meta.setLore(loreList);
}
if (meta != null)
stacki[i].setItemMeta(meta);
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++;
}
}
ItemMeta meta = stacka[a].getItemMeta();
if (!name.isEmpty())
meta.setDisplayName(name);
if (!lores.isEmpty()) {
List<String> loreList = new ArrayList<String>();
Collections.addAll(loreList, lores.split("%newline%"));
meta.setLore(loreList);
}
if (meta != null)
stacki[i].setItemMeta(meta);
a++;
} }
count++;
} }
if (reader != null) reader.close();
reader.close(); } catch (Exception e) {
return new DataFileCache(stacki, stacka, group, op, flying); e.printStackTrace();
} 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);
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 = item.getItemMeta();
Attributes attributes = null;
count = 1;
boolean v = true;
while (reader.hasNextLine() && v) {
String line = reader.nextLine();
switch (count) {
case 1:
item = new ItemStack(Material.getMaterial(line));
if (item.getType() == Material.AIR)
v = false;
count++;
continue;
case 2:
item.setDurability((short) Integer.parseInt(line));
count++;
continue;
case 3:
item.setAmount(Integer.parseInt(line));
count++;
continue;
default:
break;
}
meta = item.getItemMeta();
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>();
Collections.addAll(lore, line.split("%newline%"));
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]));
continue;
}
if (Settings.customAttributes) {
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]);
Builder build = Attribute.newBuilder();
build.amount(amount);
build.operation(operation);
build.type(type);
build.name(name);
build.uuid(uuid);
attributes.add(build.build());
} catch (Exception e) {
}
}
}
count++;
}
if (reader != null)
reader.close();
if (Settings.customAttributes && 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) {
String line = reader.nextLine();
switch (count) {
case 1:
item = new ItemStack(Material.getMaterial(line));
if (item.getType() == Material.AIR)
v = false;
meta = item.getItemMeta();
count++;
continue;
case 2:
item.setDurability((short) Integer.parseInt(line));
count++;
continue;
case 3:
item.setAmount(Integer.parseInt(line));
count++;
continue;
default:
break;
}
meta = item.getItemMeta();
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<>();
Collections.addAll(lore, line.split("%newline%"));
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 (Settings.customAttributes) {
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]);
Builder build = Attribute.newBuilder();
build.amount(amount);
build.operation(operation);
build.type(type);
build.name(name);
build.uuid(uuid);
attributes.add(build.build());
} catch (Exception e) {
}
}
}
count++;
}
if (reader != null)
reader.close();
if (attributes != null)
armours[i] = attributes.getStack();
else armours[i] = item;
}
} catch (final Exception e) {
//verbose
e.printStackTrace();
ConsoleLogger.showError("Error while reading file for " + player.getName() + ", some wipe inventory incoming...");
} finally {
if (reader != null)
reader.close();
}
return new DataFileCache(inv, armours, group, op, flying);
} }
BaseEncoding base64 = BaseEncoding.base64();
File invDir = new File(playerDir, "inventory");
for (int i = 0; i < inv.length; i++) {
byte[] bytes = Files.readAllBytes(Paths.get(invDir.getPath(), i + ".cache"));
String encodedItem = new String(bytes);
bytes = base64.decode(encodedItem);
ByteArrayInputStream baos = new ByteArrayInputStream(bytes);
BukkitObjectInputStream objectIn = new BukkitObjectInputStream(baos);
ItemStack item = (ItemStack) objectIn.readObject();
objectIn.close();
if (item == null) {
inv[i] = new ItemStack(Material.AIR);
} else {
inv[i] = item;
}
}
File armourDir = new File(playerDir, "armours");
for (int i = 0; i < armours.length; i++) {
byte[] bytes = Files.readAllBytes(Paths.get(armourDir.getPath(), i + ".cache"));
String encodedItem = new String(bytes);
bytes = base64.decode(encodedItem);
ByteArrayInputStream baos = new ByteArrayInputStream(bytes);
BukkitObjectInputStream objectIn = new BukkitObjectInputStream(baos);
ItemStack item = (ItemStack) objectIn.readObject();
objectIn.close();
if (item == null) {
armours[i] = new ItemStack(Material.AIR);
} else {
armours[i] = item;
}
}
return new DataFileCache(inv, armours, group, op, flying);
} catch (Exception e) { } catch (Exception e) {
// Verbose
e.printStackTrace(); e.printStackTrace();
ConsoleLogger.showError("Error while reading file for " + player.getName() + ", some wipe inventory incoming..."); ConsoleLogger.showError("Error while reading file for " + player.getName() + ", some wipe inventory incoming...");
return null; return null;
@ -500,23 +235,23 @@ public class FileCache {
} }
public void removeCache(Player player) { public void removeCache(Player player) {
String path = ""; String path;
try { try {
path = player.getUniqueId().toString(); path = player.getUniqueId().toString();
} catch (Exception | Error e) { } catch (Exception | Error e) {
path = player.getName().toLowerCase(); path = player.getName().toLowerCase();
} }
try { try {
File file = new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path); File file = new File(cacheDir, path);
if (file.list() != null) { if (file.list() != null) {
Utils.purgeDirectory(file); Utils.purgeDirectory(file);
file.delete(); if (!file.delete()) {
ConsoleLogger.showError("Failed to remove" + player.getName() + "cache.");
}
} else { } else {
file = new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + player.getName().toLowerCase() + ".cache"); file = new File(cacheDir, player.getName().toLowerCase() + ".cache");
if (file.isFile()) { if (file.isFile() && !file.delete()) {
file.delete(); ConsoleLogger.showError("Failed to remove" + player.getName() + "cache.");
} else {
ConsoleLogger.showError("Failed to remove" + player.getName() + "cache, it doesn't exist!");
} }
} }
} catch (Exception e) { } catch (Exception e) {
@ -525,15 +260,15 @@ public class FileCache {
} }
public boolean doesCacheExist(Player player) { public boolean doesCacheExist(Player player) {
String path = ""; String path;
try { try {
path = player.getUniqueId().toString(); path = player.getUniqueId().toString();
} catch (Exception | Error e) { } catch (Exception | Error e) {
path = player.getName().toLowerCase(); path = player.getName().toLowerCase();
} }
File file = new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path + File.separator + "playerdatas.cache"); File file = new File(cacheDir, path + File.separator + "playerdatas.cache");
if (!file.exists()) { if (!file.exists()) {
file = new File("cache/" + player.getName().toLowerCase() + ".cache"); file = new File(cacheDir, player.getName().toLowerCase() + ".cache");
} }
return file.exists(); return file.exists();

View File

@ -1,19 +1,19 @@
package fr.xephi.authme.cache.limbo; package fr.xephi.authme.cache.limbo;
import java.util.concurrent.ConcurrentHashMap; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.backup.DataFileCache;
import fr.xephi.authme.cache.backup.FileCache;
import fr.xephi.authme.events.ResetInventoryEvent;
import fr.xephi.authme.events.StoreInventoryEvent;
import fr.xephi.authme.settings.Settings;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import fr.xephi.authme.AuthMe; import java.util.concurrent.ConcurrentHashMap;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.backup.FileCache;
import fr.xephi.authme.events.ResetInventoryEvent;
import fr.xephi.authme.events.StoreInventoryEvent;
import fr.xephi.authme.settings.Settings;
public class LimboCache { public class LimboCache {
@ -48,13 +48,11 @@ public class LimboCache {
inv = null; inv = null;
arm = null; arm = null;
} }
try { DataFileCache cache = playerData.readCache(player);
playerGroup = playerData.readCache(player).getGroup(); if (cache != null) {
operator = playerData.readCache(player).getOperator(); playerGroup = cache.getGroup();
flying = playerData.readCache(player).isFlying(); operator = cache.getOperator();
} catch (Exception e) { flying = cache.isFlying();
ConsoleLogger.writeStackTrace(e);
ConsoleLogger.showError("Some error on reading cache of " + name);
} }
} else { } else {
StoreInventoryEvent event = new StoreInventoryEvent(player); StoreInventoryEvent event = new StoreInventoryEvent(player);

View File

@ -1,12 +1,11 @@
package fr.xephi.authme.events; package fr.xephi.authme.events;
import fr.xephi.authme.cache.backup.DataFileCache;
import fr.xephi.authme.cache.backup.FileCache;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import fr.xephi.authme.cache.backup.FileCache;
/** /**
*
* This event is call just before write inventory content to cache * This event is call just before write inventory content to cache
* *
* @author Xephi59 * @author Xephi59
@ -25,10 +24,11 @@ public class StoreInventoryEvent extends CustomEvent {
public StoreInventoryEvent(Player player, FileCache fileCache) { public StoreInventoryEvent(Player player, FileCache fileCache) {
this.player = player; this.player = player;
try { DataFileCache cache = fileCache.readCache(player);
this.inventory = fileCache.readCache(player).getInventory(); if (cache != null) {
this.armor = fileCache.readCache(player).getArmour(); this.inventory = cache.getInventory();
} catch (Exception e) { this.armor = cache.getArmour();
} else {
this.inventory = player.getInventory().getContents(); this.inventory = player.getInventory().getContents();
this.armor = player.getInventory().getArmorContents(); this.armor = player.getInventory().getArmorContents();
} }

View File

@ -85,8 +85,7 @@ public class AsyncronousLogout {
// create cache file for handling lost of inventories on unlogged in // create cache file for handling lost of inventories on unlogged in
// status // status
DataFileCache playerData = new DataFileCache(LimboCache.getInstance().getLimboPlayer(name).getInventory(), LimboCache.getInstance().getLimboPlayer(name).getArmour()); DataFileCache playerData = new DataFileCache(LimboCache.getInstance().getLimboPlayer(name).getInventory(), LimboCache.getInstance().getLimboPlayer(name).getArmour());
if (playerData != null) playerBackup.createCache(player, playerData, LimboCache.getInstance().getLimboPlayer(name).getGroup(), LimboCache.getInstance().getLimboPlayer(name).getOperator(), LimboCache.getInstance().getLimboPlayer(name).isFlying());
playerBackup.createCache(player, playerData, LimboCache.getInstance().getLimboPlayer(name).getGroup(), LimboCache.getInstance().getLimboPlayer(name).getOperator(), LimboCache.getInstance().getLimboPlayer(name).isFlying());
} }
sched.scheduleSyncDelayedTask(plugin, new ProcessSyncronousPlayerLogout(p, plugin)); sched.scheduleSyncDelayedTask(plugin, new ProcessSyncronousPlayerLogout(p, plugin));
} }