Accessing Flatfile from another class.

Discussion in 'Plugin Development' started by Exoaria, Mar 12, 2013.

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

    Exoaria

    Here's some code to give you an idea of how I created the flatfile.

    The script below is the class that I'm using to write the procedure.
    I want to call it from my Listener class.
    Code:
        public void SaveQuitScore(int current, String playerName) {
            File quitSc = new File("plugins/Champion");
            File quitScore = new File("plugins/Champion/SessionLog.yml");
     
         
            if (!quitSc.exists()) {
                quitSc.mkdir();
     
                if (!quitScore.exists()) {
                    try {
                        quitScore.createNewFile();
                        YamlConfiguration config = YamlConfiguration
                                .loadConfiguration(quitScore);
                        config.options()
                                .header("Player Session Times - Please do not edit this file.");
                        config.set(playerName, current);
                        config.save(quitScore);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    }else if (quitScore.exists()) {
                        try {
                            quitScore.createNewFile();
                            YamlConfiguration config = YamlConfiguration
                                    .loadConfiguration(quitScore);
                            config.options()
                            .header("Player Session Times - Please do not edit this file.");
                    config.set(playerName, current);
                    config.save(quitScore);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            MainPlugin.saveConfig();
    there's more, but that will do.
    Basically I have used my listener class to call that script and declare the (current etc.) values that can be seen at the top.
    I save them to a flatfile and now I want to go back to my Listener class and use the data that I just saved. How can I do it?
     
  2. Offline

    chasechocolate

    Either make the method static or make a variable of the class that this method is in in your other class.
     
  3. Offline

    Exoaria

    Making the method static doesn't allow me to declare any of the variables to be saved, because I need a Listener for it. I don't know what you mean by the second option though, could you explain at all?
     
  4. Offline

    microgeek

    Your wording is a bit hard for me to understand. What's preventing you from making the method static, or saving an instance of the class it's declared in?
     
  5. Offline

    Exoaria

    Listener class has var 1. (Which is changing every second)
    Special class "requests" var x.
    Listener tells the Special class that var x is "var 1".
    Special class saves "var 1" to xconfig.yml

    I now want to request "var 1" back in my Listener class from xconfig.yml which Special class set it to, when my Listener originally gave it to Special class.

    I can't just use "var 1" because the whole purpose of this is based around the fact that var1 is changing every second.

    It won't let me change it to public static. I just get a red line under the entire thing and a big error. I don't think that changes things anyway, as in, I need to call from the xconfig.yml. That's all I need to do.

    Pseudocode Example
    Listener
    Code:
    HashMap<String, Integer> exhash = new HashMap<String, Integer>();
    int value1 = System.currentTimeMillis();
     
    //value1 is changing every second
     
    @EventHandler
    public void onJoin (PlayerJoinEvent joinEvent) {
        String player = joinEvent.getPlayer();getName();
        flatfileValue = HERE IS WHERE I WANT TO PLACE THE VALUE FROM THE OTHER CLASS' CONFIG
        exhash.put(player, flatfileValue);
    }
     
    h.SpecialClass(value1)
    Special Class
    Code:
    public void SpecialClass(current);
     
    getConfig().set(current); <THAT'S THE FLATFILE VALUE I WANT TO PLACE
    That's the most simple way I can put my problem as everything is so complicated.

    value1 gets saved to "Special Class"'s config at the time that it was sent, whilst value1 continues to increase over time in the Listener, I want to be able to call the number it was at when I saved it to the Special Class's config file which becomes a .yml (although the config has all custom names - you can see it in my original post, I posted the code)
     
Thread Status:
Not open for further replies.

Share This Page