Solved What is the best way to save an indefinite number of books?

Discussion in 'Plugin Development' started by Kicksy, Jun 23, 2018.

Thread Status:
Not open for further replies.
  1. Thanks the books are saved.
    I still have a problem. When I delete a book from the middle of the file the books are no longer displayed correctly. How can I solve the problem?

    How I display the books:
    Code:
    int i = buch.customConfig.getConfigurationSection("Angebot.Miner.").getKeys(false).size() + 1;
                for(int a =1; a < i ; a++) {
                    list.setItem(a-1, buch.customConfig.getItemStack("Angebot.Miner." + a));
                    }
    How I save books:

    Code:
    if(buch.customConfig.get("Angebot.Miner.1") != null) {
                            int i = buch.customConfig.getConfigurationSection("Angebot.Miner.").getKeys(false).size() + 1;
                            buch.customConfig.set("Angebot.Miner." + i , book);
                            buch.saveCustomYml(buch.customConfig, buch.customFile);
                        }else {
                            buch.customConfig.set("Angebot.Miner.1" , book);
                            buch.saveCustomYml(buch.customConfig, buch.customFile);
                        }
     
  2. Offline

    CommonSenze

    @Kicksy
    What do you mean by
     
  3. Display correctly:
    Code:
    Angebot:
      Miner:
        '1':
          ==: org.bukkit.inventory.ItemStack
          type: WRITTEN_BOOK
          meta:
            ==: ItemMeta
            meta-type: BOOK_SIGNED
            title: aaaaaa
            author: Kicksy
            pages:
            - aaa
            generation: 0
        '2':
          ==: org.bukkit.inventory.ItemStack
          type: WRITTEN_BOOK
          meta:
            ==: ItemMeta
            meta-type: BOOK_SIGNED
            title: f
            author: Kicksy
            pages:
            - aaa
            generation: 0
        '3':
          ==: org.bukkit.inventory.ItemStack
          type: WRITTEN_BOOK
          meta:
            ==: ItemMeta
            meta-type: BOOK_SIGNED
            title: aaaaaaaaaa
            author: Kicksy
            pages:
            - TETS
            - aa
            - a
            generation: 0
    
    No longer correctly:
    Code:
    Angebot:
      Miner:
        '1':
          ==: org.bukkit.inventory.ItemStack
          type: WRITTEN_BOOK
          meta:
            ==: ItemMeta
            meta-type: BOOK_SIGNED
            title: aaaaaa
            author: Kicksy
            pages:
            - aaa
            generation: 0
        '3':
          ==: org.bukkit.inventory.ItemStack
          type: WRITTEN_BOOK
          meta:
            ==: ItemMeta
            meta-type: BOOK_SIGNED
            title: aaaaaaaaaa
            author: Kicksy
            pages:
            - TETS
            - aa
            - a
            generation: 0
    
     
  4. Offline

    CommonSenze

    @Kicksy
    When a book is removed just get the rest of the books and reset the order of which they are arranged.
     
  5. Offline

    CommonSenze

    @Kicksy
    Get all remaining books in a list. Create an incrementation for loop (the for loop that looks like "for (int i = 0; i < something; i++) {") starting at zero, set every book in your list to the corresponding number but make sure to have the number in the configuration string that is the location to the book your setting is one more then the number in the header of the incrementation for loop. If you need an example of how this would look like I would be happy to explain further.
     
  6. Offline

    CommonSenze

    @Kicksy
    Of course I would be more than happy to. So in this example I am going to collect all my names of users I've saved in number order after removing one from the list.

    In my example my config looks like this:
    Code:
    Users:
        '1':
            Name: CommonSenze
        '2':
            Name: Kicksy

    So for my example I would do something like this:
    Code:java
    1. ArrayList<String> numbers = new ArrayList<>(file.getConfig().getConfigurationSection("Users").getKeys(false));
    2.  
    3. for (int num = 0; num < users.size(); num++) {
    4. String name = file.getConfig().getString("Users."+numbers.get(num)+".Name");
    5. file.getConfig().set("Users."+numbers.get(num)+".Name", null);
    6. file.getConfig().set("Users."+(num+1)+".Name", name);
    7. }
    8.  
    9. file.saveConfig();

    So as you see, I create a new ArrayList with all the numbers thats in the configuation section named "Users". Once we have all of the numbers saved, we go in the incrementation for loop and get each number in the arraylist by giving the method the slot number (It all List in java start at zero so we set num, our variable responsible for getting the number from the array, to zero).

    Side note, all path ways (ie. the way to get to the value like "Users.1.Name") are strings that's why even though I'm calling the elements in the arrays numbers, in code, they are strings.

    So now we save the name of the user to the variable name and delete that path out of the config.

    Another side note, you might have to get many things for your books so you may need more than one variable of varies types, also you may need to remove multiple path ways.

    Anyways, after removing the path way from the config, I set the new one to the new number slot. Now because the variable num starts at zero, I need to add one to make it 1 like how my list is, hence the code above.

    Finally after the for loop is finish, I save the config to my newly set list. Hope that made sense and wasn't too complicated.
     
    Last edited: Jun 26, 2018
    Kicksy likes this.
Thread Status:
Not open for further replies.

Share This Page