How do I create a yml file and read it

Discussion in 'Plugin Development' started by aionchaos, Feb 1, 2017.

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

    aionchaos

    I want to create a file that when executing a command it stores (world, locx, locy, locz) of a block that I am looking at.
    I already got all the location of the target block, but wanted it when executing the command.
    I would check if the file exists, if it does not exist then create: Ok
    Save your data as follows:

    afwefefg.png

    I want every time I run the command it increments one more number in the sequence and adds below it (location, clear-inventary, chestpack).

    afgegfedg.png I

    The biggest problem I have and the number in quotes.
    Because I do not know how I create it or how I add +1 to each executed command.
    It's also a way of reading.
    When I run another command it checks in the .yml file if it has inside the chestpack: default2 and capture the (location, clear-inventory) only of the session that I have chestpack: default2.
     
    Last edited: Feb 1, 2017
  2. Offline

    Drkmaster83

    Last edited by a moderator: Feb 2, 2017
  3. Offline

    aionchaos

    I understand, but how do I create this ('0', '1')?

    Example:
    When I execute a command it saves in the file the chest in the section chests but always with that number in parentheses before.
     
    Last edited: Feb 2, 2017
  4. Offline

    Drkmaster83

    Code:
    int maxChestNum = 0;
    for(String s : getConfig().getConfigurationSection("chests").getKeys(false)) {//Gather up everything that's once indented underneath "chests"
      //s will be all of the chest numbers, because they're once indented (not twice, not negative once)
      try {
        int i = Integer.parseInt(s);
        if(i > maxChestNum) maxChestNum = i; //We're basically scanning and counting up to see which max number we reach so we can find our next number.
      }
      catch (Exception e) {
        continue; //Whatever we got was not a number or Int, guess we must ignore it
      }
    }
    getConfig().set("chests."+String.valueOf((maxChestNum+1)), "loc,etc,4 or so times"); //The path for your next chest, i.e. number that isn't taken yet
    
     
  5. Offline

    aionchaos

    The code is failing, where am I wrong?
    There is no error in the code plus the command fails to run.
    afwfawefewf.png
     
  6. Offline

    Zombie_Striker

    @aionchaos
    What is not working? Does the path get saved to the config? are the values for the WXYZ correct?

    BTW: @Drkmaster83 , you set it up so it always will override the path with the highest number. t will always be equal to an existing path, so it will always override one of them. For both of you, to fix this issue, add "t++;" at the end of the for loop (right before you create 'w')
     
  7. Offline

    Drkmaster83

    1. The value of t will change as your progress through your loop, so even if the chests turned out to be out of order
    ('0', '4', '1', '2', '3'), your system would still think 4 (even though it's already made) is the next number (since 3 > 0 and it's the last number the loop sees).
    2. Your original posted yaml syntax said the base key was "chests", not "Chests"
    3. You will never be able to save a chest if someone puts one of the chests as "Potato" or the like instead of '(a number here)'

    @Zombie_Striker In my original code, I got the highest number, and then
    Edit: One thing I wasn't thinking about at the time I wrote that code is the path after the next number, i.e. the location, items, etc
    @aionchaos https://www.diffchecker.com/4JQajYZd (pretty sure this is functional)
     
    Last edited: Feb 3, 2017
    Zombie_Striker likes this.
  8. Offline

    aionchaos

    I corrected that mistake. My mistake!
    More remains with the failure to execute the command. afwfawefewfafgegews.png

    afwfawefewf.png
     
  9. Offline

    Zombie_Striker

    @aionchaos
    Something is null on line 75. Is that line 75?
     
  10. Offline

    Drkmaster83

    Pretty sure that code literally does nothing different than what it had on your previous post. Also,
    @aionchaos https://www.diffchecker.com/b401k287 <-- Even more functional (excuse the added code for functionality, changes are reflected in right-hand code's comments)
    The null would be the if statement if we hadn't created our first chest yet.
    Also, @aionchaos, a word to the wise would be to check if the chest already exists in the config
     
    Last edited: Feb 3, 2017
  11. Offline

    aionchaos

    I've been trying since early today, so I changed the name to Chests.
    I made the changes.
    I added some information to the PNG field of view.
    kjaaiujbfuweivf.png

    Sorry it took me a long time to respond and because my internet has dropped more than 10 times.
    123.png
    Code worked like this.
    I just added line 77 (ch.createSection ("Chests");)
    Then he saves the first chest.
    Then I noticed a problem.
    When I try to save the second chest, save on top of the first one.
    That is, it replaces the first.
    I do not understand, line 80-83 should solve this.
    The variables are at the beginning of the class.
     

    Attached Files:

    Last edited by a moderator: Feb 3, 2017
  12. Offline

    Drkmaster83

    There's a reason in my code I had an if statement before the createSecion statement. Everytime you create it, it erases everything underneath it and starts from scratch.
    Change
    Code:
    ch.createSection("Chests");
    
    to
    Code:
    if(ch.getConfigurationSection("Chests") == null) ch.createSection("Chests");
    
     
  13. Offline

    aionchaos

    Worked perfectly.
    Thank you @Drkmaster83
    Now I just have to make a way to check if the chest already exists or not because it's added chest repeated.
    Do you have any idea to do a type check?
     
  14. Offline

    Drkmaster83

    Just check the location in your count up loop, you have the data there anyway, might as well use it
     
  15. Offline

    MaxFireIce

    I have a plugin that does something similar to what you are talking about @aionchaos Would you like to see the snippet that may work for you?
     
Thread Status:
Not open for further replies.

Share This Page