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 ...@@ -22,6 +22,7 @@ All notable changes to **CreatureChat** are documented in this file. The format
### Fixed ### Fixed
- Entity persistence is now fixed (after creating a character sheet). No more despawning mobs. - Entity persistence is now fixed (after creating a character sheet). No more despawning mobs.
- Fixed crash when PROTECT behavior targets another player - 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 ## [1.0.8] - 2024-07-16
......
...@@ -24,6 +24,7 @@ import java.util.ArrayList; ...@@ -24,6 +24,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.UUID; 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, * The {@code ClientPackets} class provides methods to send packets to/from the server for generating greetings,
...@@ -160,7 +161,7 @@ public class ClientPackets { ...@@ -160,7 +161,7 @@ public class ClientPackets {
// Parse JSON and update client chat data // Parse JSON and update client chat data
Gson GSON = new Gson(); 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); ChatDataManager.getClientInstance().entityChatDataMap = GSON.fromJson(chatDataJSON, type);
// Clear receivedChunks for future use // Clear receivedChunks for future use
......
...@@ -32,6 +32,7 @@ import java.io.*; ...@@ -32,6 +32,7 @@ import java.io.*;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -66,7 +67,7 @@ public class ChatDataManager { ...@@ -66,7 +67,7 @@ public class ChatDataManager {
} }
// HashMap to associate unique entity IDs with their chat data // 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 static class ChatMessage {
public String message; public String message;
...@@ -484,7 +485,7 @@ public class ChatDataManager { ...@@ -484,7 +485,7 @@ public class ChatDataManager {
private ChatDataManager(Boolean server_only) { private ChatDataManager(Boolean server_only) {
// Constructor // Constructor
entityChatDataMap = new HashMap<>(); entityChatDataMap = new ConcurrentHashMap<>();
if (server_only) { if (server_only) {
// Generate initial quest // Generate initial quest
...@@ -596,15 +597,15 @@ public class ChatDataManager { ...@@ -596,15 +597,15 @@ public class ChatDataManager {
if (loadFile.exists()) { if (loadFile.exists()) {
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(loadFile), StandardCharsets.UTF_8)) { 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); this.entityChatDataMap = GSON.fromJson(reader, type);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error loading chat data", e); LOGGER.error("Error loading chat data", e);
this.entityChatDataMap = new HashMap<>(); this.entityChatDataMap = new ConcurrentHashMap<>();
} }
} else { } else {
// Init empty chat data // 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