Help saving information in .yml

Discussion in 'Plugin Development' started by woarth, Nov 16, 2015.

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

    woarth

    Hi! How are you all?

    Sorry again for taking your time, but i need help in this topic...

    I am creating a warn plugin in which i want to save the warns information. So, i /warn a player for insulting, and i want the reason, player and time saved in a warns.yml.
    I have my warns.yml file generated, and my information saves, but it only saves the information of the last warn.
    I want the warns to save like in a number list, let me show you:

    Code:
    # In this File you can see all the warns made!
    Warns Amount: 3
    Player Warns:
      '1':
        Warner: woarth
        Warned: Notch
        Reason: 'Insulting'
        Time: 11/11/2015 - 18:10
      '2':
        Warner: woarth
        Warned: Minecrafter
        Reason: 'Stop hacking'
        Time: 11/11/2015 - 19:10
      '3':
        Warner: woarth
        Warned: Richard
        Reason: 'Bad boy'
        Time: 11/11/2015 - 19:15

    This is an example of how the warns.yml should look after warning three persons. But, instead of working like this, it only pastes the new warn into the last warn, giving this as a result:

    Code:
    # In this File you can see all the warns made!
    Warns Amount: 3
    Player Warns:
      '3':
        Warner: woarth
        Warned: Richard
        Reason: 'Bad boy'
        Time: 11/11/2015 - 19:15
    This is the code i am using for saving the data:


    Code:
             
        private int plusone;
        public File warns;
        public FileConfiguration warnsconfig;
           
                DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy - HH:mm");
               Date date = new Date();
               String time = dateFormat.format(date);
           
               this.plusone += 1;
              int amount = this.plugin.warnsconfig.getInt("Warns Amount") + this.plusone;
            
              this.plugin.warnsconfig.set("Warns Amount", this.plugin.warnsconfig.getInt("Warns Amount") + 1);
            
              try {
                  this.plugin.warnsconfig.save(this.plugin.warns);
                }
                catch (IOException e) {
                  e.printStackTrace();
                }
             
              this.plugin.warnsconfig.set("Player Warns." + amount + ".Warner", p.getName());
              this.plugin.warnsconfig.set("Player Warns." + amount + ".Warned", target.getName());
              this.plugin.warnsconfig.set("Player Warns." + amount + ".Reason", + WarnReson);
              this.plugin.warnsconfig.set("Player Warns." + amount + ".Time", time);
              this.plugin.warnsconfig.set("Warns Amount", amount);
               try {
                 this.plugin.warnsconfig.save(this.plugin.warns);
               }
               catch (IOException e) {
                 e.printStackTrace();
               }
    I would really appreciate if you could help me with this topic! Thanks for reading!
     
  2. Offline

    adam753

    Is the '1' '2' '3' actually inregral to the plugin? Why not just have it be a random number?
     
  3. Offline

    Scimiguy

    What's the point of this:
    this.plusone;
    ?? lol

    Also, you're saving Warns Amount twice.
     
  4. Offline

    woarth

    Hey, thanks for answering! Yes i know i am saving twice, but with this method it works!

    I think the problem is here, but dont know where or what!

    Code:
              this.plugin.warnsconfig.set("Player Warns." + amount + ".Warner", p.getName());
              this.plugin.warnsconfig.set("Player Warns." + amount + ".Warned", target.getName());
              this.plugin.warnsconfig.set("Player Warns." + amount + ".Reason", + WarnReson);
              this.plugin.warnsconfig.set("Player Warns." + amount + ".Time", time);
              this.plugin.warnsconfig.set("Warns Amount", amount);
    About the plusone, is a way of telling its an integer.
     
  5. Offline

    Scimiguy

    Why don't you just do + 1?
    And you can't say it works, because you're here, and it obviously doesn't.

    Don't save it twice, there's no need, and is probably half (if not all) your problem to be honest
     
  6. Offline

    woarth

    Thanks for answering!

    Well the + 1 didnt worked for me before.

    And this things of saving didnt affect nothing. I removed it and it still doesnt work.
     
  7. Offline

    Scimiguy

    Good, keep it removed and show us the new code
     
  8. Offline

    woarth

    Code:
             private int plusone;
           
                DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy - HH:mm");
               Date date = new Date();
               String time = dateFormat.format(date);
           
               this.plusone += 1;
              int amount = this.plugin.warnsconfig.getInt("Warns Amount") + this.plusone;
            
              this.plugin.warnsconfig.set("Warns Amount", this.plugin.warnsconfig.getInt("Warns Amount") + 1);
             
              this.plugin.warnsconfig.set("Player Warns." + amount + ".Warner", p.getName());
              this.plugin.warnsconfig.set("Player Warns." + amount + ".Warned", target.getName());
              this.plugin.warnsconfig.set("Player Warns." + amount + ".Reason", + WarnReson);
              this.plugin.warnsconfig.set("Player Warns." + amount + ".Time", time);
              this.plugin.warnsconfig.set("Warns Amount", amount);
               try {
                 this.plugin.warnsconfig.save(this.plugin.warns);
               }
               catch (IOException e) {
                 e.printStackTrace();
               }
     
  9. Offline

    woarth

    Anyone can help me?
     
  10. Offline

    Scimiguy

    Are you actually reloading the config before you're trying to get the warn int?
     
Thread Status:
Not open for further replies.

Share This Page