Last active
December 31, 2015 14:28
-
-
Save Garris0n-/8000025 to your computer and use it in GitHub Desktop.
Location <--> String serialization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//NO YAW/PITCH: | |
public static String locationToString(Location location){ //Method to turn a location into a string | |
return location.getWorld() + ";" + location.getX() + ";" + location.getY() + ";" + location.getZ(); //Return a string with the format world;x;y;z | |
} | |
public static Location stringToLocation(String string){ //turn a string into a location | |
String[] split = string.split(";"); //split the string by the ";" so world;x;y;z becomes an array with world, x, y, and z in it | |
if(split.length < 4) //the string array is too short, return null | |
return null; //null | |
World world = Bukkit.getServer().getWorld(split[0]); //the first argument is a World, get the world by name | |
if(world == null) //the world does not exist and thus is null, so return null | |
return null; | |
double x; //x | |
double y; //y | |
double z; //z | |
try{ //we're gonna need a try/catch here | |
x = Double.parseDouble(split[1]); //parse the next argument into a double | |
y = Double.parseDouble(split[2]); //parse the next argument into a double | |
z = Double.parseDouble(split[3]); //parse the next argument into a double | |
} | |
catch(NumberFormatException e){ //one of them wasn't a double, return null | |
return null; | |
} | |
return new Location(world, x, y, z); //return a new location | |
} | |
//WITH YAW/PITCH: | |
public static String locationToString(Location location){ //Method to turn a location into a string | |
return location.getWorld() + ";" + location.getX() + ";" + location.getY() + ";" + location.getZ() + ";" + location.getYaw() + ";" + location.getPitch(); //Return a string with the format world;x;y;z;yaw;pitch | |
} | |
public static Location stringToLocation(String string){ //turn a string into a location | |
String[] split = string.split(";"); //split the string by the ";" so world;x;y;z;yaw;pitch becomes an array with world, x, y, z, yaw, and pitch in it | |
if(split.length < 6) //the string array is too short, return null | |
return null; //null | |
World world = Bukkit.getServer().getWorld(split[0]); //the first argument is a World, get the world by name | |
if(world == null) //the world does not exist and thus is null, so return null | |
return null; //null | |
double x; //x | |
double y; //y | |
double z; //z | |
float yaw; //yaw | |
float pitch; //pitch | |
try{ //we're gonna need a try/catch here | |
x = Double.parseDouble(split[1]); //parse the next argument into a double | |
y = Double.parseDouble(split[2]); //parse the next argument into a double | |
z = Double.parseDouble(split[3]); //parse the next argument into a double | |
yaw = Float.parseFloat(split[4]); //parse the next argument into a float | |
pitch = Float.parseFloat(split[5]); //parse the next argument into a float | |
} | |
catch(NumberFormatException e){ //one of them wasn't a double/float, return null | |
return null; | |
} | |
return new Location(world, x, y, z, yaw, pitch); //return a new location | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment