Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package server.model.players;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import server.util.Misc;
- /**
- * Handles all player player saving
- * @author Patrick van Elderen
- * @date Nov 26 2016 21:32 PM
- */
- public class PlayerSave {
- /**
- * An executor service which saves accounts on its own thread
- */
- private static final ExecutorService executor = Executors.newCachedThreadPool();
- public static int loadGame(Player p, String playerName, String playerPass) {
- return loadGame(p, playerName, playerPass, false);
- }
- /**
- * The log directory which we can use to log data
- */
- public static final File LOG_DIRECTORY = new File("./data/characters/");
- /**
- * Loading
- *
- * @throws IOException
- */
- public static int loadGame(Player p, String playerName, String playerPass, boolean withoutPass) {
- playerName = playerName.toLowerCase();
- Path path = Paths.get(LOG_DIRECTORY.getPath(), playerName.charAt(0) + File.separator + playerName + ".txt");
- if (!Files.exists(path)) {
- try {
- Files.createDirectories(path.getParent());
- Files.createFile(path);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- try (BufferedReader reader = Files.newBufferedReader(path)) {
- int mode = 0;
- String line;
- while ((line = reader.readLine()) != null) {
- line = line.trim();
- int spot = line.indexOf("=");
- if (spot > -1) {
- String key = line.substring(0, spot).trim();
- String value = line.substring(spot + 1).trim();
- String[] values = value.split("\t");
- switch (mode) {
- case 1:
- if (key.equals("character-password")) {
- if (playerPass.equalsIgnoreCase(value) || Misc.basicEncrypt(playerPass).equals(value)) {
- playerPass = Misc.basicEncrypt(playerPass);
- } else {
- return 3;
- }
- }
- break;
- }
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return 13;
- }
- /**
- * Saves the account for a player
- *
- * @param player
- * The {@link Player to save the account for
- */
- public static void saveGame(final Player player) {
- if (!player.saveFile || player.newPlayer || !player.saveCharacter) {
- return;
- }
- if (player.playerName == null || PlayerHandler.getPlayers().get(player.playerIndex) == null) {
- return;
- }
- writeData(player);
- }
- /**
- * Writes data to the character file
- *
- * @param p
- * The {@link Player} to write the data for
- */
- public static void writeData(Player p) {
- String username = p.playerName.toLowerCase();
- Path path = Paths.get(LOG_DIRECTORY.getPath(), username.charAt(0) + File.separator + username + ".txt");
- if (!Files.exists(path)) {
- try {
- Files.createDirectories(path.getParent());
- Files.createFile(path);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- try (BufferedWriter writer = Files.newBufferedWriter(path)) {
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static boolean playerExists(String name) {
- File file = null;
- file = new File("./data/characters/" + name + ".txt");
- return file != null && file.exists();
- }
- public static ExecutorService getExecutor() {
- return executor;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement