Rework cache system - Add Javadoc for AuthMe

This commit is contained in:
Xephi
2014-08-22 06:51:56 +02:00
parent e9032d1739
commit e5189283ed
375 changed files with 93072 additions and 236 deletions
+1 -1
View File
@@ -86,7 +86,7 @@ public class AuthMe extends JavaPlugin {
public Permission permission;
private Utils utils = Utils.getInstance();
private JavaPlugin plugin;
private FileCache playerBackup = new FileCache();
private FileCache playerBackup = new FileCache(this);
public CitizensCommunicator citizens;
public SendMailSSL mail = null;
public int CitizensVersion = 0;
+401 -224
View File
@@ -6,6 +6,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
@@ -17,19 +18,27 @@ import fr.xephi.authme.api.API;
public class FileCache {
private AuthMe plugin = AuthMe.getInstance();
private AuthMe plugin;
public FileCache() {
final File folder = new File("cache");
if (!folder.exists()) {
folder.mkdirs();
}
public FileCache(AuthMe plugin) {
this.plugin = plugin;
final File file = new File(plugin.getDataFolder() + File.separator
+ "cache");
if (!file.exists()) file.mkdir();
}
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");
String path = "";
try {
path = player.getUniqueId().toString();
} catch (Exception e) {
path = player.getName();
} catch (Error e) {
path = player.getName();
}
File file = new File(plugin.getDataFolder() + File.separator + "cache"
+ File.separator + path + File.separator + "playerdatas.cache");
if (file.exists()) {
return;
@@ -40,243 +49,411 @@ public class FileCache {
file.createNewFile();
writer = new FileWriter(file);
String s = group + ";";
if (operator) s = s + "1";
else s = s + "0";
// line format Group|OperatorStatus|isFlying
if (flying) writer.write(s + ";1" + API.newline);
else writer.write(s + ";0" + API.newline);
writer.flush();
ItemStack[] invstack = playerData.getInventory();
for (int i = 0; i < invstack.length; i++) {
String itemid = "AIR";
int amount = 0;
int durability = 0;
String enchList = "";
String name = "";
String lores = "";
if (invstack[i] != null) {
itemid = invstack[i].getType().name();
amount = invstack[i].getAmount();
durability = invstack[i].getDurability();
for (Enchantment e : invstack[i].getEnchantments().keySet()) {
enchList = enchList.concat(e.getName() + ":"
+ invstack[i].getEnchantmentLevel(e) + ":");
}
if (enchList.length() > 1) enchList = enchList.substring(0,
enchList.length() - 1);
if (invstack[i].hasItemMeta()) {
if (invstack[i].getItemMeta().hasDisplayName()) {
name = invstack[i].getItemMeta().getDisplayName();
}
if (invstack[i].getItemMeta().hasLore()) {
for (String lore : invstack[i].getItemMeta()
.getLore()) {
lores = lore + "%newline%";
}
}
}
}
String writeItem = "i" + ":" + itemid + ":" + amount + ":"
+ durability + ":" + enchList + ";" + name + "\\*"
+ lores + "\r\n";
writer.write(writeItem);
writer.flush();
}
ItemStack[] armorstack = playerData.getArmour();
for (int i = 0; i < armorstack.length; i++) {
String itemid = "AIR";
int amount = 0;
int durability = 0;
String enchList = "";
String name = "";
String lores = "";
if (armorstack[i] != null) {
itemid = armorstack[i].getType().name();
amount = armorstack[i].getAmount();
durability = armorstack[i].getDurability();
for (Enchantment e : armorstack[i].getEnchantments()
.keySet()) {
enchList = enchList.concat(e.getName() + ":"
+ armorstack[i].getEnchantmentLevel(e) + ":");
}
if (enchList.length() > 1) enchList = enchList.substring(0,
enchList.length() - 1);
if (armorstack[i].hasItemMeta()) {
if (armorstack[i].getItemMeta().hasDisplayName()) {
name = armorstack[i].getItemMeta().getDisplayName();
}
if (armorstack[i].getItemMeta().hasLore()) {
for (String lore : armorstack[i].getItemMeta()
.getLore()) {
lores = lore + "%newline%";
}
}
}
}
String writeItem = "w" + ":" + itemid + ":" + amount + ":"
+ durability + ":" + enchList + ";" + name + "\\*"
+ lores + "\r\n";
writer.write(writeItem);
writer.flush();
}
writer.write(group + API.newline);
writer.write(String.valueOf(operator) + API.newline);
writer.write(String.valueOf(flying) + API.newline);
writer.close();
file = new File(plugin.getDataFolder() + File.separator + "cache"
+ File.separator + path + File.separator + "inventory");
file.mkdir();
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");
writer = new FileWriter(file);
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);
}
writer.close();
}
file = new File(plugin.getDataFolder() + File.separator + "cache"
+ File.separator + path + File.separator + "armours");
file.mkdir();
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");
writer = new FileWriter(file);
if (item != null) {
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);
}
} else {
writer.write("AIR" + API.newline);
}
writer.close();
}
} catch (final Exception e) {
e.printStackTrace();
}
}
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];
String group = null;
boolean op = false;
boolean flying = false;
if (!file.exists()) {
return new DataFileCache(stacki, stacka);
}
Scanner reader = null;
String path = "";
try {
reader = new Scanner(file);
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 (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;
}
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 {
ItemMeta meta = plugin.getServer().getItemFactory()
.getItemMeta(stacki[i].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) {
}
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 (meta != null) stacki[i].setItemMeta(meta);
} catch (Exception e) {
}
a++;
}
}
} catch (final Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
path = player.getUniqueId().toString();
} catch (Exception e) {
path = player.getName();
} catch (Error e) {
path = player.getName();
}
return new DataFileCache(stacki, stacka, group, op, flying);
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];
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);
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 (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;
}
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 {
ItemMeta meta = plugin.getServer().getItemFactory()
.getItemMeta(stacki[i].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) {
}
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 (meta != null) stacki[i].setItemMeta(meta);
} catch (Exception e) {
}
a++;
}
}
} 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;
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;
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]));
}
count++;
}
if (reader != null)
reader.close();
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;
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]));
}
count++;
}
if (reader != null)
reader.close();
armours[i] = item;
}
} catch (final Exception e) {
e.printStackTrace();
} finally {
if (reader != null)
reader.close();
}
return new DataFileCache(inv, armours, group, op, flying);
}
}
public void removeCache(Player player) {
String playername = player.getName();
File file = new File("cache/" + playername + ".cache");
String path = "";
try {
path = player.getUniqueId().toString();
} catch (Exception e) {
path = player.getName();
} catch (Error e) {
path = player.getName();
}
File file = new File(plugin.getDataFolder() + File.separator + "cache"
+ File.separator + path);
if (!file.exists()) {
playername = player.getName().toLowerCase();
file = new File("cache/" + playername + ".cache");
file = new File("cache/" + player.getName().toLowerCase()
+ ".cache");
}
if (file.exists()) {
file.delete();
if (file.isDirectory()) {
for (File f : file.listFiles())
if (f.isDirectory()) for (File a : f.listFiles())
a.delete();
else f.delete();
} else file.delete();
}
}
public boolean doesCacheExist(Player player) {
String playername = player.getName();
File file = new File("cache/" + playername + ".cache");
String path = "";
try {
path = player.getUniqueId().toString();
} catch (Exception e) {
path = player.getName();
} catch (Error e) {
path = player.getName();
}
File file = new File(plugin.getDataFolder() + File.separator + "cache"
+ File.separator + path);
if (!file.exists()) {
playername = player.getName().toLowerCase();
file = new File("cache/" + playername + ".cache");
file = new File("cache/" + player.getName().toLowerCase()
+ ".cache");
}
if (file.exists()) {
+2 -1
View File
@@ -19,12 +19,13 @@ public class LimboCache {
private static LimboCache singleton = null;
public HashMap<String, LimboPlayer> cache;
private FileCache playerData = new FileCache();
private FileCache playerData;
public AuthMe plugin;
private LimboCache(AuthMe plugin) {
this.plugin = plugin;
this.cache = new HashMap<String, LimboPlayer>();
this.playerData = new FileCache(plugin);
}
public void addLimboPlayer(Player player) {
@@ -33,11 +33,12 @@ public class LogoutCommand implements CommandExecutor {
private AuthMe plugin;
private DataSource database;
private Utils utils = Utils.getInstance();
private FileCache playerBackup = new FileCache();
private FileCache playerBackup;
public LogoutCommand(AuthMe plugin, DataSource database) {
this.plugin = plugin;
this.database = database;
this.playerBackup = new FileCache(plugin);
}
@Override
@@ -34,11 +34,12 @@ public class UnregisterCommand implements CommandExecutor {
private Messages m = Messages.getInstance();
public AuthMe plugin;
private DataSource database;
private FileCache playerCache = new FileCache();
private FileCache playerCache;
public UnregisterCommand(AuthMe plugin, DataSource database) {
this.plugin = plugin;
this.database = database;
this.playerCache = new FileCache(plugin);
}
@Override
@@ -755,8 +755,7 @@ public class FlatFileThread extends Thread implements DataSource {
int result = 0;
try {
br = new BufferedReader(new FileReader(source));
String line;
while ((line = br.readLine()) != null) {
while ((br.readLine()) != null) {
result++;
}
} catch (FileNotFoundException ex) {
@@ -70,13 +70,14 @@ public class AuthMePlayerListener implements Listener {
private Messages m = Messages.getInstance();
public AuthMe plugin;
private DataSource data;
private FileCache playerBackup = new FileCache();
private FileCache playerBackup;
public boolean causeByAuthMe = false;
private HashMap<String, PlayerLoginEvent> antibot = new HashMap<String, PlayerLoginEvent>();
public AuthMePlayerListener(AuthMe plugin, DataSource data) {
this.plugin = plugin;
this.data = data;
this.playerBackup = new FileCache(plugin);
}
@EventHandler(priority = EventPriority.LOWEST)
@@ -31,7 +31,7 @@ public class ProcessSyncronousPlayerLogin implements Runnable {
private AuthMe plugin;
private DataSource database;
private PluginManager pm;
private FileCache playerCache = new FileCache();
private FileCache playerCache;
public ProcessSyncronousPlayerLogin(Player player, AuthMe plugin,
DataSource data) {
@@ -42,6 +42,7 @@ public class ProcessSyncronousPlayerLogin implements Runnable {
this.name = player.getName().toLowerCase();
this.limbo = LimboCache.getInstance().getLimboPlayer(name);
this.auth = database.getAuth(name);
this.playerCache = new FileCache(plugin);
}
public LimboPlayer getLimbo() {
@@ -2,8 +2,8 @@ package fr.xephi.authme.task;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.cache.backup.FileCache;
@@ -14,14 +14,15 @@ import fr.xephi.authme.settings.Messages;
public class TimeoutTask implements Runnable {
private JavaPlugin plugin;
private AuthMe plugin;
private String name;
private Messages m = Messages.getInstance();
private FileCache playerCache = new FileCache();
private FileCache playerCache;
public TimeoutTask(JavaPlugin plugin, String name) {
public TimeoutTask(AuthMe plugin, String name) {
this.plugin = plugin;
this.name = name;
this.playerCache = new FileCache(plugin);
}
public String getName() {