FlatfileDatabaseManager.java 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  1. package com.gmail.nossr50.database;
  2. import com.gmail.nossr50.datatypes.MobHealthbarType;
  3. import com.gmail.nossr50.datatypes.database.DatabaseType;
  4. import com.gmail.nossr50.datatypes.database.PlayerStat;
  5. import com.gmail.nossr50.datatypes.player.PlayerProfile;
  6. import com.gmail.nossr50.datatypes.player.UniqueDataType;
  7. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  8. import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
  9. import com.gmail.nossr50.mcMMO;
  10. import com.gmail.nossr50.util.Misc;
  11. import com.gmail.nossr50.util.StringUtils;
  12. import org.bukkit.OfflinePlayer;
  13. import java.io.*;
  14. import java.util.*;
  15. public final class FlatfileDatabaseManager implements DatabaseManager {
  16. private final HashMap<PrimarySkillType, List<PlayerStat>> playerStatHash = new HashMap<PrimarySkillType, List<PlayerStat>>();
  17. private final List<PlayerStat> powerLevels = new ArrayList<PlayerStat>();
  18. private long lastUpdate = 0;
  19. private final long UPDATE_WAIT_TIME = 600000L; // 10 minutes
  20. private final File usersFile;
  21. private static final Object fileWritingLock = new Object();
  22. protected FlatfileDatabaseManager() {
  23. usersFile = new File(mcMMO.getUsersFilePath());
  24. checkStructure();
  25. updateLeaderboards();
  26. /*if (mcMMO.getUpgradeManager().shouldUpgrade(UpgradeType.ADD_UUIDS)) {
  27. new UUIDUpdateAsyncTask(mcMMO.p, getStoredUsers()).runTaskAsynchronously(mcMMO.p);
  28. }*/
  29. }
  30. public void purgePowerlessUsers() {
  31. int purgedUsers = 0;
  32. mcMMO.p.getLogger().info("Purging powerless users...");
  33. BufferedReader in = null;
  34. FileWriter out = null;
  35. String usersFilePath = mcMMO.getUsersFilePath();
  36. // This code is O(n) instead of O(n²)
  37. synchronized (fileWritingLock) {
  38. try {
  39. in = new BufferedReader(new FileReader(usersFilePath));
  40. StringBuilder writer = new StringBuilder();
  41. String line;
  42. while ((line = in.readLine()) != null) {
  43. String[] character = line.split(":");
  44. Map<PrimarySkillType, Integer> skills = getSkillMapFromLine(character);
  45. boolean powerless = true;
  46. for (int skill : skills.values()) {
  47. if (skill > mcMMO.getPlayerLevelingSettings().getConfigSectionLevelingGeneral().getStartingLevel()) {
  48. powerless = false;
  49. break;
  50. }
  51. }
  52. // If they're still around, rewrite them to the file.
  53. if (!powerless) {
  54. writer.append(line).append("\r\n");
  55. }
  56. else {
  57. purgedUsers++;
  58. }
  59. }
  60. // Write the new file
  61. out = new FileWriter(usersFilePath);
  62. out.write(writer.toString());
  63. }
  64. catch (IOException e) {
  65. mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
  66. }
  67. finally {
  68. if (in != null) {
  69. try {
  70. in.close();
  71. }
  72. catch (IOException e) {
  73. // Ignore
  74. }
  75. }
  76. if (out != null) {
  77. try {
  78. out.close();
  79. }
  80. catch (IOException e) {
  81. // Ignore
  82. }
  83. }
  84. }
  85. }
  86. mcMMO.p.getLogger().info("Purged " + purgedUsers + " users from the database.");
  87. }
  88. public void purgeOldUsers() {
  89. int removedPlayers = 0;
  90. long currentTime = System.currentTimeMillis();
  91. mcMMO.p.getLogger().info("Purging old users...");
  92. BufferedReader in = null;
  93. FileWriter out = null;
  94. String usersFilePath = mcMMO.getUsersFilePath();
  95. // This code is O(n) instead of O(n²)
  96. synchronized (fileWritingLock) {
  97. try {
  98. in = new BufferedReader(new FileReader(usersFilePath));
  99. StringBuilder writer = new StringBuilder();
  100. String line;
  101. while ((line = in.readLine()) != null) {
  102. String[] character = line.split(":");
  103. String name = character[USERNAME];
  104. long lastPlayed = 0;
  105. boolean rewrite = false;
  106. try {
  107. lastPlayed = Long.parseLong(character[37]) * Misc.TIME_CONVERSION_FACTOR;
  108. }
  109. catch (NumberFormatException e) {
  110. e.printStackTrace();
  111. }
  112. if (lastPlayed == 0) {
  113. OfflinePlayer player = mcMMO.p.getServer().getOfflinePlayer(name);
  114. lastPlayed = player.getLastPlayed();
  115. rewrite = true;
  116. }
  117. if (currentTime - lastPlayed > PURGE_TIME) {
  118. removedPlayers++;
  119. }
  120. else {
  121. if (rewrite) {
  122. // Rewrite their data with a valid time
  123. character[37] = Long.toString(lastPlayed);
  124. String newLine = org.apache.commons.lang.StringUtils.join(character, ":");
  125. writer.append(newLine).append("\r\n");
  126. }
  127. else {
  128. writer.append(line).append("\r\n");
  129. }
  130. }
  131. }
  132. // Write the new file
  133. out = new FileWriter(usersFilePath);
  134. out.write(writer.toString());
  135. }
  136. catch (IOException e) {
  137. mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
  138. }
  139. finally {
  140. if (in != null) {
  141. try {
  142. in.close();
  143. }
  144. catch (IOException e) {
  145. // Ignore
  146. }
  147. }
  148. if (out != null) {
  149. try {
  150. out.close();
  151. }
  152. catch (IOException e) {
  153. // Ignore
  154. }
  155. }
  156. }
  157. }
  158. mcMMO.p.getLogger().info("Purged " + removedPlayers + " users from the database.");
  159. }
  160. public boolean removeUser(String playerName) {
  161. boolean worked = false;
  162. BufferedReader in = null;
  163. FileWriter out = null;
  164. String usersFilePath = mcMMO.getUsersFilePath();
  165. synchronized (fileWritingLock) {
  166. try {
  167. in = new BufferedReader(new FileReader(usersFilePath));
  168. StringBuilder writer = new StringBuilder();
  169. String line;
  170. while ((line = in.readLine()) != null) {
  171. // Write out the same file but when we get to the player we want to remove, we skip his line.
  172. if (!worked && line.split(":")[USERNAME].equalsIgnoreCase(playerName)) {
  173. mcMMO.p.getLogger().info("User found, removing...");
  174. worked = true;
  175. continue; // Skip the player
  176. }
  177. writer.append(line).append("\r\n");
  178. }
  179. out = new FileWriter(usersFilePath); // Write out the new file
  180. out.write(writer.toString());
  181. }
  182. catch (Exception e) {
  183. mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
  184. }
  185. finally {
  186. if (in != null) {
  187. try {
  188. in.close();
  189. }
  190. catch (IOException e) {
  191. // Ignore
  192. }
  193. }
  194. if (out != null) {
  195. try {
  196. out.close();
  197. }
  198. catch (IOException e) {
  199. // Ignore
  200. }
  201. }
  202. }
  203. }
  204. Misc.profileCleanup(playerName);
  205. return worked;
  206. }
  207. public boolean saveUser(PlayerProfile profile) {
  208. String playerName = profile.getPlayerName();
  209. UUID uuid = profile.getUniqueId();
  210. BufferedReader in = null;
  211. FileWriter out = null;
  212. String usersFilePath = mcMMO.getUsersFilePath();
  213. synchronized (fileWritingLock) {
  214. try {
  215. // Open the file
  216. in = new BufferedReader(new FileReader(usersFilePath));
  217. StringBuilder writer = new StringBuilder();
  218. String line;
  219. boolean wroteUser = false;
  220. // While not at the end of the file
  221. while ((line = in.readLine()) != null) {
  222. // Read the line in and copy it to the output if it's not the player we want to edit
  223. String[] character = line.split(":");
  224. if (!(uuid != null && character[UUID_INDEX].equalsIgnoreCase(uuid.toString())) && !character[USERNAME].equalsIgnoreCase(playerName)) {
  225. writer.append(line).append("\r\n");
  226. }
  227. else {
  228. // Otherwise write the new player information
  229. writeUserToLine(profile, playerName, uuid, writer);
  230. wroteUser = true;
  231. }
  232. }
  233. /*
  234. * If we couldn't find the user in the DB we need to add him
  235. */
  236. if(!wroteUser)
  237. {
  238. writeUserToLine(profile, playerName, uuid, writer);
  239. }
  240. // Write the new file
  241. out = new FileWriter(usersFilePath);
  242. out.write(writer.toString());
  243. return true;
  244. }
  245. catch (Exception e) {
  246. e.printStackTrace();
  247. return false;
  248. }
  249. finally {
  250. if (in != null) {
  251. try {
  252. in.close();
  253. }
  254. catch (IOException e) {
  255. // Ignore
  256. }
  257. }
  258. if (out != null) {
  259. try {
  260. out.close();
  261. }
  262. catch (IOException e) {
  263. // Ignore
  264. }
  265. }
  266. }
  267. }
  268. }
  269. private void writeUserToLine(PlayerProfile profile, String playerName, UUID uuid, StringBuilder writer) {
  270. writer.append(playerName).append(":");
  271. writer.append(profile.getSkillLevel(PrimarySkillType.MINING)).append(":");
  272. writer.append(":");
  273. writer.append(":");
  274. writer.append(profile.getSkillXpLevel(PrimarySkillType.MINING)).append(":");
  275. writer.append(profile.getSkillLevel(PrimarySkillType.WOODCUTTING)).append(":");
  276. writer.append(profile.getSkillXpLevel(PrimarySkillType.WOODCUTTING)).append(":");
  277. writer.append(profile.getSkillLevel(PrimarySkillType.REPAIR)).append(":");
  278. writer.append(profile.getSkillLevel(PrimarySkillType.UNARMED)).append(":");
  279. writer.append(profile.getSkillLevel(PrimarySkillType.HERBALISM)).append(":");
  280. writer.append(profile.getSkillLevel(PrimarySkillType.EXCAVATION)).append(":");
  281. writer.append(profile.getSkillLevel(PrimarySkillType.ARCHERY)).append(":");
  282. writer.append(profile.getSkillLevel(PrimarySkillType.SWORDS)).append(":");
  283. writer.append(profile.getSkillLevel(PrimarySkillType.AXES)).append(":");
  284. writer.append(profile.getSkillLevel(PrimarySkillType.ACROBATICS)).append(":");
  285. writer.append(profile.getSkillXpLevel(PrimarySkillType.REPAIR)).append(":");
  286. writer.append(profile.getSkillXpLevel(PrimarySkillType.UNARMED)).append(":");
  287. writer.append(profile.getSkillXpLevel(PrimarySkillType.HERBALISM)).append(":");
  288. writer.append(profile.getSkillXpLevel(PrimarySkillType.EXCAVATION)).append(":");
  289. writer.append(profile.getSkillXpLevel(PrimarySkillType.ARCHERY)).append(":");
  290. writer.append(profile.getSkillXpLevel(PrimarySkillType.SWORDS)).append(":");
  291. writer.append(profile.getSkillXpLevel(PrimarySkillType.AXES)).append(":");
  292. writer.append(profile.getSkillXpLevel(PrimarySkillType.ACROBATICS)).append(":");
  293. writer.append(":");
  294. writer.append(profile.getSkillLevel(PrimarySkillType.TAMING)).append(":");
  295. writer.append(profile.getSkillXpLevel(PrimarySkillType.TAMING)).append(":");
  296. writer.append((int) profile.getAbilityDATS(SuperAbilityType.BERSERK)).append(":");
  297. writer.append((int) profile.getAbilityDATS(SuperAbilityType.GIGA_DRILL_BREAKER)).append(":");
  298. writer.append((int) profile.getAbilityDATS(SuperAbilityType.TREE_FELLER)).append(":");
  299. writer.append((int) profile.getAbilityDATS(SuperAbilityType.GREEN_TERRA)).append(":");
  300. writer.append((int) profile.getAbilityDATS(SuperAbilityType.SERRATED_STRIKES)).append(":");
  301. writer.append((int) profile.getAbilityDATS(SuperAbilityType.SKULL_SPLITTER)).append(":");
  302. writer.append((int) profile.getAbilityDATS(SuperAbilityType.SUPER_BREAKER)).append(":");
  303. writer.append(":");
  304. writer.append(profile.getSkillLevel(PrimarySkillType.FISHING)).append(":");
  305. writer.append(profile.getSkillXpLevel(PrimarySkillType.FISHING)).append(":");
  306. writer.append((int) profile.getAbilityDATS(SuperAbilityType.BLAST_MINING)).append(":");
  307. writer.append(System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR).append(":");
  308. MobHealthbarType mobHealthbarType = profile.getMobHealthbarType();
  309. writer.append(mobHealthbarType == null ? mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString() : mobHealthbarType.toString()).append(":");
  310. writer.append(profile.getSkillLevel(PrimarySkillType.ALCHEMY)).append(":");
  311. writer.append(profile.getSkillXpLevel(PrimarySkillType.ALCHEMY)).append(":");
  312. writer.append(uuid != null ? uuid.toString() : "NULL").append(":");
  313. writer.append(profile.getScoreboardTipsShown()).append(":");
  314. writer.append(profile.getUniqueData(UniqueDataType.CHIMAERA_WING_DATS)).append(":");
  315. writer.append("\r\n");
  316. }
  317. public List<PlayerStat> readLeaderboard(PrimarySkillType skill, int pageNumber, int statsPerPage) {
  318. updateLeaderboards();
  319. List<PlayerStat> statsList = skill == null ? powerLevels : playerStatHash.get(skill);
  320. int fromIndex = (Math.max(pageNumber, 1) - 1) * statsPerPage;
  321. return statsList.subList(Math.min(fromIndex, statsList.size()), Math.min(fromIndex + statsPerPage, statsList.size()));
  322. }
  323. public Map<PrimarySkillType, Integer> readRank(String playerName) {
  324. updateLeaderboards();
  325. Map<PrimarySkillType, Integer> skills = new HashMap<PrimarySkillType, Integer>();
  326. for (PrimarySkillType skill : PrimarySkillType.NON_CHILD_SKILLS) {
  327. skills.put(skill, getPlayerRank(playerName, playerStatHash.get(skill)));
  328. }
  329. skills.put(null, getPlayerRank(playerName, powerLevels));
  330. return skills;
  331. }
  332. public void newUser(String playerName, UUID uuid) {
  333. BufferedWriter out = null;
  334. synchronized (fileWritingLock) {
  335. try {
  336. // Open the file to write the player
  337. out = new BufferedWriter(new FileWriter(mcMMO.getUsersFilePath(), true));
  338. String startingLevel = mcMMO.getPlayerLevelingSettings().getConfigSectionLevelingGeneral().getStartingLevel() + ":";
  339. // Add the player to the end
  340. out.append(playerName).append(":");
  341. out.append(startingLevel); // Mining
  342. out.append(":");
  343. out.append(":");
  344. out.append("0:"); // Xp
  345. out.append(startingLevel); // Woodcutting
  346. out.append("0:"); // WoodCuttingXp
  347. out.append(startingLevel); // Repair
  348. out.append(startingLevel); // Unarmed
  349. out.append(startingLevel); // Herbalism
  350. out.append(startingLevel); // Excavation
  351. out.append(startingLevel); // Archery
  352. out.append(startingLevel); // Swords
  353. out.append(startingLevel); // Axes
  354. out.append(startingLevel); // Acrobatics
  355. out.append("0:"); // RepairXp
  356. out.append("0:"); // UnarmedXp
  357. out.append("0:"); // HerbalismXp
  358. out.append("0:"); // ExcavationXp
  359. out.append("0:"); // ArcheryXp
  360. out.append("0:"); // SwordsXp
  361. out.append("0:"); // AxesXp
  362. out.append("0:"); // AcrobaticsXp
  363. out.append(":");
  364. out.append(startingLevel); // Taming
  365. out.append("0:"); // TamingXp
  366. out.append("0:"); // DATS
  367. out.append("0:"); // DATS
  368. out.append("0:"); // DATS
  369. out.append("0:"); // DATS
  370. out.append("0:"); // DATS
  371. out.append("0:"); // DATS
  372. out.append("0:"); // DATS
  373. out.append(":");
  374. out.append(startingLevel); // Fishing
  375. out.append("0:"); // FishingXp
  376. out.append("0:"); // Blast Mining
  377. out.append(String.valueOf(System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR)).append(":"); // LastLogin
  378. out.append(mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString()).append(":"); // Mob Healthbar HUD
  379. out.append(startingLevel); // Alchemy
  380. out.append("0:"); // AlchemyXp
  381. out.append(uuid != null ? uuid.toString() : "NULL").append(":"); // UUID
  382. out.append("0:"); // ConfigScoreboard tips shown
  383. // Add more in the same format as the line above
  384. out.newLine();
  385. }
  386. catch (Exception e) {
  387. e.printStackTrace();
  388. }
  389. finally {
  390. if (out != null) {
  391. try {
  392. out.close();
  393. }
  394. catch (IOException e) {
  395. // Ignore
  396. }
  397. }
  398. }
  399. }
  400. }
  401. @Deprecated
  402. public PlayerProfile loadPlayerProfile(String playerName, boolean create) {
  403. return loadPlayerProfile(playerName, null, false);
  404. }
  405. public PlayerProfile loadPlayerProfile(UUID uuid) {
  406. return loadPlayerProfile("", uuid, false);
  407. }
  408. public PlayerProfile loadPlayerProfile(String playerName, UUID uuid, boolean create) {
  409. BufferedReader in = null;
  410. String usersFilePath = mcMMO.getUsersFilePath();
  411. synchronized (fileWritingLock) {
  412. try {
  413. // Open the user file
  414. in = new BufferedReader(new FileReader(usersFilePath));
  415. String line;
  416. while ((line = in.readLine()) != null) {
  417. // Find if the line contains the player we want.
  418. String[] character = line.split(":");
  419. // Compare names because we don't have a valid uuid for that player even
  420. // if input uuid is not null
  421. if (character[UUID_INDEX].equalsIgnoreCase("NULL")) {
  422. if (!character[USERNAME].equalsIgnoreCase(playerName)) {
  423. continue;
  424. }
  425. }
  426. // If input uuid is not null then we should compare uuids
  427. else if ((uuid != null && !character[UUID_INDEX].equalsIgnoreCase(uuid.toString())) || (uuid == null && !character[USERNAME].equalsIgnoreCase(playerName))) {
  428. continue;
  429. }
  430. // Update playerName in database after name change
  431. if (!character[USERNAME].equalsIgnoreCase(playerName)) {
  432. mcMMO.p.debug("Name change detected: " + character[USERNAME] + " => " + playerName);
  433. character[USERNAME] = playerName;
  434. }
  435. return loadFromLine(character);
  436. }
  437. // Didn't find the player, create a new one
  438. if (create) {
  439. if (uuid == null) {
  440. newUser(playerName, uuid);
  441. return new PlayerProfile(playerName, true);
  442. }
  443. newUser(playerName, uuid);
  444. return new PlayerProfile(playerName, uuid, true);
  445. }
  446. }
  447. catch (Exception e) {
  448. e.printStackTrace();
  449. }
  450. finally {
  451. // I have no idea why it's necessary to inline tryClose() here, but it removes
  452. // a resource leak warning, and I'm trusting the compiler on this one.
  453. if (in != null) {
  454. try {
  455. in.close();
  456. }
  457. catch (IOException e) {
  458. // Ignore
  459. }
  460. }
  461. }
  462. }
  463. // Return unloaded profile
  464. if (uuid == null) {
  465. return new PlayerProfile(playerName);
  466. }
  467. return new PlayerProfile(playerName, uuid);
  468. }
  469. public void convertUsers(DatabaseManager destination) {
  470. BufferedReader in = null;
  471. String usersFilePath = mcMMO.getUsersFilePath();
  472. int convertedUsers = 0;
  473. long startMillis = System.currentTimeMillis();
  474. synchronized (fileWritingLock) {
  475. try {
  476. // Open the user file
  477. in = new BufferedReader(new FileReader(usersFilePath));
  478. String line;
  479. while ((line = in.readLine()) != null) {
  480. String[] character = line.split(":");
  481. try {
  482. destination.saveUser(loadFromLine(character));
  483. }
  484. catch (Exception e) {
  485. e.printStackTrace();
  486. }
  487. convertedUsers++;
  488. Misc.printProgress(convertedUsers, progressInterval, startMillis);
  489. }
  490. }
  491. catch (Exception e) {
  492. e.printStackTrace();
  493. }
  494. finally {
  495. if (in != null) {
  496. try {
  497. in.close();
  498. }
  499. catch (IOException e) {
  500. // Ignore
  501. }
  502. }
  503. }
  504. }
  505. }
  506. public boolean saveUserUUID(String userName, UUID uuid) {
  507. boolean worked = false;
  508. int i = 0;
  509. BufferedReader in = null;
  510. FileWriter out = null;
  511. String usersFilePath = mcMMO.getUsersFilePath();
  512. synchronized (fileWritingLock) {
  513. try {
  514. in = new BufferedReader(new FileReader(usersFilePath));
  515. StringBuilder writer = new StringBuilder();
  516. String line;
  517. while ((line = in.readLine()) != null) {
  518. String[] character = line.split(":");
  519. if (!worked && character[USERNAME].equalsIgnoreCase(userName)) {
  520. if (character.length < 42) {
  521. mcMMO.p.getLogger().severe("Could not update UUID for " + userName + "!");
  522. mcMMO.p.getLogger().severe("Database entry is invalid.");
  523. continue;
  524. }
  525. line = line.replace(character[UUID_INDEX], uuid.toString());
  526. worked = true;
  527. }
  528. i++;
  529. writer.append(line).append("\r\n");
  530. }
  531. out = new FileWriter(usersFilePath); // Write out the new file
  532. out.write(writer.toString());
  533. }
  534. catch (Exception e) {
  535. mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
  536. }
  537. finally {
  538. mcMMO.p.getLogger().info(i + " entries written while saving UUID for " + userName);
  539. if (in != null) {
  540. try {
  541. in.close();
  542. }
  543. catch (IOException e) {
  544. // Ignore
  545. }
  546. }
  547. if (out != null) {
  548. try {
  549. out.close();
  550. }
  551. catch (IOException e) {
  552. // Ignore
  553. }
  554. }
  555. }
  556. }
  557. return worked;
  558. }
  559. public boolean saveUserUUIDs(Map<String, UUID> fetchedUUIDs) {
  560. BufferedReader in = null;
  561. FileWriter out = null;
  562. String usersFilePath = mcMMO.getUsersFilePath();
  563. int i = 0;
  564. synchronized (fileWritingLock) {
  565. try {
  566. in = new BufferedReader(new FileReader(usersFilePath));
  567. StringBuilder writer = new StringBuilder();
  568. String line;
  569. while (((line = in.readLine()) != null)) {
  570. String[] character = line.split(":");
  571. if (!fetchedUUIDs.isEmpty() && fetchedUUIDs.containsKey(character[USERNAME])) {
  572. if (character.length < 42) {
  573. mcMMO.p.getLogger().severe("Could not update UUID for " + character[USERNAME] + "!");
  574. mcMMO.p.getLogger().severe("Database entry is invalid.");
  575. continue;
  576. }
  577. character[UUID_INDEX] = fetchedUUIDs.remove(character[USERNAME]).toString();
  578. line = new StringBuilder(org.apache.commons.lang.StringUtils.join(character, ":")).append(":").toString();
  579. }
  580. i++;
  581. writer.append(line).append("\r\n");
  582. }
  583. out = new FileWriter(usersFilePath); // Write out the new file
  584. out.write(writer.toString());
  585. }
  586. catch (Exception e) {
  587. mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
  588. }
  589. finally {
  590. mcMMO.p.getLogger().info(i + " entries written while saving UUID batch");
  591. if (in != null) {
  592. try {
  593. in.close();
  594. }
  595. catch (IOException e) {
  596. // Ignore
  597. }
  598. }
  599. if (out != null) {
  600. try {
  601. out.close();
  602. }
  603. catch (IOException e) {
  604. // Ignore
  605. }
  606. }
  607. }
  608. }
  609. return true;
  610. }
  611. public List<String> getStoredUsers() {
  612. ArrayList<String> users = new ArrayList<String>();
  613. BufferedReader in = null;
  614. String usersFilePath = mcMMO.getUsersFilePath();
  615. synchronized (fileWritingLock) {
  616. try {
  617. // Open the user file
  618. in = new BufferedReader(new FileReader(usersFilePath));
  619. String line;
  620. while ((line = in.readLine()) != null) {
  621. String[] character = line.split(":");
  622. users.add(character[USERNAME]);
  623. }
  624. }
  625. catch (Exception e) {
  626. e.printStackTrace();
  627. }
  628. finally {
  629. if (in != null) {
  630. try {
  631. in.close();
  632. }
  633. catch (IOException e) {
  634. // Ignore
  635. }
  636. }
  637. }
  638. }
  639. return users;
  640. }
  641. /**
  642. * Update the leader boards.
  643. */
  644. private void updateLeaderboards() {
  645. // 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
  646. if (System.currentTimeMillis() < lastUpdate + UPDATE_WAIT_TIME) {
  647. return;
  648. }
  649. String usersFilePath = mcMMO.getUsersFilePath();
  650. lastUpdate = System.currentTimeMillis(); // Log when the last update was run
  651. powerLevels.clear(); // Clear old values from the power levels
  652. // Initialize lists
  653. List<PlayerStat> mining = new ArrayList<PlayerStat>();
  654. List<PlayerStat> woodcutting = new ArrayList<PlayerStat>();
  655. List<PlayerStat> herbalism = new ArrayList<PlayerStat>();
  656. List<PlayerStat> excavation = new ArrayList<PlayerStat>();
  657. List<PlayerStat> acrobatics = new ArrayList<PlayerStat>();
  658. List<PlayerStat> repair = new ArrayList<PlayerStat>();
  659. List<PlayerStat> swords = new ArrayList<PlayerStat>();
  660. List<PlayerStat> axes = new ArrayList<PlayerStat>();
  661. List<PlayerStat> archery = new ArrayList<PlayerStat>();
  662. List<PlayerStat> unarmed = new ArrayList<PlayerStat>();
  663. List<PlayerStat> taming = new ArrayList<PlayerStat>();
  664. List<PlayerStat> fishing = new ArrayList<PlayerStat>();
  665. List<PlayerStat> alchemy = new ArrayList<PlayerStat>();
  666. BufferedReader in = null;
  667. String playerName = null;
  668. // Read from the FlatFile database and fill our arrays with information
  669. synchronized (fileWritingLock) {
  670. try {
  671. in = new BufferedReader(new FileReader(usersFilePath));
  672. String line;
  673. while ((line = in.readLine()) != null) {
  674. String[] data = line.split(":");
  675. playerName = data[USERNAME];
  676. int powerLevel = 0;
  677. Map<PrimarySkillType, Integer> skills = getSkillMapFromLine(data);
  678. powerLevel += putStat(acrobatics, playerName, skills.get(PrimarySkillType.ACROBATICS));
  679. powerLevel += putStat(alchemy, playerName, skills.get(PrimarySkillType.ALCHEMY));
  680. powerLevel += putStat(archery, playerName, skills.get(PrimarySkillType.ARCHERY));
  681. powerLevel += putStat(axes, playerName, skills.get(PrimarySkillType.AXES));
  682. powerLevel += putStat(excavation, playerName, skills.get(PrimarySkillType.EXCAVATION));
  683. powerLevel += putStat(fishing, playerName, skills.get(PrimarySkillType.FISHING));
  684. powerLevel += putStat(herbalism, playerName, skills.get(PrimarySkillType.HERBALISM));
  685. powerLevel += putStat(mining, playerName, skills.get(PrimarySkillType.MINING));
  686. powerLevel += putStat(repair, playerName, skills.get(PrimarySkillType.REPAIR));
  687. powerLevel += putStat(swords, playerName, skills.get(PrimarySkillType.SWORDS));
  688. powerLevel += putStat(taming, playerName, skills.get(PrimarySkillType.TAMING));
  689. powerLevel += putStat(unarmed, playerName, skills.get(PrimarySkillType.UNARMED));
  690. powerLevel += putStat(woodcutting, playerName, skills.get(PrimarySkillType.WOODCUTTING));
  691. putStat(powerLevels, playerName, powerLevel);
  692. }
  693. }
  694. catch (Exception e) {
  695. mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " during user " + playerName + " (Are you sure you formatted it correctly?) " + e.toString());
  696. }
  697. finally {
  698. if (in != null) {
  699. try {
  700. in.close();
  701. }
  702. catch (IOException e) {
  703. // Ignore
  704. }
  705. }
  706. }
  707. }
  708. SkillComparator c = new SkillComparator();
  709. Collections.sort(mining, c);
  710. Collections.sort(woodcutting, c);
  711. Collections.sort(repair, c);
  712. Collections.sort(unarmed, c);
  713. Collections.sort(herbalism, c);
  714. Collections.sort(excavation, c);
  715. Collections.sort(archery, c);
  716. Collections.sort(swords, c);
  717. Collections.sort(axes, c);
  718. Collections.sort(acrobatics, c);
  719. Collections.sort(taming, c);
  720. Collections.sort(fishing, c);
  721. Collections.sort(alchemy, c);
  722. Collections.sort(powerLevels, c);
  723. playerStatHash.put(PrimarySkillType.MINING, mining);
  724. playerStatHash.put(PrimarySkillType.WOODCUTTING, woodcutting);
  725. playerStatHash.put(PrimarySkillType.REPAIR, repair);
  726. playerStatHash.put(PrimarySkillType.UNARMED, unarmed);
  727. playerStatHash.put(PrimarySkillType.HERBALISM, herbalism);
  728. playerStatHash.put(PrimarySkillType.EXCAVATION, excavation);
  729. playerStatHash.put(PrimarySkillType.ARCHERY, archery);
  730. playerStatHash.put(PrimarySkillType.SWORDS, swords);
  731. playerStatHash.put(PrimarySkillType.AXES, axes);
  732. playerStatHash.put(PrimarySkillType.ACROBATICS, acrobatics);
  733. playerStatHash.put(PrimarySkillType.TAMING, taming);
  734. playerStatHash.put(PrimarySkillType.FISHING, fishing);
  735. playerStatHash.put(PrimarySkillType.ALCHEMY, alchemy);
  736. }
  737. /**
  738. * Checks that the file is present and valid
  739. */
  740. private void checkStructure() {
  741. if (usersFile.exists()) {
  742. BufferedReader in = null;
  743. FileWriter out = null;
  744. String usersFilePath = mcMMO.getUsersFilePath();
  745. synchronized (fileWritingLock) {
  746. try {
  747. in = new BufferedReader(new FileReader(usersFilePath));
  748. StringBuilder writer = new StringBuilder();
  749. String line;
  750. HashSet<String> usernames = new HashSet<String>();
  751. HashSet<String> players = new HashSet<String>();
  752. while ((line = in.readLine()) != null) {
  753. // Remove empty lines from the file
  754. if (line.isEmpty()) {
  755. continue;
  756. }
  757. // Length checks depend on last character being ':'
  758. if (line.charAt(line.length() - 1) != ':') {
  759. line = line.concat(":");
  760. }
  761. boolean updated = false;
  762. String[] character = line.split(":");
  763. // Prevent the same username from being present multiple times
  764. if (!usernames.add(character[USERNAME])) {
  765. character[USERNAME] = "_INVALID_OLD_USERNAME_'";
  766. updated = true;
  767. if (character.length < UUID_INDEX + 1 || character[UUID_INDEX].equals("NULL")) {
  768. continue;
  769. }
  770. }
  771. // Prevent the same player from being present multiple times
  772. if (character.length >= 42 && (!character[UUID_INDEX].isEmpty() && !character[UUID_INDEX].equals("NULL") && !players.add(character[UUID_INDEX]))) {
  773. continue;
  774. }
  775. if (character.length < 33) {
  776. // Before Version 1.0 - Drop
  777. mcMMO.p.getLogger().warning("Dropping malformed or before version 1.0 line from database - " + line);
  778. continue;
  779. }
  780. String oldVersion = null;
  781. if (character.length > 33 && !character[33].isEmpty()) {
  782. // Removal of Spout Support
  783. // Version 1.4.07-dev2
  784. // commit 7bac0e2ca5143bce84dc160617fed97f0b1cb968
  785. character[33] = "";
  786. if (oldVersion == null) {
  787. oldVersion = "1.4.07";
  788. }
  789. updated = true;
  790. }
  791. if (mcMMO.getPlayerLevelingSettings().getConfigSectionLevelCaps().getReducePlayerSkillsAboveCap()) {
  792. for (PrimarySkillType skill : PrimarySkillType.NON_CHILD_SKILLS) {
  793. int index = getSkillIndex(skill);
  794. if (index >= character.length) {
  795. continue;
  796. }
  797. //Level Cap
  798. if(mcMMO.getPlayerLevelingSettings().isLevelCapEnabled(skill))
  799. {
  800. int cap = mcMMO.getPlayerLevelingSettings().getLevelCap(skill);
  801. if (Integer.valueOf(character[index]) > cap) {
  802. mcMMO.p.getLogger().warning("Truncating " + skill.getName() + " to configured max level for player " + character[USERNAME]);
  803. character[index] = cap + "";
  804. updated = true;
  805. }
  806. }
  807. }
  808. }
  809. // If they're valid, rewrite them to the file.
  810. if (!updated && character.length == 43) {
  811. writer.append(line).append("\r\n");
  812. continue;
  813. }
  814. if (character.length <= 33) {
  815. // Introduction of HUDType
  816. // Version 1.1.06
  817. // commit 78f79213cdd7190cd11ae54526f3b4ea42078e8a
  818. character = Arrays.copyOf(character, character.length + 1);
  819. character[character.length - 1] = "";
  820. oldVersion = "1.1.06";
  821. }
  822. if (character.length <= 35) {
  823. // Introduction of Fishing
  824. // Version 1.2.00
  825. // commit a814b57311bc7734661109f0e77fc8bab3a0bd29
  826. character = Arrays.copyOf(character, character.length + 2);
  827. character[character.length - 1] = "0";
  828. character[character.length - 2] = "0";
  829. if (oldVersion == null) {
  830. oldVersion = "1.2.00";
  831. }
  832. }
  833. if (character.length <= 36) {
  834. // Introduction of Blast Mining cooldowns
  835. // Version 1.3.00-dev
  836. // commit fadbaf429d6b4764b8f1ad0efaa524a090e82ef5
  837. character = Arrays.copyOf(character, character.length + 1);
  838. character[character.length - 1] = "0";
  839. if (oldVersion == null) {
  840. oldVersion = "1.3.00";
  841. }
  842. }
  843. if (character.length <= 37) {
  844. // Making old-purge work with flatfile
  845. // Version 1.4.00-dev
  846. // commmit 3f6c07ba6aaf44e388cc3b882cac3d8f51d0ac28
  847. // XXX Cannot create an OfflinePlayer at startup, use 0 and fix in purge
  848. character = Arrays.copyOf(character, character.length + 1);
  849. character[character.length - 1] = "0";
  850. if (oldVersion == null) {
  851. oldVersion = "1.4.00";
  852. }
  853. }
  854. if (character.length <= 38) {
  855. // Addition of mob healthbars
  856. // Version 1.4.06
  857. // commit da29185b7dc7e0d992754bba555576d48fa08aa6
  858. character = Arrays.copyOf(character, character.length + 1);
  859. character[character.length - 1] = mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString();
  860. if (oldVersion == null) {
  861. oldVersion = "1.4.06";
  862. }
  863. }
  864. if (character.length <= 39) {
  865. // Addition of Alchemy
  866. // Version 1.4.08
  867. character = Arrays.copyOf(character, character.length + 2);
  868. character[character.length - 1] = "0";
  869. character[character.length - 2] = "0";
  870. if (oldVersion == null) {
  871. oldVersion = "1.4.08";
  872. }
  873. }
  874. if (character.length <= 41) {
  875. // Addition of UUIDs
  876. // Version 1.5.01
  877. // Add a value because otherwise it gets removed
  878. character = Arrays.copyOf(character, character.length + 1);
  879. character[character.length - 1] = "NULL";
  880. if (oldVersion == null) {
  881. oldVersion = "1.5.01";
  882. }
  883. }
  884. if (character.length <= 42) {
  885. // Addition of scoreboard tips auto disable
  886. // Version 1.5.02
  887. character = Arrays.copyOf(character, character.length + 1);
  888. character[character.length - 1] = "0";
  889. if (oldVersion == null) {
  890. oldVersion = "1.5.02";
  891. }
  892. }
  893. boolean corrupted = false;
  894. for (int i = 0; i < character.length; i++) {
  895. if (character[i].isEmpty() && !(i == 2 || i == 3 || i == 23 || i == 33 || i == 41)) {
  896. corrupted = true;
  897. if (i == 37) {
  898. character[i] = String.valueOf(System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR);
  899. }
  900. else if (i == 38) {
  901. character[i] = mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString();
  902. }
  903. else {
  904. character[i] = "0";
  905. }
  906. }
  907. if (StringUtils.isInt(character[i]) && i == 38) {
  908. corrupted = true;
  909. character[i] = mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString();
  910. }
  911. if (!StringUtils.isInt(character[i]) && !(i == 0 || i == 2 || i == 3 || i == 23 || i == 33 || i == 38 || i == 41)) {
  912. corrupted = true;
  913. character[i] = "0";
  914. }
  915. }
  916. if (corrupted) {
  917. mcMMO.p.debug("Updating corrupted database line for player " + character[USERNAME]);
  918. }
  919. if (oldVersion != null) {
  920. mcMMO.p.debug("Updating database line from before version " + oldVersion + " for player " + character[USERNAME]);
  921. }
  922. updated |= corrupted;
  923. updated |= oldVersion != null;
  924. if (mcMMO.getPlayerLevelingSettings().getConfigSectionLevelCaps().getReducePlayerSkillsAboveCap()) {
  925. Map<PrimarySkillType, Integer> skills = getSkillMapFromLine(character);
  926. for (PrimarySkillType skill : PrimarySkillType.NON_CHILD_SKILLS) {
  927. int cap = Integer.MAX_VALUE;
  928. if (skills.get(skill) > cap) {
  929. updated = true;
  930. }
  931. }
  932. }
  933. if (updated) {
  934. line = new StringBuilder(org.apache.commons.lang.StringUtils.join(character, ":")).append(":").toString();
  935. }
  936. writer.append(line).append("\r\n");
  937. }
  938. // Write the new file
  939. out = new FileWriter(usersFilePath);
  940. out.write(writer.toString());
  941. }
  942. catch (IOException e) {
  943. mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
  944. }
  945. finally {
  946. if (in != null) {
  947. try {
  948. in.close();
  949. }
  950. catch (IOException e) {
  951. // Ignore
  952. }
  953. }
  954. if (out != null) {
  955. try {
  956. out.close();
  957. }
  958. catch (IOException e) {
  959. // Ignore
  960. }
  961. }
  962. }
  963. }
  964. /*mcMMO.getUpgradeManager().setUpgradeCompleted(UpgradeType.ADD_FISHING);
  965. mcMMO.getUpgradeManager().setUpgradeCompleted(UpgradeType.ADD_BLAST_MINING_COOLDOWN);
  966. mcMMO.getUpgradeManager().setUpgradeCompleted(UpgradeType.ADD_SQL_INDEXES);
  967. mcMMO.getUpgradeManager().setUpgradeCompleted(UpgradeType.ADD_MOB_HEALTHBARS);
  968. mcMMO.getUpgradeManager().setUpgradeCompleted(UpgradeType.DROP_SQL_PARTY_NAMES);
  969. mcMMO.getUpgradeManager().setUpgradeCompleted(UpgradeType.DROP_SPOUT);
  970. mcMMO.getUpgradeManager().setUpgradeCompleted(UpgradeType.ADD_ALCHEMY);*/
  971. return;
  972. }
  973. usersFile.getParentFile().mkdir();
  974. try {
  975. mcMMO.p.debug("Creating mcmmo.users file...");
  976. new File(mcMMO.getUsersFilePath()).createNewFile();
  977. }
  978. catch (IOException e) {
  979. e.printStackTrace();
  980. }
  981. }
  982. private Integer getPlayerRank(String playerName, List<PlayerStat> statsList) {
  983. if (statsList == null) {
  984. return null;
  985. }
  986. int currentPos = 1;
  987. for (PlayerStat stat : statsList) {
  988. if (stat.name.equalsIgnoreCase(playerName)) {
  989. return currentPos;
  990. }
  991. currentPos++;
  992. }
  993. return null;
  994. }
  995. private int putStat(List<PlayerStat> statList, String playerName, int statValue) {
  996. statList.add(new PlayerStat(playerName, statValue));
  997. return statValue;
  998. }
  999. private class SkillComparator implements Comparator<PlayerStat> {
  1000. @Override
  1001. public int compare(PlayerStat o1, PlayerStat o2) {
  1002. return (o2.statVal - o1.statVal);
  1003. }
  1004. }
  1005. private PlayerProfile loadFromLine(String[] character) {
  1006. Map<PrimarySkillType, Integer> skills = getSkillMapFromLine(character); // Skill levels
  1007. Map<PrimarySkillType, Float> skillsXp = new EnumMap<PrimarySkillType, Float>(PrimarySkillType.class); // Skill & XP
  1008. Map<SuperAbilityType, Integer> skillsDATS = new EnumMap<SuperAbilityType, Integer>(SuperAbilityType.class); // Ability & Cooldown
  1009. Map<UniqueDataType, Integer> uniquePlayerDataMap = new EnumMap<UniqueDataType, Integer>(UniqueDataType.class);
  1010. MobHealthbarType mobHealthbarType;
  1011. int scoreboardTipsShown;
  1012. // TODO on updates, put new values in a try{} ?
  1013. skillsXp.put(PrimarySkillType.TAMING, (float) Integer.valueOf(character[EXP_TAMING]));
  1014. skillsXp.put(PrimarySkillType.MINING, (float) Integer.valueOf(character[EXP_MINING]));
  1015. skillsXp.put(PrimarySkillType.REPAIR, (float) Integer.valueOf(character[EXP_REPAIR]));
  1016. skillsXp.put(PrimarySkillType.WOODCUTTING, (float) Integer.valueOf(character[EXP_WOODCUTTING]));
  1017. skillsXp.put(PrimarySkillType.UNARMED, (float) Integer.valueOf(character[EXP_UNARMED]));
  1018. skillsXp.put(PrimarySkillType.HERBALISM, (float) Integer.valueOf(character[EXP_HERBALISM]));
  1019. skillsXp.put(PrimarySkillType.EXCAVATION, (float) Integer.valueOf(character[EXP_EXCAVATION]));
  1020. skillsXp.put(PrimarySkillType.ARCHERY, (float) Integer.valueOf(character[EXP_ARCHERY]));
  1021. skillsXp.put(PrimarySkillType.SWORDS, (float) Integer.valueOf(character[EXP_SWORDS]));
  1022. skillsXp.put(PrimarySkillType.AXES, (float) Integer.valueOf(character[EXP_AXES]));
  1023. skillsXp.put(PrimarySkillType.ACROBATICS, (float) Integer.valueOf(character[EXP_ACROBATICS]));
  1024. skillsXp.put(PrimarySkillType.FISHING, (float) Integer.valueOf(character[EXP_FISHING]));
  1025. skillsXp.put(PrimarySkillType.ALCHEMY, (float) Integer.valueOf(character[EXP_ALCHEMY]));
  1026. // Taming - Unused
  1027. skillsDATS.put(SuperAbilityType.SUPER_BREAKER, Integer.valueOf(character[COOLDOWN_SUPER_BREAKER]));
  1028. // Repair - Unused
  1029. skillsDATS.put(SuperAbilityType.TREE_FELLER, Integer.valueOf(character[COOLDOWN_TREE_FELLER]));
  1030. skillsDATS.put(SuperAbilityType.BERSERK, Integer.valueOf(character[COOLDOWN_BERSERK]));
  1031. skillsDATS.put(SuperAbilityType.GREEN_TERRA, Integer.valueOf(character[COOLDOWN_GREEN_TERRA]));
  1032. skillsDATS.put(SuperAbilityType.GIGA_DRILL_BREAKER, Integer.valueOf(character[COOLDOWN_GIGA_DRILL_BREAKER]));
  1033. // Archery - Unused
  1034. skillsDATS.put(SuperAbilityType.SERRATED_STRIKES, Integer.valueOf(character[COOLDOWN_SERRATED_STRIKES]));
  1035. skillsDATS.put(SuperAbilityType.SKULL_SPLITTER, Integer.valueOf(character[COOLDOWN_SKULL_SPLITTER]));
  1036. // Acrobatics - Unused
  1037. skillsDATS.put(SuperAbilityType.BLAST_MINING, Integer.valueOf(character[COOLDOWN_BLAST_MINING]));
  1038. try {
  1039. mobHealthbarType = MobHealthbarType.valueOf(character[HEALTHBAR]);
  1040. }
  1041. catch (Exception e) {
  1042. mobHealthbarType = mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType();
  1043. }
  1044. UUID uuid;
  1045. try {
  1046. uuid = UUID.fromString(character[UUID_INDEX]);
  1047. }
  1048. catch (Exception e) {
  1049. uuid = null;
  1050. }
  1051. try {
  1052. scoreboardTipsShown = Integer.valueOf(character[SCOREBOARD_TIPS]);
  1053. }
  1054. catch (Exception e) {
  1055. scoreboardTipsShown = 0;
  1056. }
  1057. try {
  1058. uniquePlayerDataMap.put(UniqueDataType.CHIMAERA_WING_DATS, Integer.valueOf(character[COOLDOWN_CHIMAERA_WING]));
  1059. }
  1060. catch (Exception e) {
  1061. uniquePlayerDataMap.put(UniqueDataType.CHIMAERA_WING_DATS, 0);
  1062. }
  1063. return new PlayerProfile(character[USERNAME], uuid, skills, skillsXp, skillsDATS, mobHealthbarType, scoreboardTipsShown, uniquePlayerDataMap);
  1064. }
  1065. private Map<PrimarySkillType, Integer> getSkillMapFromLine(String[] character) {
  1066. Map<PrimarySkillType, Integer> skills = new EnumMap<PrimarySkillType, Integer>(PrimarySkillType.class); // Skill & Level
  1067. skills.put(PrimarySkillType.TAMING, Integer.valueOf(character[SKILLS_TAMING]));
  1068. skills.put(PrimarySkillType.MINING, Integer.valueOf(character[SKILLS_MINING]));
  1069. skills.put(PrimarySkillType.REPAIR, Integer.valueOf(character[SKILLS_REPAIR]));
  1070. skills.put(PrimarySkillType.WOODCUTTING, Integer.valueOf(character[SKILLS_WOODCUTTING]));
  1071. skills.put(PrimarySkillType.UNARMED, Integer.valueOf(character[SKILLS_UNARMED]));
  1072. skills.put(PrimarySkillType.HERBALISM, Integer.valueOf(character[SKILLS_HERBALISM]));
  1073. skills.put(PrimarySkillType.EXCAVATION, Integer.valueOf(character[SKILLS_EXCAVATION]));
  1074. skills.put(PrimarySkillType.ARCHERY, Integer.valueOf(character[SKILLS_ARCHERY]));
  1075. skills.put(PrimarySkillType.SWORDS, Integer.valueOf(character[SKILLS_SWORDS]));
  1076. skills.put(PrimarySkillType.AXES, Integer.valueOf(character[SKILLS_AXES]));
  1077. skills.put(PrimarySkillType.ACROBATICS, Integer.valueOf(character[SKILLS_ACROBATICS]));
  1078. skills.put(PrimarySkillType.FISHING, Integer.valueOf(character[SKILLS_FISHING]));
  1079. skills.put(PrimarySkillType.ALCHEMY, Integer.valueOf(character[SKILLS_ALCHEMY]));
  1080. return skills;
  1081. }
  1082. public DatabaseType getDatabaseType() {
  1083. return DatabaseType.FLATFILE;
  1084. }
  1085. @Override
  1086. public void onDisable() { }
  1087. private int getSkillIndex(PrimarySkillType skill) {
  1088. switch (skill) {
  1089. case ACROBATICS:
  1090. return SKILLS_ACROBATICS;
  1091. case ALCHEMY:
  1092. return SKILLS_ALCHEMY;
  1093. case ARCHERY:
  1094. return SKILLS_ARCHERY;
  1095. case AXES:
  1096. return SKILLS_AXES;
  1097. case EXCAVATION:
  1098. return SKILLS_EXCAVATION;
  1099. case FISHING:
  1100. return SKILLS_FISHING;
  1101. case HERBALISM:
  1102. return SKILLS_HERBALISM;
  1103. case MINING:
  1104. return SKILLS_MINING;
  1105. case REPAIR:
  1106. return SKILLS_REPAIR;
  1107. case SWORDS:
  1108. return SKILLS_SWORDS;
  1109. case TAMING:
  1110. return SKILLS_TAMING;
  1111. case UNARMED:
  1112. return SKILLS_UNARMED;
  1113. case WOODCUTTING:
  1114. return SKILLS_WOODCUTTING;
  1115. default:
  1116. throw new RuntimeException("Primary Skills only");
  1117. }
  1118. }
  1119. public static int USERNAME = 0;
  1120. public static int SKILLS_MINING = 1;
  1121. public static int EXP_MINING = 4;
  1122. public static int SKILLS_WOODCUTTING = 5;
  1123. public static int EXP_WOODCUTTING = 6;
  1124. public static int SKILLS_REPAIR = 7;
  1125. public static int SKILLS_UNARMED = 8;
  1126. public static int SKILLS_HERBALISM = 9;
  1127. public static int SKILLS_EXCAVATION = 10;
  1128. public static int SKILLS_ARCHERY = 11;
  1129. public static int SKILLS_SWORDS = 12;
  1130. public static int SKILLS_AXES = 13;
  1131. public static int SKILLS_ACROBATICS = 14;
  1132. public static int EXP_REPAIR = 15;
  1133. public static int EXP_UNARMED = 16;
  1134. public static int EXP_HERBALISM = 17;
  1135. public static int EXP_EXCAVATION = 18;
  1136. public static int EXP_ARCHERY = 19;
  1137. public static int EXP_SWORDS = 20;
  1138. public static int EXP_AXES = 21;
  1139. public static int EXP_ACROBATICS = 22;
  1140. public static int SKILLS_TAMING = 24;
  1141. public static int EXP_TAMING = 25;
  1142. public static int COOLDOWN_BERSERK = 26;
  1143. public static int COOLDOWN_GIGA_DRILL_BREAKER = 27;
  1144. public static int COOLDOWN_TREE_FELLER = 28;
  1145. public static int COOLDOWN_GREEN_TERRA = 29;
  1146. public static int COOLDOWN_SERRATED_STRIKES = 30;
  1147. public static int COOLDOWN_SKULL_SPLITTER = 31;
  1148. public static int COOLDOWN_SUPER_BREAKER = 32;
  1149. public static int SKILLS_FISHING = 34;
  1150. public static int EXP_FISHING = 35;
  1151. public static int COOLDOWN_BLAST_MINING = 36;
  1152. public static int LAST_LOGIN = 37;
  1153. public static int HEALTHBAR = 38;
  1154. public static int SKILLS_ALCHEMY = 39;
  1155. public static int EXP_ALCHEMY = 40;
  1156. public static int UUID_INDEX = 41;
  1157. public static int SCOREBOARD_TIPS = 42;
  1158. public static int COOLDOWN_CHIMAERA_WING = 43;
  1159. public void resetMobHealthSettings() {
  1160. BufferedReader in = null;
  1161. FileWriter out = null;
  1162. String usersFilePath = mcMMO.getUsersFilePath();
  1163. synchronized (fileWritingLock) {
  1164. try {
  1165. in = new BufferedReader(new FileReader(usersFilePath));
  1166. StringBuilder writer = new StringBuilder();
  1167. String line;
  1168. while ((line = in.readLine()) != null) {
  1169. // Remove empty lines from the file
  1170. if (line.isEmpty()) {
  1171. continue;
  1172. }
  1173. String[] character = line.split(":");
  1174. character[HEALTHBAR] = mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString();
  1175. line = new StringBuilder(org.apache.commons.lang.StringUtils.join(character, ":")).append(":").toString();
  1176. writer.append(line).append("\r\n");
  1177. }
  1178. // Write the new file
  1179. out = new FileWriter(usersFilePath);
  1180. out.write(writer.toString());
  1181. }
  1182. catch (IOException e) {
  1183. mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
  1184. }
  1185. finally {
  1186. if (in != null) {
  1187. try {
  1188. in.close();
  1189. }
  1190. catch (IOException e) {
  1191. // Ignore
  1192. }
  1193. }
  1194. if (out != null) {
  1195. try {
  1196. out.close();
  1197. }
  1198. catch (IOException e) {
  1199. // Ignore
  1200. }
  1201. }
  1202. }
  1203. }
  1204. }
  1205. }