summaryrefslogtreecommitdiffstats
path: root/EssentialsSpawn/src/com/earth2me/essentials/spawn/SpawnStorage.java
blob: c8e12563c4c2386e23ec3ade316e6162545a5573 (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
116
117
118
119
120
121
122
123
124
125
126
127
package com.earth2me.essentials.spawn;

import com.earth2me.essentials.IEssentialsModule;
import com.earth2me.essentials.settings.Spawns;
import com.earth2me.essentials.storage.AsyncStorageObjectHolder;
import net.ess3.api.IEssentials;
import java.io.File;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.bukkit.Location;
import org.bukkit.World;


public class SpawnStorage extends AsyncStorageObjectHolder<Spawns> implements IEssentialsModule
{
	private ConcurrentMap<String, Location> spawns;

	public SpawnStorage(final IEssentials ess)
	{
		super(ess, Spawns.class);
		reloadConfig();
	}

	@Override
	public final void reloadConfig()
	{
		super.reloadConfig();
		spawns = new ConcurrentHashMap<String, Location>();
	}

	@Override
	public File getStorageFile()
	{
		return new File(ess.getDataFolder(), "spawn.yml");
	}

	@Override
	public void finishRead()
	{
	}

	@Override
	public void finishWrite()
	{
	}

	public void setSpawn(final Location loc, final String group)
	{
		acquireWriteLock();
		try
		{
			if (getData().getSpawns() == null)
			{
				getData().setSpawns(new HashMap<String, Location>());
			}
			getData().getSpawns().put(group.toLowerCase(Locale.ENGLISH), loc);
		}
		finally
		{
			unlock();
		}

		spawns.clear();

		if ("default".equalsIgnoreCase(group))
		{
			loc.getWorld().setSpawnLocation(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
		}
	}

	public Location getSpawn(final String group)
	{
		Location spawnLocation;
		if (spawns.containsKey(group))
		{
			spawnLocation = spawns.get(group);
		}
		else
		{
			acquireReadLock();
			try
			{
				if (getData().getSpawns() == null || group == null)
				{
					return getWorldSpawn();
				}
				final Map<String, Location> spawnMap = getData().getSpawns();
				String groupName = group.toLowerCase(Locale.ENGLISH);
				if (!spawnMap.containsKey(groupName))
				{
					groupName = "default";
				}
				if (!spawnMap.containsKey(groupName))
				{
					spawnLocation = getWorldSpawn();
				}
				else
				{
					spawnLocation = spawnMap.get(groupName);
				}
			}
			finally
			{
				unlock();
			}

			spawns.put(group, spawnLocation);
		}
		return spawnLocation;
	}

	private Location getWorldSpawn()
	{
		for (World world : ess.getServer().getWorlds())
		{
			if (world.getEnvironment() != World.Environment.NORMAL)
			{
				continue;
			}
			return world.getSpawnLocation();
		}
		return ess.getServer().getWorlds().get(0).getSpawnLocation();
	}
}