Skip to content

Commit 563e59b

Browse files
authored
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 YamlFile per world, so synchronizing on the instance wouldn't necessarily help.
1 parent 44516ba commit 563e59b

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
@@ -190,66 +190,68 @@ public Set<ProtectedRegion> loadAll(FlagRegistry flagRegistry) throws StorageExc
190190
}
191191

192192
@Override
193-
public synchronized void saveAll(Set<ProtectedRegion> regions) throws StorageException {
193+
public void saveAll(Set<ProtectedRegion> regions) throws StorageException {
194194
checkNotNull(regions);
195195

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

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

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

246-
config.setHeader(FILE_HEADER);
247-
config.save();
247+
config.setHeader(FILE_HEADER);
248+
config.save();
248249

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

0 commit comments

Comments
 (0)