Solved Problems with retrieving lists from files

Discussion in 'Spigot Plugin Development' started by PartyMC, Mar 13, 2020.

  1. I am currently making a simple warp command for my server, and all the data gets saved to a warps.yml file. When you do not give any arguments to my /warp command, you would get a list of warps. Im having struggles doing so. Here's my code:
    Code (Java):
                    if(args.length == 0) {
                        List<String> warps = FileManager.warps.getStringList("warps");
                        p.sendMessage(Color.color("&eAll warps:"));
                        p.sendMessage(Color.color("&7&o" + warps));
                    }
    My warps.yml is formatted as followed:
    Code (Text):
    warps:
      test:
        x: 295.18423036123284
        y: 36.244595864246044
        z: 82.03997221379954
        pitch: 12.309959
        yaw: -129.89153
        world: PartyParks
      test2:
        x: 292.7587205584538
        y: 36.244595864246044
        z: 83.03656761476806
        pitch: 12.309959
        yaw: -129.89153
        world: PartyParks
    The message I get back when sending 0 arguments is just "All warps:" and then "[]". How should I fix this? I tried restarting my server, because it may have been a problem with updating the file. Also everything is saved properly, my normal warping stuff works.
     
  2. warps is not a list, it's a configuration section
     
  3. to add onto the above, a list would look like this:
    Code (Text):
    warps:
    - one
    - two
    - three
     
  4. But then how would I be able to retrieve all warp names without disturbing the format?
     
  5. Try using FileManager.warps.getConfigurationSection("warps").getKeys(false)
     
  6. Im almost there, but the ouput for now is "[test,test2]" and I want to replace the [] but replace is not an available method. How can I do this?
     
  7. as stated above...

    Code (Text):
    HashMap<String,Location> warps = new HashMap<String,Location>(); <-a way to store the warps
    for(String key : getConfigurationSection("warps").getKeys(false){
    code,with key as identifier,
    warps.add(key,(Location)get("warps."+key)); <-adding the name of warp + its location(serialized)

    }
    Basically "key" is the warpname in your case test and test2

    Calling (Location) get("warps."+key); should return your location (depending on the way you saved those locations)
     
  8. Code (Java):
    String.join(FileManager.warps.getConfigurationSection("warps").getKeys(false), ", ")
    should do it for you