1.15.2 Looping throught a Collection<List<Location>> from a yml

Discussion in 'Spigot Plugin Development' started by gochi9, Jul 1, 2020 at 2:26 AM.

  1. So noob question.I have this collection from a file
    Code (Text):

    - - ==: org.bukkit.Location
        world: world
        x: 106.0
        y: 83.0
        z: 155.0
        pitch: 0.0
        yaw: 0.0
      - ==: org.bukkit.Location
        world: world
        x: 107.0
        y: 83.0
        z: 154.0
        pitch: 0.0
        yaw: 0.0
    - - ==: org.bukkit.Location
        world: world
        x: 102.0
        y: 83.0
        z: 148.0
        pitch: 0.0
        yaw: 0.0
      - ==: org.bukkit.Location
        world: world
        x: 103.0
        y: 83.0
        z: 150.0
        pitch: 0.0
        yaw: 0.0
      - ==: org.bukkit.Location
        world: world
        x: 104.0
        y: 83.0
        z: 149.0
        pitch: 0.0
        yaw: 0.0
      - ==: org.bukkit.Location
        world: world
        x: 105.0
        y: 83.0
        z: 148.0
        pitch: 0.0
        yaw: 0.0
     
    and i want to get them separately.Any ideas?
     
    • Agree Agree x 1
  2. Yes i.ve found that but this format is quite strange for me and i don't know how to approach it.Thanks for the answer tho
     
  3. You could change "==: org.bukkit.Location" to a number. 1,2,3,4...
    Than you loop through with a for()
    Little example:
    Code (Java):
    double x;
    for(int i = 0; i != listFromYML.size(); i++) {

    // code

    x = ...getDouble(i + ".X");

    }
     
     
  4. Well i would if i could. But i am saving multiple List<Location> (like l) at the same time.Also if i try something like this
    Code (Text):
             getLocsConfig().set("treeLocations", l);
    it will generate something like this
    Code (Text):

    treeLocations:
    - - ==: org.bukkit.Location
        world: world
        x: 106.0
        y: 83.0
        z: 155.0
        pitch: 0.0
        yaw: 0.0
      - ==: org.bukkit.Location
        world: world
        x: 107.0
        y: 83.0
        z: 154.0
        pitch: 0.0
        yaw: 0.0
    - - ==: org.bukkit.Location
        world: world
        x: 102.0
        y: 83.0
        z: 148.0
        pitch: 0.0
        yaw: 0.0
      - ==: org.bukkit.Location
        world: world
        x: 103.0
        y: 83.0
        z: 150.0
        pitch: 0.0
        yaw: 0.0
    treeLocations is not connected to neither of these 2 lists.
     
  5. That is, because you save a location. You have to save the parts of a location seperate. Loop through your List<Location> and save their world,x,y,z,yaw,pitch with a custom name in your YML.
     
  6. I tried that already but i will get one big List<Location> instead of getting them separately.
     
  7. I played around a bit with it and i got to this
    Code (Text):

    treeLocations:
      9621f86b-c347-407f-b8e1-2d9fd8df71c50: '[Location{world=CraftWorld{name=world},x=106.0,y=83.0,z=155.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=107.0,y=83.0,z=154.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=108.0,y=83.0,z=153.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=108.0,y=83.0,z=154.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=109.0,y=83.0,z=153.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=106.0,y=83.0,z=153.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=107.0,y=83.0,z=153.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=107.0,y=79.0,z=154.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=108.0,y=81.0,z=155.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=107.0,y=79.0,z=155.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=108.0,y=79.0,z=154.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=108.0,y=81.0,z=156.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=106.0,y=81.0,z=155.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=107.0,y=81.0,z=154.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=107.0,y=83.0,z=156.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=107.0,y=81.0,z=155.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=108.0,y=81.0,z=154.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=108.0,y=83.0,z=156.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=109.0,y=81.0,z=153.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=108.0,y=79.0,z=155.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=106.0,y=82.0,z=155.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=107.0,y=82.0,z=154.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=108.0,y=82.0,z=153.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=107.0,y=84.0,z=153.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=108.0,y=84.0,z=152.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=106.0,y=82.0,z=153.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=108.0,y=84.0,z=153.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=107.0,y=80.0,z=155.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=108.0,y=80.0,z=154.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=108.0,y=82.0,z=156.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=107.0,y=78.0,z=154.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=108.0,y=80.0,z=155.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=107.0,y=82.0,z=155.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=108.0,y=82.0,z=154.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=109.0,y=82.0,z=153.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=107.0,y=80.0,z=154.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=107.0,y=82.0,z=156.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=108.0,y=82.0,z=155.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=107.0,y=78.0,z=155.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=108.0,y=78.0,z=154.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=108.0,y=80.0,z=156.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=108.0,y=78.0,z=155.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=107.0,y=84.0,z=152.0,pitch=0.0,yaw=0.0}]'
      9621f86b-c347-407f-b8e1-2d9fd8df71c51: '[Location{world=CraftWorld{name=world},x=102.0,y=83.0,z=148.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=103.0,y=83.0,z=150.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=104.0,y=83.0,z=149.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=105.0,y=83.0,z=148.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=105.0,y=85.0,z=150.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=103.0,y=81.0,z=149.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=104.0,y=81.0,z=148.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=104.0,y=83.0,z=150.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=103.0,y=83.0,z=148.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=104.0,y=85.0,z=149.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=102.0,y=83.0,z=150.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=103.0,y=83.0,z=149.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=104.0,y=83.0,z=148.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=104.0,y=85.0,z=150.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=105.0,y=85.0,z=149.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=103.0,y=81.0,z=150.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=104.0,y=81.0,z=149.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=104.0,y=81.0,z=150.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=102.0,y=84.0,z=148.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=103.0,y=84.0,z=148.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=103.0,y=82.0,z=149.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=104.0,y=82.0,z=148.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=104.0,y=84.0,z=150.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=103.0,y=82.0,z=150.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=104.0,y=82.0,z=149.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=102.0,y=82.0,z=148.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=102.0,y=84.0,z=150.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=103.0,y=84.0,z=149.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=104.0,y=84.0,z=148.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=103.0,y=82.0,z=148.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=103.0,y=84.0,z=150.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=104.0,y=84.0,z=149.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=105.0,y=84.0,z=148.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=104.0,y=80.0,z=150.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=103.0,y=80.0,z=149.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=104.0,y=80.0,z=148.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=104.0,y=82.0,z=150.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=103.0,y=80.0,z=150.0,pitch=0.0,yaw=0.0},
        Location{world=CraftWorld{name=world},x=104.0,y=80.0,z=149.0,pitch=0.0,yaw=0.0}]'
    But i can't figure out how to get (for example) the 2 List<List<Location>> individually

    Also i tried something like this
    Code (Text):
     
                List<List<Location>> k = new ArrayList<List<Location>>();
                k.add((List<Location>) getLocsConfig().get(s));
    But it return null
     
    #8 gochi9, Jul 1, 2020 at 12:39 PM
    Last edited: Jul 1, 2020 at 1:07 PM
  8. You need improve your serialization of location, after deserialize your location and insert in a list
     
  9. Yeah i ended up saving one location from the list and then when i needed to get the tree i used my method of gettting the tree to create the big list again
     
  10. drives_a_ford

    Junior Mod

    All this is, is a list within a list. You can automatically deserialize it. There's no custom magic needed.

    You'd first ConfigurationSection#getList.
    You'd null-check.
    You'd iterate over the objects.
    Check if they're instance of a List<Location> (which they will be given the above config).
    You'd cast.
    You'd iterate over the list of Locations.

    Or you can do whatever else you want to do with the data at any point within description.