Advertisement
Guest User

Untitled

a guest
May 27th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.95 KB | None | 0 0
  1. package com.astrect.rs2.saving;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.nio.file.Files;
  10. import java.nio.file.Path;
  11. import java.nio.file.Paths;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. import java.util.LinkedList;
  15. import java.util.List;
  16. import java.util.Map;
  17. import java.util.concurrent.ExecutorService;
  18. import java.util.concurrent.Executors;
  19.  
  20. import com.astrect.AstrectConstants;
  21. import com.astrect.rs2.commands.Command;
  22. import com.astrect.rs2.commands.CommandHandler;
  23. import com.astrect.rs2.model.Location;
  24. import com.astrect.rs2.model.item.Item;
  25. import com.astrect.rs2.model.players.DonatorStatus;
  26. import com.astrect.rs2.model.players.PkStatus;
  27. import com.astrect.rs2.model.players.Player;
  28. import com.astrect.rs2.model.players.PlayerRights;
  29. import com.astrect.rs2.saving.impl.SaveAccValue;
  30. import com.astrect.rs2.saving.impl.SaveArmaKills;
  31. import com.astrect.rs2.saving.impl.SaveAtkType;
  32. import com.astrect.rs2.saving.impl.SaveBandosKills;
  33. import com.astrect.rs2.saving.impl.SaveBank;
  34. import com.astrect.rs2.saving.impl.SaveBountyPoints;
  35. import com.astrect.rs2.saving.impl.SaveClan;
  36. import com.astrect.rs2.saving.impl.SaveCleaned;
  37. import com.astrect.rs2.saving.impl.SaveCreatedLong;
  38. import com.astrect.rs2.saving.impl.SaveCreatedString;
  39. import com.astrect.rs2.saving.impl.SaveCustomInventory;
  40. import com.astrect.rs2.saving.impl.SaveDeathcount;
  41. import com.astrect.rs2.saving.impl.SaveDiced;
  42. import com.astrect.rs2.saving.impl.SaveDonatorPoints;
  43. import com.astrect.rs2.saving.impl.SaveDonatorPointsBought;
  44. import com.astrect.rs2.saving.impl.SaveDonatorStatus;
  45. import com.astrect.rs2.saving.impl.SaveEarningPotential;
  46. import com.astrect.rs2.saving.impl.SaveEnergy;
  47. import com.astrect.rs2.saving.impl.SaveExpLock;
  48. import com.astrect.rs2.saving.impl.SaveFightCavesWave;
  49. import com.astrect.rs2.saving.impl.SaveHunting;
  50. import com.astrect.rs2.saving.impl.SaveIP;
  51. import com.astrect.rs2.saving.impl.SaveInventory;
  52. import com.astrect.rs2.saving.impl.SaveKillcount;
  53. import com.astrect.rs2.saving.impl.SaveLastLoyaltyPointsReward;
  54. import com.astrect.rs2.saving.impl.SaveLastVoted;
  55. import com.astrect.rs2.saving.impl.SaveLocation;
  56. import com.astrect.rs2.saving.impl.SaveLoyaltyPoints;
  57. import com.astrect.rs2.saving.impl.SaveMagicbook;
  58. import com.astrect.rs2.saving.impl.SavePass;
  59. import com.astrect.rs2.saving.impl.SavePkPoints;
  60. import com.astrect.rs2.saving.impl.SavePrayerBook;
  61. import com.astrect.rs2.saving.impl.SavePreviousSessionTime;
  62. import com.astrect.rs2.saving.impl.SavePvPRating;
  63. import com.astrect.rs2.saving.impl.SaveRights;
  64. import com.astrect.rs2.saving.impl.SaveSaraKills;
  65. import com.astrect.rs2.saving.impl.SaveSkullTimer;
  66. import com.astrect.rs2.saving.impl.SaveSlayerAmount;
  67. import com.astrect.rs2.saving.impl.SaveSlayerTask;
  68. import com.astrect.rs2.saving.impl.SaveSpec;
  69. import com.astrect.rs2.saving.impl.SaveStatus;
  70. import com.astrect.rs2.saving.impl.SaveUsername;
  71. import com.astrect.rs2.saving.impl.SaveVotePoints;
  72. import com.astrect.rs2.saving.impl.SaveZamorakKills;
  73. import com.astrect.rs2.util.TextUtils;
  74.  
  75. public class PlayerSaving
  76. {
  77.  
  78. /**
  79. * The saving directory.
  80. */
  81. public static final File CHARACTER_FILE_DIRECTORY = new File(System.getProperty("user.home"), ""+AstrectConstants.SERVER_NAME+"-data/users/");
  82.  
  83. /**
  84. * The buffer size used for saving and loading.
  85. */
  86. public static final int BUFFER_SIZE = 1024;
  87.  
  88. /**
  89. * The single thread work service used for queued saving.
  90. */
  91. private final ExecutorService saveThread = Executors.newSingleThreadExecutor();
  92.  
  93. /**
  94. * Holds all the SaveObjects meant for loading.
  95. */
  96. private final Map<String, SaveObject> saveData;
  97.  
  98. /**
  99. * The collection of SaveObjects meant for iteration.
  100. */
  101. private final List<SaveObject> saveList;
  102.  
  103. /**
  104. * The PlayerSaving singleton.
  105. */
  106. private final static PlayerSaving singleton = new PlayerSaving();
  107.  
  108. public static void saveGame(Player player) {
  109. String username = player.getName();
  110. Path path = Paths.get(CHARACTER_FILE_DIRECTORY + File.separator + username.charAt(0) + "/", username + ".txt");
  111.  
  112. if (Files.notExists(path)) {
  113. try {
  114. Files.createDirectories(path.getParent());
  115. } catch (IOException e) {
  116. e.printStackTrace();
  117. }
  118. }
  119.  
  120. try (BufferedWriter writer = Files.newBufferedWriter(path)) {
  121. writer.write("PLAYER_INFO"); writer.newLine();
  122. writer.write("username = " + username); writer.newLine();
  123. writer.write("password = " + player.getPassword()); writer.newLine();
  124. writer.write("ip-address = " + player.getFullIP()); writer.newLine();
  125. writer.write("rights = " + player.getRights().toString()); writer.newLine();
  126. writer.write("donator-rights = " + player.getDonatorStatus().toString()); writer.newLine();
  127. writer.write("location = " + player.getLocation().getX() + "\t" + player.getLocation().getY() + "\t" + player.getLocation().getZ()); writer.newLine();
  128.  
  129. writer.write("PLAYER_POINTS"); writer.newLine();
  130. writer.write("donator-points = " + player.getPoints().getDonatorPoints()); writer.newLine();
  131. writer.write("total-donated = " + player.getPoints().getDonatorPointsBought()); writer.newLine();
  132. writer.write("killcount = " + player.getKillCount()); writer.newLine();
  133. writer.write("death-count = " + player.getDeathCount()); writer.newLine();
  134. writer.write("pk-points = " + player.getPoints().getPkPoints()); writer.newLine();
  135. writer.write("bounty-points = " + player.getPoints().getBountyPoints()); writer.newLine();
  136. writer.write("loyalty-points = " + player.getPoints().getLoyaltyPoints()); writer.newLine();
  137. writer.write("vote-points = " + player.getPoints().getVotingPoints()); writer.newLine();
  138.  
  139. writer.write("PLAYER_MISC"); writer.newLine();
  140. writer.write("godwars-kills = " + player.godWarsKillCount[0] + "\t" + player.godWarsKillCount[1] + "\t" + player.godWarsKillCount[2] + "\t" + player.godWarsKillCount[3]); writer.newLine();
  141. writer.write("attack-type = " + player.getCombatEntity().getAtkType()); writer.newLine();
  142. writer.write("clan-chat = " + player.getAttribute().getString("clan_chat_name")); writer.newLine();
  143. writer.write("player-diced = " + player.getDiced()); writer.newLine();
  144. writer.write("earning-potential = " + player.earningPotential); writer.newLine();
  145. writer.write("energy = " + player.getAttribute().getInt("ENERGY")); writer.newLine();
  146. writer.write("fight-caves-wave = " + player.fightCavesWave); writer.newLine();
  147. writer.write("exp-lock = " + player.xpLock); writer.newLine();
  148. writer.write("bounty-hunting = " + player.isBountyHunting()); writer.newLine();
  149. writer.write("skull-timer = " + player.getSkullTimer()); writer.newLine();
  150. writer.write("special-energy = " + player.getSpecBar().getAmount()); writer.newLine();
  151. writer.write("slayer-task = " + player.slayerTask); writer.newLine();
  152. writer.write("slayer-amount = " + player.slayerAm); writer.newLine();
  153. writer.write("pk-status = " + player.getPkStatus().toString()); writer.newLine();
  154. writer.write("magic-book = " + player.getSpellBook().toInteger()); writer.newLine();
  155. writer.write("prayer-book = " + player.getPrayers().isDefaultPrayerbook());
  156. writer.write("last-voted = " + player.getLastVoted()); writer.newLine();
  157.  
  158. writer.write("PLAYER_APPEARANCE"); writer.newLine();
  159. for (int i = 0; i < player.getAppearance().getLook().length; i++) {
  160. writer.write("character-look = " + i + "\t" + player.getAppearance().getLook()[i]);
  161. }
  162. writer.newLine();
  163.  
  164. writer.write("PLAYER_INVENTORY"); writer.newLine();
  165. for (int i = 0; i < player.getInventory().toArray().length; i++) {
  166. Item item = player.getInventory().toArray()[i];
  167. if (item != null)
  168. writer.write("inventory-slot = " + i + "\t" + item.getId() + "\t" + item.getCount());
  169. }
  170. writer.newLine();
  171.  
  172. writer.write("PLAYER_EQUIPMENT"); writer.newLine();
  173. for (int i = 0; i < player.getEquipment().toArray().length; i++) {
  174. Item item = player.getEquipment().toArray()[i];
  175. writer.write("equip-slot = " + i + "\t" + (item == null ? "-1" : item.getId()) + "\t" + (item == null ? "0" : item.getCount()));
  176. }
  177. writer.newLine();
  178.  
  179. writer.write("PLAYER_BANK"); writer.newLine();
  180. for (int i = 0; i < player.getBank().toArray().length; i++) {
  181. Item item = player.getBank().toArray()[i];
  182. if (item != null)
  183. writer.write("bank-slot = " + i + "\t" + item.getId() + "\t" + item.getCount());
  184. }
  185. writer.newLine();
  186.  
  187. writer.write("PLAYER_CUSTOM_EQUIPMENT"); writer.newLine();
  188. for (int i = 0; i < player.getCustomEquipment().toArray().length; i++) {
  189. Item item = player.getCustomEquipment().toArray()[i];
  190. if (item != null)
  191. writer.write("custom-equip-slot = " + i + "\t" + item.getId() + "\t" + item.getCount());
  192. }
  193. writer.newLine();
  194.  
  195. writer.write("PLAYER_FRIENDS"); writer.newLine();
  196. for (int i = 0; i < player.getFriends().toArray().length; i++) {
  197. if (player.getFriends().toArray()[i] > 0) {
  198. writer.write("character-friend = ");
  199. writer.write(Integer.toString(i));
  200. writer.write("\t");
  201. writer.write(Long.toString(player.getFriends().toArray()[i]));
  202. writer.newLine();
  203. }
  204. }
  205. writer.newLine();
  206.  
  207. /*
  208. * Dont add anything after this
  209. */
  210. writer.write("[EOF]");
  211. } catch (IOException e) {
  212. e.printStackTrace();
  213. }
  214. }
  215.  
  216. private static final int PLAYER_DETAILS = 0, PLAYER_POINTS = 1, PLAYER_MISC = 2, PLAYER_INVENTORY = 3, PLAYER_EQUIPMENT = 4, PLAYER_BANK = 5, PLAYER_CUSTOM_EQUIPMENT = 6, PLAYER_APPEARANCE = 7, PLAYER_SKILLS = 8, PLAYER_FRIENDS = 9;
  217.  
  218. public static boolean loadGame(Player player, String password) {
  219. String username = player.getName();
  220. Path path = Paths.get(CHARACTER_FILE_DIRECTORY + File.separator + username.charAt(0) + "/", username + ".txt");
  221.  
  222. if (Files.notExists(path)) {
  223. return false;
  224. }
  225.  
  226. try (BufferedReader reader = Files.newBufferedReader(path)) {
  227. String line;
  228. int mode = 0;
  229. while ((line = reader.readLine()) != null) {
  230. line = line.trim();
  231. int spot = line.indexOf("=");
  232. if (spot > -1) {
  233. String key = line.substring(0, spot).trim();
  234. String dataValue = line.substring(spot + 1).trim();
  235. String[] dataValues = dataValue.split("\t");
  236. switch (mode) {
  237.  
  238. case PLAYER_DETAILS:
  239. switch (key) {
  240.  
  241. case "password":
  242. if (!password.equalsIgnoreCase(dataValue)) {
  243. return false;
  244. }
  245. break;
  246.  
  247. case "rights":
  248. PlayerRights rights = PlayerRights.PLAYER;
  249. try {
  250. rights = PlayerRights.valueOf(dataValue);
  251. } catch (Exception e) {
  252.  
  253. }
  254. player.setRights(rights);
  255. break;
  256.  
  257. case "donator-rights":
  258. DonatorStatus donator = DonatorStatus.NON_DONATOR;
  259. try {
  260. donator = DonatorStatus.valueOf(dataValue);
  261. } catch (Exception e) {
  262.  
  263. }
  264. player.setDonatorStatus(donator);
  265. break;
  266.  
  267. case "location":
  268. int x = Integer.parseInt(dataValues[0]);
  269. int y = Integer.parseInt(dataValues[1]);
  270. int z = Integer.parseInt(dataValues[2]);
  271. player.setLocation(Location.create(x, y, z));
  272. break;
  273. }
  274.  
  275. case PLAYER_POINTS:
  276. switch (key) {
  277. case "donator-points":
  278. player.getPoints().setDonatorPoints(Integer.parseInt(dataValue));
  279. break;
  280. case "total-donated":
  281. player.getPoints().setDonatorsBought(Integer.parseInt(dataValue));
  282. break;
  283.  
  284. case "killcount":
  285. player.setKillCount(Integer.parseInt(dataValue));
  286. break;
  287.  
  288. case "death-count":
  289. player.setDeathCount(Integer.parseInt(dataValue));
  290. break;
  291.  
  292. case "pk-points":
  293. player.getPoints().setPkPoints(Integer.parseInt(dataValue));
  294. break;
  295. case "bounty-points":
  296. player.getPoints().setBountyPoints(Integer.parseInt(dataValue));
  297. break;
  298. case "loyalty-points":
  299. player.getPoints().setLoyaltyPoints(Integer.parseInt(dataValue));
  300. break;
  301. case "vote-points":
  302. player.getPoints().setVotingPoints(Integer.parseInt(dataValue));
  303. break;
  304. }
  305.  
  306. case PLAYER_MISC:
  307. switch (key) {
  308.  
  309. case "godwars-kills":
  310. for (int i = 0; i < dataValues.length; i++) {
  311. player.godWarsKillCount[i] = Integer.parseInt(dataValues[i]);
  312. }
  313. break;
  314.  
  315. case "attack-type":
  316. player.getCombatEntity().setAtkType(Integer.parseInt(dataValue));
  317. break;
  318.  
  319. case "clan-chat":
  320. player.getAttribute().put("clan_chat_name", dataValue);
  321. break;
  322.  
  323. case "player-diced":
  324. player.setDiced(Integer.parseInt(dataValue));
  325. break;
  326.  
  327. case "earning-potential":
  328. player.earningPotential = Integer.parseInt(dataValue);
  329. break;
  330. case "energy":
  331. player.getAttribute().put("ENERGY", Integer.parseInt(dataValue));
  332. break;
  333.  
  334. case "fight-caves-wave":
  335. player.fightCavesWave = Integer.parseInt(dataValue);
  336. break;
  337. case "exp-lock":
  338. player.xpLock = Boolean.parseBoolean(dataValue);
  339. break;
  340.  
  341. case "bounty-hunting":
  342. player.setBountyHunting(Boolean.parseBoolean(dataValue));
  343. break;
  344.  
  345. case "skull-timer":
  346. player.setSkullTimer(Integer.parseInt(dataValue));
  347. break;
  348. case "special-energy":
  349. player.getSpecBar().setAmount(Integer.parseInt(dataValue));
  350. break;
  351.  
  352. case "slayer-task":
  353. player.slayerTask = Integer.parseInt(dataValue);
  354. break;
  355.  
  356. case "slayer-amount":
  357. player.slayerAm = Integer.parseInt(dataValue);
  358. break;
  359.  
  360. case "pk-status":
  361. player.setStatus(PkStatus.valueOf(dataValue));
  362. break;
  363. case "magic-book":
  364. player.getSpellBook().changeSpellBook(Integer.parseInt(dataValue));
  365. break;
  366.  
  367. case "prayer-book":
  368. player.getPrayers().setPrayerbook(Boolean.parseBoolean(dataValue));
  369. break;
  370.  
  371. case "last-voted":
  372. player.setLastVoted(Long.parseLong(dataValue));
  373. break;
  374. }
  375. break;
  376.  
  377. case PLAYER_APPEARANCE:
  378. if (key.equals("character-look")) {
  379. player.getAppearance().getLook()[Integer.parseInt(dataValues[0])] = Integer.parseInt(dataValues[1]);
  380. }
  381. break;
  382.  
  383. case PLAYER_SKILLS:
  384. if (key.equals("character-skill")) {
  385. final int level = Integer.parseInt(dataValues[1]);
  386. double experience = Double.parseDouble(dataValues[2]);
  387.  
  388. if (experience < 0) {
  389. experience = 0;
  390. } else if (experience > 200000000) {
  391. experience = 200000000;
  392. }
  393. player.getSkills().setLevel(Integer.parseInt(dataValues[0]), level);
  394. player.getSkills().setExperience(Integer.parseInt(dataValues[0]), (int) experience);
  395. }
  396. break;
  397.  
  398. case PLAYER_INVENTORY:
  399. if (key.equals("inventory-slot")) {
  400. int index = Integer.parseInt(dataValues[0]);
  401. int id = Integer.parseInt(dataValues[1]);
  402. int amount = Integer.parseInt(dataValues[2]);
  403. player.getInventory().add(new Item(id, amount), index);
  404. }
  405. break;
  406.  
  407. case PLAYER_EQUIPMENT:
  408. if (key.equals("equip-slot")) {
  409. int index = Integer.parseInt(dataValues[0]);
  410. int item = Integer.parseInt(dataValues[1]);
  411. int amount = Integer.parseInt(dataValues[2]);
  412. player.getEquipment().set(index, new Item(item, amount));
  413. }
  414. break;
  415.  
  416. case PLAYER_BANK:
  417. if (key.equals("bank-slot")) {
  418. int index = Integer.parseInt(dataValues[0]);
  419. int id = Integer.parseInt(dataValues[1]);
  420. int amount = Integer.parseInt(dataValues[2]);
  421. player.getBank().add(new Item(id, amount), index);
  422. }
  423. break;
  424.  
  425. case PLAYER_CUSTOM_EQUIPMENT:
  426. if (key.equals("custom-equip-slot")) {
  427. int index = Integer.parseInt(dataValues[0]);
  428. int id = Integer.parseInt(dataValues[1]);
  429. int amount = Integer.parseInt(dataValues[2]);
  430. player.getCustomEquipment().add(new Item(id, amount), index);
  431. }
  432. break;
  433.  
  434. case PLAYER_FRIENDS:
  435. if (key.equals("character-friend")) {
  436. player.getFriends().toArray()[Integer.parseInt(dataValues[0])] = Long.parseLong(dataValues[1]);
  437. }
  438. break;
  439.  
  440. }
  441. } else {
  442. if (line.equals("PLAYER_DETAILS")) {
  443. mode = PLAYER_DETAILS;
  444. } else if (line.equals("PLAYER_POINTS")) {
  445. mode = PLAYER_POINTS;
  446. } else if (line.equals("PLAYER_MISC")) {
  447. mode = PLAYER_MISC;
  448. } else if (line.equals("PLAYER_EQUIPMENT")) {
  449. mode = PLAYER_EQUIPMENT;
  450. } else if (line.equals("PLAYER_APPEARANCE")) {
  451. mode = PLAYER_APPEARANCE;
  452. } else if (line.equals("PLAYER_SKILLS")) {
  453. mode = PLAYER_SKILLS;
  454. } else if (line.equals("PLAYER_INVENTORY")) {
  455. mode = PLAYER_INVENTORY;
  456. } else if (line.equals("PLAYER_BANK")) {
  457. mode = PLAYER_BANK;
  458. } else if (line.equals("CUSTOM_EQUIPMENT")) {
  459. mode = PLAYER_CUSTOM_EQUIPMENT;
  460. } else if (line.equals("FRIENDS")) {
  461. mode = PLAYER_FRIENDS;
  462. }else if (line.equals("[EOF]")) {
  463. try {
  464. reader.close();
  465. return true;
  466. } catch (IOException ignored) {
  467. }
  468. }
  469. }
  470. }
  471. } catch (IOException e) {
  472. e.printStackTrace();
  473. }
  474. return true;
  475. }
  476.  
  477. /**
  478. * Constructs a new PlayerSaving.
  479. */
  480. public PlayerSaving()
  481. {
  482. saveList = new ArrayList<>();
  483. initSaveObjects();
  484. ((ArrayList<SaveObject>) saveList).trimToSize();
  485. saveData = new HashMap<>();
  486.  
  487. if (!CHARACTER_FILE_DIRECTORY.exists())
  488. {
  489. if (!CHARACTER_FILE_DIRECTORY.mkdirs())
  490. {
  491. throw new InternalError("Unable to make users directory.");
  492. }
  493. }
  494.  
  495. for (SaveObject so : saveList)
  496. {
  497. saveData.put(so.getName(), so);
  498. }
  499. }
  500.  
  501. /**
  502. * Initializes all SaveObjects in a specific order.
  503. */
  504. private void initSaveObjects()
  505. {
  506. saveList.add(new SaveUsername("Name"));
  507. saveList.add(new SavePass("Pass"));
  508. saveList.add(new SaveAccValue("AccValue"));
  509. saveList.add(new SaveIP("hostAddress"));
  510. saveList.add(new SaveRights("Rights"));
  511. saveList.add(new SaveStatus("PkStatus"));
  512. saveList.add(new SaveCreatedString("CreatedStr"));
  513. saveList.add(new SaveLocation("Location"));
  514. saveList.add(new SaveEnergy("Energy"));
  515. saveList.add(new SavePvPRating("PvPRating"));
  516. saveList.add(new SaveBountyPoints("BHPoints"));
  517. saveList.add(new SaveDiced("Diced"));
  518. saveList.add(new SavePreviousSessionTime("PreviousTime"));
  519. saveList.add(new SaveCreatedLong("CreatedLong"));
  520. saveList.add(new SaveLastLoyaltyPointsReward("lastLoyalty"));
  521. saveList.add(new SaveSpec("Spec"));
  522. saveList.add(new SaveCustomInventory("custom-inv"));
  523. saveList.add(new SaveCustomEquipment("custom-equip"));
  524. saveList.add(new SaveHunting("Hunting"));
  525. saveList.add(new SaveAtkType("AtkType"));
  526. saveList.add(new SaveMagicbook("MagicBook"));
  527. saveList.add(new SaveExpLock("XpLock"));
  528. saveList.add(new SavePrayerBook("Altar"));
  529. saveList.add(new SaveClan("Clan"));
  530. saveList.add(new SaveDonatorPointsBought("DonatorsBought"));
  531. saveList.add(new SaveDonatorPoints("DonatorPoints"));
  532. saveList.add(new SavePkPoints("PkPoints"));
  533. saveList.add(new SaveVotePoints("VotePoints"));
  534. saveList.add(new SaveLoyaltyPoints("LoyaltyPoints"));
  535. saveList.add(new SaveSkullTimer("Skull"));
  536. saveList.add(new SaveEarningPotential("earningPotential"));
  537. saveList.add(new SaveArmaKills("Arma-KC"));
  538. saveList.add(new SaveBandosKills("Band-KC"));
  539. saveList.add(new SaveZamorakKills("Zammy-KC"));
  540. saveList.add(new SaveSaraKills("Sara-KC"));
  541. saveList.add(new SaveSlayerTask("slayerTask"));
  542. saveList.add(new SaveSlayerAmount("taskAmount"));
  543. saveList.add(new SaveKillcount("KillCount"));
  544. saveList.add(new SaveDeathcount("DeathCount"));
  545. saveList.add(new SaveCleaned("Cleaned"));
  546. saveList.add(new SaveFightCavesWave("FightCavesWave"));
  547. saveList.add(new SaveLastVoted("LastVoted"));
  548. saveList.add(new SaveDonatorStatus());
  549.  
  550. // Containers, skills etc
  551. saveList.add(new SaveSkills("Skills"));
  552. saveList.add(new SaveInventory("Inventory"));
  553. saveList.add(new SaveBank("Bank"));
  554. saveList.add(new SaveEquipment("Equip"));
  555. saveList.add(new SaveLook("Look"));
  556. saveList.add(new SaveFriends("Friends"));
  557. }
  558.  
  559. /**
  560. * @param player
  561. * @param message
  562. */
  563. public void save(Player player, String message)
  564. {
  565. if (player.getRights().superiorTo(PlayerRights.ADMINISTRATOR))
  566. {
  567. player.getActionSender().sendMessage(message);
  568. }
  569.  
  570. save(player);
  571. }
  572.  
  573. /**
  574. * Saves the player's data to his character file.
  575. *
  576. * @param player
  577. * @return true if successful, false if not
  578. */
  579. public boolean save(Player player)
  580. {
  581. // try (BufferedWriter out = new BufferedWriter(new FileWriter(getCharFile(player)), BUFFER_SIZE))
  582. // {
  583. // for (SaveObject so : saveList)
  584. // {
  585. // boolean saved = so.save(player, out);
  586. //
  587. // if (saved)
  588. // {
  589. // out.newLine();
  590. // }
  591. // }
  592. //
  593. // return true;
  594. // }
  595. // catch (IOException ex)
  596. // {
  597. // System.out.println("Player's name: " + player.getName());
  598. // ex.printStackTrace();
  599. // return false;
  600. // }
  601. saveGame(player);
  602. return true;
  603. }
  604.  
  605. /**
  606. * Loads the player's data from his character file.
  607. *
  608. * @param player
  609. */
  610. public void load(Player player)
  611. {
  612. try (BufferedReader in = new BufferedReader(new FileReader(getCharFile(player)), BUFFER_SIZE))
  613. {
  614. String line;
  615.  
  616. while ((line = in.readLine()) != null)
  617. {
  618. if (line.length() <= 1)
  619. {
  620. continue;
  621. }
  622.  
  623. String[] parts = line.split("=");
  624. String name = parts[0].trim();
  625. String values = null;
  626.  
  627. if (parts.length > 1)
  628. {
  629. values = parts[1].trim();
  630. }
  631.  
  632. SaveObject so = saveData.get(name);
  633.  
  634. if (so != null)
  635. {
  636. so.load(player, values, in);
  637. }
  638. else
  639. {
  640. System.out.println("Nulled saveobject for " + player.getName() + " line: " + line);
  641. }
  642. }
  643.  
  644. //TODO highscores saving
  645. }
  646. catch (IOException ex)
  647. {
  648. ex.printStackTrace();
  649. }
  650.  
  651. player.init();
  652. }
  653.  
  654. public void saveLog(final String file, LinkedList<String> lines)
  655. {
  656. saveLog(new File(file), lines);
  657. }
  658.  
  659. /**
  660. * Saves a line to the specified log file.
  661. *
  662. * @param file
  663. * @param line
  664. */
  665. public void saveLog(final File file, List<String> lines)
  666. {
  667. String[] stringArray = new String[lines.size()];
  668. int idx = 0;
  669.  
  670. for (String line : lines)
  671. {
  672. stringArray[idx++] = line;
  673. }
  674.  
  675. saveLog(file, stringArray);
  676. }
  677.  
  678. /**
  679. * Saves a line to the specified log file.
  680. *
  681. * @param file
  682. * @param line
  683. */
  684. public void saveLog(final String file, final String... lines)
  685. {
  686. saveLog(new File(file), lines);
  687. }
  688.  
  689. public void saveLog(final File file, final String... lines)
  690. {
  691. Runnable runnable = () -> TextUtils.writeToFile(file, lines);
  692. saveThread.submit(runnable);
  693. }
  694.  
  695. /**
  696. * Executes the runnable on the save thread.
  697. *
  698. * @param runnable
  699. */
  700. public void submit(Runnable runnable)
  701. {
  702. saveThread.submit(runnable);
  703. }
  704.  
  705. /**
  706. * Gets the character file for specified player.
  707. *
  708. * @param username The player username.
  709. * @returns The players save file.
  710. */
  711. public static File getCharFile(String username)
  712. {
  713. return new File(CHARACTER_FILE_DIRECTORY, username.toLowerCase() + ".txt");
  714. }
  715.  
  716. /**
  717. * Gets the character file for specified player.
  718. *
  719. * @param player The player.
  720. * @returns The players save file.
  721. */
  722. public static File getCharFile(Player player)
  723. {
  724. String username = player.getName();
  725. return getCharFile(username);
  726. }
  727.  
  728. /**
  729. * Gets the PlayerSaving singleton.
  730. */
  731. public static PlayerSaving getSaving()
  732. {
  733. return singleton;
  734. }
  735.  
  736. static
  737. {
  738. CommandHandler.submit(new Command("create", PlayerRights.OWNER)
  739. {
  740.  
  741. @Override
  742. public boolean execute(Player player, String input) throws Exception
  743. {
  744. System.out.println("Creating!");
  745. PlayerSaving.getSaving().save(player);
  746. return false;
  747. }
  748.  
  749. });
  750. }
  751.  
  752. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement