Advertisement
Guest User

Untitled

a guest
May 16th, 2013
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. public class SerializedLocation
  2. {
  3. private Location location;
  4. private String raw;
  5.  
  6. public SerializedLocation(String raw)
  7. {
  8. this.raw = raw;
  9. this.location = parseLocation(this.raw);
  10. }
  11.  
  12. public SerializedLocation(World world, double x, double y, double z)
  13. {
  14. this.location = new Location(world, x, y, z);
  15. this.raw = serializeLocation(this.location);
  16. }
  17.  
  18. public SerializedLocation(World world, double x, double y, double z, float yaw, float pitch)
  19. {
  20. this.location = new Location(world, x, y, z, yaw, pitch);
  21. this.raw = serializeLocation(this.location);
  22. }
  23.  
  24. public SerializedLocation(Location location)
  25. {
  26. this.location = location;
  27. this.raw = serializeLocation(this.location);
  28. }
  29.  
  30. private String serializeLocation(Location location)
  31. {
  32. if ((location.getYaw() == 0.0F) && (location.getPitch() == 0.0F)) {
  33. return location.getWorld().getName() + ":" + location.getX() + ":" + location.getY() + ":" + location.getBlockZ();
  34. }
  35. return location.getWorld().getName() + ":" + location.getX() + ":" + location.getY() + ":" + location.getBlockZ() + ":" + location.getYaw() + ":" + location.getPitch();
  36. }
  37.  
  38. private Location parseLocation(String raw)
  39. {
  40. World world = null;
  41. double x = 0.0D;
  42. double y = 0.0D;
  43. double z = 0.0D;
  44. float yaw = 0.0F;
  45. float pitch = 0.0F;
  46.  
  47. int index = -1;
  48. for (String s : raw.split(":")) {
  49. index++;
  50. switch (index) {
  51. case 0:
  52. world = Bukkit.getWorld(s);
  53. if (world == null) {
  54. world = (World)Bukkit.getWorlds().get(0);
  55. MainWrapper.instance.getLogger().warning("World '" + s + "' could not be found, setting it to '" + world.getName() + "'");
  56. }
  57. break;
  58. case 1:
  59. x = Double.parseDouble(s);
  60. break;
  61. case 2:
  62. y = Double.parseDouble(s);
  63. break;
  64. case 3:
  65. z = Double.parseDouble(s);
  66. break;
  67. case 4:
  68. yaw = Float.parseFloat(s);
  69. break;
  70. case 5:
  71. pitch = Float.parseFloat(s);
  72. }
  73.  
  74. }
  75.  
  76. if (index == 3)
  77. return new Location(world, x, y, z);
  78. if (index == 5) {
  79. return new Location(world, x, y, z, yaw, pitch);
  80. }
  81. return null;
  82. }
  83.  
  84. public Location getLocation()
  85. {
  86. return this.location;
  87. }
  88.  
  89. public String getRaw()
  90. {
  91. return this.raw;
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement