Custom Configuration Files

Discussion in 'Plugin Development' started by Treeclimber, Mar 30, 2013.

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

    Treeclimber

    So I'm trying to create another config file for my plugin. To create the file I am using:
    Code:
    newfile = new File(getDataFolder(), "File.yml");
    but that saves the file to the server's main folder. I thought that using:

    Code:
    this.plugin = plugin;
    newfile = new File(plugin.getDataFolder(), "File.yml");
    would save the file into the plugin's folder, but every time I run my plugin I get a null pointer exception to this line.

    Please help me I'm really stuck here! :/
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    Treeclimber
    Most methods in JavaPlugin do not have full functionality until the plugin is initialized. Try moving the variable assignment into onEnable.
     
  3. Offline

    Treeclimber

    Sagacious_Zed

    So... you mean moving "public File newfile;" to the onEnable?

    I have tried this, but I ended up moving the file creation/reloading/getting/etc. to a new class because I need to access these file creation methods in other classes. I have had trouble using methods from the main class in other classes. Would you suggest that I create a new onEnable class and use that in the main class so that I can use the file creation methods in the onEnable class in the other classes they are needed in?
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    Variable declaration and variable assignment are two separate things. They may be combined in one line but don't have to be.
     
  5. place that somewhere in the code NOT IN ENABLE:
    Code:
    public File newfile;
    Then in the onEnable section:
    Code:
    newfile = new File(plugin.getDataFolder(), "File.yml");
    hopefully this helps you
     
  6. Offline

    Treeclimber

    Sagacious_Zed plarsootje

    ... ..... ....... ......... :/
    Here let me explain more clearly...
    My main class looks like this:

    Code:
    package com.treeclimber17.kingdoms;
     
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class Kingdoms extends JavaPlugin implements Listener  {
     
    private Kingdoms plugin;
     
    KingdomLocationsFile klocfile = new KingdomLocationsFile (plugin);
     
    @Override
    public void onEnable() {
    getServer().getPluginManager().registerEvents(this, this);
    if (getConfig().getString("Town_Separation_Distance") == null) {
    getConfig().set("Town_Separation_Distance", 64);
    saveConfig();
    }
    if (getConfig().getString("Block_Place_Wait_Time(Milliseconds)") == null) {
    getConfig().set("Block_Place_Wait_Time(Milliseconds)", 100);
    saveConfig();
    }
    reloadConfig();
     
    if (klocfile.getKingdomLocations().getString("This_Is_The_Kingdom_Locations_File") == null) {
    klocfile.getKingdomLocations().set("This_Is_The_Kingdom_Locations_File", true);
    klocfile.saveKingdomLocations();
    }
    klocfile.reloadKingdomLocations();
     
    getCommand("kcreate").setExecutor(new KingdomCreation(this));
    }
     
    public void onDisable() {}
    }
    
    and here is my KingdomLocationsFile class:

    Code:
    package com.treeclimber17.kingdoms;
     
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Set;
    import java.util.logging.Level;
     
    import org.bukkit.Location;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class KingdomLocationsFile extends JavaPlugin implements Listener{
     
    public Kingdoms plugin;
     
    public KingdomLocationsFile(Kingdoms plugin) {
    this.plugin = plugin;
    }
     
    private File KingdomLocationsFile;
    private FileConfiguration KingdomLocations;
     
    public void reloadKingdomLocations() {
      if (KingdomLocationsFile == null) {
      KingdomLocationsFile = new File(getDataFolder(), "KingdomLocations.yml");
      }
      KingdomLocations = YamlConfiguration.loadConfiguration(KingdomLocationsFile);
    }
     
    public FileConfiguration getKingdomLocations() {
            if (KingdomLocations == null) {
                this.reloadKingdomLocations();
            }
            return KingdomLocations;
        }
     
    public void saveKingdomLocations() {
            if (KingdomLocations == null || KingdomLocationsFile == null) {
                return;
            } else {
                try {
                    getKingdomLocations().save(KingdomLocationsFile);
                } catch (IOException ex) {
                    plugin.getLogger().log(Level.SEVERE, "Could not save config to " + KingdomLocationsFile, ex);
                }
            }
        }
     
    public boolean force = false;
     
    public List<Location> getLocationsList(final String path) {
    final List<Location> List = new ArrayList<Location>();
    if (this.KingdomLocations.contains(path) && this.KingdomLocations.isConfigurationSection(path)) {
    final Set<String> keys = this.KingdomLocations.getConfigurationSection(path).getKeys(false);
    if (keys.size() > 0) {
    final Location[] key = (Location[]) keys.toArray();
    for (final Location element : key) {
    List.add(this.getKingdomLocation(path +"." +(Location) element));
    }
    }
    }
    return List;
    }
     
    private Location getKingdomLocation(String path) {
    Location loc = (Location) this.KingdomLocations.get(path);
    return loc;
    }
    }
    
    What do I need to do to make "KingdomLocations.yml" show up in the PLUGIN'S data folder (not the server's data folder)???

    p.s. I am quite frustrated right now because I've been working on this for the whole day :(
     
  7. Offline

    Sagacious_Zed Bukkit Docs

    Treeclimber
    Okay, to be more specific, getDataFolder only returns the correct folder after your plugin has been initialized. hence you need to move the assignment of your KindomLocationFile into onEnable.
     
Thread Status:
Not open for further replies.

Share This Page