Get 1st, 2nd, ... item in config node

Discussion in 'Plugin Development' started by Julian1Reinhardt, Sep 22, 2013.

Thread Status:
Not open for further replies.
  1. So I have a config with a node htat looks like this:
    Code:
    test:
      284,64,270: AB|CD|EF
      536,85,360: GH|IJ|KL
      26,182,113: MN|OP|QR
      345,23,60: ST|UV|WX
    Now I want to get the first n (n as a variable) lines, of the "test"-node tosend them via the chat.
    I laready have this for-loop
    Code:
    for(int i = 1; i<= howmanylines; i=i+1){
                    sender.sendMessage(); //this should send the line of code
                }
     
  2. Offline

    Vandrake

    you might want to store them as a list instead? then get it back?
     
  3. Vandrake
    But it should be restart-proof, so that it is still htere when you restart the server
     
  4. Offline

    Jogy34

    I think he meant to store it in a list in your config so it would be something like this:

    Code:java
    1.  
    2. //Adding it to the config (if you aren't using a default config)
    3. List<String> list = new ArrayList<String>();
    4. list.add("284,64,270: AB|CD|EF");
    5. list.add("536,85,360: GH|IJ|KL");
    6. list.add("26,182,113: MN|OP|QR");
    7. list.add("345,23,60: ST|UV|WX");
    8. config.set("test", list);
    9.  
    10. //In the config it would look like this:
    11. test:
    12. - 284,64,270: AB|CD|EF
    13. - 536,85,360: GH|IJ|KL
    14. - 26,182,113: MN|OP|QR
    15. - 345,23,60: ST|UV|WX
    16.  
    17. //Then to get it back you would just do something like this:
    18. List<String> list = config.getStringList("test");
    19. for(int i = 0; i < numLines && i < list.size(); i++)
    20. {
    21. player.sendMessage(list.get(i));
    22. }
     
  5. Offline

    Vandrake

    Code:java
    1.  
    2.  
    3. exactly XP
     
  6. Vandrake Jogy34
    I haven't used arraylists that much yet. Is it possible to add one list item at a time, so you can always add a new list item without overwriting the content?
     
  7. Offline

    Rprrr

    Jogy34 Vandrake Julian1Reinhardt
    No. It's not even necesarry to change his configuration. You can simply use getConfigurationSection("test").getKeys(false).size();. Don't do what Jogy34 said.
     
  8. Offline

    Jogy34

    Lists are pretty much dynamically sized arrays. There are a few more things that you can do with them though but that's the general idea of them. And yes, well sort of. What I do is to retrieve the list from the config, add the item, and then reset it in the config config. So something like this:
    Code:java
    1.  
    2. List<String> list = config.getStringList("test");
    3. list.add("Some other thing");
    4. config.set("test", list);
    5.  


    Lists are a lot easier to read and to work with.
     
  9. Rprrr
    I don't get how this piece of code works...
     
  10. Offline

    Rprrr

    Jogy34
    If you're only aiming for getting the amount of lines in his configuration section named "test", creating a list would be pointless. Also, you're forgetting that now he would have to split those strings in that list to separate the value and the key, which isn't really better to work with in my opinion.

    Julian1Reinhardt
    int amountOfLines = config.getConfigurationSection("test").getKeys(false).size();
     
  11. Rprrr
    Oh, I think you misunderstood my question. I wanted to get the first x items of the list. Not how many there are in total.
     
  12. Offline

    Rprrr

    Julian1Reinhardt
    I guess this would work.
    Code:
            Set<String> abc = config.getConfigurationSection("test").getKeys(false);
            String[] stringArray = (String[]) abc.toArray();
           
            int x = //How many lines.
            for (int i = 0; i < x && x < stringArray.length; i++) {
                String keyString = stringArray[i];
                Bukkit.broadcastMessage(config.getString("test." + keyString));
            }
     
  13. Offline

    Jogy34

    I still think it would be easier and cleaner to just use lists.
     
Thread Status:
Not open for further replies.

Share This Page