Advertisement
Guest User

Untitled

a guest
Nov 26th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. package server.model.players;
  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.IOException;
  8. import java.nio.file.Files;
  9. import java.nio.file.Path;
  10. import java.nio.file.Paths;
  11. import java.util.concurrent.ExecutorService;
  12. import java.util.concurrent.Executors;
  13.  
  14. import server.util.Misc;
  15.  
  16.  
  17.  
  18. /**
  19. * Handles all player player saving
  20. * @author Patrick van Elderen
  21. * @date Nov 26 2016 21:32 PM
  22. */
  23. public class PlayerSave {
  24.  
  25. /**
  26. * An executor service which saves accounts on its own thread
  27. */
  28. private static final ExecutorService executor = Executors.newCachedThreadPool();
  29.  
  30. public static int loadGame(Player p, String playerName, String playerPass) {
  31. return loadGame(p, playerName, playerPass, false);
  32. }
  33.  
  34. /**
  35. * The log directory which we can use to log data
  36. */
  37. public static final File LOG_DIRECTORY = new File("./data/characters/");
  38.  
  39. /**
  40. * Loading
  41. *
  42. * @throws IOException
  43. */
  44. public static int loadGame(Player p, String playerName, String playerPass, boolean withoutPass) {
  45. playerName = playerName.toLowerCase();
  46.  
  47. Path path = Paths.get(LOG_DIRECTORY.getPath(), playerName.charAt(0) + File.separator + playerName + ".txt");
  48.  
  49. if (!Files.exists(path)) {
  50. try {
  51. Files.createDirectories(path.getParent());
  52. Files.createFile(path);
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57.  
  58. try (BufferedReader reader = Files.newBufferedReader(path)) {
  59. int mode = 0;
  60. String line;
  61. while ((line = reader.readLine()) != null) {
  62. line = line.trim();
  63. int spot = line.indexOf("=");
  64. if (spot > -1) {
  65.  
  66. String key = line.substring(0, spot).trim();
  67. String value = line.substring(spot + 1).trim();
  68. String[] values = value.split("\t");
  69.  
  70. switch (mode) {
  71. case 1:
  72. if (key.equals("character-password")) {
  73. if (playerPass.equalsIgnoreCase(value) || Misc.basicEncrypt(playerPass).equals(value)) {
  74. playerPass = Misc.basicEncrypt(playerPass);
  75. } else {
  76. return 3;
  77. }
  78. }
  79. break;
  80. }
  81. }
  82. }
  83. } catch (IOException e) {
  84. e.printStackTrace();
  85. }
  86. return 13;
  87. }
  88.  
  89. /**
  90. * Saves the account for a player
  91. *
  92. * @param player
  93. * The {@link Player to save the account for
  94. */
  95. public static void saveGame(final Player player) {
  96. if (!player.saveFile || player.newPlayer || !player.saveCharacter) {
  97. return;
  98. }
  99. if (player.playerName == null || PlayerHandler.getPlayers().get(player.playerIndex) == null) {
  100. return;
  101. }
  102. writeData(player);
  103. }
  104.  
  105. /**
  106. * Writes data to the character file
  107. *
  108. * @param p
  109. * The {@link Player} to write the data for
  110. */
  111. public static void writeData(Player p) {
  112. String username = p.playerName.toLowerCase();
  113. Path path = Paths.get(LOG_DIRECTORY.getPath(), username.charAt(0) + File.separator + username + ".txt");
  114.  
  115. if (!Files.exists(path)) {
  116. try {
  117. Files.createDirectories(path.getParent());
  118. Files.createFile(path);
  119. } catch (IOException e) {
  120. e.printStackTrace();
  121. }
  122. }
  123.  
  124. try (BufferedWriter writer = Files.newBufferedWriter(path)) {
  125.  
  126.  
  127.  
  128.  
  129. } catch (IOException e) {
  130. e.printStackTrace();
  131. }
  132. }
  133.  
  134. public static boolean playerExists(String name) {
  135. File file = null;
  136. file = new File("./data/characters/" + name + ".txt");
  137. return file != null && file.exists();
  138. }
  139.  
  140. public static ExecutorService getExecutor() {
  141. return executor;
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement