Solved add multiple txt file to read and display

Discussion in 'Spigot Plugin Development' started by jodgy, Feb 12, 2020.

  1. Code (Text):
                    // Define file you to read from.
                    File fileToRead = new File(Server_File_Size);;
                    // Setup BufferedReader
                    BufferedReader br = null;
                    try {
                        br = new BufferedReader(new FileReader(fileToRead));
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    }
                    String worldsize = null;
                    try {
                        while ((worldsize = br.readLine()) != null) {
                            sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "---------------------------------------------");
                            sender.sendMessage(ChatColor.AQUA + "Server's worldsize is " + ChatColor.AQUA + worldsize + ChatColor.AQUA + " GB");
                            sender.sendMessage(PlaceholderAPI.setPlaceholders((Player)sender, "&b%server_unique_joins% unique players have joined"));
                            sender.sendMessage(ChatColor.AQUA + "Server age is " + ChatColor.AQUA + serverage + ChatColor.AQUA + " days");
                            sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "---------------------------------------------");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();        
            return true;
                    }
                }
            }
            return true;
        }
    }
    the current code is like this it read from a txt file to display worldsize and i would like to add date since start but i dont know how to add another txt file so it read from that txt file and display in "serverage" any idea how i can do this?
     
  2. how about using the config?

    Edit: Or any file format that can be integrated into java
     
  3. You should probably work with custom configurations. That link explains everything you need to get what you want.
     
  4. why i use txt because i have
    cronjob that run every 30minute to check server/folder total size and save to a txt file
    and i also have date from first day calculated and saved to a text file so everytime player do /stats it will pull from the txt file but i dont know how to add another file so it will display for serverage
     
  5. drives_a_ford

    Junior Mod

    With regards to the server age, why not simplify your life by writing down the date at which your server started (i.e in a config), parse that and calculate the age (exactly) from that?
     
  6. In that case, just add another line I guess. The first line is for World size, second for Server-age and so on.
    But why do you have a cronjob for that and don't get these data on-demand?
     
  7. because when using java to calculate worldsize server could freeze if multiple player doing it because the size is more than 200GB that why i use cronjob and print to a file
     
  8. thats what a method is for. create a method that reads your data from an argument file and returns lines
    (also tip, Files java api has all this functionality already, Files.readAllLines(Path))
    then process the data based on the file you obtained data from

    /e or do what others said and use one file with multiple lines
     
  9. Correct me if I‘m wrong but isn’t the file size stored in the INode-Structure for ext and therefore doesn’t have to be computed?
    And how does a Cronjob solve this issue?
     
  10. used to use this

    Code (Text):
    long size = FileUtils.sizeOfDirectory(new File("/home/serv/"));
                double bytes = size;
                double kilobytes = (bytes / 1024);
                double megabytes = (kilobytes / 1024);
                double gigabytes = (megabytes / 1024);
                double worldsize = (double)(Math.round(gigabytes * 100.0) / 100.0);

                commandSender.sendMessage(ChatColor.AQUA + "The server's current world size is " + worldsize + "GB");

            }
            return true;
        }
    }
    but when multiple people doing it it cause the server to lag :/
     
  11. You could also schedule a regular Task that would accomplish what your Cj does but is in Bukkit and so you don‘t need to go that indirection of creating a file...
    But if you have to, as @Warren1001 stated, just set each statistic to a separate line and then read line by line
     
  12. do that asynchronously, as youre not accessing bukkit methods that arent thread safe
     
  13. ok got it to work now thank yall