Commit 9a25ad12 by Jonathan Thomas

Switch from HashMap to ConcurrentHashMap to prevent an error when saving (while…

Switch from HashMap to ConcurrentHashMap to prevent an error when saving (while modifying the HashMap)
parent 7489adbb
Pipeline #12721 passed with stages
in 2 minutes 29 seconds
......@@ -22,6 +22,7 @@ All notable changes to **CreatureChat** are documented in this file. The format
### Fixed
- Entity persistence is now fixed (after creating a character sheet). No more despawning mobs.
- Fixed crash when PROTECT behavior targets another player
- Fixed error when saving chat data while generating a new chat message
## [1.0.8] - 2024-07-16
......
......@@ -24,6 +24,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
/**
* The {@code ClientPackets} class provides methods to send packets to/from the server for generating greetings,
......@@ -160,7 +161,7 @@ public class ClientPackets {
// Parse JSON and update client chat data
Gson GSON = new Gson();
Type type = new TypeToken<HashMap<String, ChatDataManager.EntityChatData>>(){}.getType();
Type type = new TypeToken<ConcurrentHashMap<String, ChatDataManager.EntityChatData>>(){}.getType();
ChatDataManager.getClientInstance().entityChatDataMap = GSON.fromJson(chatDataJSON, type);
// Clear receivedChunks for future use
......
......@@ -32,6 +32,7 @@ import java.io.*;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
......@@ -66,7 +67,7 @@ public class ChatDataManager {
}
// HashMap to associate unique entity IDs with their chat data
public HashMap<String, EntityChatData> entityChatDataMap;
public ConcurrentHashMap<String, EntityChatData> entityChatDataMap;
public static class ChatMessage {
public String message;
......@@ -484,7 +485,7 @@ public class ChatDataManager {
private ChatDataManager(Boolean server_only) {
// Constructor
entityChatDataMap = new HashMap<>();
entityChatDataMap = new ConcurrentHashMap<>();
if (server_only) {
// Generate initial quest
......@@ -596,15 +597,15 @@ public class ChatDataManager {
if (loadFile.exists()) {
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(loadFile), StandardCharsets.UTF_8)) {
Type type = new TypeToken<HashMap<String, EntityChatData>>(){}.getType();
Type type = new TypeToken<ConcurrentHashMap<String, EntityChatData>>(){}.getType();
this.entityChatDataMap = GSON.fromJson(reader, type);
} catch (Exception e) {
LOGGER.error("Error loading chat data", e);
this.entityChatDataMap = new HashMap<>();
this.entityChatDataMap = new ConcurrentHashMap<>();
}
} else {
// Init empty chat data
this.entityChatDataMap = new HashMap<>();
this.entityChatDataMap = new ConcurrentHashMap<>();
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment