-
Notifications
You must be signed in to change notification settings - Fork 41
Fix text chat functionality #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1598,6 +1598,12 @@ void ClientConnection::handleChat(shared_ptr<ChatPacket> packet) | |||||||||||||||||||||||
| message = replaceAll(message,L"{*PLAYER*}",playerDisplayName); | ||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| case ChatPacket::e_ChatCustom: | ||||||||||||||||||||||||
| // Raw text message (e.g. "<Player> message") — use first string arg directly | ||||||||||||||||||||||||
| if (!packet->m_stringArgs.empty()) | ||||||||||||||||||||||||
| message = packet->m_stringArgs[0]; | ||||||||||||||||||||||||
|
Comment on lines
+1603
to
+1604
|
||||||||||||||||||||||||
| if (!packet->m_stringArgs.empty()) | |
| message = packet->m_stringArgs[0]; | |
| if (!packet->m_stringArgs.empty()) | |
| { | |
| message = packet->m_stringArgs[0]; | |
| } | |
| else | |
| { | |
| // No payload text: don't display an empty chat line | |
| displayOnGui = false; | |
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3598,10 +3598,13 @@ void Minecraft::tick(bool bFirst, bool bUpdateTextures) | |||||
| // #endif | ||||||
|
|
||||||
|
|
||||||
| if(Keyboard::isKeyDown(options->keyChat->key)) | ||||||
| static bool s_chatKeyWasDown = false; | ||||||
| bool chatKeyDown = Keyboard::isKeyDown(options->keyChat->key) || g_KBMInput.IsKeyDown('T'); | ||||||
|
||||||
| bool chatKeyDown = Keyboard::isKeyDown(options->keyChat->key) || g_KBMInput.IsKeyDown('T'); | |
| bool chatKeyDown = Keyboard::isKeyDown(options->keyChat->key); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -126,7 +126,7 @@ void PlayerConnection::disconnect(DisconnectPacket::eDisconnectReason reason) | |||||||||||||||||||||||||||||||||||||
| send( shared_ptr<DisconnectPacket>( new DisconnectPacket(reason) )); | ||||||||||||||||||||||||||||||||||||||
| connection->sendAndQuit(); | ||||||||||||||||||||||||||||||||||||||
| // 4J-PB - removed, since it needs to be localised in the language the client is in | ||||||||||||||||||||||||||||||||||||||
| //server->players->broadcastAll( shared_ptr<ChatPacket>( new ChatPacket(L"�e" + player->name + L" left the game.") ) ); | ||||||||||||||||||||||||||||||||||||||
| //server->players->broadcastAll( shared_ptr<ChatPacket>( new ChatPacket(L"�e" + player->name + L" left the game.") ) ); | ||||||||||||||||||||||||||||||||||||||
| if(getWasKicked()) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| server->getPlayers()->broadcastAll( shared_ptr<ChatPacket>( new ChatPacket(player->name, ChatPacket::e_ChatPlayerKickedFromGame) ) ); | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -569,7 +569,7 @@ void PlayerConnection::onDisconnect(DisconnectPacket::eDisconnectReason reason, | |||||||||||||||||||||||||||||||||||||
| if( done ) return; | ||||||||||||||||||||||||||||||||||||||
| // logger.info(player.name + " lost connection: " + reason); | ||||||||||||||||||||||||||||||||||||||
| // 4J-PB - removed, since it needs to be localised in the language the client is in | ||||||||||||||||||||||||||||||||||||||
| //server->players->broadcastAll( shared_ptr<ChatPacket>( new ChatPacket(L"�e" + player->name + L" left the game.") ) ); | ||||||||||||||||||||||||||||||||||||||
| //server->players->broadcastAll( shared_ptr<ChatPacket>( new ChatPacket(L"�e" + player->name + L" left the game.") ) ); | ||||||||||||||||||||||||||||||||||||||
| if(getWasKicked()) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| server->getPlayers()->broadcastAll( shared_ptr<ChatPacket>( new ChatPacket(player->name, ChatPacket::e_ChatPlayerKickedFromGame) ) ); | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -636,38 +636,24 @@ void PlayerConnection::handleSetCarriedItem(shared_ptr<SetCarriedItemPacket> pac | |||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| void PlayerConnection::handleChat(shared_ptr<ChatPacket> packet) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| // 4J - TODO | ||||||||||||||||||||||||||||||||||||||
| #if 0 | ||||||||||||||||||||||||||||||||||||||
| wstring message = packet->message; | ||||||||||||||||||||||||||||||||||||||
| if (packet->m_stringArgs.empty()) return; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| wstring message = packet->m_stringArgs[0]; | ||||||||||||||||||||||||||||||||||||||
| if (message.length() > SharedConstants::maxChatLength) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| disconnect(L"Chat message too long"); | ||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| message = message.trim(); | ||||||||||||||||||||||||||||||||||||||
| for (int i = 0; i < message.length(); i++) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| if (SharedConstants.acceptableLetters.indexOf(message.charAt(i)) < 0 && (int) message.charAt(i) < 32) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| disconnect(L"Illegal characters in chat"); | ||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if (message.startsWith("/")) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| handleCommand(message); | ||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||
| message = "<" + player.name + "> " + message; | ||||||||||||||||||||||||||||||||||||||
| logger.info(message); | ||||||||||||||||||||||||||||||||||||||
| server.players.broadcastAll(new ChatPacket(message)); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| chatSpamTickCount += SharedConstants::TICKS_PER_SECOND; | ||||||||||||||||||||||||||||||||||||||
| if (chatSpamTickCount > SharedConstants::TICKS_PER_SECOND * 10) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| disconnect("disconnect.spam"); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||
| // Trim leading/trailing spaces | ||||||||||||||||||||||||||||||||||||||
| size_t start = message.find_first_not_of(L' '); | ||||||||||||||||||||||||||||||||||||||
| size_t end = message.find_last_not_of(L' '); | ||||||||||||||||||||||||||||||||||||||
| if (start == wstring::npos) return; | ||||||||||||||||||||||||||||||||||||||
| message = message.substr(start, end - start + 1); | ||||||||||||||||||||||||||||||||||||||
| if (message.empty()) return; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Format as "<PlayerName> message" and broadcast to all clients | ||||||||||||||||||||||||||||||||||||||
| wstring formatted = L"<" + player->name + L"> " + message; | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+654
to
+655
|
||||||||||||||||||||||||||||||||||||||
| // Format as "<PlayerName> message" and broadcast to all clients | |
| wstring formatted = L"<" + player->name + L"> " + message; | |
| // Remove disallowed control characters to prevent chat/log injection | |
| wstring cleanMessage; | |
| cleanMessage.reserve(message.size()); | |
| for (wstring::const_iterator it = message.begin(); it != message.end(); ++it) | |
| { | |
| wchar_t ch = *it; | |
| // Allow printable characters only (exclude control chars < 0x20) | |
| if (ch >= 0x20) | |
| { | |
| cleanMessage.push_back(ch); | |
| } | |
| } | |
| if (cleanMessage.empty()) return; | |
| // Format as "<PlayerName> message" and broadcast to all clients | |
| wstring formatted = L"<" + player->name + L"> " + cleanMessage; |
Copilot
AI
Mar 8, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handleChat() no longer increments chatSpamTickCount, but PlayerConnection::tick() still decrements it every tick. This effectively disables the existing chat spam throttling logic and leaves a counter that never increases. Reintroduce spam tracking (increment per message and disconnect/throttle when over the threshold) or remove the counter/logic if spam protection is intentionally being dropped.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code correctly queries
SharedConstants::acceptableLettersat runtime, butChatScreen::allowedChars(defined at file scope) is still initialized fromSharedConstants::acceptableLettersbeforeSharedConstants::staticCtor()populates it, soallowedCharswill remain an empty copy. Since it’s no longer used here, consider removingallowedChars(or making it a runtime reference/accessor) to avoid the original empty-allowed-chars bug resurfacing later.