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.
This commit is contained in:
TomyLobo 2023-06-12 03:16:14 +02:00
parent 7ba0fd3f53
commit 5763f53415
1 changed files with 49 additions and 47 deletions

View File

@ -192,67 +192,69 @@ public class YamlRegionFile implements RegionDatabase {
} }
@Override @Override
public synchronized void saveAll(Set<ProtectedRegion> regions) throws StorageException { public void saveAll(Set<ProtectedRegion> regions) throws StorageException {
checkNotNull(regions); checkNotNull(regions);
File tempFile = new File(file.getParentFile(), file.getName() + ".tmp"); synchronized (YamlRegionFile.class) {
YAMLProcessor config = createYamlProcessor(tempFile); File tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
YAMLProcessor config = createYamlProcessor(tempFile);
config.clear(); config.clear();
YAMLNode regionsNode = config.addNode("regions"); YAMLNode regionsNode = config.addNode("regions");
Map<String, Object> map = regionsNode.getMap(); Map<String, Object> map = regionsNode.getMap();
for (ProtectedRegion region : regions) { for (ProtectedRegion region : regions) {
Map<String, Object> nodeMap = new HashMap<>(); Map<String, Object> nodeMap = new HashMap<>();
map.put(region.getId(), nodeMap); map.put(region.getId(), nodeMap);
YAMLNode node = new YAMLNode(nodeMap, false); YAMLNode node = new YAMLNode(nodeMap, false);
if (region instanceof ProtectedCuboidRegion) { if (region instanceof ProtectedCuboidRegion) {
ProtectedCuboidRegion cuboid = (ProtectedCuboidRegion) region; ProtectedCuboidRegion cuboid = (ProtectedCuboidRegion) region;
node.setProperty("type", "cuboid"); node.setProperty("type", "cuboid");
node.setProperty("min", cuboid.getMinimumPoint()); node.setProperty("min", cuboid.getMinimumPoint());
node.setProperty("max", cuboid.getMaximumPoint()); node.setProperty("max", cuboid.getMaximumPoint());
} else if (region instanceof ProtectedPolygonalRegion) { } else if (region instanceof ProtectedPolygonalRegion) {
ProtectedPolygonalRegion poly = (ProtectedPolygonalRegion) region; ProtectedPolygonalRegion poly = (ProtectedPolygonalRegion) region;
node.setProperty("type", "poly2d"); node.setProperty("type", "poly2d");
node.setProperty("min-y", poly.getMinimumPoint().getBlockY()); node.setProperty("min-y", poly.getMinimumPoint().getBlockY());
node.setProperty("max-y", poly.getMaximumPoint().getBlockY()); node.setProperty("max-y", poly.getMaximumPoint().getBlockY());
List<Map<String, Object>> points = new ArrayList<>(); List<Map<String, Object>> points = new ArrayList<>();
for (BlockVector2 point : poly.getPoints()) { for (BlockVector2 point : poly.getPoints()) {
Map<String, Object> data = new HashMap<>(); Map<String, Object> data = new HashMap<>();
data.put("x", point.getBlockX()); data.put("x", point.getBlockX());
data.put("z", point.getBlockZ()); data.put("z", point.getBlockZ());
points.add(data); points.add(data);
}
node.setProperty("points", points);
} else if (region instanceof GlobalProtectedRegion) {
node.setProperty("type", "global");
} else {
node.setProperty("type", region.getClass().getCanonicalName());
} }
node.setProperty("points", points); node.setProperty("priority", region.getPriority());
} else if (region instanceof GlobalProtectedRegion) { node.setProperty("flags", getFlagData(region));
node.setProperty("type", "global"); node.setProperty("owners", getDomainData(region.getOwners()));
} else { node.setProperty("members", getDomainData(region.getMembers()));
node.setProperty("type", region.getClass().getCanonicalName());
ProtectedRegion parent = region.getParent();
if (parent != null) {
node.setProperty("parent", parent.getId());
}
} }
node.setProperty("priority", region.getPriority()); config.setHeader(FILE_HEADER);
node.setProperty("flags", getFlagData(region)); config.save();
node.setProperty("owners", getDomainData(region.getOwners()));
node.setProperty("members", getDomainData(region.getMembers()));
ProtectedRegion parent = region.getParent(); //noinspection ResultOfMethodCallIgnored
if (parent != null) { file.delete();
node.setProperty("parent", parent.getId()); if (!tempFile.renameTo(file)) {
throw new StorageException("Failed to rename temporary regions file to " + file.getAbsolutePath());
} }
} }
config.setHeader(FILE_HEADER);
config.save();
//noinspection ResultOfMethodCallIgnored
file.delete();
if (!tempFile.renameTo(file)) {
throw new StorageException("Failed to rename temporary regions file to " + file.getAbsolutePath());
}
} }
@Override @Override