File Help

Discussion in 'Spigot Plugin Development' started by Azmidium, Mar 11, 2016.

  1. How would you go about specifying file locations in a config file AND using it?

    I would like to get a file location from a config and use it. I worded the question differently yesterday, but it didn't help with getting user input for it. :)
  2. What do you mean? like in the config there's a file location and you want to read the file? if so:

    Code (Java):

    File file = new File(getConfig().getString("file-location").replace("\\", File.separator));
    try (Scanner sc = new Scanner(new FileReader(file))) {
        while (sc.hasNextLine) {
            String nextLine = sc.nextLine; //this is looping through every line.
    } catch (FileNotFoundException e) {
    //cant find file
  3. Yes that is what I meant! Since I want to put a guide in the plugin, what would they do to get the file path?

    It would be awkward to have this feature without the user knowing how to use it. ;)

    Thanks in advance!

    • Like Like x 1
  4. Where I added the comment of "this is looping through every line" just use a StringBuilder to append it all to one string, then you can send a message to the player like so:

    Code (Java):

    player.sendMessage(ChatColor.translateAlternateColorCodes('&', stringBuilder.toString()));
    and remember to just save it to memory on startup (asynchronously or in another thread) otherwise it may cause lagg. And make a reload command to reload config and reload the String of the file contents.
  5. Thanks for that! That really cleared up what that code did. :)

    What would the user put in the config? If they are using something like Multicraft or MCMyAdmin there are other folders which hold the whole server. How would the user go about specifying a location? This is from a user-standpoint not mine. :)

    Thanks in advance! ;)

  6. Make them specify a file like "message.yml" and make them put the file in the /plugins/[plugin-name] folder. Then for the location do
    Code (Java):

    try(Scanner sc = new Scanner(new FileReader(new File(getDataFolder(), getConfig().getString("message-file")))))
    getDataFolder() is the folder for your plugin, if you wanted the /plugins folder it would be
    Code (Java):
    getDataFolder() + "../"
    ../ means up one directory.
  7. So if I did:
    Code (Text):
    getDataFolder() + "./"
    It would be the main server folder?
  8. @Azmidium Should be. In an FTP software, the base server directory is ./ so yep.
  9. Alright! So if the user puts this in the config:
    Code (Text):
    file-location: "C:/Plugin/<GoIntoAnotherPlugin>/"
    That would work if I did something like this?
    Code (Text):
    File specifiedFileLocation = new File("./"+getConfig().getString("file-location"))
    Or am I confused?!

    Thanks in advance, I am okay with files as working with them AND user input is a new concept for me. ;)

    Sincerely, Azmidium
  10. ok, don't do that... on shared hosting it will go to some random fucking directory... do getDataFolder() + "../../" -> the main directory of the server.
  11. If I were to replace that part of the example with what you said, would it work?

    Thanks for the speedy responses! ;)

  12. It should work no matter what, this is just efficient for shared hosting.
  13. Will it work with what I said here?
    Except I replace the "./" with ' getDataFolder() + "../../" '

    Thanks for helping in advance! ;)

  14. Ah, hmm. I don't know due to "c:/" I don't know if it can be used
  15. So when the user is configuring the plugin, do I just let them know to remove the C:/ part? I can also have the plugin automagically do it. :)

    Thanks for helping! ;)

  16. Make it so it has to be in the root server folder, then say getDataFolder() + "../../" + File.seperator + <filename.extension> and it will find the file in the ROOT directory of the server.