From 2321135a1e08d4ab76f3c2ce15223b1a156917cd Mon Sep 17 00:00:00 2001 From: DrPerkyLegit Date: Fri, 10 Apr 2026 00:56:39 -0400 Subject: [PATCH] change for chat input handling on color has a bug where the text after the cursor gets stripped of its color, need to make a function to backstep on a string and find the last used color codes, or get all color codes used before the string is split, and apply them to the start of the next string --- Minecraft.Client/ChatScreen.cpp | 33 ++++++++++++++++++++++++++----- Minecraft.World/SharedConstants.h | 3 ++- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Minecraft.Client/ChatScreen.cpp b/Minecraft.Client/ChatScreen.cpp index 53c907224..106095b36 100644 --- a/Minecraft.Client/ChatScreen.cpp +++ b/Minecraft.Client/ChatScreen.cpp @@ -17,6 +17,29 @@ bool ChatScreen::isAllowedChatChar(wchar_t c) return c >= 0x20 && (c == L'\u00A7' || allowedChars.empty() || allowedChars.find(c) != wstring::npos); } +int VisibleCharLength(wstring message) { + static std::unordered_set validStyleCharacters = { + L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7', L'8', L'9', + L'a', L'b', L'c', L'd', L'e', L'f', + }; + int results = 0; + + + for (int i = 0; i < message.size(); i++) { + wchar_t _char = message[i]; + if (_char == L'§' && (i + 1) < message.size()) { + wchar_t colorChar = message[i + 1]; + if (validStyleCharacters.find(colorChar) != validStyleCharacters.end()) { + i++; //do this so it increments 2 instead of 1 + continue; + } + } + results++; + } + + return results; +} + ChatScreen::ChatScreen() { frame = 0; @@ -131,7 +154,7 @@ void ChatScreen::keyPressed(wchar_t ch, int eventKey) cursorIndex--; return; } - if (isAllowedChatChar(ch) && static_cast(message.length()) < SharedConstants::maxChatLength) + if (isAllowedChatChar(ch) && VisibleCharLength(message) < SharedConstants::maxVisibleLength) { message.insert(cursorIndex, 1, ch); cursorIndex++; @@ -147,12 +170,12 @@ void ChatScreen::render(int xm, int ym, float a) x += font->width(prefix); wstring beforeCursor = message.substr(0, cursorIndex); wstring afterCursor = message.substr(cursorIndex); - drawStringLiteral(font, beforeCursor, x, height - 12, 0xe0e0e0); - x += font->widthLiteral(beforeCursor); + drawString(font, beforeCursor, x, height - 12, 0xe0e0e0); + x += font->width(beforeCursor); if (frame / 6 % 2 == 0) - drawString(font, L"_", x, height - 12, 0xe0e0e0); + drawString(font, L"_", x, height - 12, 0xe0e0e0); //todo: fix this breaking the chat color in afterString x += font->width(L"_"); - drawStringLiteral(font, afterCursor, x, height - 12, 0xe0e0e0); + drawString(font, afterCursor, x, height - 12, 0xe0e0e0); Screen::render(xm, ym, a); } diff --git a/Minecraft.World/SharedConstants.h b/Minecraft.World/SharedConstants.h index 0c91379cf..c4db88ae8 100644 --- a/Minecraft.World/SharedConstants.h +++ b/Minecraft.World/SharedConstants.h @@ -20,7 +20,8 @@ class SharedConstants static wstring readAcceptableChars(); public: - static const int maxChatLength = 1024; + static const int maxChatLength = 255; + static const int maxVisibleLength = 100; //to be changed static wstring acceptableLetters; static const int ILLEGAL_FILE_CHARACTERS_LENGTH = 15;