Skip to content

Commit 4d06481

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 ccea106 commit 4d06481

1 file changed

Lines changed: 54 additions & 52 deletions

File tree

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

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

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -191,66 +191,68 @@ public Set<ProtectedRegion> loadAll(FlagRegistry flagRegistry) throws StorageExc
191191
}
192192

193193
@Override
194-
public synchronized void saveAll(Set<ProtectedRegion> regions) throws StorageException {
194+
public void saveAll(Set<ProtectedRegion> regions) throws StorageException {
195195
checkNotNull(regions);
196196

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

229-
node.setProperty("points", points);
230-
} else if (region instanceof GlobalProtectedRegion) {
231-
node.setProperty("type", "global");
232-
} else {
233-
node.setProperty("type", region.getClass().getCanonicalName());
234-
}
235-
236-
node.setProperty("priority", region.getPriority());
237-
node.setProperty("flags", getFlagData(region));
238-
node.setProperty("owners", getDomainData(region.getOwners()));
239-
node.setProperty("members", getDomainData(region.getMembers()));
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()));
240241

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

247-
config.setHeader(FILE_HEADER);
248-
config.save();
248+
config.setHeader(FILE_HEADER);
249+
config.save();
249250

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

0 commit comments

Comments
 (0)