Per player config file - best way of reading and writing data?

Discussion in 'Plugin Development' started by Captain Dory, Jan 16, 2015.

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

    Captain Dory

    Hi there,

    I'm making a plugin that creates a config file when a player joins (with their UUID if one doesn't already exist), and I've run into a plugin; I cannot read or write to them.

    I haven't used Java IO before, although I've attempted before. Is there a way for the Bukkit config system to like... turn a yml into a config?

    Any help would be appreciated.
     
  2. Offline

    Skionz

    @Captain Dory A 'config' is just what most Bukkit developers call a YAML file.
     
  3. Offline

    teej107

  4. Offline

    Captain Dory

    @teej107 thanks for advice; I've written some methods.
    However, I cannot get the plugin to save a default to the disk. This is my code:

    Creating a file for UUID:
    PHP:
    @EventHandler
        
    public void onJoin(PlayerJoinEvent e) {
           
            
    File f = new File("config files/" e.getPlayer().getUniqueId() + ".yml");
            if (!(
    f.exists())) {
               
                try {
                   
                    
    f.createNewFile();
                    
    e.getPlayer().sendMessage("Creating file...");
                    new 
    Methods(e.getPlayer().getUniqueId()).createDefaults();
                    new 
    Methods(e.getPlayer().getUniqueId()).saveConfig();
                   
                } catch (
    IOException ex) {
                   
                    
    ex.printStackTrace();
                   
                }
               
            }
           
        }
    This has some methods; getConfig, createDefaults etc
    PHP:
    UUID u;
       
        public 
    Methods(UUID u) {
           
            
    this.u;
           
        }
       
        public 
    FileConfiguration getConfig() {
           
            
    FileConfiguration temp YamlConfiguration.loadConfiguration(new File("config files/" ".yml"));
           
            return 
    temp;
           
        }
       
        public 
    void createDefaults() {
           
            
    getConfig().set("rank""prisoner");
           
        }
       
        public 
    void saveConfig() {
           
            try {
               
                
    getConfig().save(new File("config files/" ".yml"));
               
            } catch (
    IOException e) {
               
               
    e.printStackTrace();
               
            }
           
        }
    There are no errors in IDE, it's just that it doesn't work. It will create the file, but nothing will be in it.
     
  5. Offline

    teej107

    Where did you look? Because your current file path won't be saving it within the plugins folder.
     
  6. Offline

    Captain Dory

    I understand; I want to save it in a folder in the root directory. Is this possible?
     
  7. Offline

    teej107

  8. Offline

    Captain Dory

    Ok. So directing back to the last question; The file will create but no text will be in it. What would you use to save text to a yml file?
     
  9. Offline

    teej107

  10. Offline

    Captain Dory

    Still having some problems:
    PHP:
    public class Create implements Listener {
       
        @
    EventHandler
        
    public void onJoin(PlayerJoinEvent e) {
           
            
    Methods m = new Methods(e.getPlayer().getUniqueId());
           
            if (
    m.f.exists()) {
               
                
    e.getPlayer().sendMessage("Welcome back!");
               
            } else {
               
                
    e.getPlayer().sendMessage("Creating file...");
               
                
    m.createConfig();
                
    m.createDefaults();
                
    m.saveConfig();
               
            }
           
        }

    }

    PHP:
    public class Methods {
       
        
    UUID u;
        public 
    Methods(UUID u) {
           
            
    this.u;
           
        }
       
        
    File f = new File("config files/" ".yml");
        
    FileConfiguration c YamlConfiguration.loadConfiguration(f);
       
        public 
    void createConfig() {
           
            try {
               
                
    f.createNewFile();
               
            } catch (
    IOException e) {
               
                
    e.printStackTrace();
               
            }
           
        }
       
        public 
    void createDefaults() {
           
            
    c.set("rank""prisoner");
            
    c.set("money"0);
           
        }
       
        public 
    FileConfiguration getConfig() {
           
            return 
    c;
           
        }
       
        public 
    void saveConfig() {
           
            try {
               
                
    getConfig().save(f);
               
            } catch (
    IOException e) {
               
                
    e.printStackTrace();
               
            }
           
        }
       
    }

    It will write 'rank' and 'money' to the file, but the file is called 'null' :B
    any suggestions?
     
  11. Offline

    xTrollxDudex

    Look at your initialization sequence. You try to assign your files and configurations when it is constructed, and eager initialized fields come before constructor initialized fields. Your UUID is not eager initialized, and becomes set after all the eager initialized, the File and FileCofiguration fields are set. This is a case where all fields depend on the other being non-null, so place your initialization code IN ORDER inside your constructor. Also, please use final.
     
  12. Offline

    Captain Dory

    @xTrollxDudex

    I'm thankful for your help, but I don't really understand what you mean. I would greatly appreciate if you could provide a few examples to fix the plugin and my techniques. Thank you :)
     
    Last edited: Jan 17, 2015
  13. Offline

    xTrollxDudex

    You're using the UUID before it is set by the constructor, when it is null.
     
  14. Offline

    Captain Dory

    Oh ok, it's working now. Thanks :)
     
Thread Status:
Not open for further replies.

Share This Page