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