|
@@ -2,6 +2,7 @@ package com.gmail.nossr50.database;
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.BufferedWriter;
|
|
|
+import java.io.Closeable;
|
|
|
import java.io.File;
|
|
|
import java.io.FileReader;
|
|
|
import java.io.FileWriter;
|
|
@@ -13,6 +14,7 @@ import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import org.bukkit.Bukkit;
|
|
|
import org.bukkit.OfflinePlayer;
|
|
|
|
|
|
import com.gmail.nossr50.mcMMO;
|
|
@@ -32,6 +34,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
|
|
|
|
|
private final long UPDATE_WAIT_TIME = 600000L; // 10 minutes
|
|
|
private final File usersFile;
|
|
|
+ private static final Object fileWritingLock = new Object();
|
|
|
|
|
|
protected FlatfileDatabaseManager() {
|
|
|
usersFile = new File(mcMMO.getUsersFilePath());
|
|
@@ -44,9 +47,49 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
|
|
|
|
|
mcMMO.p.getLogger().info("Purging powerless users...");
|
|
|
|
|
|
- for (PlayerStat stat : powerLevels) {
|
|
|
- if (stat.statVal == 0 && mcMMO.p.getServer().getPlayerExact(stat.name) == null && removeUser(stat.name)) {
|
|
|
- purgedUsers++;
|
|
|
+ BufferedReader in = null;
|
|
|
+ FileWriter out = null;
|
|
|
+ String usersFilePath = mcMMO.getUsersFilePath();
|
|
|
+
|
|
|
+ // This code is O(n) instead of O(n²)
|
|
|
+ synchronized (fileWritingLock) {
|
|
|
+ try {
|
|
|
+ in = new BufferedReader(new FileReader(usersFilePath));
|
|
|
+ StringBuilder writer = new StringBuilder();
|
|
|
+ String line = "";
|
|
|
+
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
+ String[] character = line.split(":");
|
|
|
+ Map<SkillType, Integer> skills = getSkillMapFromLine(character);
|
|
|
+
|
|
|
+ boolean powerless = true;
|
|
|
+ for (int skill : skills.values()) {
|
|
|
+ if (skill != 0) {
|
|
|
+ powerless = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // If they're still around, rewrite them to the file.
|
|
|
+ if (!powerless) {
|
|
|
+ writer.append(line).append("\r\n");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ purgedUsers++;
|
|
|
+ Misc.profileCleanup(character[0]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Write the new file
|
|
|
+ out = new FileWriter(usersFilePath);
|
|
|
+ out.write(writer.toString());
|
|
|
+ }
|
|
|
+ catch (IOException e) {
|
|
|
+ mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ tryClose(in);
|
|
|
+ tryClose(out);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -59,9 +102,46 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
|
|
|
|
|
mcMMO.p.getLogger().info("Purging old users...");
|
|
|
|
|
|
- for (OfflinePlayer player : mcMMO.p.getServer().getOfflinePlayers()) {
|
|
|
- if (!player.isOnline() && (currentTime - player.getLastPlayed() > PURGE_TIME && removeUser(player.getName()))) {
|
|
|
- removedPlayers++;
|
|
|
+
|
|
|
+ BufferedReader in = null;
|
|
|
+ FileWriter out = null;
|
|
|
+ String usersFilePath = mcMMO.getUsersFilePath();
|
|
|
+
|
|
|
+ // This code is O(n) instead of O(n²)
|
|
|
+ synchronized (fileWritingLock) {
|
|
|
+ try {
|
|
|
+ in = new BufferedReader(new FileReader(usersFilePath));
|
|
|
+ StringBuilder writer = new StringBuilder();
|
|
|
+ String line = "";
|
|
|
+
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
+ String[] character = line.split(":");
|
|
|
+ String name = character[0];
|
|
|
+ OfflinePlayer player = Bukkit.getOfflinePlayer(name);
|
|
|
+ boolean old = true;
|
|
|
+ if (player != null) {
|
|
|
+ old = (currentTime - player.getLastPlayed()) > PURGE_TIME;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!old) {
|
|
|
+ writer.append(line).append("\r\n");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ removedPlayers++;
|
|
|
+ Misc.profileCleanup(name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Write the new file
|
|
|
+ out = new FileWriter(usersFilePath);
|
|
|
+ out.write(writer.toString());
|
|
|
+ }
|
|
|
+ catch (IOException e) {
|
|
|
+ mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ tryClose(in);
|
|
|
+ tryClose(out);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -75,45 +155,32 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
|
|
FileWriter out = null;
|
|
|
String usersFilePath = mcMMO.getUsersFilePath();
|
|
|
|
|
|
- try {
|
|
|
- in = new BufferedReader(new FileReader(usersFilePath));
|
|
|
- StringBuilder writer = new StringBuilder();
|
|
|
- String line = "";
|
|
|
-
|
|
|
- while ((line = in.readLine()) != null) {
|
|
|
- // Write out the same file but when we get to the player we want to remove, we skip his line.
|
|
|
- if (!worked && line.split(":")[0].equalsIgnoreCase(playerName)) {
|
|
|
- mcMMO.p.getLogger().info("User found, removing...");
|
|
|
- worked = true;
|
|
|
- continue; // Skip the player
|
|
|
- }
|
|
|
+ synchronized (fileWritingLock) {
|
|
|
+ try {
|
|
|
+ in = new BufferedReader(new FileReader(usersFilePath));
|
|
|
+ StringBuilder writer = new StringBuilder();
|
|
|
+ String line = "";
|
|
|
|
|
|
- writer.append(line).append("\r\n");
|
|
|
- }
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
+ // Write out the same file but when we get to the player we want to remove, we skip his line.
|
|
|
+ if (!worked && line.split(":")[0].equalsIgnoreCase(playerName)) {
|
|
|
+ mcMMO.p.getLogger().info("User found, removing...");
|
|
|
+ worked = true;
|
|
|
+ continue; // Skip the player
|
|
|
+ }
|
|
|
|
|
|
- out = new FileWriter(usersFilePath); // Write out the new file
|
|
|
- out.write(writer.toString());
|
|
|
- }
|
|
|
- catch (Exception e) {
|
|
|
- mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
|
|
|
- }
|
|
|
- finally {
|
|
|
- if (in != null) {
|
|
|
- try {
|
|
|
- in.close();
|
|
|
- }
|
|
|
- catch (IOException ex) {
|
|
|
- ex.printStackTrace();
|
|
|
+ writer.append(line).append("\r\n");
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- if (out != null) {
|
|
|
- try {
|
|
|
- out.close();
|
|
|
- }
|
|
|
- catch (IOException ex) {
|
|
|
- ex.printStackTrace();
|
|
|
- }
|
|
|
+ out = new FileWriter(usersFilePath); // Write out the new file
|
|
|
+ out.write(writer.toString());
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ tryClose(in);
|
|
|
+ tryClose(out);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -129,89 +196,76 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
|
|
FileWriter out = null;
|
|
|
String usersFilePath = mcMMO.getUsersFilePath();
|
|
|
|
|
|
- try {
|
|
|
- // Open the file
|
|
|
- in = new BufferedReader(new FileReader(usersFilePath));
|
|
|
- StringBuilder writer = new StringBuilder();
|
|
|
- String line;
|
|
|
-
|
|
|
- // While not at the end of the file
|
|
|
- while ((line = in.readLine()) != null) {
|
|
|
- // Read the line in and copy it to the output it's not the player we want to edit
|
|
|
- if (!line.split(":")[0].equalsIgnoreCase(playerName)) {
|
|
|
- writer.append(line).append("\r\n");
|
|
|
- }
|
|
|
- else {
|
|
|
- // Otherwise write the new player information
|
|
|
- writer.append(playerName).append(":");
|
|
|
- writer.append(profile.getSkillLevel(SkillType.MINING)).append(":");
|
|
|
- writer.append(":");
|
|
|
- writer.append(":");
|
|
|
- writer.append(profile.getSkillXpLevel(SkillType.MINING)).append(":");
|
|
|
- writer.append(profile.getSkillLevel(SkillType.WOODCUTTING)).append(":");
|
|
|
- writer.append(profile.getSkillXpLevel(SkillType.WOODCUTTING)).append(":");
|
|
|
- writer.append(profile.getSkillLevel(SkillType.REPAIR)).append(":");
|
|
|
- writer.append(profile.getSkillLevel(SkillType.UNARMED)).append(":");
|
|
|
- writer.append(profile.getSkillLevel(SkillType.HERBALISM)).append(":");
|
|
|
- writer.append(profile.getSkillLevel(SkillType.EXCAVATION)).append(":");
|
|
|
- writer.append(profile.getSkillLevel(SkillType.ARCHERY)).append(":");
|
|
|
- writer.append(profile.getSkillLevel(SkillType.SWORDS)).append(":");
|
|
|
- writer.append(profile.getSkillLevel(SkillType.AXES)).append(":");
|
|
|
- writer.append(profile.getSkillLevel(SkillType.ACROBATICS)).append(":");
|
|
|
- writer.append(profile.getSkillXpLevel(SkillType.REPAIR)).append(":");
|
|
|
- writer.append(profile.getSkillXpLevel(SkillType.UNARMED)).append(":");
|
|
|
- writer.append(profile.getSkillXpLevel(SkillType.HERBALISM)).append(":");
|
|
|
- writer.append(profile.getSkillXpLevel(SkillType.EXCAVATION)).append(":");
|
|
|
- writer.append(profile.getSkillXpLevel(SkillType.ARCHERY)).append(":");
|
|
|
- writer.append(profile.getSkillXpLevel(SkillType.SWORDS)).append(":");
|
|
|
- writer.append(profile.getSkillXpLevel(SkillType.AXES)).append(":");
|
|
|
- writer.append(profile.getSkillXpLevel(SkillType.ACROBATICS)).append(":");
|
|
|
- writer.append(":");
|
|
|
- writer.append(profile.getSkillLevel(SkillType.TAMING)).append(":");
|
|
|
- writer.append(profile.getSkillXpLevel(SkillType.TAMING)).append(":");
|
|
|
- writer.append((int) profile.getSkillDATS(AbilityType.BERSERK)).append(":");
|
|
|
- writer.append((int) profile.getSkillDATS(AbilityType.GIGA_DRILL_BREAKER)).append(":");
|
|
|
- writer.append((int) profile.getSkillDATS(AbilityType.TREE_FELLER)).append(":");
|
|
|
- writer.append((int) profile.getSkillDATS(AbilityType.GREEN_TERRA)).append(":");
|
|
|
- writer.append((int) profile.getSkillDATS(AbilityType.SERRATED_STRIKES)).append(":");
|
|
|
- writer.append((int) profile.getSkillDATS(AbilityType.SKULL_SPLITTER)).append(":");
|
|
|
- writer.append((int) profile.getSkillDATS(AbilityType.SUPER_BREAKER)).append(":");
|
|
|
- HudType hudType = profile.getHudType();
|
|
|
- writer.append(hudType == null ? "STANDARD" : hudType.toString()).append(":");
|
|
|
- writer.append(profile.getSkillLevel(SkillType.FISHING)).append(":");
|
|
|
- writer.append(profile.getSkillXpLevel(SkillType.FISHING)).append(":");
|
|
|
- writer.append((int) profile.getSkillDATS(AbilityType.BLAST_MINING)).append(":");
|
|
|
- writer.append(System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR).append(":");
|
|
|
- MobHealthbarType mobHealthbarType = profile.getMobHealthbarType();
|
|
|
- writer.append(mobHealthbarType == null ? Config.getInstance().getMobHealthbarDefault().toString() : mobHealthbarType.toString()).append(":");
|
|
|
- writer.append("\r\n");
|
|
|
+ synchronized (fileWritingLock) {
|
|
|
+ try {
|
|
|
+ // Open the file
|
|
|
+ in = new BufferedReader(new FileReader(usersFilePath));
|
|
|
+ StringBuilder writer = new StringBuilder();
|
|
|
+ String line;
|
|
|
+
|
|
|
+ // While not at the end of the file
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
+ // Read the line in and copy it to the output it's not the player we want to edit
|
|
|
+ if (!line.split(":")[0].equalsIgnoreCase(playerName)) {
|
|
|
+ writer.append(line).append("\r\n");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // Otherwise write the new player information
|
|
|
+ writer.append(playerName).append(":");
|
|
|
+ writer.append(profile.getSkillLevel(SkillType.MINING)).append(":");
|
|
|
+ writer.append(":");
|
|
|
+ writer.append(":");
|
|
|
+ writer.append(profile.getSkillXpLevel(SkillType.MINING)).append(":");
|
|
|
+ writer.append(profile.getSkillLevel(SkillType.WOODCUTTING)).append(":");
|
|
|
+ writer.append(profile.getSkillXpLevel(SkillType.WOODCUTTING)).append(":");
|
|
|
+ writer.append(profile.getSkillLevel(SkillType.REPAIR)).append(":");
|
|
|
+ writer.append(profile.getSkillLevel(SkillType.UNARMED)).append(":");
|
|
|
+ writer.append(profile.getSkillLevel(SkillType.HERBALISM)).append(":");
|
|
|
+ writer.append(profile.getSkillLevel(SkillType.EXCAVATION)).append(":");
|
|
|
+ writer.append(profile.getSkillLevel(SkillType.ARCHERY)).append(":");
|
|
|
+ writer.append(profile.getSkillLevel(SkillType.SWORDS)).append(":");
|
|
|
+ writer.append(profile.getSkillLevel(SkillType.AXES)).append(":");
|
|
|
+ writer.append(profile.getSkillLevel(SkillType.ACROBATICS)).append(":");
|
|
|
+ writer.append(profile.getSkillXpLevel(SkillType.REPAIR)).append(":");
|
|
|
+ writer.append(profile.getSkillXpLevel(SkillType.UNARMED)).append(":");
|
|
|
+ writer.append(profile.getSkillXpLevel(SkillType.HERBALISM)).append(":");
|
|
|
+ writer.append(profile.getSkillXpLevel(SkillType.EXCAVATION)).append(":");
|
|
|
+ writer.append(profile.getSkillXpLevel(SkillType.ARCHERY)).append(":");
|
|
|
+ writer.append(profile.getSkillXpLevel(SkillType.SWORDS)).append(":");
|
|
|
+ writer.append(profile.getSkillXpLevel(SkillType.AXES)).append(":");
|
|
|
+ writer.append(profile.getSkillXpLevel(SkillType.ACROBATICS)).append(":");
|
|
|
+ writer.append(":");
|
|
|
+ writer.append(profile.getSkillLevel(SkillType.TAMING)).append(":");
|
|
|
+ writer.append(profile.getSkillXpLevel(SkillType.TAMING)).append(":");
|
|
|
+ writer.append((int) profile.getSkillDATS(AbilityType.BERSERK)).append(":");
|
|
|
+ writer.append((int) profile.getSkillDATS(AbilityType.GIGA_DRILL_BREAKER)).append(":");
|
|
|
+ writer.append((int) profile.getSkillDATS(AbilityType.TREE_FELLER)).append(":");
|
|
|
+ writer.append((int) profile.getSkillDATS(AbilityType.GREEN_TERRA)).append(":");
|
|
|
+ writer.append((int) profile.getSkillDATS(AbilityType.SERRATED_STRIKES)).append(":");
|
|
|
+ writer.append((int) profile.getSkillDATS(AbilityType.SKULL_SPLITTER)).append(":");
|
|
|
+ writer.append((int) profile.getSkillDATS(AbilityType.SUPER_BREAKER)).append(":");
|
|
|
+ HudType hudType = profile.getHudType();
|
|
|
+ writer.append(hudType == null ? "STANDARD" : hudType.toString()).append(":");
|
|
|
+ writer.append(profile.getSkillLevel(SkillType.FISHING)).append(":");
|
|
|
+ writer.append(profile.getSkillXpLevel(SkillType.FISHING)).append(":");
|
|
|
+ writer.append((int) profile.getSkillDATS(AbilityType.BLAST_MINING)).append(":");
|
|
|
+ writer.append(System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR).append(":");
|
|
|
+ MobHealthbarType mobHealthbarType = profile.getMobHealthbarType();
|
|
|
+ writer.append(mobHealthbarType == null ? Config.getInstance().getMobHealthbarDefault().toString() : mobHealthbarType.toString()).append(":");
|
|
|
+ writer.append("\r\n");
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- // Write the new file
|
|
|
- out = new FileWriter(usersFilePath);
|
|
|
- out.write(writer.toString());
|
|
|
- }
|
|
|
- catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- finally {
|
|
|
- if (in != null) {
|
|
|
- try {
|
|
|
- in.close();
|
|
|
- }
|
|
|
- catch (IOException ex) {
|
|
|
- ex.printStackTrace();
|
|
|
- }
|
|
|
+ // Write the new file
|
|
|
+ out = new FileWriter(usersFilePath);
|
|
|
+ out.write(writer.toString());
|
|
|
}
|
|
|
-
|
|
|
- if (out != null) {
|
|
|
- try {
|
|
|
- out.close();
|
|
|
- }
|
|
|
- catch (IOException ex) {
|
|
|
- ex.printStackTrace();
|
|
|
- }
|
|
|
+ catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ tryClose(in);
|
|
|
+ tryClose(out);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -239,145 +293,168 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
|
|
}
|
|
|
|
|
|
public void newUser(String playerName) {
|
|
|
- try {
|
|
|
- // Open the file to write the player
|
|
|
- BufferedWriter out = new BufferedWriter(new FileWriter(mcMMO.getUsersFilePath(), true));
|
|
|
-
|
|
|
- // Add the player to the end
|
|
|
- out.append(playerName).append(":");
|
|
|
- out.append("0:"); // Mining
|
|
|
- out.append(":");
|
|
|
- out.append(":");
|
|
|
- out.append("0:"); // Xp
|
|
|
- out.append("0:"); // Woodcutting
|
|
|
- out.append("0:"); // WoodCuttingXp
|
|
|
- out.append("0:"); // Repair
|
|
|
- out.append("0:"); // Unarmed
|
|
|
- out.append("0:"); // Herbalism
|
|
|
- out.append("0:"); // Excavation
|
|
|
- out.append("0:"); // Archery
|
|
|
- out.append("0:"); // Swords
|
|
|
- out.append("0:"); // Axes
|
|
|
- out.append("0:"); // Acrobatics
|
|
|
- out.append("0:"); // RepairXp
|
|
|
- out.append("0:"); // UnarmedXp
|
|
|
- out.append("0:"); // HerbalismXp
|
|
|
- out.append("0:"); // ExcavationXp
|
|
|
- out.append("0:"); // ArcheryXp
|
|
|
- out.append("0:"); // SwordsXp
|
|
|
- out.append("0:"); // AxesXp
|
|
|
- out.append("0:"); // AcrobaticsXp
|
|
|
- out.append(":");
|
|
|
- out.append("0:"); // Taming
|
|
|
- out.append("0:"); // TamingXp
|
|
|
- out.append("0:"); // DATS
|
|
|
- out.append("0:"); // DATS
|
|
|
- out.append("0:"); // DATS
|
|
|
- out.append("0:"); // DATS
|
|
|
- out.append("0:"); // DATS
|
|
|
- out.append("0:"); // DATS
|
|
|
- out.append("0:"); // DATS
|
|
|
- out.append("STANDARD").append(":"); // HUD
|
|
|
- out.append("0:"); // Fishing
|
|
|
- out.append("0:"); // FishingXp
|
|
|
- out.append("0:"); // Blast Mining
|
|
|
- out.append(String.valueOf(System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR)).append(":"); // LastLogin
|
|
|
- out.append(Config.getInstance().getMobHealthbarDefault().toString()).append(":"); // Mob Healthbar HUD
|
|
|
-
|
|
|
- // Add more in the same format as the line above
|
|
|
-
|
|
|
- out.newLine();
|
|
|
- out.close();
|
|
|
- }
|
|
|
- catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ BufferedWriter out = null;
|
|
|
+ synchronized (fileWritingLock) {
|
|
|
+ try {
|
|
|
+ // Open the file to write the player
|
|
|
+ out = new BufferedWriter(new FileWriter(mcMMO.getUsersFilePath(), true));
|
|
|
+
|
|
|
+ // Add the player to the end
|
|
|
+ out.append(playerName).append(":");
|
|
|
+ out.append("0:"); // Mining
|
|
|
+ out.append(":");
|
|
|
+ out.append(":");
|
|
|
+ out.append("0:"); // Xp
|
|
|
+ out.append("0:"); // Woodcutting
|
|
|
+ out.append("0:"); // WoodCuttingXp
|
|
|
+ out.append("0:"); // Repair
|
|
|
+ out.append("0:"); // Unarmed
|
|
|
+ out.append("0:"); // Herbalism
|
|
|
+ out.append("0:"); // Excavation
|
|
|
+ out.append("0:"); // Archery
|
|
|
+ out.append("0:"); // Swords
|
|
|
+ out.append("0:"); // Axes
|
|
|
+ out.append("0:"); // Acrobatics
|
|
|
+ out.append("0:"); // RepairXp
|
|
|
+ out.append("0:"); // UnarmedXp
|
|
|
+ out.append("0:"); // HerbalismXp
|
|
|
+ out.append("0:"); // ExcavationXp
|
|
|
+ out.append("0:"); // ArcheryXp
|
|
|
+ out.append("0:"); // SwordsXp
|
|
|
+ out.append("0:"); // AxesXp
|
|
|
+ out.append("0:"); // AcrobaticsXp
|
|
|
+ out.append(":");
|
|
|
+ out.append("0:"); // Taming
|
|
|
+ out.append("0:"); // TamingXp
|
|
|
+ out.append("0:"); // DATS
|
|
|
+ out.append("0:"); // DATS
|
|
|
+ out.append("0:"); // DATS
|
|
|
+ out.append("0:"); // DATS
|
|
|
+ out.append("0:"); // DATS
|
|
|
+ out.append("0:"); // DATS
|
|
|
+ out.append("0:"); // DATS
|
|
|
+ out.append("STANDARD").append(":"); // HUD
|
|
|
+ out.append("0:"); // Fishing
|
|
|
+ out.append("0:"); // FishingXp
|
|
|
+ out.append("0:"); // Blast Mining
|
|
|
+ out.append(String.valueOf(System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR)).append(":"); // LastLogin
|
|
|
+ out.append(Config.getInstance().getMobHealthbarDefault().toString()).append(":"); // Mob Healthbar HUD
|
|
|
+
|
|
|
+ // Add more in the same format as the line above
|
|
|
+
|
|
|
+ out.newLine();
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ tryClose(out);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public List<String> loadPlayerData(String playerName) {
|
|
|
- List<String> playerData = new ArrayList<String>();
|
|
|
- try {
|
|
|
- // Open the user file
|
|
|
- FileReader file = new FileReader(mcMMO.getUsersFilePath());
|
|
|
- BufferedReader in = new BufferedReader(file);
|
|
|
- String line;
|
|
|
+ public PlayerProfile loadPlayerProfile(String playerName, boolean create) {
|
|
|
+ BufferedReader in = null;
|
|
|
+ String usersFilePath = mcMMO.getUsersFilePath();
|
|
|
|
|
|
- while ((line = in.readLine()) != null) {
|
|
|
- // Find if the line contains the player we want.
|
|
|
- String[] character = line.split(":");
|
|
|
+ synchronized (fileWritingLock) {
|
|
|
+ try {
|
|
|
+ // Open the user file
|
|
|
+ in = new BufferedReader(new FileReader(usersFilePath));
|
|
|
+ String line;
|
|
|
|
|
|
- if (!character[0].equalsIgnoreCase(playerName)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
+ // Find if the line contains the player we want.
|
|
|
+ String[] character = line.split(":");
|
|
|
|
|
|
- // Skill levels
|
|
|
- playerData.add(character[24]); // Taming
|
|
|
- playerData.add(character[1]); // Mining
|
|
|
- playerData.add(character[7]); // Repair
|
|
|
- playerData.add(character[5]); // Woodcutting
|
|
|
- playerData.add(character[8]); // Unarmed
|
|
|
- playerData.add(character[9]); // Herbalism
|
|
|
- playerData.add(character[10]); // Excavation
|
|
|
- playerData.add(character[11]); // Archery
|
|
|
- playerData.add(character[12]); // Swords
|
|
|
- playerData.add(character[13]); // Axes
|
|
|
- playerData.add(character[14]); // Acrobatics
|
|
|
- playerData.add(character[34]); // Fishing
|
|
|
-
|
|
|
- // Experience
|
|
|
- playerData.add(character[25]); // Taming
|
|
|
- playerData.add(character[4]); // Mining
|
|
|
- playerData.add(character[15]); // Repair
|
|
|
- playerData.add(character[6]); // Woodcutting
|
|
|
- playerData.add(character[16]); // Unarmed
|
|
|
- playerData.add(character[17]); // Herbalism
|
|
|
- playerData.add(character[18]); // Excavation
|
|
|
- playerData.add(character[19]); // Archery
|
|
|
- playerData.add(character[20]); // Swords
|
|
|
- playerData.add(character[21]); // Axes
|
|
|
- playerData.add(character[22]); // Acrobatics
|
|
|
- playerData.add(character[35]); // Fishing
|
|
|
-
|
|
|
- // Cooldowns
|
|
|
- playerData.add(null); // Taming
|
|
|
- playerData.add(character[32]); // SuperBreaker
|
|
|
- playerData.add(null); // Repair
|
|
|
- playerData.add(character[28]); // Tree Feller
|
|
|
- playerData.add(character[26]); // Beserk
|
|
|
- playerData.add(character[29]); // Green Terra
|
|
|
- playerData.add(character[27]); // Giga Drill Breaker
|
|
|
- playerData.add(null); // Archery
|
|
|
- playerData.add(character[30]); // Serrated Strikes
|
|
|
- playerData.add(character[31]); // Skull Splitter
|
|
|
- playerData.add(null); // Acrobatics
|
|
|
- playerData.add(character[36]); // Blast Mining
|
|
|
-
|
|
|
- playerData.add(character.length > 33 ? character[33] : null); // HudType
|
|
|
- playerData.add(character.length > 38 ? character[38] : null); // MobHealthBar
|
|
|
- }
|
|
|
+ if (!character[0].equalsIgnoreCase(playerName)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- in.close();
|
|
|
- }
|
|
|
- catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ PlayerProfile p = loadFromLine(character);
|
|
|
+ in.close();
|
|
|
+ return p;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ tryClose(in);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- return playerData;
|
|
|
+ if (create) {
|
|
|
+ newUser(playerName);
|
|
|
+ return new PlayerProfile(playerName, true);
|
|
|
+ }
|
|
|
+ return new PlayerProfile(playerName);
|
|
|
}
|
|
|
|
|
|
- public boolean convert(String[] character) throws Exception {
|
|
|
- // Not implemented
|
|
|
- return false;
|
|
|
+ public void convertUsers(DatabaseManager destination) {
|
|
|
+ BufferedReader in = null;
|
|
|
+ String usersFilePath = mcMMO.getUsersFilePath();
|
|
|
+
|
|
|
+ synchronized (fileWritingLock) {
|
|
|
+ try {
|
|
|
+ // Open the user file
|
|
|
+ in = new BufferedReader(new FileReader(usersFilePath));
|
|
|
+ String line;
|
|
|
+
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
+ String[] character = line.split(":");
|
|
|
+
|
|
|
+ try {
|
|
|
+ destination.saveUser(loadFromLine(character));
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ tryClose(in);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public boolean checkConnected() {
|
|
|
// Not implemented
|
|
|
- return false;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<String> getStoredUsers() {
|
|
|
+ ArrayList<String> users = new ArrayList<String>();
|
|
|
+ BufferedReader in = null;
|
|
|
+ String usersFilePath = mcMMO.getUsersFilePath();
|
|
|
+
|
|
|
+ synchronized (fileWritingLock) {
|
|
|
+ try {
|
|
|
+ // Open the user file
|
|
|
+ in = new BufferedReader(new FileReader(usersFilePath));
|
|
|
+ String line;
|
|
|
+
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
+ String[] character = line.split(":");
|
|
|
+ users.add(character[0]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ tryClose(in);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return users;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
-* Update the leader boards.
|
|
|
-*/
|
|
|
+ * Update the leader boards.
|
|
|
+ */
|
|
|
private void updateLeaderboards() {
|
|
|
// Only update FFS leaderboards every 10 minutes.. this puts a lot of strain on the server (depending on the size of the database) and should not be done frequently
|
|
|
if (System.currentTimeMillis() < lastUpdate + UPDATE_WAIT_TIME) {
|
|
@@ -402,43 +479,50 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
|
|
List<PlayerStat> taming = new ArrayList<PlayerStat>();
|
|
|
List<PlayerStat> fishing = new ArrayList<PlayerStat>();
|
|
|
|
|
|
+ BufferedReader in = null;
|
|
|
// Read from the FlatFile database and fill our arrays with information
|
|
|
- try {
|
|
|
- BufferedReader in = new BufferedReader(new FileReader(usersFilePath));
|
|
|
- String line = "";
|
|
|
- ArrayList<String> players = new ArrayList<String>();
|
|
|
-
|
|
|
- while ((line = in.readLine()) != null) {
|
|
|
- String[] data = line.split(":");
|
|
|
- String playerName = data[0];
|
|
|
- int powerLevel = 0;
|
|
|
-
|
|
|
- // Prevent the same player from being added multiple times (I'd like to note that this shouldn't happen...)
|
|
|
- if (players.contains(playerName)) {
|
|
|
- continue;
|
|
|
+ synchronized (fileWritingLock) {
|
|
|
+ try {
|
|
|
+ in = new BufferedReader(new FileReader(usersFilePath));
|
|
|
+ String line = "";
|
|
|
+ ArrayList<String> players = new ArrayList<String>();
|
|
|
+
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
+ String[] data = line.split(":");
|
|
|
+ String playerName = data[0];
|
|
|
+ int powerLevel = 0;
|
|
|
+
|
|
|
+ // Prevent the same player from being added multiple times (I'd like to note that this shouldn't happen...)
|
|
|
+ if (players.contains(playerName)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ players.add(playerName);
|
|
|
+
|
|
|
+ Map<SkillType, Integer> skills = getSkillMapFromLine(data);
|
|
|
+
|
|
|
+ powerLevel += putStat(acrobatics, playerName, skills.get(SkillType.ACROBATICS));
|
|
|
+ powerLevel += putStat(archery, playerName, skills.get(SkillType.ARCHERY));
|
|
|
+ powerLevel += putStat(axes, playerName, skills.get(SkillType.AXES));
|
|
|
+ powerLevel += putStat(excavation, playerName, skills.get(SkillType.EXCAVATION));
|
|
|
+ powerLevel += putStat(fishing, playerName, skills.get(SkillType.FISHING));
|
|
|
+ powerLevel += putStat(herbalism, playerName, skills.get(SkillType.HERBALISM));
|
|
|
+ powerLevel += putStat(mining, playerName, skills.get(SkillType.MINING));
|
|
|
+ powerLevel += putStat(repair, playerName, skills.get(SkillType.REPAIR));
|
|
|
+ powerLevel += putStat(swords, playerName, skills.get(SkillType.SWORDS));
|
|
|
+ powerLevel += putStat(taming, playerName, skills.get(SkillType.TAMING));
|
|
|
+ powerLevel += putStat(unarmed, playerName, skills.get(SkillType.UNARMED));
|
|
|
+ powerLevel += putStat(woodcutting, playerName, skills.get(SkillType.WOODCUTTING));
|
|
|
+
|
|
|
+ putStat(powerLevels, playerName, powerLevel);
|
|
|
}
|
|
|
-
|
|
|
- players.add(playerName);
|
|
|
-
|
|
|
- powerLevel += loadStat(mining, playerName, data, 1);
|
|
|
- powerLevel += loadStat(woodcutting, playerName, data, 5);
|
|
|
- powerLevel += loadStat(repair, playerName, data, 7);
|
|
|
- powerLevel += loadStat(unarmed, playerName, data, 8);
|
|
|
- powerLevel += loadStat(herbalism, playerName, data, 9);
|
|
|
- powerLevel += loadStat(excavation, playerName, data, 10);
|
|
|
- powerLevel += loadStat(archery, playerName, data, 11);
|
|
|
- powerLevel += loadStat(swords, playerName, data, 12);
|
|
|
- powerLevel += loadStat(axes, playerName, data, 13);
|
|
|
- powerLevel += loadStat(acrobatics, playerName, data, 14);
|
|
|
- powerLevel += loadStat(taming, playerName, data, 24);
|
|
|
- powerLevel += loadStat(fishing, playerName, data, 34);
|
|
|
-
|
|
|
- powerLevels.add(new PlayerStat(playerName, powerLevel));
|
|
|
}
|
|
|
- in.close();
|
|
|
- }
|
|
|
- catch (Exception e) {
|
|
|
- mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
|
|
|
+ catch (Exception e) {
|
|
|
+ mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ tryClose(in);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
SkillComparator c = new SkillComparator();
|
|
@@ -487,6 +571,15 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void tryClose(Closeable c) {
|
|
|
+ if (c == null) return;
|
|
|
+ try {
|
|
|
+ c.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private Integer getPlayerRank(String playerName, List<PlayerStat> statsList) {
|
|
|
if (statsList == null) {
|
|
|
return null;
|
|
@@ -505,14 +598,8 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- private int loadStat(List<PlayerStat> statList, String playerName, String[] data, int dataIndex) {
|
|
|
- if (data.length <= dataIndex) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- int statValue = Integer.parseInt(data[dataIndex]);
|
|
|
+ private int putStat(List<PlayerStat> statList, String playerName, int statValue) {
|
|
|
statList.add(new PlayerStat(playerName, statValue));
|
|
|
-
|
|
|
return statValue;
|
|
|
}
|
|
|
|
|
@@ -522,4 +609,75 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
|
|
|
return (o2.statVal - o1.statVal);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private PlayerProfile loadFromLine(String[] character) throws Exception {
|
|
|
+ Map<SkillType, Integer> skills = getSkillMapFromLine(character); // Skill levels
|
|
|
+ Map<SkillType, Float> skillsXp = new HashMap<SkillType, Float>(); // Skill & XP
|
|
|
+ Map<AbilityType, Integer> skillsDATS = new HashMap<AbilityType, Integer>(); // Ability & Cooldown
|
|
|
+ HudType hudType;
|
|
|
+ MobHealthbarType mobHealthbarType;
|
|
|
+
|
|
|
+ // TODO on updates, put new values in a try{} ?
|
|
|
+
|
|
|
+ skillsXp.put(SkillType.TAMING, (float) Integer.valueOf(character[25]));
|
|
|
+ skillsXp.put(SkillType.MINING, (float) Integer.valueOf(character[4]));
|
|
|
+ skillsXp.put(SkillType.REPAIR, (float) Integer.valueOf(character[15]));
|
|
|
+ skillsXp.put(SkillType.WOODCUTTING, (float) Integer.valueOf(character[6]));
|
|
|
+ skillsXp.put(SkillType.UNARMED, (float) Integer.valueOf(character[16]));
|
|
|
+ skillsXp.put(SkillType.HERBALISM, (float) Integer.valueOf(character[17]));
|
|
|
+ skillsXp.put(SkillType.EXCAVATION, (float) Integer.valueOf(character[18]));
|
|
|
+ skillsXp.put(SkillType.ARCHERY, (float) Integer.valueOf(character[19]));
|
|
|
+ skillsXp.put(SkillType.SWORDS, (float) Integer.valueOf(character[20]));
|
|
|
+ skillsXp.put(SkillType.AXES, (float) Integer.valueOf(character[21]));
|
|
|
+ skillsXp.put(SkillType.ACROBATICS, (float) Integer.valueOf(character[22]));
|
|
|
+ skillsXp.put(SkillType.FISHING, (float) Integer.valueOf(character[35]));
|
|
|
+
|
|
|
+ // Taming - Unused
|
|
|
+ skillsDATS.put(AbilityType.SUPER_BREAKER, Integer.valueOf(character[32]));
|
|
|
+ // Repair - Unused
|
|
|
+ skillsDATS.put(AbilityType.TREE_FELLER, Integer.valueOf(character[28]));
|
|
|
+ skillsDATS.put(AbilityType.BERSERK, Integer.valueOf(character[26]));
|
|
|
+ skillsDATS.put(AbilityType.GREEN_TERRA, Integer.valueOf(character[29]));
|
|
|
+ skillsDATS.put(AbilityType.GIGA_DRILL_BREAKER, Integer.valueOf(character[27]));
|
|
|
+ // Archery - Unused
|
|
|
+ skillsDATS.put(AbilityType.SERRATED_STRIKES, Integer.valueOf(character[30]));
|
|
|
+ skillsDATS.put(AbilityType.SKULL_SPLITTER, Integer.valueOf(character[31]));
|
|
|
+ // Acrobatics - Unused
|
|
|
+ skillsDATS.put(AbilityType.BLAST_MINING, Integer.valueOf(character[36]));
|
|
|
+
|
|
|
+ try {
|
|
|
+ hudType = HudType.valueOf(character[33]);
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ hudType = HudType.STANDARD; // Shouldn't happen unless database is being tampered with
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ mobHealthbarType = MobHealthbarType.valueOf(character[38]);
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ mobHealthbarType = Config.getInstance().getMobHealthbarDefault();
|
|
|
+ }
|
|
|
+
|
|
|
+ return new PlayerProfile(character[0], skills, skillsXp, skillsDATS, hudType, mobHealthbarType);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<SkillType, Integer> getSkillMapFromLine(String[] character) {
|
|
|
+ Map<SkillType, Integer> skills = new HashMap<SkillType, Integer>(); // Skill & Level
|
|
|
+
|
|
|
+ skills.put(SkillType.TAMING, Integer.valueOf(character[24]));
|
|
|
+ skills.put(SkillType.MINING, Integer.valueOf(character[1]));
|
|
|
+ skills.put(SkillType.REPAIR, Integer.valueOf(character[7]));
|
|
|
+ skills.put(SkillType.WOODCUTTING, Integer.valueOf(character[5]));
|
|
|
+ skills.put(SkillType.UNARMED, Integer.valueOf(character[8]));
|
|
|
+ skills.put(SkillType.HERBALISM, Integer.valueOf(character[9]));
|
|
|
+ skills.put(SkillType.EXCAVATION, Integer.valueOf(character[10]));
|
|
|
+ skills.put(SkillType.ARCHERY, Integer.valueOf(character[11]));
|
|
|
+ skills.put(SkillType.SWORDS, Integer.valueOf(character[12]));
|
|
|
+ skills.put(SkillType.AXES, Integer.valueOf(character[13]));
|
|
|
+ skills.put(SkillType.ACROBATICS, Integer.valueOf(character[14]));
|
|
|
+ skills.put(SkillType.FISHING, Integer.valueOf(character[34]));
|
|
|
+
|
|
|
+ return skills;
|
|
|
+ }
|
|
|
}
|