Converting a String List into a Location List

Discussion in 'Plugin Development' started by Bamco6657, Jan 18, 2017.

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

    Bamco6657

    Hi! I'm trying to convert a list of strings in a config into a list of locations.

    This is how the location is added to the list:

    Code:
    plugin.getConfig().createSection("advancedcraftingtablelocations." + event.getBlock().getLocation());
    And this is how I call it to a String List:

    Code:
    List<String> advconfig = plugin.getConfig().getStringList("advancedcraftingtablelocations");
    I need to figure out how to turn this into a List<Location>. Someone help plez D:

    P.S. The code is in a separate class, not the main one.
     
  2. Offline

    Zombie_Striker

    @Bamco6657
    1. Create a list of strings, where each string is a location following the format "worldname,x,y,z".
    2. Save that list to the config.
    3. When you want to get the list, load the stringlist from the config and loop through it.
    4. For each string, seperate all the components by splitting at the commas. Create a new location variable with the data from the string.
    5. Add that new location to a new list.
     
  3. Offline

    Bamco6657

    How would I do this for each string? The number of strings could be any number for the setup i'm going for, that's why I wanted to convert the string list into a location list.
     
  4. Offline

    kameronn

    @Bamco6657
    Use a simple for loop
    Code:
    for(String s : list) {
    }
     
  5. Offline

    Bamco6657

    @Zombie_Striker @kameronn Going good so far, I'm just stuck at this part:

    Code:
            for(String s : advconfig){
                s.split(",");
               
                //What do I put here? 0_o
               
            }
     
  6. Offline

    mythbusterma

    @Bamco6657

    You need to parse the information out, each one of those is going to be a different portion of the information that makes up the Location object.
     
  7. Offline

    kameronn

    @Bamco6657
    Please do what mythbusterma said, create a new Location object and parse the information out.
    When you're doing s.split(",") that is returning a string array, but you do not set it as an object so you would never be able to call it unless you did something like this s.split(",")[0];

    When you are spliting a string, to get each string after it turns into a string array you must do array[0]. The number inside the brackets is what your getting in that string array, and it starts from 0.

    "Hey.My,Name,Is,Kam"
    array[0] returns Hey
    array[1] returns My

    However, be careful because if you try to do something like array[25] and there is no 25 in that array, then you will get an ArrayOutOfBounceException.

    Code:
    String[] stringloc = s.split(",");
    Location loc = new Location(stringloc[0], blah, blah);
     
  8. Offline

    Bamco6657

    @mythbusterma @kameronn A little confused on what to do there, I tried to do all of ways but just got a bunch of syntax errors. Maybe a better example? Also, I ran into another problem, my location doesn't look right in the config when I add it...

    Line of code to add it:
    Code:
    plugin.getConfig().createSection("advancedcraftingtable." + loc.getWorld() + loc.getX() + loc.getY() + loc.getZ());
    What it looks like in the config.yml:
    config.yml (open)
    advancedcraftingtable:
    CraftWorld{name=world}-128:
    '040':
    '0981':
    '0': {}
     
  9. Offline

    kameronn

    @Bamco6657
    Where are you getting synxtax errors please screenshot also, then just add it as a sring list in the config, and set it to the format you want
     
  10. Offline

    Bamco6657

    There's the picture. But how would I add it as a string list? Please specify.
     

    Attached Files:

  11. Offline

    Zombie_Striker

    @Bamco6657
    You problem is because you are providing string, and your not turning those strings into values. Use Bukkit.getWorld(...[0]) to turn the first arg into a world, and use Double.parseDouble(...[X]) to turn those last few args into the XYZ.
     
  12. Offline

    Bamco6657

    Ok, but I'm still having problems with the config format being all wonky, which I think is something that causing problems. How would I add the location to the config in the first place?
     
  13. Offline

    Zombie_Striker

    @Bamco6657
    By combining like I posted before:
    Code:
    String the_location = location.geWorld()+","+locatyion.getX()+","+location.getY()+","+location.getZ();
     
  14. Offline

    Bamco6657

    Still looks like this as a result in the config:

    advancedcraftingtable:
    CraftWorld{name=world},-121:
    0,40:
    0,985:
    '0': {}
     
  15. Offline

    Zombie_Striker

    @Bamco6657
    I made one mistake there; I forgot to add ".getName()" to the end of "getWorld". After that, all those variables should be fine.
     
  16. Offline

    Bamco6657

    @Zombie_Striker Ok, so I was searching around trying to fix bugs in my ode, and I was able to put my locations in the config like this:

    advancedcraftingtable:
    - CraftWorld{name=world},-131,40,982
    - CraftWorld{name=world},-126,40,985
    - CraftWorld{name=world},-129,40,986
    - CraftWorld{name=world},-131,40,984
    - CraftWorld{name=world},-129,40,983

    Now, I just need to figure out the code to get these to my location list. This is the code I'm using right now, but it's not working:

    Code:
    List<String> advconfig = plugin.getConfig().getStringList("advancedcraftingtable");
    
            for(String s : advconfig){
                String[] stringloc = s.split(",");
                Location loc = new Location(Bukkit.getWorld(stringloc[0]), Double.parseDouble(stringloc[1]),  Double.parseDouble(stringloc[2]),  Double.parseDouble(stringloc[3]));
    
                advcrafttable.add(loc);
    
            }
    
    //This code is located in my constructor, if that might be a problem.
    This might narrow down some problems. :)
     
  17. Offline

    Zombie_Striker

    @Bamco6657
    You have not added .getName() yet, or if you have, have not redone those saves. For right now, replace
    with
     
  18. Offline

    Bamco6657

    Ok, now they output like this:

    - world,-123,40,985

    But same result, it doesn't add the location to the List<Location> advcraftingtable
     
  19. Offline

    Zombie_Striker

    @Bamco6657
    Is the location null? If you look at the world, the XYZ, are all those values correct?
     
  20. Offline

    Bamco6657

    What do you mean? Do you mean if the coordinates that are written in the config are the ones that are in the game?
     
  21. Offline

    Zombie_Striker

    @Bamco6657
    Preferably, both. What you doing now is checking why the values are not being added to the list. Reasons why it is not being added is that the location is null or that the values are not correct.
     
  22. Offline

    Bamco6657

    How would I know if the value is null?
     
  23. Offline

    Zombie_Striker

    @Bamco6657
    if(loc == null)Bukkit.broadcastMessage("The location is null!!!")
     
  24. Offline

    Bamco6657

    So I changed some things. Instead of having that code in the constructor, I made this method:
    Code:
     public void convertStringLoc(String stringListName, List<Location> locationList){
            List<String> sl = plugin.getConfig().getStringList(stringListName);
    
            for(String s : sl){
                String[] stringloc = s.split(",");
                Location loc = new Location(Bukkit.getWorld(stringloc[0]), Integer.parseInt(stringloc[1]),  Integer.parseInt(stringloc[2]),  Integer.parseInt(stringloc[3]));
                locationList.add(loc);
                if(loc == null)Bukkit.broadcastMessage("The location is null!!!");
            }
        }
    Now, I can just do this:
    Code:
        public void onJoin(PlayerJoinEvent e){
            convertStringLoc("advancedcraftingtable", advcrafttable);
        }
    and this when a location is being added to the list:
    Code:
                        System.out.println("Test Success");
                        int x = loc.getBlockX() + 1;
                        int y = loc.getBlockY();
                        int z = loc.getBlockZ() + 1;
    
                        String s = loc.getWorld().getName() + "," + x + "," + y + "," + z ;
                        addString(s, "advancedcraftingtable");
                        plugin.saveConfig();
    
                        convertStringLoc("advancedcraftingtable", advcrafttable);
    (I added 1 to x and z because the block location was offset, so now these are the exact positions.)


    Anyways, back to your response, as you can see, I added that line of code that you told me to add in the convertStringLoc method. It gave no output in the console, so I don't think the location is null. But it's still not adding the location to the list! :'(
     
  25. Offline

    Zombie_Striker

    @Bamco6657
    If that is the case, now you need to check the values. Replace the null check with
    Code:
    if(loc != null)Bukkit.broadcastMessage("The location is "+loc.getX()+" "+loc.getY()+" " +loc.getZ()+" " +loc.getWorld()getName());
     
  26. Offline

    Bamco6657

    Ok, I get outputs like this:

    3:57:57 PM [INFO] The location is -123.0 40.0 973.0 world
    3:57:57 PM [INFO] The location is -122.0 40.0 972.0 world
    3:57:57 PM [INFO] The location is -129.0 40.0 975.0 world
     
Thread Status:
Not open for further replies.

Share This Page