Skip to content

Commit 337e6c6

Browse files
fix: only allow one write at a time
1 parent 0337a47 commit 337e6c6

File tree

1 file changed

+16
-6
lines changed
  • PistonChat/src/main/java/net/pistonmaster/pistonchat/storage/file

1 file changed

+16
-6
lines changed

PistonChat/src/main/java/net/pistonmaster/pistonchat/storage/file/FileStorage.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private void loadData() {
8686
}
8787
}
8888

89-
private void saveMap(Map<?, ?> data, Path file) {
89+
private synchronized void saveMap(Map<?, ?> data, Path file) {
9090
try {
9191
Files.createDirectories(file.getParent());
9292
try (Writer writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) {
@@ -100,7 +100,9 @@ private void saveMap(Map<?, ?> data, Path file) {
100100
@Override
101101
public void setChatEnabled(UUID uuid, boolean enabled) {
102102
chatSettings.put(uuid, enabled);
103-
saveMap(chatSettings, chatSettingsFile);
103+
synchronized (chatSettings) {
104+
saveMap(chatSettings, chatSettingsFile);
105+
}
104106
}
105107

106108
@Override
@@ -111,7 +113,9 @@ public boolean isChatEnabled(UUID uuid) {
111113
@Override
112114
public void setWhisperingEnabled(UUID uuid, boolean enabled) {
113115
whisperSettings.put(uuid, enabled);
114-
saveMap(whisperSettings, whisperSettingsFile);
116+
synchronized (whisperSettings) {
117+
saveMap(whisperSettings, whisperSettingsFile);
118+
}
115119
}
116120

117121
@Override
@@ -125,11 +129,15 @@ public HardReturn hardIgnorePlayer(UUID ignoringReceiver, UUID ignoredChatter) {
125129

126130
if (ignored.contains(ignoredChatter)) {
127131
ignored.remove(ignoredChatter);
128-
saveMap(ignoreList, ignoreListFile);
132+
synchronized (ignored) {
133+
saveMap(ignoreList, ignoreListFile);
134+
}
129135
return HardReturn.UN_IGNORE;
130136
} else {
131137
ignored.add(ignoredChatter);
132-
saveMap(ignoreList, ignoreListFile);
138+
synchronized (ignored) {
139+
saveMap(ignoreList, ignoreListFile);
140+
}
133141
return HardReturn.IGNORE;
134142
}
135143
}
@@ -149,6 +157,8 @@ public List<UUID> getIgnoredList(UUID uuid) {
149157
@Override
150158
public void clearIgnoredPlayers(UUID player) {
151159
ignoreList.remove(player);
152-
saveMap(ignoreList, ignoreListFile);
160+
synchronized (ignoreList) {
161+
saveMap(ignoreList, ignoreListFile);
162+
}
153163
}
154164
}

0 commit comments

Comments
 (0)