summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/storage/Location.java
blob: 06210504b1ac4e77e39e8cedb068a52707f2bf93 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package net.ess3.storage;

import java.lang.ref.WeakReference;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.World;


public class Location
{
	private WeakReference<org.bukkit.Location> location;
	private final String worldname;
	private UUID worldUID = null;
	private final double x;
	private final double y;
	private final double z;
	private final float yaw;
	private final float pitch;

	public Location(org.bukkit.Location loc)
	{
		location = new WeakReference<org.bukkit.Location>(loc);
		worldname = loc.getWorld().getName();
		worldUID = loc.getWorld().getUID();
		x = loc.getX();
		y = loc.getY();
		z = loc.getZ();
		yaw = loc.getYaw();
		pitch = loc.getPitch();
	}

	public Location(String worldname, double x, double y, double z, float yaw, float pitch)
	{
		this.worldname = worldname;
		this.x = x;
		this.y = y;
		this.z = z;
		this.yaw = yaw;
		this.pitch = pitch;
	}

	public Location(String worldname, double x, double y, double z)
	{
		this.worldname = worldname;
		this.x = x;
		this.y = y;
		this.z = z;
		this.yaw = 0f;
		this.pitch = 0f;
	}

	public org.bukkit.Location getBukkitLocation() throws WorldNotLoadedException
	{

		org.bukkit.Location loc = location == null ? null : location.get();
		if (loc == null)
		{
			World world = null;
			if (worldUID != null)
			{
				world = Bukkit.getWorld(worldUID);
			}
			if (world == null)
			{
				world = Bukkit.getWorld(worldname);
			}
			if (world == null)
			{
				throw new WorldNotLoadedException(worldname);
			}
			loc = new org.bukkit.Location(world, getX(), getY(), getZ(), getYaw(), getPitch());
			location = new WeakReference<org.bukkit.Location>(loc);
		}
		return loc;
	}

	public String getWorldName()
	{
		return worldname;
	}

	public double getX()
	{
		return x;
	}

	public double getY()
	{
		return y;
	}

	public double getZ()
	{
		return z;
	}

	public float getYaw()
	{
		return yaw;
	}

	public float getPitch()
	{
		return pitch;
	}


	public static class WorldNotLoadedException extends Exception
	{
		public WorldNotLoadedException(String worldname)
		{
			super("World " + worldname + " is not loaded.");
		}
	}
}