Store data to file

Discussion in 'Plugin Development' started by ExecuterMC, Mar 11, 2017.

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

    ExecuterMC

    Hi i am currently coding an AntiCheat and I want to save the "Hackinglevel" of a Player in a .yml file and I dindt found anything helpful.l Could somebody post some write and read methods?
    like:
    Code:
    public void saveData(){
    //save the date
    }
    public void readData(){
    //read the date
    }
    thx;)
     
    Last edited: Mar 11, 2017
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    ExecuterMC

    Yeah but the config.yml is not made for that. It is for the settings.
    Is there anything else you know?
     
    Last edited: Mar 12, 2017
  4. Online

    timtower Administrator Administrator Moderator

  5. Offline

    ExecuterMC

    Ok thanks will try that
     
  6. Offline

    ExecuterMC

    I have done :
    Code:
    private File folder = plugin.getDatafolder();
    private File f = new File(folder,"hackingp.yml");
    but its throws an error :mad:

    what can I do?
    @timtower
     
  7. Online

    timtower Administrator Administrator Moderator

    @ExecuterMC You run that in your onEnable, not before plugin is set.
     
  8. Offline

    ExecuterMC

    I run that after the onEnable() I run it in an External Listener...
    @timtower
     
  9. Online

    timtower Administrator Administrator Moderator

    @ExecuterMC The private states that it is a field in the class.
    Your constructor is called after that.
    On the moment that "private File folder = plugin.getDatafolder();" runs, plugin is not initialized yet.
     
  10. Offline

    ExecuterMC

    Ok, so I should do it in the onEnable() ?
    Code:
    public File file;
    public void onEnable(){
    this.file = new File(getDatafolder(),"hackingp.yml")
    }
    is this working?
    and to get it
    Code:
    private core main = new core();
    
    YamlConfiguration cfg = YamlConfiguration.leadConfiguration(main.file);
    ?
     
  11. Online

    timtower Administrator Administrator Moderator

    @ExecuterMC Post your entire listener class please.
     
  12. Offline

    ExecuterMC

    Ok can I post It tomorow?
    I cant do it today
     
  13. Online

    timtower Administrator Administrator Moderator

    @ExecuterMC I don't have any deadlines for something that you are making. So everything is fine for me.
     
  14. Offline

    ExecuterMC

    ;)

    Code:
    public class antistep implements Listener{
    
    private Folder f;
    private core plugin;
    public antistep(core main){
    this.plugin = main;
    this.f = new File(main.getDatafolder(),"hackingp.yml")
    if(!f.exists())
        f.createnewfile();
    //try and catch
    }
    @EventHandler
    public void listener(){
    //my code
    }
    }
    this should work ,right?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 12, 2017
  15. Online

    timtower Administrator Administrator Moderator

  16. Offline

    ExecuterMC

    Ok thanks a LOT!

    @timtower.
    It crashes when I launch it
    at the line: this.f = new File(core.getDatafolder(),"hackingp.yml");
    I think its beacause of the getDatafolder()
    I could try getDatafolder().getAbsolutepath() and saving in a string so that Ican do
    new File(core.getDatafolder().getAbsolutepath() + "\hackingp.yml")

    and I have a bug with bukkit I cant login to my account on the website why?
    maybea beacause I am already logged in on two other devices?
    thats why I cant post the error-.-
     
    Last edited: Mar 13, 2017
  17. Online

    timtower Administrator Administrator Moderator

    @ExecuterMC Then please post your server log and your code where the line throwing the error is marked
     
Thread Status:
Not open for further replies.

Share This Page