FlatfileDatabaseManager.java 34 KB

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