Solved Creating a File in the Plugin's Datafolder

Discussion in 'Plugin Development' started by KeybordPiano459, Dec 29, 2012.

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

    fireblast709

    The plugin:
    Code:java
    1. public <main class> plugin;

    The constructor:
    Code:java
    1. public thisClass(<main class> plugin)
    2. {
    3. this.plugin = plugin;
    4. }

    The rest would be using plugin.getConfig(), plugin.saveConfig(), etc.
     
  2. Offline

    KeybordPiano459

    No, this is a custom file, I'm not using the Bukkit API.
     
  3. Offline

    fireblast709

    though you are using config.yml ;3
     
  4. Offline

    KeybordPiano459

    I wasn't meaning to =3
    Do you know how I can fix the NPE though?
     
  5. Offline

    fireblast709

    The File object:
    Code:java
    1. private File someFile;

    The constructor:
    Code:java
    1. public thisClass(<main class> plugin)
    2. {
    3. someFile = new File(plugin.getDataFolder(), "file.yml");
    4. }

    reloadConfig method:
    Code:java
    1. public void reloadFile()
    2. {
    3. if (!someFile.exists())
    4. {
    5. try
    6. {
    7. someFile.mkdirs();
    8. if(!someFile.createNewFile())
    9. {
    10. throw new IOException("Failed to create file");
    11. }
    12. }
    13. catch(IOException ex)
    14. {
    15. //log.severe("Failed to create file")
    16. return;
    17. }
    18. }
    19. fileConfiguration = YamlConfiguration.loadConfiguration(someFile);
    20.  
    21. InputStream defConfigStream = plugin.getResource("file.yml");
    22. if (defConfigStream != null)
    23. {
    24. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    25. fileConfiguration.setDefaults(defConfig);
    26. }
    27. }
    Now keep your methods off the static keyword >:|
     
    KeybordPiano459 likes this.
  6. Offline

    KeybordPiano459

    fireblast709
    First of all, I like static :(
    Second, now I'm getting an NPE on the the seventh-last line in what you just wrote, and my file.yml doesn't exist even though at some point I'm calling .createNewFile() on thisFile =/
     
  7. Offline

    fireblast709

    post the stacktrace and current code :3. Also, static is not for convenience, can most of the time be avoided easily, and in your case its very erronous :p
     
  8. Offline

    KeybordPiano459

  9. Offline

    Sagacious_Zed Bukkit Docs

    You should not hard code the folder it is in. your data folder may not reside in the directory named 'plugins'

    EDIT: fix spelling
     
  10. Offline

    KeybordPiano459

    Oh yeah... Odd how there's a folder called config.yml in my server directory... xD
     
  11. Offline

    fireblast709

    wait wait wait... why does the error tell he the JavaPlugin's classloader is null... You ain't calling the method before onEnable() right?
     
  12. Offline

    KeybordPiano459

    I'm calling the method in the onEnable() :p

    Anyways, I messed around with the code some more, and now the file.yml won't generate as a directory or a file, anywhere =/

    EDIT: AND there's no stack trace in the config :O

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  13. Offline

    tommycake50

    fire i just realised it actually would because getDataFolder(); still returns an unmade file even if its not there aka the path is in the file variable it just doesnt exist!
    im pretty sure thats the case anyway.
    but if it was on the plugin variable then no it wouldnt.
     
  14. Offline

    KeybordPiano459

    fireblast709
    tommycake50
    Just realized something- when I delete the kEssentials folder, it doesn't regenerate when I reload the server :O

    EDIT: mkdirs is definitely being called on the folder...
    EDIT2: Does anyone have skype? It would really help =/
     
  15. Offline

    fireblast709

  16. Offline

    tommycake50

    dont use mkdirs use mkdir btw ;P
    oh and getDataFolder() has the path already so use getDataFolder.mkDir();
    rather than fussing around with instances.
     
  17. Offline

    fireblast709

    Explain to me, as afaik even if the file at the path does not exist, the File object is not null. The NPE occurs as you try to invoke the .mkdirs

    [edit] nvm you found out yourself
     
  18. Offline

    tommycake50

    well if its null explain to me how this method works in one of my plugins.
    Show Spoiler
    Code:java
    1. private void listFileChecks() {
    2. if(!getDataFolder().exists()){
    3. getDataFolder().mkdir();
    4. File fl = new File(getDataFolder(), "logfile.log");
    5. if(fl.exists()){
    6. log = fl;
    7. }else{
    8. try {
    9. fl.createNewFile();
    10. log = fl;
    11. } catch (IOException e) {
    12. e.printStackTrace();
    13. }
    14. }
    15. }else{
    16. File fl = new File(getDataFolder(), "logfile.log");
    17. if(fl.exists()){
    18. log = fl;
    19. }else{
    20. try {
    21. fl.createNewFile();
    22. log = fl;
    23. } catch (IOException e) {
    24. e.printStackTrace();
    25. }
    26. }
    27. }
    28. }

    i would love to know.
     
  19. Offline

    KeybordPiano459

    tommycake50
    Replace any if (file == null) with if (!file.exists()). fireblast709 and I got it to work that way :)
     
  20. Offline

    tommycake50

    no no i dont have a problem with that method i was just saying if its null how come it works.
     
Thread Status:
Not open for further replies.

Share This Page