Getting Information from YML Files...

Discussion in 'Plugin Development' started by k0nflikt, Sep 19, 2011.

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

    k0nflikt

    Okay so I have a CommandExecutor class that handles all of my commands. But I am having trouble figuring out how to write and read my YML in this class. How can I read and write to a YML file (such as adding a username to a users.yml) from my CommandExecutor class that isn't my main class? I hope my question makes sense, thanks for the help!

    - Ryan
     
  2. Offline

    DeanDip

    in your main class, you can pass your class into the CommandExecutor class constructor by doing this:

    Code:
    myExecutor = new PluginNameCommandExecutor(this);  //where, of course, PluginNameCommandExecutor is replaced with whatever you named your CommandExecutor class
    And then in your CommandExectutor class have something like this

    Code:
    private PluginName myPlugin; // substitute PluginName for the name of the main class
    
        public PluginTestCommandExecutor(PluginName plugin){
            myPlugin = plugin;
        }
    Then you can manage your main class with myPlugin (or whatever you want to name it).
     
  3. Offline

    k0nflikt

    Yeah I've already got that. Then in my main class I have
    Code:
    File users_core = new File(getDataFolder() + File.separator + "users.yml");
            Configuration fileUSERS = new Configuration(users_core);
    and in my CommandExecutor class, in my onCommand I have
    Code:
    PluginName.fileUSERS.setProperty("test.stuff","test");
    but it gives me an error on fileUSERS saying "fileUSERS cannot be resolved or is not a field". What am I missing? Thanks for the assistance.
     
  4. Offline

    DeanDip

    Make sure these are not local variables (not inside the onEnable method or other methods). Other than that, I don't see why it would give that error unless there's a spelling error with fileUSERS (which doesn't seem to be the case if you copied and pasted).

    If you're sure everything is without error, try saving all your project files and see if the error goes away after that.
     
  5. Offline

    k0nflikt

    Yeah it was inside onEnable, so I took it out. All my errors went away, but now I can't edit the YML files from inside the main class in the onEnable. It doesn't give any errors, it just doesn't create the file on start up (It worked before I moved that code from onEnable).

    Here is my code (with other junk taken out, only showing the code I am needing help with)

    Code:
    public class PluginName extends JavaPlugin{
        File users_core = new File(getDataFolder() + File.separator + "users.yml");
        Configuration fileUSERS = new Configuration(users_core);
    
        @Override
        public void onEnable() {
            fileUSERS.load();
            if(!users_core.exists()){
                fileUSERS.setProperty("test.stuff", "This is a test string");
                fileUSERS.save();
            }
        }
    
    }
    Again, thanks for the help.
     
  6. Offline

    DeanDip

    Oh you probably need to put
    Code:
    fileUSERS = new Configuration(users_core);
    in onEnable() too.
     
Thread Status:
Not open for further replies.

Share This Page