config.yml - Lists, and getting their values?

Discussion in 'Plugin Development' started by mccrafter1212, May 27, 2017.

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

    mccrafter1212

    So, recently I've been working on this waypoints plugin. It includes a config.yml file where server owners can create server waypoints (like spawn, or a shop) and specify their x-coordinate, y-coordinate, and z-coordinate. I'm not a 100% pro with java though. So, I have my config.yml:

    waypoints:
    - Spawn:
    - x: 0
    - y: 50
    - z: 0
    - name: Spawn

    Sorry, I don't really know how to put this in a code block :/ - Anyways right now what I'm trying to do is loop through my waypoints. I've been trying to get the name of the waypoint it's currently looping through but I'm not sure how to. Code:

    Code:
    for (int i = 0; i > plugin.getConfig().getList("waypoints").size(); i++) {
                ItemStack newItem = new ItemStack(Material.BEACON); // Ignore this
                ItemMeta newItemMeta = newItem.getItemMeta(); // Ignore this
             
                newItemMeta.setDisplayName(ChatColor.DARK_PURPLE + plugin.getConfig().getList("waypoints."); // So right here is where I want to get the name of the waypoint it's currently at in the for loop. I'm confused with config.yml files though, and how java interprets lists.
    }
    
    Thanks! Any help is appreciated! :D

    EDIT:
    Alright, so I went ahead and did:
    Code:
    String waypointName = plugin.getConfig().getList("waypoints").get(0).toString();
    Bukkit.getServer().broadcastMessage(ChatColor.GREEN + waypointName);
    
    That gives me:
    {Spawn=[{x=0}, {y=50}, {z=0}, {name=Spawn}]}
    So, how can I access name?

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2017
  2. Offline

    FisheyLP

    Please bump only once every 24 hours

    It doesn't really make sense to store the waypoint name twice in the config..
    Anyways:
    You can get the name of each waypoint by looping through config.getConfigurationSection("waypoints").getKeys(false)

    For example your config looks like this:
    Code:
    waypoints:
      spawn:
        ...
      shop:
        ...
    Then the method above will return a list containing "spawn" and "shop"
     
  3. Offline

    mccrafter1212

    Ok, so I setup the config file as you stated (by removing "- name: Spawn") although, how can I get the name of each item in the list?

    So you provided me with the code to get a list. Can I do:

    getConfig().getConfigurationSection("waypoints").getKeys(false).get(0) // To get what's in the 0th position in the list?

    Also, I need to access X, Y, and Z values eventually so how could I also do that?
     
  4. Offline

    Horsey

    This is how you would structure your config.yml:
    Code:
    waypoints:
      Spawn:
        X: 1
        Y: 1
        Z: 1
      Test:
        X: 5
        Y: 5
        Z: 5
    You need to use the getConfig().getConfigurationSection("waypoints").getKeys(false); to get a list of all the warps.
    Say you wanted to use a command to warp, you would do this: getConfig().getDouble("waypoints."+args[0]+"X"); and so on for y and z values. Add a check to make sure all the values exist.
    You would then teleport the sender to those x,y and z location.
     
  5. Offline

    mccrafter1212

    This is exactly the explanation I was looking for. Thank you very much! Also, after args[0] inside of the string that's being concatenated wouldn't you add a "." before the "X"? Maybe I'm wrong, but that's how it seems it should be done. Anyways, thanks once again! :D Gonna go test it now!

    I actually have another issue now... When I do:

    Code:
    plugin.getConfig().getConfigurationSection("waypoints").getKeys(false)[0] // It says I can't, because it should be an array but it's resolved to Set<String>. Solution?
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2017
  6. Offline

    Horsey

    @mccrafter1212 yeah, you need to use a dot before the 'X'.
    I don't know why the getKeys() method didn't work for you.
    @timtower Some advice?
     
  7. Offline

    timtower Moderator Moderator

Thread Status:
Not open for further replies.

Share This Page