Fixing config keys

Discussion in 'Plugin Development' started by Mike111177, Jul 9, 2012.

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

    Mike111177

    I have a plugin that uses config files to make list of objects. It is crucial that they are in the right order and that there are no skips. the list also can be of infinite size. For example. if i have a config file like this:
    Code:
    Apples:
       '1':
            banna: 14
            orange:76
        '5':
            banna:34
            orange:24
        '3':
            banna:23
            orange:45
    
    How would i reliably change it to:
    Code:
    Apples:
       '1':
            banna: 14
            orange:76
        '2':
            banna:23
            orange:45
        '3':
            banna:34
            orange:24
    
     
  2. Offline

    ItsHarry

    Use a list sorter?
     
  3. Offline

    Mike111177

    uhh whats that
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    Why not an actual list. As yaml does not guarantee the order of a map.
    Code:
    Apples:
      - banna: 14
        orange: 76
      - banna:  23
        orange: 45
      - banna: 34
        orange: 23
     
    ferrybig likes this.
  5. Offline

    Mike111177

    because there has to be an index. this is the acual config
    Code:
    
    Tickets:
      '1':
        open: false
        time: 0
      '2':
        open: true
        player: Mike111177
        message: hi
        time: 1341791945043
      '3':
        open: true
        player: Mike111177
        message: hi
        time: 1341792008548
      '4':
        open: true
        player: Mike111177
        message: hi again
        time: 1341843993440
      '5':
        open: true
        player: Mike111177
        message: hi again
        time: 1341843995140
      '6':
        open: true
        player: Mike111177
        message: hi again
        time: 1341843995990
        comments:
        - 6  lolololololol
        - 6  lolololololol
        - lolololololol
      '7':
        open: true
        player: CONSOLE
        message: hi
        time: 1341844211890
      '8':
        open: true
        player: Mike111177
        message: lollololollololololololololololololololololololololololololololololololololololololol
        time: 1341844292890
    
    Code:java
    1. public void initiate() {//This is used on startup
    2. Set<String> keys = config.getConfig().getConfigurationSection("Tickets").getKeys(false);//gets a list of tickets
    3. Ticket[] list = new Ticket[keys.size()];//makes an array to be converted to a list
    4. if (keys.size()!=0){//skips if there is no tickets
    5. for (String indexstring : keys){
    6. int num = Integer.parseInt(indexstring);//gets the configured value
    7. Ticket ticket = new Ticket(num, config);//creates the object ticket to managment
    8. ticket.load();//loads the ticket from the config
    9. list[num - 1] = ticket;//saves it to the array
    10. }
    11. for (Ticket ticket : list){
    12. tickets.add(ticket);//converts the array to a list
    13. }
    14. refreshYamlOrder();//my method for reordering
    15. }
    16.  
    17. }
    18. private void refreshYamlOrder(){
    19. config.getConfig().set("Tickets", null);//deletes all of the tickets
    20. for (Ticket tickt : tickets){//resaves them
    21. tickt.save();
    22. }
    23. }

    This is how i reorder them but it doesnt work for when its missing one

    bascily how do i remove one ticket and have all of the rest of them move down a number accordingly

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  6. Offline

    bitWolfy

    Why not just serialize the Ticket object?

    But if it against your religion to serialize objects, you can store the total number of tickets somewhere in the configuration, then, at ticket removal, go through the entire config and update the ticket id's. But this is a pain in the ass, you better come up with a better way to store ticket data.
     
  7. Offline

    Mike111177

    its more of a thing that i don't dont know what serializing is:/
     
  8. one4me likes this.
Thread Status:
Not open for further replies.

Share This Page