[Utils] Location Serializers

Discussion in 'Resources' started by EnderTroll68, Aug 17, 2014.

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

    EnderTroll68

    Hello Bukkit Forums!

    I don't know if this is a little too common for it to be that helpful, but I use these a LOT and I thought it would be helpful to others.

    What the following does, is allow you to serialize and deserialize different aspects of locations in Bukkit. For those who don't know, to serialize is to turn an object into a String in order to easily store it in files, which is exactly what I use the following for. I hope you like it!
    Code:java
    1. import org.bukkit.Bukkit;
    2. import org.bukkit.Chunk;
    3. import org.bukkit.Location;
    4.  
    5. public class LocationUtils {
    6.  
    7. public static Chunk stringToChunk(String string) {
    8. String[] splits = string.split(";");
    9. return Bukkit.getWorld(splits[0]).getChunkAt(Integer.valueOf(splits[1]), Integer.valueOf(splits[2]));
    10. }
    11.  
    12. public static String chunkToString(Chunk chunk) {
    13. return chunk.getWorld().getName() + ";" + chunk.getX() + ";" + chunk.getZ();
    14. }
    15.  
    16. public static String locationToString(Location loc) {
    17. return loc.getWorld().getName() + ";" + loc.getX() + ";" + loc.getY() + ";" + loc.getZ() + ";" + loc.getYaw() + ";" + loc.getPitch();
    18. }
    19.  
    20. public static Location stringToLocation(String str) {
    21. String[] strar = str.split(";");
    22. Location newLoc = new Location(Bukkit.getWorld(strar[0]), Double.valueOf(strar[1]).doubleValue(), Double.valueOf(strar[2]).doubleValue(), Double.valueOf(strar[3]).doubleValue(), Float.valueOf(strar[4]).floatValue(), Float.valueOf(strar[5]).floatValue());
    23. return newLoc.clone();
    24. }
    25.  
    26. }
     
    KingFaris11, Bammerbom and lukasmcd14 like this.
Thread Status:
Not open for further replies.

Share This Page