blob: ac98b592101e2fd0030ba1df81db905f1dd311e6 (
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
package com.earth2me.essentials.spawn;
import com.earth2me.essentials.IConf;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.IEssentialsModule;
import com.earth2me.essentials.settings.Spawns;
import com.earth2me.essentials.storage.AbstractDelayedYamlFileReader;
import com.earth2me.essentials.storage.AbstractDelayedYamlFileWriter;
import com.earth2me.essentials.storage.StorageObject;
import java.io.File;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.bukkit.Location;
import org.bukkit.World;
public class SpawnStorage implements IConf, IEssentialsModule
{
private transient Spawns spawns;
private final transient IEssentials ess;
private final transient File spawnfile;
private final transient ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
public SpawnStorage(final IEssentials ess)
{
this.ess = ess;
spawnfile = new File(ess.getDataFolder(), "spawn.yml");
new SpawnReader();
}
public void setSpawn(final Location loc, final String group)
{
rwl.writeLock().lock();
try
{
if (spawns.getSpawns() == null)
{
spawns.setSpawns(new HashMap<String, Location>());
}
spawns.getSpawns().put(group.toLowerCase(Locale.ENGLISH), loc);
}
finally
{
rwl.writeLock().unlock();
}
new SpawnWriter();
if ("default".equalsIgnoreCase(group))
{
loc.getWorld().setSpawnLocation(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
}
}
public Location getSpawn(final String group)
{
rwl.readLock().lock();
try
{
if (spawns == null || spawns.getSpawns() == null || group == null)
{
return getWorldSpawn();
}
final Map<String, Location> spawnMap = spawns.getSpawns();
String groupName = group.toLowerCase(Locale.ENGLISH);
if (!spawnMap.containsKey(groupName))
{
groupName = "default";
}
if (!spawnMap.containsKey(groupName))
{
return getWorldSpawn();
}
return spawnMap.get(groupName);
}
finally
{
rwl.readLock().unlock();
}
}
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();
}
@Override
public void reloadConfig()
{
new SpawnReader();
}
private class SpawnWriter extends AbstractDelayedYamlFileWriter
{
public SpawnWriter()
{
super(ess, spawnfile);
}
@Override
public StorageObject getObject()
{
rwl.readLock().lock();
return spawns;
}
@Override
public void onFinish()
{
rwl.readLock().unlock();
}
}
private class SpawnReader extends AbstractDelayedYamlFileReader<Spawns>
{
public SpawnReader()
{
super(ess, spawnfile, Spawns.class);
}
@Override
public void onStart()
{
rwl.writeLock().lock();
}
@Override
public void onFinish(final Spawns object)
{
spawns = object;
rwl.writeLock().unlock();
}
}
}
|