1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.github.JamesNorris.Class;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;

/**
 * The class that allows location to be serialized, and be used
 * in a file. This class should not be changed, otherwise it
 * will not work for older files.
 * 
 * @author Jnorr44
 */

public final class SerializableLocation implements Serializable {
	private static final long serialVersionUID = 8650311534439769069L;

	private final String world;
	private final String uuid;
	private final double x, y, z;
	private final float yaw, pitch;
	private transient Location loc;

	/**
	 * Creates a new SerializableLocation instance of any org.bukkit.Location.
	 * 
	 * @param l
	 */

	public SerializableLocation(Location l) {
		this.world = l.getWorld().getName();
		this.uuid = l.getWorld().getUID().toString();
		this.x = l.getX();
		this.y = l.getY();
		this.z = l.getZ();
		this.yaw = l.getYaw();
		this.pitch = l.getPitch();
	}

	/**
	 * Gets the org.bukkit.Location back from any SerializableLocation.
	 * 
	 * @param l
	 * @return
	 */

	public static Location returnLocation(SerializableLocation l) {
		float pitch = l.pitch;
		float yaw = l.yaw;
		double x = l.x;
		double y = l.y;
		double z = l.z;
		World world = Bukkit.getWorld(l.world);
		Location location = new Location(world, x, y, z, yaw, pitch);
		return location;
	}

	// FROM HERE ON NEEDS DOC NOTES

	public SerializableLocation(Map<String, Object> map) {
		this.world = (String) map.get("world");
		this.uuid = (String) map.get("uuid");
		this.x = (Double) map.get("x");
		this.y = (Double) map.get("y");
		this.z = (Double) map.get("z");
		this.yaw = ((Float) map.get("yaw")).floatValue();
		this.pitch = ((Float) map.get("pitch")).floatValue();
	}

	public final Map<String, Object> serialize() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("world", this.world);
		map.put("uuid", this.uuid);
		map.put("x", this.x);
		map.put("y", this.y);
		map.put("z", this.z);
		map.put("yaw", this.yaw);
		map.put("pitch", this.pitch);
		return map;
	}

	public final Location getLocation(Server server) {
		if (loc == null) {
			World world = server.getWorld(this.uuid);
			if (world == null) {
				world = server.getWorld(this.world);
			}
			loc = new Location(world, x, y, z, yaw, pitch);
		}
		return loc;
	}
}