Skip to content

Commit 5763f53

Browse files
committed
Synchronize on the class instead of the instance
I didn't realize it before, but it looks like there is at least one instance of YamlRegionFile per world, so synchronizing on the instance wouldn't necessarily help.
1 parent 7ba0fd3 commit 5763f53

File tree

1 file changed

+54
-52
lines changed
  • worldguard-core/src/main/java/com/sk89q/worldguard/protection/managers/storage/file

1 file changed

+54
-52
lines changed

worldguard-core/src/main/java/com/sk89q/worldguard/protection/managers/storage/file/YamlRegionFile.java

+54-52
Original file line numberDiff line numberDiff line change
@@ -192,66 +192,68 @@ public Set<ProtectedRegion> loadAll(FlagRegistry flagRegistry) throws StorageExc
192192
}
193193

194194
@Override
195-
public synchronized void saveAll(Set<ProtectedRegion> regions) throws StorageException {
195+
public void saveAll(Set<ProtectedRegion> regions) throws StorageException {
196196
checkNotNull(regions);
197197

198-
File tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
199-
YAMLProcessor config = createYamlProcessor(tempFile);
200-
201-
config.clear();
202-
203-
YAMLNode regionsNode = config.addNode("regions");
204-
Map<String, Object> map = regionsNode.getMap();
205-
206-
for (ProtectedRegion region : regions) {
207-
Map<String, Object> nodeMap = new HashMap<>();
208-
map.put(region.getId(), nodeMap);
209-
YAMLNode node = new YAMLNode(nodeMap, false);
210-
211-
if (region instanceof ProtectedCuboidRegion) {
212-
ProtectedCuboidRegion cuboid = (ProtectedCuboidRegion) region;
213-
node.setProperty("type", "cuboid");
214-
node.setProperty("min", cuboid.getMinimumPoint());
215-
node.setProperty("max", cuboid.getMaximumPoint());
216-
} else if (region instanceof ProtectedPolygonalRegion) {
217-
ProtectedPolygonalRegion poly = (ProtectedPolygonalRegion) region;
218-
node.setProperty("type", "poly2d");
219-
node.setProperty("min-y", poly.getMinimumPoint().getBlockY());
220-
node.setProperty("max-y", poly.getMaximumPoint().getBlockY());
221-
222-
List<Map<String, Object>> points = new ArrayList<>();
223-
for (BlockVector2 point : poly.getPoints()) {
224-
Map<String, Object> data = new HashMap<>();
225-
data.put("x", point.getBlockX());
226-
data.put("z", point.getBlockZ());
227-
points.add(data);
198+
synchronized (YamlRegionFile.class) {
199+
File tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
200+
YAMLProcessor config = createYamlProcessor(tempFile);
201+
202+
config.clear();
203+
204+
YAMLNode regionsNode = config.addNode("regions");
205+
Map<String, Object> map = regionsNode.getMap();
206+
207+
for (ProtectedRegion region : regions) {
208+
Map<String, Object> nodeMap = new HashMap<>();
209+
map.put(region.getId(), nodeMap);
210+
YAMLNode node = new YAMLNode(nodeMap, false);
211+
212+
if (region instanceof ProtectedCuboidRegion) {
213+
ProtectedCuboidRegion cuboid = (ProtectedCuboidRegion) region;
214+
node.setProperty("type", "cuboid");
215+
node.setProperty("min", cuboid.getMinimumPoint());
216+
node.setProperty("max", cuboid.getMaximumPoint());
217+
} else if (region instanceof ProtectedPolygonalRegion) {
218+
ProtectedPolygonalRegion poly = (ProtectedPolygonalRegion) region;
219+
node.setProperty("type", "poly2d");
220+
node.setProperty("min-y", poly.getMinimumPoint().getBlockY());
221+
node.setProperty("max-y", poly.getMaximumPoint().getBlockY());
222+
223+
List<Map<String, Object>> points = new ArrayList<>();
224+
for (BlockVector2 point : poly.getPoints()) {
225+
Map<String, Object> data = new HashMap<>();
226+
data.put("x", point.getBlockX());
227+
data.put("z", point.getBlockZ());
228+
points.add(data);
229+
}
230+
231+
node.setProperty("points", points);
232+
} else if (region instanceof GlobalProtectedRegion) {
233+
node.setProperty("type", "global");
234+
} else {
235+
node.setProperty("type", region.getClass().getCanonicalName());
228236
}
229237

230-
node.setProperty("points", points);
231-
} else if (region instanceof GlobalProtectedRegion) {
232-
node.setProperty("type", "global");
233-
} else {
234-
node.setProperty("type", region.getClass().getCanonicalName());
235-
}
236-
237-
node.setProperty("priority", region.getPriority());
238-
node.setProperty("flags", getFlagData(region));
239-
node.setProperty("owners", getDomainData(region.getOwners()));
240-
node.setProperty("members", getDomainData(region.getMembers()));
238+
node.setProperty("priority", region.getPriority());
239+
node.setProperty("flags", getFlagData(region));
240+
node.setProperty("owners", getDomainData(region.getOwners()));
241+
node.setProperty("members", getDomainData(region.getMembers()));
241242

242-
ProtectedRegion parent = region.getParent();
243-
if (parent != null) {
244-
node.setProperty("parent", parent.getId());
243+
ProtectedRegion parent = region.getParent();
244+
if (parent != null) {
245+
node.setProperty("parent", parent.getId());
246+
}
245247
}
246-
}
247248

248-
config.setHeader(FILE_HEADER);
249-
config.save();
249+
config.setHeader(FILE_HEADER);
250+
config.save();
250251

251-
//noinspection ResultOfMethodCallIgnored
252-
file.delete();
253-
if (!tempFile.renameTo(file)) {
254-
throw new StorageException("Failed to rename temporary regions file to " + file.getAbsolutePath());
252+
//noinspection ResultOfMethodCallIgnored
253+
file.delete();
254+
if (!tempFile.renameTo(file)) {
255+
throw new StorageException("Failed to rename temporary regions file to " + file.getAbsolutePath());
256+
}
255257
}
256258
}
257259

0 commit comments

Comments
 (0)