Commit 6b8ee14d by Jonathan Thomas

Seperated Player and Entity message broadcasts (different packets for…

Seperated Player and Entity message broadcasts (different packets for simplicity). Removed extra message broadcast (which was unnecessary).
parent 5cabaa05
Pipeline #13272 passed with stages
in 2 minutes 12 seconds
...@@ -13,9 +13,13 @@ All notable changes to **CreatureChat** are documented in this file. The format ...@@ -13,9 +13,13 @@ All notable changes to **CreatureChat** are documented in this file. The format
- Improved LLM Unit tests (to prevent rate limit issues from certain providers when running all tests) - Improved LLM Unit tests (to prevent rate limit issues from certain providers when running all tests)
- Check friendship direction (+ or -) in LLM unit tests (to verify friendship direction is output correctly) - Check friendship direction (+ or -) in LLM unit tests (to verify friendship direction is output correctly)
### Changed
- Seperated Player and Entity message broadcasts (different packets for simplicity)
### Fixed ### Fixed
- Fixed death messages for mobs with no chat data - Fixed death messages for mobs with no chat data
- Fixed transparent background behind chat screen for Minecraft 1.20 and 1.20.1. - Fixed transparent background behind chat screen for Minecraft 1.20 and 1.20.1.
- Removed extra message broadcast (which was unnecessary)
## [1.2.1] - 2025-01-01 ## [1.2.1] - 2025-01-01
......
...@@ -108,17 +108,9 @@ public class ClientPackets { ...@@ -108,17 +108,9 @@ public class ClientPackets {
public static void register() { public static void register() {
// Client-side packet handler, message sync // Client-side packet handler, message sync
ClientPlayNetworking.registerGlobalReceiver(ServerPackets.PACKET_S2C_MESSAGE, (client, handler, buffer, responseSender) -> { ClientPlayNetworking.registerGlobalReceiver(ServerPackets.PACKET_S2C_ENTITY_MESSAGE, (client, handler, buffer, responseSender) -> {
// Read the data from the server packet // Read the data from the server packet
UUID entityId = UUID.fromString(buffer.readString()); UUID entityId = UUID.fromString(buffer.readString());
String sendingPlayerIdStr = buffer.readString(32767);
String senderPlayerName = buffer.readString(32767);
UUID senderPlayerId;
if (!sendingPlayerIdStr.isEmpty()) {
senderPlayerId = UUID.fromString(sendingPlayerIdStr);
} else {
senderPlayerId = null;
}
String message = buffer.readString(32767); String message = buffer.readString(32767);
int line = buffer.readInt(); int line = buffer.readInt();
String status_name = buffer.readString(32767); String status_name = buffer.readString(32767);
...@@ -146,27 +138,40 @@ public class ClientPackets { ...@@ -146,27 +138,40 @@ public class ClientPackets {
ChatDataManager chatDataManager = ChatDataManager.getClientInstance(); ChatDataManager chatDataManager = ChatDataManager.getClientInstance();
EntityChatData chatData = chatDataManager.getOrCreateChatData(entity.getUuidAsString()); EntityChatData chatData = chatDataManager.getOrCreateChatData(entity.getUuidAsString());
if (senderPlayerId != null && sender == ChatDataManager.ChatSender.USER && status == ChatDataManager.ChatStatus.DISPLAY) { // Add entity message
// Add player message to queue for rendering if (!message.isEmpty()) {
PlayerMessageManager.addMessage(senderPlayerId, message, senderPlayerName, ChatDataManager.TICKS_TO_DISPLAY_USER_MESSAGE); chatData.currentMessage = message;
chatData.status = ChatDataManager.ChatStatus.PENDING;
} else {
// Add entity message
if (!message.isEmpty()) {
chatData.currentMessage = message;
}
chatData.currentLineNumber = line;
chatData.status = status;
chatData.sender = sender;
chatData.players = players;
} }
chatData.currentLineNumber = line;
chatData.status = status;
chatData.sender = sender;
chatData.players = players;
// Play sound with volume based on distance (from player or entity) // Play sound with volume based on distance (from player or entity)
playNearbyUISound(client, entity, 0.2f); playNearbyUISound(client, entity, 0.2f);
}); });
}); });
// Client-side packet handler, message sync
ClientPlayNetworking.registerGlobalReceiver(ServerPackets.PACKET_S2C_PLAYER_MESSAGE, (client, handler, buffer, responseSender) -> {
// Read the data from the server packet
UUID senderPlayerId = UUID.fromString(buffer.readString());
String senderPlayerName = buffer.readString(32767);
String message = buffer.readString(32767);
// Update the chat data manager on the client-side
client.execute(() -> { // Make sure to run on the client thread
// Ensure client.player is initialized
if (client.player == null || client.world == null) {
LOGGER.warn("Client not fully initialized. Dropping message for sender '{}'.", senderPlayerId);
return;
}
// Add player message to queue for rendering
PlayerMessageManager.addMessage(senderPlayerId, message, senderPlayerName, ChatDataManager.TICKS_TO_DISPLAY_USER_MESSAGE);
});
});
// Client-side player login: get all chat data // Client-side player login: get all chat data
ClientPlayNetworking.registerGlobalReceiver(ServerPackets.PACKET_S2C_LOGIN, (client, handler, buffer, responseSender) -> { ClientPlayNetworking.registerGlobalReceiver(ServerPackets.PACKET_S2C_LOGIN, (client, handler, buffer, responseSender) -> {
int sequenceNumber = buffer.readInt(); // Sequence number of the current packet int sequenceNumber = buffer.readInt(); // Sequence number of the current packet
......
...@@ -85,7 +85,7 @@ public class ChatDataManager { ...@@ -85,7 +85,7 @@ public class ChatDataManager {
LOGGER.info("Updated chat data from UUID (" + oldUUID + ") to UUID (" + newUUID + ")"); LOGGER.info("Updated chat data from UUID (" + oldUUID + ") to UUID (" + newUUID + ")");
// Broadcast to all players // Broadcast to all players
ServerPackets.BroadcastPacketMessage(data, null); ServerPackets.BroadcastEntityMessage(data);
} else { } else {
LOGGER.info("Unable to update chat data, UUID not found: " + oldUUID); LOGGER.info("Unable to update chat data, UUID not found: " + oldUUID);
} }
......
...@@ -556,9 +556,6 @@ public class EntityChatData { ...@@ -556,9 +556,6 @@ public class EntityChatData {
previousMessages.clear(); previousMessages.clear();
} }
} }
// Broadcast to all players
ServerPackets.BroadcastPacketMessage(this, player);
}); });
} }
...@@ -603,15 +600,17 @@ public class EntityChatData { ...@@ -603,15 +600,17 @@ public class EntityChatData {
// Determine status for message // Determine status for message
if (sender == ChatDataManager.ChatSender.ASSISTANT) { if (sender == ChatDataManager.ChatSender.ASSISTANT) {
status = ChatDataManager.ChatStatus.DISPLAY; status = ChatDataManager.ChatStatus.DISPLAY;
} else if (sender == ChatDataManager.ChatSender.USER && systemPrompt.equals("system-chat")) {
// Only show system-chat messages above players (not system-character ones)
status = ChatDataManager.ChatStatus.DISPLAY;
} else { } else {
status = ChatDataManager.ChatStatus.PENDING; status = ChatDataManager.ChatStatus.PENDING;
} }
// Broadcast to all players if (sender == ChatDataManager.ChatSender.USER && systemPrompt.equals("system-chat") && auto_generated == 0) {
ServerPackets.BroadcastPacketMessage(this, player); // Broadcast new player message (when not auto-generated)
ServerPackets.BroadcastPlayerMessage(this, player);
}
// Broadcast new entity message status (i.e. pending)
ServerPackets.BroadcastEntityMessage(this);
} }
// Get wrapped lines // Get wrapped lines
...@@ -631,13 +630,13 @@ public class EntityChatData { ...@@ -631,13 +630,13 @@ public class EntityChatData {
currentLineNumber = Math.min(Math.max(lineNumber, 0), totalLines); currentLineNumber = Math.min(Math.max(lineNumber, 0), totalLines);
// Broadcast to all players // Broadcast to all players
ServerPackets.BroadcastPacketMessage(this, null); ServerPackets.BroadcastEntityMessage(this);
} }
public void setStatus(ChatDataManager.ChatStatus new_status) { public void setStatus(ChatDataManager.ChatStatus new_status) {
status = new_status; status = new_status;
// Broadcast to all players // Broadcast to all players
ServerPackets.BroadcastPacketMessage(this, null); ServerPackets.BroadcastEntityMessage(this);
} }
} }
\ No newline at end of file
...@@ -57,7 +57,8 @@ public class ServerPackets { ...@@ -57,7 +57,8 @@ public class ServerPackets {
public static final Identifier PACKET_C2S_OPEN_CHAT = new Identifier("creaturechat", "packet_c2s_open_chat"); public static final Identifier PACKET_C2S_OPEN_CHAT = new Identifier("creaturechat", "packet_c2s_open_chat");
public static final Identifier PACKET_C2S_CLOSE_CHAT = new Identifier("creaturechat", "packet_c2s_close_chat"); public static final Identifier PACKET_C2S_CLOSE_CHAT = new Identifier("creaturechat", "packet_c2s_close_chat");
public static final Identifier PACKET_C2S_SEND_CHAT = new Identifier("creaturechat", "packet_c2s_send_chat"); public static final Identifier PACKET_C2S_SEND_CHAT = new Identifier("creaturechat", "packet_c2s_send_chat");
public static final Identifier PACKET_S2C_MESSAGE = new Identifier("creaturechat", "packet_s2c_message"); public static final Identifier PACKET_S2C_ENTITY_MESSAGE = new Identifier("creaturechat", "packet_s2c_entity_message");
public static final Identifier PACKET_S2C_PLAYER_MESSAGE = new Identifier("creaturechat", "packet_s2c_player_message");
public static final Identifier PACKET_S2C_LOGIN = new Identifier("creaturechat", "packet_s2c_login"); public static final Identifier PACKET_S2C_LOGIN = new Identifier("creaturechat", "packet_s2c_login");
public static final Identifier PACKET_S2C_WHITELIST = new Identifier("creaturechat", "packet_s2c_whitelist"); public static final Identifier PACKET_S2C_WHITELIST = new Identifier("creaturechat", "packet_s2c_whitelist");
public static final Identifier PACKET_S2C_PLAYER_STATUS = new Identifier("creaturechat", "packet_s2c_player_status"); public static final Identifier PACKET_S2C_PLAYER_STATUS = new Identifier("creaturechat", "packet_s2c_player_status");
...@@ -342,15 +343,12 @@ public class ServerPackets { ...@@ -342,15 +343,12 @@ public class ServerPackets {
} }
// Send new message to all connected players // Send new message to all connected players
public static void BroadcastPacketMessage(EntityChatData chatData, ServerPlayerEntity sender) { public static void BroadcastEntityMessage(EntityChatData chatData) {
// Log useful information before looping through all players // Log useful information before looping through all players
LOGGER.info("Broadcasting message: sender={}, entityId={}, status={}, currentMessage={}, currentLineNumber={}, senderType={}", LOGGER.info("Broadcasting entity message: entityId={}, status={}, currentMessage={}, currentLineNumber={}, senderType={}",
sender != null ? sender.getDisplayName().getString() : "Unknown", chatData.entityId, chatData.status,
chatData.entityId,
chatData.status,
chatData.currentMessage.length() > 24 ? chatData.currentMessage.substring(0, 24) + "..." : chatData.currentMessage, chatData.currentMessage.length() > 24 ? chatData.currentMessage.substring(0, 24) + "..." : chatData.currentMessage,
chatData.currentLineNumber, chatData.currentLineNumber, chatData.sender);
chatData.sender);
for (ServerWorld world : serverInstance.getWorlds()) { for (ServerWorld world : serverInstance.getWorlds()) {
UUID entityId = UUID.fromString(chatData.entityId); UUID entityId = UUID.fromString(chatData.entityId);
...@@ -373,16 +371,7 @@ public class ServerPackets { ...@@ -373,16 +371,7 @@ public class ServerPackets {
// Iterate over all players and send the packet // Iterate over all players and send the packet
for (ServerPlayerEntity player : serverInstance.getPlayerManager().getPlayerList()) { for (ServerPlayerEntity player : serverInstance.getPlayerManager().getPlayerList()) {
PacketByteBuf buffer = new PacketByteBuf(Unpooled.buffer()); PacketByteBuf buffer = new PacketByteBuf(Unpooled.buffer());
// Write the entity's chat updated data
buffer.writeString(chatData.entityId); buffer.writeString(chatData.entityId);
if (sender != null && chatData.auto_generated == 0) {
buffer.writeString(sender.getUuidAsString());
buffer.writeString(sender.getDisplayName().getString());
} else {
buffer.writeString("");
buffer.writeString("Unknown");
}
buffer.writeString(chatData.currentMessage); buffer.writeString(chatData.currentMessage);
buffer.writeInt(chatData.currentLineNumber); buffer.writeInt(chatData.currentLineNumber);
buffer.writeString(chatData.status.toString()); buffer.writeString(chatData.status.toString());
...@@ -390,7 +379,7 @@ public class ServerPackets { ...@@ -390,7 +379,7 @@ public class ServerPackets {
writePlayerDataMap(buffer, chatData.players); writePlayerDataMap(buffer, chatData.players);
// Send message to player // Send message to player
ServerPlayNetworking.send(player, PACKET_S2C_MESSAGE, buffer); ServerPlayNetworking.send(player, PACKET_S2C_ENTITY_MESSAGE, buffer);
} }
break; break;
} }
...@@ -398,6 +387,26 @@ public class ServerPackets { ...@@ -398,6 +387,26 @@ public class ServerPackets {
} }
// Send new message to all connected players // Send new message to all connected players
public static void BroadcastPlayerMessage(EntityChatData chatData, ServerPlayerEntity sender) {
// Log the specific data being sent
LOGGER.info("Broadcasting player message: senderUUID={}, message={}", sender.getUuidAsString(),
chatData.currentMessage);
// Create the buffer for the packet
PacketByteBuf buffer = new PacketByteBuf(Unpooled.buffer());
// Write the sender's UUID and the chat message to the buffer
buffer.writeString(sender.getUuidAsString());
buffer.writeString(sender.getDisplayName().getString());
buffer.writeString(chatData.currentMessage);
// Iterate over all connected players and send the packet
for (ServerPlayerEntity serverPlayer : serverInstance.getPlayerManager().getPlayerList()) {
ServerPlayNetworking.send(serverPlayer, PACKET_S2C_PLAYER_MESSAGE, buffer);
}
}
// Send new message to all connected players
public static void BroadcastPlayerStatus(PlayerEntity player, boolean isChatOpen) { public static void BroadcastPlayerStatus(PlayerEntity player, boolean isChatOpen) {
PacketByteBuf buffer = new PacketByteBuf(Unpooled.buffer()); PacketByteBuf buffer = new PacketByteBuf(Unpooled.buffer());
......
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