Solved Skipped while loop?

Discussion in 'Plugin Development' started by PreFiXAUT, Mar 5, 2014.

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

    PreFiXAUT

    Hey Guys, I'm here again with a weird Problem.
    In my Plugin I'm using multible Databases, and to reduce the Bugs I added an Method to delete all dead Code in the Databases.
    Here I'm updating the 2 Main-Databases, where I'm looking in all Worlds for all Signs and check their Coordinates with the Data in the Database. When no Sign was found on the Coordinates, it'll removed.
    The Problem is currently with the secound Phace of it. When it's beeing removed from Database 1, I also want to remove it from Database 2, si I call the remove-Method.
    It works for the first time, and then the weird thing happens > It looks like it's skipping everything and goes into the next Method, which is reading Database 2.
    Now I can't delete/edit Database 2 anymore, and Method 1 get's useless.

    Source Code for 4 Methods:
    Show Spoiler
    Method to update it Step-by-Step
    Show Spoiler

    Code:java
    1. public static void updateData()
    2. {
    3. if (Lobbys.updateData0()) {
    4. if (Lobbys.updateData1()) {
    5. System.out.println("[" + Main.name + "] Update successful!");
    6. return;
    7. } else {
    8. System.out.println("[" + Main.name + "] ERROR: Update-Data 1.");
    9. return;
    10. }
    11. } else {
    12. System.out.println("[" + Main.name + "] ERROR: Update-Data 0.");
    13. return;
    14. }
    15. }
    updateData0:
    Show Spoiler

    Code:java
    1. private static boolean updateData0() {
    2. checkFile();
    3. try {
    4. reader = new FileReader(file);
    5. BufferedReader br = new BufferedReader(reader);
    6. File tempFile = new File("plugins/Lobbys/temp","updatedata.pptf");
    7. if (!tempFile.exists()) tempFile.createNewFile();
    8. writer = new FileWriter(tempFile);
    9. BufferedWriter bw = new BufferedWriter(writer);
    10. String read;
    11. boolean flag = false;
    12. boolean flag1 = false;
    13. while ((read = br.readLine()) != null) {
    14. System.out.println("DEBUG: Read > "+ read);
    15. String[] data = read.split("µ");
    16. if (!Players.checkGroupExistance(data[0])) Players.addGroup(data[0]);
    17. for(final World world : Bukkit.getWorlds())
    18. {
    19. for(final Chunk chunk : world.getLoadedChunks())
    20. {
    21. for(final BlockState b : chunk.getTileEntities())
    22. {
    23. if(b instanceof Sign)
    24. {
    25. Sign sign = (Sign) b;
    26. if (sign.getX() == Double.parseDouble(data[2]) && sign.getY() == Double.parseDouble(data[3]) && sign.getZ() == Double.parseDouble(data[4])) {
    27. flag1 = true;
    28. flag = true;
    29. System.out.println("DEBUG: Found Sign on Coordinates: " + data[2] + ", " + data[3] + ", " + data[4] + ", with ID: " + data[0]);
    30. bw.write(read);
    31. bw.newLine();
    32. }
    33. }
    34. }
    35. }
    36. }
    37. if (flag1 == false) {
    38. System.out.println("DEBUG: Trying to remove: "+ data[0]);
    39. if (Players.removeGroup(data[0])) System.out.println("[" + Main.name + "] Removed Group with ID: " + data[0]);
    40. else System.out.println("DEBUG: ERROR by Removing group with Group ID > " + data[0]);
    41. flag=true;
    42. } else System.out.println("DEBUG: No remove for GroupID > " + data[0]);
    43. flag1=false;
    44. continue;
    45. }
    46. br.close();
    47. reader.close();
    48. bw.close();
    49. writer.close();
    50. if (flag) {
    51. file.delete();
    52. tempFile.renameTo(file);
    53. } tempFile.delete();
    54. return flag;
    55. } catch (Exception e) {
    56. e.printStackTrace();
    57. return false;
    58. }
    59. }
    updateData1:
    Show Spoiler

    Code:java
    1. private static boolean updateData1() {
    2. try {
    3. System.out.println("DEBUG: updateData1 called");
    4. reader = new FileReader(Main.groups);
    5. BufferedReader br = new BufferedReader(reader);
    6. String read1;
    7. while ((read1 = br.readLine()) != null) {
    8. String[] data = read1.split(",");
    9. if (Lobbys.getData(data[0]) == null) {
    10. if (!Players.removeGroup(data[0])) System.out.println("[" + Main.name + "] ERROR by removing Group!");
    11. }
    12. continue;
    13. }
    14. reader.close();
    15. br.close();
    16. return true;
    17. } catch (Exception e) {
    18. e.printStackTrace();
    19. return false;
    20. }
    21. }
    removeGroup (Works 100%)
    Show Spoiler

    Code:java
    1. public static boolean removeGroup(String id) {
    2. System.out.println("DEBUG: removeGroup called with ID: " + id);
    3. checkFile();
    4. try {
    5. System.out.println("DEBUG: in try");
    6. reader = new FileReader(file);
    7. BufferedReader br = new BufferedReader(reader);
    8. File tempFile = new File("plugins/Lobbys/temp","removegroup.pptf");
    9. if (!tempFile.exists()) tempFile.createNewFile();
    10. writer = new FileWriter(tempFile);
    11. BufferedWriter bw = new BufferedWriter(writer);
    12. String read;
    13. boolean flag = false;
    14. while ((read = br.readLine()) != null) {
    15. System.out.println("DEBUG: read > " + read);
    16. String[] data = read.split(",");
    17. if (!data[0].equalsIgnoreCase(id)) {
    18. System.out.println("DEBUG: ID != given ID, writing");
    19. bw.write(read);
    20. bw.newLine();
    21. } else {
    22. System.out.println("DEBUG: ID == given ID, will not write");
    23. flag = true;
    24. }
    25. }
    26. br.close();
    27. reader.close();
    28. bw.close();
    29. writer.close();
    30. if (flag) {
    31. file.delete();
    32. tempFile.renameTo(file);
    33. } else tempFile.delete();
    34. System.out.println("DEBUG: returning > " + flag);
    35. return flag;
    36. } catch (Exception e) {
    37. e.printStackTrace();
    38. return false;
    39. }
    40. }

    Console Output:
    Show Spoiler
    [15:49:53] [Server thread/INFO]: Updating all Signs![m
    [15:49:53] [Server thread/INFO]: It could take a bit and it could cause some laggs![m
    [15:49:53] [Server thread/INFO]: DEBUG: Read > hg01µhungergames01µ-108.0µ81.0µ31.0µworldµ~2µ~1µ~2µ_µ24µ0
    [15:49:53] [Server thread/INFO]: DEBUG: Found Sign on Coordinates: -108.0, 81.0, 31.0, with ID: hg01
    [15:49:53] [Server thread/INFO]: DEBUG: No remove for GroupID > hg01
    [15:49:53] [Server thread/INFO]: DEBUG: updateData1 called
    [15:49:53] [Server thread/INFO]: DEBUG: removeGroup called with ID: hg02
    [15:49:53] [Server thread/INFO]: DEBUG: in try
    [15:49:53] [Server thread/INFO]: DEBUG: read > hg01
    [15:49:53] [Server thread/INFO]: DEBUG: ID != given ID, writing
    [15:49:53] [Server thread/INFO]: DEBUG: read > hg02
    [15:49:53] [Server thread/INFO]: DEBUG: ID == given ID, will not write
    [15:49:53] [Server thread/INFO]: DEBUG: read > hg03
    [15:49:53] [Server thread/INFO]: DEBUG: ID != given ID, writing
    [15:49:53] [Server thread/INFO]: DEBUG: returning > true
    [15:49:53] [Server thread/INFO]: DEBUG: removeGroup called with ID: hg03
    [15:49:53] [Server thread/INFO]: DEBUG: in try
    [15:49:53] [Server thread/INFO]: DEBUG: read > hg01
    [15:49:53] [Server thread/INFO]: DEBUG: ID != given ID, writing
    [15:49:53] [Server thread/INFO]: DEBUG: read > hg02
    [15:49:53] [Server thread/INFO]: DEBUG: ID != given ID, writing
    [15:49:53] [Server thread/INFO]: DEBUG: read > hg03
    [15:49:53] [Server thread/INFO]: DEBUG: ID == given ID, will not write
    [15:49:53] [Server thread/INFO]: DEBUG: returning > true
    [15:49:53] [Server thread/INFO]: [Lobbys] Update successful!


    Somebody knows why it's happening/whats going on?
    Thanks in advance.

    bump?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  2. Offline

    Barinade

    reader = new FileReader(Main.groups);

    Make groups non-static and get the instance of Main, use that
     
  3. Offline

    PreFiXAUT

    Barinade Sorry, had no time to do something yet > I tried to, but I'ld need to change the Entire Plugin (I made all Methods to static because I need them in other Classes) :/
    And why should it be/could it be caused by a simple static thing?
     
  4. Offline

    Barinade

    If they are variables that are set/changed during or after onEnable then they need to not be static, static methods and variables return values that aren't meant to change, non-static variables need the instance grabbed to get

    Just add constructors to your classes and pass the instance of your main to them
     
Thread Status:
Not open for further replies.

Share This Page