FlatfileDatabaseManager.java 34 KB

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