Solved Creating a file in a Listener

Discussion in 'Plugin Development' started by JUSTCAMH, Apr 22, 2014.

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

    JUSTCAMH

    So my problem;
    I want to create a file ( for player data ) when a player joins / leaves the server. So I know how to create a file in the main class however I cannot get it to work outside of it ( such as Listeners ). So the code that I'm working with at the moment that returns errors is this:
    Code:
          try {
                File f = new File(getDataFolder() + File.separator + "PlayerData" + File.separator + event.getPlayer().getName() + ".yml");
                if (!f.exists()) {
                    FileConfiguration con = YamlConfiguration.loadConfiguration(f);
                    con.set("Data", "");
                    con.set("LastLocation", "");
                    con.set("Friends", "");
                    con.save(f);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    So what I need is for this to work in a Listener. If anyone could that would be awesome! ;)

    Also if this doesn't work could you please give me a work around? Thanks
     
  2. Offline

    amhokies

    JUSTCAMH
    The getDataFolder() method is a member of the Plugin class, so you can only access it that way from within your main class. In order to access it from outside, you need an instance of your main class which you can pass around to different classes via constructors.
     
  3. Offline

    JUSTCAMH

    Could you give me an example?

    Don't worry about it I've worked it out. Thanx for your help

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Offline

    amhokies

  5. Offline

    JUSTCAMH

    Thanks Amhokies this has helped alot! :)
     
Thread Status:
Not open for further replies.

Share This Page