Solved Reseting Players in File

Discussion in 'Plugin Development' started by PreFiXAUT, Feb 1, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    PreFiXAUT

    Hey Guys, I'm trying to create an Lobby-System which should read out Data out of my "Database" (and File which I created).
    When the Server stops, all Players should be removed so I also have to update my File. For this I'm using this Function:
    Show Spoiler

    Code:java
    1. public static boolean resetAllPlayers()
    2. {
    3. checkFile();
    4. File tempFile = new File("plugins/Lobbys","reset.tmp");
    5. try {
    6. reader = new FileReader(file);
    7. writer = new FileWriter(tempFile);
    8. BufferedReader br = new BufferedReader(reader);
    9. BufferedWriter bw = new BufferedWriter(writer);
    10. lnr.skip(Long.MAX_VALUE);
    11. for (int i=0; i <= lnr.getLineNumber(); i++)
    12. {
    13. br.skip(i);
    14. if (br.readLine() == null) continue;
    15. String [] data = br.readLine().split(",");
    16. bw.write(data[0] + "," + data[1] + "," + data[2] + "," + data[3] + "," + 0);
    17. }
    18. lnr.close();
    19. br.close();
    20. bw.close();
    21. writer.close();
    22. reader.close();
    23. file.delete();
    24. tempFile.renameTo(file);
    25. return true;
    26. } catch (Exception e) {
    27. e.printStackTrace();
    28. return false;
    29. }
    30. }

    Not the problem is, that it won't update the Players (last number in line) in the File, it erases the entire Line. I didn't try it out with more Lines yet (maybe it's deleting the hole File).

    Could someone help me? Thanks in advance.
     
  2. Offline

    nate_alex

    So do you want the players inventory to be cleared when the join or do you want the player data to be removed?


    Edit:
    So what is the reason that you need to remove all the player data on shutdown?
     
  3. Offline

    PreFiXAUT

    It's because otherwise the the Data would say (example): 20 Players are in Lobby1. Max Players are 24. Only 4 can join now > Server has started and there are just 5 Players on.
    So I'ld like to remove them when the Server shutsdown so when the Server starts again, all Lobbys are joinable.
    But instead of removing/replacing the Players in the File, it's removing the entire Line.
     
  4. Offline

    igwb

    I think the problem is the way you read the file. According to the Java documentation BufferedReader.skip(Integer) skips that number of Charachters, not lines. Also, as far as I know BufferedReader.readLine() returns the line the reader is on and move on to the next automatically, so you might want to try something like:


    Code:java
    1. String readLine = br.readLine();
    2.  
    3. while(readLine != null) {
    4.  
    5. String [] data = readLine.split(",");
    6.  
    7. bw.write(data[0] + "," + data[1] + "," + data[2] + "," + data[3] + "," + 0);
    8.  
    9. readLine = br.readLine();
    10.  
    11. }


    http://docs.oracle.com/javase/6/docs/api/java/io/BufferedReader.html

    Also I don't quite get why you don't delete the file if it only holds the players you want to reset anyway?
     
  5. Offline

    PreFiXAUT

    igwb Oh sorry I didn't update the Code yet :/
    I allready cleared that out, new Code:
    Show Spoiler

    Code:java
    1. public static boolean resetAllPlayers()
    2. {
    3. checkFile();
    4. File tempFile = new File("plugins/Lobbys","reset.tmp");
    5. try {
    6. reader = new FileReader(file);
    7. writer = new FileWriter(tempFile);
    8. BufferedReader br = new BufferedReader(reader);
    9. BufferedWriter bw = new BufferedWriter(writer);
    10. for (int i=0; br.ready(); i++)
    11. {
    12. if (br.readLine() == null) continue;
    13. String [] data = br.readLine().split(",");
    14. bw.write(data[0] + "," + data[1] + "," + data[2] + "," + data[3] + "," + 0);
    15. }
    16. br.close();
    17. bw.close();
    18. writer.close();
    19. reader.close();
    20. file.delete();
    21. tempFile.renameTo(file);
    22. return true;
    23. } catch (Exception e) {
    24. e.printStackTrace();
    25. return false;
    26. }
    27. }


    I guess I allready have the problem, 1 mom
     
  6. Offline

    igwb

    PreFiXAUT Could you perhaps post the file you are trying to read and the output file?
     
  7. Offline

    PreFiXAUT

    igwb The output file is a temp file created at the beginning of the function. The original File is deleted and the tempfile is renamed to the original File.
    file:
    Code:
    -108,81,32,24,10
    I have no Idea what I've changed but now it's working xD
    Code I'm using now:
    Show Spoiler
    Code:java
    1. public static boolean resetAllPlayers()
    2. {
    3. checkFile();
    4. File tempFile = new File("plugins/Lobbys","reset.tmp");
    5. try {
    6. reader = new FileReader(file);
    7. writer = new FileWriter(tempFile);
    8. BufferedReader br = new BufferedReader(reader);
    9. BufferedWriter bw = new BufferedWriter(writer);
    10. String temp;
    11. for (int i=0; (temp = br.readLine()) != null ; i++)
    12. {
    13. String [] data = temp.split(",");
    14. bw.write(data[0] + "," + data[1] + "," + data[2] + "," + data[3] + "," + 0);
    15. }
    16. br.close();
    17. bw.close();
    18. writer.close();
    19. reader.close();
    20. file.delete();
    21. tempFile.renameTo(file);
    22. return true;
    23. } catch (Exception e) {
    24. e.printStackTrace();
    25. return false;
    26. }
    27. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page