Commit 6ced5668 by Jonathan Thomas

Fixing EntityChatDataLight, to correctly inflate from the full EntityChatData…

Fixing EntityChatDataLight, to correctly inflate from the full EntityChatData instances, including the current playerData.
parent c80b0fee
Pipeline #12735 passed with stages
in 2 minutes 4 seconds
...@@ -139,9 +139,7 @@ public class ChatDataManager { ...@@ -139,9 +139,7 @@ public class ChatDataManager {
// Create "light" version of entire chat data HashMap // Create "light" version of entire chat data HashMap
HashMap<String, EntityChatDataLight> lightVersionMap = new HashMap<>(); HashMap<String, EntityChatDataLight> lightVersionMap = new HashMap<>();
this.entityChatDataMap.forEach((id, entityChatData) -> lightVersionMap.put(id, entityChatData.toLightVersion(playerId))); this.entityChatDataMap.forEach((id, entityChatData) -> lightVersionMap.put(id, entityChatData.toLightVersion(playerId)));
return GSON.toJson(lightVersionMap);
// Convert light chat data to JSON string
return GSON.toJson(lightVersionMap).toString();
} catch (Exception e) { } catch (Exception e) {
// Handle exceptions // Handle exceptions
return ""; return "";
...@@ -153,6 +151,9 @@ public class ChatDataManager { ...@@ -153,6 +151,9 @@ public class ChatDataManager {
File saveFile = new File(server.getSavePath(WorldSavePath.ROOT).toFile(), "chatdata.json"); File saveFile = new File(server.getSavePath(WorldSavePath.ROOT).toFile(), "chatdata.json");
LOGGER.info("Saving chat data to " + saveFile.getAbsolutePath()); LOGGER.info("Saving chat data to " + saveFile.getAbsolutePath());
// Clean up blank, temp entities in data
entityChatDataMap.values().removeIf(entityChatData -> entityChatData.status == ChatStatus.NONE);
try (Writer writer = new OutputStreamWriter(new FileOutputStream(saveFile), StandardCharsets.UTF_8)) { try (Writer writer = new OutputStreamWriter(new FileOutputStream(saveFile), StandardCharsets.UTF_8)) {
GSON.toJson(this.entityChatDataMap, writer); GSON.toJson(this.entityChatDataMap, writer);
} catch (Exception e) { } catch (Exception e) {
...@@ -172,6 +173,9 @@ public class ChatDataManager { ...@@ -172,6 +173,9 @@ public class ChatDataManager {
Type type = new TypeToken<ConcurrentHashMap<String, EntityChatData>>(){}.getType(); Type type = new TypeToken<ConcurrentHashMap<String, EntityChatData>>(){}.getType();
this.entityChatDataMap = GSON.fromJson(reader, type); this.entityChatDataMap = GSON.fromJson(reader, type);
// Clean up blank, temp entities in data
entityChatDataMap.values().removeIf(entityChatData -> entityChatData.status == ChatStatus.NONE);
// Post-process each EntityChatData object // Post-process each EntityChatData object
for (EntityChatData entityChatData : entityChatDataMap.values()) { for (EntityChatData entityChatData : entityChatDataMap.values()) {
entityChatData.postDeserializeInitialization(); entityChatData.postDeserializeInitialization();
......
...@@ -107,6 +107,10 @@ public class EntityChatData { ...@@ -107,6 +107,10 @@ public class EntityChatData {
// Get the player data (or fallback to the blank player) // Get the player data (or fallback to the blank player)
public PlayerData getPlayerData(UUID playerId) { public PlayerData getPlayerData(UUID playerId) {
if (this.players == null) {
return new PlayerData();
}
// Check if the playerId exists in the players map // Check if the playerId exists in the players map
String playerIdStr = playerId.toString(); String playerIdStr = playerId.toString();
if (this.players.containsKey(playerIdStr)) { if (this.players.containsKey(playerIdStr)) {
...@@ -115,25 +119,12 @@ public class EntityChatData { ...@@ -115,25 +119,12 @@ public class EntityChatData {
// If the specific player ID is not found, fall back to the "" blank player // If the specific player ID is not found, fall back to the "" blank player
return this.players.get(""); return this.players.get("");
} }
return null; return new PlayerData();
} }
// Generate light version of chat data (no previous messages) // Generate light version of chat data (no previous messages)
public EntityChatDataLight toLightVersion(UUID playerId) { public EntityChatDataLight toLightVersion(UUID playerId) {
EntityChatDataLight light = new EntityChatDataLight(); return new EntityChatDataLight(this, playerId);
light.entityId = this.entityId;
light.currentMessage = this.currentMessage;
light.currentLineNumber = this.currentLineNumber;
light.status = this.status;
light.sender = this.sender;
PlayerData playerData = this.getPlayerData(playerId);
if (playerData != null) {
light.friendship = playerData.friendship;
} else {
light.friendship = 0;
}
return light;
} }
public String getCharacterProp(String propertyName) { public String getCharacterProp(String propertyName) {
......
package com.owlmaddie.chat; package com.owlmaddie.chat;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/** /**
* The {@code EntityChatDataLight} class represents the current displayed message, and no * The {@code EntityChatDataLight} class represents the current displayed message, and no
* previous messages or player message history. This is primarily used to broadcast the * previous messages or player message history. This is primarily used to broadcast the
...@@ -11,5 +15,20 @@ public class EntityChatDataLight { ...@@ -11,5 +15,20 @@ public class EntityChatDataLight {
public int currentLineNumber; public int currentLineNumber;
public ChatDataManager.ChatStatus status; public ChatDataManager.ChatStatus status;
public ChatDataManager.ChatSender sender; public ChatDataManager.ChatSender sender;
public int friendship; public Map<String, PlayerData> players;
// Constructor to initialize the light version from the full version
public EntityChatDataLight(EntityChatData fullData, UUID playerId) {
this.entityId = fullData.entityId;
this.currentMessage = fullData.currentMessage;
this.currentLineNumber = fullData.currentLineNumber;
this.status = fullData.status;
this.sender = fullData.sender;
// Initialize the players map and add only the current player's data
this.players = new HashMap<>();
String playerIdStr = playerId.toString();
PlayerData playerData = fullData.getPlayerData(playerId);
this.players.put(playerIdStr, playerData);
}
} }
\ 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