Converting a String to Location

Discussion in 'Plugin Development' started by Licio123, Oct 19, 2014.

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

    Licio123

    I am making a plugin and........
    Whatever I need to make "Location{world=CraftWorld{name=Nether},x=8.0,y=80.0,z=-1104.0,pitch=0.0,yaw=0.0}" be a Location back. Any Idea how I could to this?
     
  2. Offline

    Dudemister1999

    Licio123 It'd be better if you made a 'key' of sort, so a Location > String would say [Nether*8.0*80.0*-1104.0*0.0*0.0], then when you get the String you can do something along the lines of:

    Code:java
    1.  
    2. String[] pieces = inputtedLocationString.replaceFirst("[", "").replaceLast("]", "").split("*");
    3.  
    4. World world = Bukkit.getWorld(pieces[0]);
    5. double x = pieces[1];
    6. double y = pieces[2];
    7. double z = pieces[3];
    8.  
    9. float pitch = pieces[4];
    10. float yaw = pieces[5];
    11.  


    Make sure you validate that the inputted string actually has that format, and make sure each piece is in fact a double, float or String as such. Just the way I do it, you might find something better.
     
  3. Offline

    FerusGrim

    Dudemister1999 His answer seems about right. I remember doing something a bit like that, in the past... though I think mine was a good bit lengthier. Good job.

    Really, though. If we can use Location#toString, there should really be a built-in Location#fromString.
     
    Dudemister1999 likes this.
  4. Offline

    Dudemister1999

    FerusGrim I agree! I made a class called LocationStringer, I'll post it in just a second (Need to dig around a little bit and find it :p )

    EDIT (Found it!)

    Code:java
    1. import org.bukkit.Bukkit;
    2. import org.bukkit.Location;
    3. import org.bukkit.World;
    4.  
    5. public class LocationStringer
    6. {
    7. public static String format = "%world%><%x%><%y%><%z%";
    8.  
    9. public static String toString(Location loc)
    10. {
    11. String location = format
    12. .replaceAll("%world%", loc.getWorld().getName())
    13. .replaceAll("%x%", String.valueOf(loc.getX()))
    14. .replaceAll("%y%", String.valueOf(loc.getY()))
    15. .replaceAll("%z%", String.valueOf(loc.getZ()));
    16.  
    17. return location;
    18. }
    19.  
    20. public static Location fromString(String loc)
    21. {
    22. String[] parts = loc.split("><");
    23. World world = Bukkit.getWorld(parts[0]);
    24. double xPos = Double.valueOf(parts[1]);
    25. double yPos = Double.valueOf(parts[2]);
    26. double zPos = Double.valueOf(parts[3]);
    27.  
    28. return new Location(world, xPos, yPos, zPos);
    29. }
    30. }
    31.  


    But as you can see, I never did any validation. (Therefor going against what I said). So take my advice from before, and use the code from directly above.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
    FerusGrim likes this.
  5. Offline

    FerusGrim

  6. Offline

    Dudemister1999

  7. Offline

    Licio123

    Thanks guys.... This is why I LOVE this comunity Learning and teaching at the same time
     
  8. Offline

    Dudemister1999

    Licio123 Indeed! And to think, people are saying the DMCA is tearing us apart. Lies, I say! LIES!
     
  9. Offline

    Licio123

Thread Status:
Not open for further replies.

Share This Page