FlatfileDatabaseManager.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. package com.gmail.nossr50.database;
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.Closeable;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.util.ArrayList;
  10. import java.util.Collections;
  11. import java.util.Comparator;
  12. import java.util.HashMap;
  13. import java.util.List;
  14. import java.util.Map;
  15. import org.bukkit.Bukkit;
  16. import org.bukkit.OfflinePlayer;
  17. import com.gmail.nossr50.mcMMO;
  18. import com.gmail.nossr50.config.Config;
  19. import com.gmail.nossr50.datatypes.MobHealthbarType;
  20. import com.gmail.nossr50.datatypes.database.PlayerStat;
  21. import com.gmail.nossr50.datatypes.player.PlayerProfile;
  22. import com.gmail.nossr50.datatypes.skills.AbilityType;
  23. import com.gmail.nossr50.datatypes.skills.SkillType;
  24. import com.gmail.nossr50.datatypes.spout.huds.HudType;
  25. import com.gmail.nossr50.util.Misc;
  26. import com.gmail.nossr50.util.StringUtils;
  27. public final class FlatfileDatabaseManager implements DatabaseManager {
  28. private final HashMap<SkillType, List<PlayerStat>> playerStatHash = new HashMap<SkillType, List<PlayerStat>>();
  29. private final List<PlayerStat> powerLevels = new ArrayList<PlayerStat>();
  30. private long lastUpdate = 0;
  31. private final long UPDATE_WAIT_TIME = 600000L; // 10 minutes
  32. private final File usersFile;
  33. private static final Object fileWritingLock = new Object();
  34. protected FlatfileDatabaseManager() {
  35. usersFile = new File(mcMMO.getUsersFilePath());
  36. checkStructure();
  37. updateLeaderboards();
  38. }
  39. public void purgePowerlessUsers() {
  40. int purgedUsers = 0;
  41. mcMMO.p.getLogger().info("Purging powerless users...");
  42. BufferedReader in = null;
  43. FileWriter out = null;
  44. String usersFilePath = mcMMO.getUsersFilePath();
  45. // This code is O(n) instead of O(n²)
  46. synchronized (fileWritingLock) {
  47. try {
  48. in = new BufferedReader(new FileReader(usersFilePath));
  49. StringBuilder writer = new StringBuilder();
  50. String line = "";
  51. while ((line = in.readLine()) != null) {
  52. String[] character = line.split(":");
  53. Map<SkillType, Integer> skills = getSkillMapFromLine(character);
  54. boolean powerless = true;
  55. for (int skill : skills.values()) {
  56. if (skill != 0) {
  57. powerless = false;
  58. break;
  59. }
  60. }
  61. // If they're still around, rewrite them to the file.
  62. if (!powerless) {
  63. writer.append(line).append("\r\n");
  64. }
  65. else {
  66. purgedUsers++;
  67. Misc.profileCleanup(character[0]);
  68. }
  69. }
  70. // Write the new file
  71. out = new FileWriter(usersFilePath);
  72. out.write(writer.toString());
  73. }
  74. catch (IOException e) {
  75. mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
  76. }
  77. finally {
  78. tryClose(in);
  79. tryClose(out);
  80. }
  81. }
  82. mcMMO.p.getLogger().info("Purged " + purgedUsers + " users from the database.");
  83. }
  84. public void purgeOldUsers() {
  85. int removedPlayers = 0;
  86. long currentTime = System.currentTimeMillis();
  87. mcMMO.p.getLogger().info("Purging old users...");
  88. BufferedReader in = null;
  89. FileWriter out = null;
  90. String usersFilePath = mcMMO.getUsersFilePath();
  91. // This code is O(n) instead of O(n²)
  92. synchronized (fileWritingLock) {
  93. try {
  94. in = new BufferedReader(new FileReader(usersFilePath));
  95. StringBuilder writer = new StringBuilder();
  96. String line = "";
  97. while ((line = in.readLine()) != null) {
  98. // Length checks depend on last character being ':'
  99. if (line.charAt(line.length() - 1) != ':') {
  100. line = line + ":";
  101. }
  102. String[] character = line.split(":");
  103. String name = character[0];
  104. long lastPlayed = 0;
  105. boolean rewrite = false;
  106. try {
  107. lastPlayed = Long.parseLong(character[37]) * Misc.TIME_CONVERSION_FACTOR;
  108. } catch (NumberFormatException e) {
  109. }
  110. if (lastPlayed == 0) {
  111. OfflinePlayer player = Bukkit.getOfflinePlayer(name);
  112. lastPlayed = player.getLastPlayed();
  113. rewrite = true;
  114. }
  115. if (currentTime - lastPlayed > PURGE_TIME) {
  116. removedPlayers++;
  117. Misc.profileCleanup(name);
  118. }
  119. else {
  120. if (rewrite) {
  121. // Rewrite their data with a valid time
  122. character[37] = Long.toString(lastPlayed);
  123. String newLine = org.apache.commons.lang.StringUtils.join(character, ":");
  124. writer.append(newLine).append("\r\n");
  125. }
  126. else {
  127. writer.append(line).append("\r\n");
  128. }
  129. }
  130. }
  131. // Write the new file
  132. out = new FileWriter(usersFilePath);
  133. out.write(writer.toString());
  134. }
  135. catch (IOException e) {
  136. mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
  137. }
  138. finally {
  139. tryClose(in);
  140. tryClose(out);
  141. }
  142. }
  143. mcMMO.p.getLogger().info("Purged " + removedPlayers + " users from the database.");
  144. }
  145. public boolean removeUser(String playerName) {
  146. boolean worked = false;
  147. BufferedReader in = null;
  148. FileWriter out = null;
  149. String usersFilePath = mcMMO.getUsersFilePath();
  150. synchronized (fileWritingLock) {
  151. try {
  152. in = new BufferedReader(new FileReader(usersFilePath));
  153. StringBuilder writer = new StringBuilder();
  154. String line = "";
  155. while ((line = in.readLine()) != null) {
  156. // Write out the same file but when we get to the player we want to remove, we skip his line.
  157. if (!worked && line.split(":")[0].equalsIgnoreCase(playerName)) {
  158. mcMMO.p.getLogger().info("User found, removing...");
  159. worked = true;
  160. continue; // Skip the player
  161. }
  162. writer.append(line).append("\r\n");
  163. }
  164. out = new FileWriter(usersFilePath); // Write out the new file
  165. out.write(writer.toString());
  166. }
  167. catch (Exception e) {
  168. mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
  169. }
  170. finally {
  171. tryClose(in);
  172. tryClose(out);
  173. }
  174. }
  175. Misc.profileCleanup(playerName);
  176. return worked;
  177. }
  178. public void saveUser(PlayerProfile profile) {
  179. String playerName = profile.getPlayerName();
  180. BufferedReader in = null;
  181. FileWriter out = null;
  182. String usersFilePath = mcMMO.getUsersFilePath();
  183. synchronized (fileWritingLock) {
  184. try {
  185. // Open the file
  186. in = new BufferedReader(new FileReader(usersFilePath));
  187. StringBuilder writer = new StringBuilder();
  188. String line;
  189. // While not at the end of the file
  190. while ((line = in.readLine()) != null) {
  191. // Read the line in and copy it to the output it's not the player we want to edit
  192. if (!line.split(":")[0].equalsIgnoreCase(playerName)) {
  193. writer.append(line).append("\r\n");
  194. }
  195. else {
  196. // Otherwise write the new player information
  197. writer.append(playerName).append(":");
  198. writer.append(profile.getSkillLevel(SkillType.MINING)).append(":");
  199. writer.append(":");
  200. writer.append(":");
  201. writer.append(profile.getSkillXpLevel(SkillType.MINING)).append(":");
  202. writer.append(profile.getSkillLevel(SkillType.WOODCUTTING)).append(":");
  203. writer.append(profile.getSkillXpLevel(SkillType.WOODCUTTING)).append(":");
  204. writer.append(profile.getSkillLevel(SkillType.REPAIR)).append(":");
  205. writer.append(profile.getSkillLevel(SkillType.UNARMED)).append(":");
  206. writer.append(profile.getSkillLevel(SkillType.HERBALISM)).append(":");
  207. writer.append(profile.getSkillLevel(SkillType.EXCAVATION)).append(":");
  208. writer.append(profile.getSkillLevel(SkillType.ARCHERY)).append(":");
  209. writer.append(profile.getSkillLevel(SkillType.SWORDS)).append(":");
  210. writer.append(profile.getSkillLevel(SkillType.AXES)).append(":");
  211. writer.append(profile.getSkillLevel(SkillType.ACROBATICS)).append(":");
  212. writer.append(profile.getSkillXpLevel(SkillType.REPAIR)).append(":");
  213. writer.append(profile.getSkillXpLevel(SkillType.UNARMED)).append(":");
  214. writer.append(profile.getSkillXpLevel(SkillType.HERBALISM)).append(":");
  215. writer.append(profile.getSkillXpLevel(SkillType.EXCAVATION)).append(":");
  216. writer.append(profile.getSkillXpLevel(SkillType.ARCHERY)).append(":");
  217. writer.append(profile.getSkillXpLevel(SkillType.SWORDS)).append(":");
  218. writer.append(profile.getSkillXpLevel(SkillType.AXES)).append(":");
  219. writer.append(profile.getSkillXpLevel(SkillType.ACROBATICS)).append(":");
  220. writer.append(":");
  221. writer.append(profile.getSkillLevel(SkillType.TAMING)).append(":");
  222. writer.append(profile.getSkillXpLevel(SkillType.TAMING)).append(":");
  223. writer.append((int) profile.getSkillDATS(AbilityType.BERSERK)).append(":");
  224. writer.append((int) profile.getSkillDATS(AbilityType.GIGA_DRILL_BREAKER)).append(":");
  225. writer.append((int) profile.getSkillDATS(AbilityType.TREE_FELLER)).append(":");
  226. writer.append((int) profile.getSkillDATS(AbilityType.GREEN_TERRA)).append(":");
  227. writer.append((int) profile.getSkillDATS(AbilityType.SERRATED_STRIKES)).append(":");
  228. writer.append((int) profile.getSkillDATS(AbilityType.SKULL_SPLITTER)).append(":");
  229. writer.append((int) profile.getSkillDATS(AbilityType.SUPER_BREAKER)).append(":");
  230. HudType hudType = profile.getHudType();
  231. writer.append(hudType == null ? "STANDARD" : hudType.toString()).append(":");
  232. writer.append(profile.getSkillLevel(SkillType.FISHING)).append(":");
  233. writer.append(profile.getSkillXpLevel(SkillType.FISHING)).append(":");
  234. writer.append((int) profile.getSkillDATS(AbilityType.BLAST_MINING)).append(":");
  235. writer.append(System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR).append(":");
  236. MobHealthbarType mobHealthbarType = profile.getMobHealthbarType();
  237. writer.append(mobHealthbarType == null ? Config.getInstance().getMobHealthbarDefault().toString() : mobHealthbarType.toString()).append(":");
  238. writer.append("\r\n");
  239. }
  240. }
  241. // Write the new file
  242. out = new FileWriter(usersFilePath);
  243. out.write(writer.toString());
  244. }
  245. catch (Exception e) {
  246. e.printStackTrace();
  247. }
  248. finally {
  249. tryClose(in);
  250. tryClose(out);
  251. }
  252. }
  253. }
  254. public List<PlayerStat> readLeaderboard(String skillName, int pageNumber, int statsPerPage) {
  255. updateLeaderboards();
  256. List<PlayerStat> statsList = skillName.equalsIgnoreCase("all") ? powerLevels : playerStatHash.get(SkillType.getSkill(skillName));
  257. int fromIndex = (Math.max(pageNumber, 1) - 1) * statsPerPage;
  258. return statsList.subList(Math.min(fromIndex, statsList.size()), Math.min(fromIndex + statsPerPage, statsList.size()));
  259. }
  260. public Map<String, Integer> readRank(String playerName) {
  261. updateLeaderboards();
  262. Map<String, Integer> skills = new HashMap<String, Integer>();
  263. for (SkillType skill : SkillType.nonChildSkills()) {
  264. skills.put(skill.name(), getPlayerRank(playerName, playerStatHash.get(skill)));
  265. }
  266. skills.put("ALL", getPlayerRank(playerName, powerLevels));
  267. return skills;
  268. }
  269. public void newUser(String playerName) {
  270. BufferedWriter out = null;
  271. synchronized (fileWritingLock) {
  272. try {
  273. // Open the file to write the player
  274. out = new BufferedWriter(new FileWriter(mcMMO.getUsersFilePath(), true));
  275. // Add the player to the end
  276. out.append(playerName).append(":");
  277. out.append("0:"); // Mining
  278. out.append(":");
  279. out.append(":");
  280. out.append("0:"); // Xp
  281. out.append("0:"); // Woodcutting
  282. out.append("0:"); // WoodCuttingXp
  283. out.append("0:"); // Repair
  284. out.append("0:"); // Unarmed
  285. out.append("0:"); // Herbalism
  286. out.append("0:"); // Excavation
  287. out.append("0:"); // Archery
  288. out.append("0:"); // Swords
  289. out.append("0:"); // Axes
  290. out.append("0:"); // Acrobatics
  291. out.append("0:"); // RepairXp
  292. out.append("0:"); // UnarmedXp
  293. out.append("0:"); // HerbalismXp
  294. out.append("0:"); // ExcavationXp
  295. out.append("0:"); // ArcheryXp
  296. out.append("0:"); // SwordsXp
  297. out.append("0:"); // AxesXp
  298. out.append("0:"); // AcrobaticsXp
  299. out.append(":");
  300. out.append("0:"); // Taming
  301. out.append("0:"); // TamingXp
  302. out.append("0:"); // DATS
  303. out.append("0:"); // DATS
  304. out.append("0:"); // DATS
  305. out.append("0:"); // DATS
  306. out.append("0:"); // DATS
  307. out.append("0:"); // DATS
  308. out.append("0:"); // DATS
  309. out.append("STANDARD").append(":"); // HUD
  310. out.append("0:"); // Fishing
  311. out.append("0:"); // FishingXp
  312. out.append("0:"); // Blast Mining
  313. out.append(String.valueOf(System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR)).append(":"); // LastLogin
  314. out.append(Config.getInstance().getMobHealthbarDefault().toString()).append(":"); // Mob Healthbar HUD
  315. // Add more in the same format as the line above
  316. out.newLine();
  317. }
  318. catch (Exception e) {
  319. e.printStackTrace();
  320. }
  321. finally {
  322. tryClose(out);
  323. }
  324. }
  325. }
  326. public PlayerProfile loadPlayerProfile(String playerName, boolean create) {
  327. BufferedReader in = null;
  328. String usersFilePath = mcMMO.getUsersFilePath();
  329. synchronized (fileWritingLock) {
  330. try {
  331. // Open the user file
  332. in = new BufferedReader(new FileReader(usersFilePath));
  333. String line;
  334. while ((line = in.readLine()) != null) {
  335. // Find if the line contains the player we want.
  336. String[] character = line.split(":");
  337. if (!character[0].equalsIgnoreCase(playerName)) {
  338. continue;
  339. }
  340. PlayerProfile p = loadFromLine(character);
  341. in.close();
  342. return p;
  343. }
  344. }
  345. catch (Exception e) {
  346. e.printStackTrace();
  347. }
  348. finally {
  349. tryClose(in);
  350. }
  351. }
  352. if (create) {
  353. newUser(playerName);
  354. return new PlayerProfile(playerName, true);
  355. }
  356. return new PlayerProfile(playerName);
  357. }
  358. public void convertUsers(DatabaseManager destination) {
  359. BufferedReader in = null;
  360. String usersFilePath = mcMMO.getUsersFilePath();
  361. synchronized (fileWritingLock) {
  362. try {
  363. // Open the user file
  364. in = new BufferedReader(new FileReader(usersFilePath));
  365. String line;
  366. while ((line = in.readLine()) != null) {
  367. String[] character = line.split(":");
  368. try {
  369. destination.saveUser(loadFromLine(character));
  370. }
  371. catch (Exception e) {
  372. e.printStackTrace();
  373. }
  374. }
  375. }
  376. catch (Exception e) {
  377. e.printStackTrace();
  378. }
  379. finally {
  380. tryClose(in);
  381. }
  382. }
  383. }
  384. public boolean checkConnected() {
  385. // Not implemented
  386. return true;
  387. }
  388. public List<String> getStoredUsers() {
  389. ArrayList<String> users = new ArrayList<String>();
  390. BufferedReader in = null;
  391. String usersFilePath = mcMMO.getUsersFilePath();
  392. synchronized (fileWritingLock) {
  393. try {
  394. // Open the user file
  395. in = new BufferedReader(new FileReader(usersFilePath));
  396. String line;
  397. while ((line = in.readLine()) != null) {
  398. String[] character = line.split(":");
  399. users.add(character[0]);
  400. }
  401. }
  402. catch (Exception e) {
  403. e.printStackTrace();
  404. }
  405. finally {
  406. tryClose(in);
  407. }
  408. }
  409. return users;
  410. }
  411. /**
  412. * Update the leader boards.
  413. */
  414. private void updateLeaderboards() {
  415. // 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
  416. if (System.currentTimeMillis() < lastUpdate + UPDATE_WAIT_TIME) {
  417. return;
  418. }
  419. String usersFilePath = mcMMO.getUsersFilePath();
  420. lastUpdate = System.currentTimeMillis(); // Log when the last update was run
  421. powerLevels.clear(); // Clear old values from the power levels
  422. // Initialize lists
  423. List<PlayerStat> mining = new ArrayList<PlayerStat>();
  424. List<PlayerStat> woodcutting = new ArrayList<PlayerStat>();
  425. List<PlayerStat> herbalism = new ArrayList<PlayerStat>();
  426. List<PlayerStat> excavation = new ArrayList<PlayerStat>();
  427. List<PlayerStat> acrobatics = new ArrayList<PlayerStat>();
  428. List<PlayerStat> repair = new ArrayList<PlayerStat>();
  429. List<PlayerStat> swords = new ArrayList<PlayerStat>();
  430. List<PlayerStat> axes = new ArrayList<PlayerStat>();
  431. List<PlayerStat> archery = new ArrayList<PlayerStat>();
  432. List<PlayerStat> unarmed = new ArrayList<PlayerStat>();
  433. List<PlayerStat> taming = new ArrayList<PlayerStat>();
  434. List<PlayerStat> fishing = new ArrayList<PlayerStat>();
  435. BufferedReader in = null;
  436. // Read from the FlatFile database and fill our arrays with information
  437. synchronized (fileWritingLock) {
  438. try {
  439. in = new BufferedReader(new FileReader(usersFilePath));
  440. String line = "";
  441. ArrayList<String> players = new ArrayList<String>();
  442. while ((line = in.readLine()) != null) {
  443. String[] data = line.split(":");
  444. String playerName = data[0];
  445. int powerLevel = 0;
  446. // Prevent the same player from being added multiple times (I'd like to note that this shouldn't happen...)
  447. if (players.contains(playerName)) {
  448. continue;
  449. }
  450. players.add(playerName);
  451. Map<SkillType, Integer> skills = getSkillMapFromLine(data);
  452. powerLevel += putStat(acrobatics, playerName, skills.get(SkillType.ACROBATICS));
  453. powerLevel += putStat(archery, playerName, skills.get(SkillType.ARCHERY));
  454. powerLevel += putStat(axes, playerName, skills.get(SkillType.AXES));
  455. powerLevel += putStat(excavation, playerName, skills.get(SkillType.EXCAVATION));
  456. powerLevel += putStat(fishing, playerName, skills.get(SkillType.FISHING));
  457. powerLevel += putStat(herbalism, playerName, skills.get(SkillType.HERBALISM));
  458. powerLevel += putStat(mining, playerName, skills.get(SkillType.MINING));
  459. powerLevel += putStat(repair, playerName, skills.get(SkillType.REPAIR));
  460. powerLevel += putStat(swords, playerName, skills.get(SkillType.SWORDS));
  461. powerLevel += putStat(taming, playerName, skills.get(SkillType.TAMING));
  462. powerLevel += putStat(unarmed, playerName, skills.get(SkillType.UNARMED));
  463. powerLevel += putStat(woodcutting, playerName, skills.get(SkillType.WOODCUTTING));
  464. putStat(powerLevels, playerName, powerLevel);
  465. }
  466. }
  467. catch (Exception e) {
  468. mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
  469. }
  470. finally {
  471. tryClose(in);
  472. }
  473. }
  474. SkillComparator c = new SkillComparator();
  475. Collections.sort(mining, c);
  476. Collections.sort(woodcutting, c);
  477. Collections.sort(repair, c);
  478. Collections.sort(unarmed, c);
  479. Collections.sort(herbalism, c);
  480. Collections.sort(excavation, c);
  481. Collections.sort(archery, c);
  482. Collections.sort(swords, c);
  483. Collections.sort(axes, c);
  484. Collections.sort(acrobatics, c);
  485. Collections.sort(taming, c);
  486. Collections.sort(fishing, c);
  487. Collections.sort(powerLevels, c);
  488. playerStatHash.put(SkillType.MINING, mining);
  489. playerStatHash.put(SkillType.WOODCUTTING, woodcutting);
  490. playerStatHash.put(SkillType.REPAIR, repair);
  491. playerStatHash.put(SkillType.UNARMED, unarmed);
  492. playerStatHash.put(SkillType.HERBALISM, herbalism);
  493. playerStatHash.put(SkillType.EXCAVATION, excavation);
  494. playerStatHash.put(SkillType.ARCHERY, archery);
  495. playerStatHash.put(SkillType.SWORDS, swords);
  496. playerStatHash.put(SkillType.AXES, axes);
  497. playerStatHash.put(SkillType.ACROBATICS, acrobatics);
  498. playerStatHash.put(SkillType.TAMING, taming);
  499. playerStatHash.put(SkillType.FISHING, fishing);
  500. }
  501. /**
  502. * Checks that the file is present and valid
  503. */
  504. private void checkStructure() {
  505. if (usersFile.exists()) {
  506. BufferedReader in = null;
  507. FileWriter out = null;
  508. String usersFilePath = mcMMO.getUsersFilePath();
  509. synchronized (fileWritingLock) {
  510. try {
  511. in = new BufferedReader(new FileReader(usersFilePath));
  512. StringBuilder writer = new StringBuilder();
  513. String line = "";
  514. while ((line = in.readLine()) != null) {
  515. String[] character = line.split(":");
  516. // If they're valid, rewrite them to the file.
  517. if (character.length >= 37) {
  518. writer.append(line).append("\r\n");
  519. } else {
  520. String oldVersion = null;
  521. StringBuilder newLine = new StringBuilder(line);
  522. boolean announce = false;
  523. if (character.length <= 33) {
  524. // Introduction of HUDType
  525. // Version 1.1.06
  526. // commit 78f79213cdd7190cd11ae54526f3b4ea42078e8a
  527. newLine.append("STANDARD").append(":");
  528. oldVersion = "1.1.06";
  529. }
  530. if (character.length <= 35) {
  531. // Introduction of Fishing
  532. // Version 1.2.00
  533. // commit a814b57311bc7734661109f0e77fc8bab3a0bd29
  534. newLine.append(0).append(":");
  535. newLine.append(0).append(":");
  536. if (oldVersion == null) oldVersion = "1.2.00";
  537. }
  538. if (character.length <= 36) {
  539. // Introduction of Blast Mining cooldowns
  540. // Version 1.3.00-dev
  541. // commit fadbaf429d6b4764b8f1ad0efaa524a090e82ef5
  542. newLine.append((int) 0).append(":");
  543. if (oldVersion == null) oldVersion = "1.3.00";
  544. }
  545. if (character.length <= 37) {
  546. // Making old-purge work with flatfile
  547. // Version 1.4.00-dev
  548. // commmit 3f6c07ba6aaf44e388cc3b882cac3d8f51d0ac28
  549. // XXX Cannot create an OfflinePlayer at startup, use 0 and fix in purge
  550. newLine.append("0").append(":");
  551. announce = true; // TODO move this down
  552. if (oldVersion == null) oldVersion = "1.4.00";
  553. }
  554. if (character.length <= 38) {
  555. // Addition of mob healthbars
  556. // Version 1.4.06
  557. // commit da29185b7dc7e0d992754bba555576d48fa08aa6
  558. newLine.append(Config.getInstance().getMobHealthbarDefault().toString()).append(":");
  559. if (oldVersion == null) oldVersion = "1.4.06";
  560. }
  561. if (announce) {
  562. mcMMO.p.debug("Updating database line for player " + character[0] + " from before version " + oldVersion);
  563. }
  564. writer.append(newLine).append("\r\n");
  565. }
  566. }
  567. // Write the new file
  568. out = new FileWriter(usersFilePath);
  569. out.write(writer.toString());
  570. }
  571. catch (IOException e) {
  572. mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
  573. }
  574. finally {
  575. tryClose(in);
  576. tryClose(out);
  577. }
  578. }
  579. return;
  580. }
  581. usersFile.getParentFile().mkdir();
  582. try {
  583. mcMMO.p.debug("Creating mcmmo.users file...");
  584. new File(mcMMO.getUsersFilePath()).createNewFile();
  585. }
  586. catch (IOException e) {
  587. e.printStackTrace();
  588. }
  589. }
  590. private void tryClose(Closeable c) {
  591. if (c == null) return;
  592. try {
  593. c.close();
  594. } catch (IOException e) {
  595. e.printStackTrace();
  596. }
  597. }
  598. private Integer getPlayerRank(String playerName, List<PlayerStat> statsList) {
  599. if (statsList == null) {
  600. return null;
  601. }
  602. int currentPos = 1;
  603. for (PlayerStat stat : statsList) {
  604. if (stat.name.equalsIgnoreCase(playerName)) {
  605. return currentPos;
  606. }
  607. currentPos++;
  608. }
  609. return null;
  610. }
  611. private int putStat(List<PlayerStat> statList, String playerName, int statValue) {
  612. statList.add(new PlayerStat(playerName, statValue));
  613. return statValue;
  614. }
  615. private class SkillComparator implements Comparator<PlayerStat> {
  616. @Override
  617. public int compare(PlayerStat o1, PlayerStat o2) {
  618. return (o2.statVal - o1.statVal);
  619. }
  620. }
  621. private PlayerProfile loadFromLine(String[] character) throws Exception {
  622. Map<SkillType, Integer> skills = getSkillMapFromLine(character); // Skill levels
  623. Map<SkillType, Float> skillsXp = new HashMap<SkillType, Float>(); // Skill & XP
  624. Map<AbilityType, Integer> skillsDATS = new HashMap<AbilityType, Integer>(); // Ability & Cooldown
  625. HudType hudType;
  626. MobHealthbarType mobHealthbarType;
  627. // TODO on updates, put new values in a try{} ?
  628. skillsXp.put(SkillType.TAMING, (float) Integer.valueOf(character[25]));
  629. skillsXp.put(SkillType.MINING, (float) Integer.valueOf(character[4]));
  630. skillsXp.put(SkillType.REPAIR, (float) Integer.valueOf(character[15]));
  631. skillsXp.put(SkillType.WOODCUTTING, (float) Integer.valueOf(character[6]));
  632. skillsXp.put(SkillType.UNARMED, (float) Integer.valueOf(character[16]));
  633. skillsXp.put(SkillType.HERBALISM, (float) Integer.valueOf(character[17]));
  634. skillsXp.put(SkillType.EXCAVATION, (float) Integer.valueOf(character[18]));
  635. skillsXp.put(SkillType.ARCHERY, (float) Integer.valueOf(character[19]));
  636. skillsXp.put(SkillType.SWORDS, (float) Integer.valueOf(character[20]));
  637. skillsXp.put(SkillType.AXES, (float) Integer.valueOf(character[21]));
  638. skillsXp.put(SkillType.ACROBATICS, (float) Integer.valueOf(character[22]));
  639. skillsXp.put(SkillType.FISHING, (float) Integer.valueOf(character[35]));
  640. // Taming - Unused
  641. skillsDATS.put(AbilityType.SUPER_BREAKER, Integer.valueOf(character[32]));
  642. // Repair - Unused
  643. skillsDATS.put(AbilityType.TREE_FELLER, Integer.valueOf(character[28]));
  644. skillsDATS.put(AbilityType.BERSERK, Integer.valueOf(character[26]));
  645. skillsDATS.put(AbilityType.GREEN_TERRA, Integer.valueOf(character[29]));
  646. skillsDATS.put(AbilityType.GIGA_DRILL_BREAKER, Integer.valueOf(character[27]));
  647. // Archery - Unused
  648. skillsDATS.put(AbilityType.SERRATED_STRIKES, Integer.valueOf(character[30]));
  649. skillsDATS.put(AbilityType.SKULL_SPLITTER, Integer.valueOf(character[31]));
  650. // Acrobatics - Unused
  651. skillsDATS.put(AbilityType.BLAST_MINING, Integer.valueOf(character[36]));
  652. try {
  653. hudType = HudType.valueOf(character[33]);
  654. }
  655. catch (Exception e) {
  656. hudType = HudType.STANDARD; // Shouldn't happen unless database is being tampered with
  657. }
  658. try {
  659. mobHealthbarType = MobHealthbarType.valueOf(character[38]);
  660. }
  661. catch (Exception e) {
  662. mobHealthbarType = Config.getInstance().getMobHealthbarDefault();
  663. }
  664. return new PlayerProfile(character[0], skills, skillsXp, skillsDATS, hudType, mobHealthbarType);
  665. }
  666. private Map<SkillType, Integer> getSkillMapFromLine(String[] character) {
  667. Map<SkillType, Integer> skills = new HashMap<SkillType, Integer>(); // Skill & Level
  668. skills.put(SkillType.TAMING, Integer.valueOf(character[24]));
  669. skills.put(SkillType.MINING, Integer.valueOf(character[1]));
  670. skills.put(SkillType.REPAIR, Integer.valueOf(character[7]));
  671. skills.put(SkillType.WOODCUTTING, Integer.valueOf(character[5]));
  672. skills.put(SkillType.UNARMED, Integer.valueOf(character[8]));
  673. skills.put(SkillType.HERBALISM, Integer.valueOf(character[9]));
  674. skills.put(SkillType.EXCAVATION, Integer.valueOf(character[10]));
  675. skills.put(SkillType.ARCHERY, Integer.valueOf(character[11]));
  676. skills.put(SkillType.SWORDS, Integer.valueOf(character[12]));
  677. skills.put(SkillType.AXES, Integer.valueOf(character[13]));
  678. skills.put(SkillType.ACROBATICS, Integer.valueOf(character[14]));
  679. skills.put(SkillType.FISHING, Integer.valueOf(character[34]));
  680. return skills;
  681. }
  682. }