add chat support for html formatting

This commit is contained in:
DrPerkyLegit 2026-04-06 22:21:44 -04:00
parent b928351e0b
commit 973dcf6e3a
4 changed files with 28 additions and 7 deletions

View file

@ -65,6 +65,7 @@
#include "..\Minecraft.World\DurangoStats.h" #include "..\Minecraft.World\DurangoStats.h"
#include "..\Minecraft.World\GenericStats.h" #include "..\Minecraft.World\GenericStats.h"
#endif #endif
#include <regex>
ClientConnection::ClientConnection(Minecraft *minecraft, const wstring& ip, int port) ClientConnection::ClientConnection(Minecraft *minecraft, const wstring& ip, int port)
{ {
@ -1546,17 +1547,36 @@ void ClientConnection::handleChat(shared_ptr<ChatPacket> packet)
bool replaceEntitySource = false; bool replaceEntitySource = false;
bool replaceItem = false; bool replaceItem = false;
static std::wregex IDS_Pattern(LR"(\{\*IDS_(\d+)\*\})"); //maybe theres a better way to do translateable IDS
int stringArgsSize = packet->m_stringArgs.size();
wstring playerDisplayName = L""; wstring playerDisplayName = L"";
wstring sourceDisplayName = L""; wstring sourceDisplayName = L"";
// On platforms other than Xbox One this just sets display name to gamertag // On platforms other than Xbox One this just sets display name to gamertag
if (packet->m_stringArgs.size() >= 1) playerDisplayName = GetDisplayNameByGamertag(packet->m_stringArgs[0]); if (stringArgsSize >= 1) playerDisplayName = GetDisplayNameByGamertag(packet->m_stringArgs[0]);
if (packet->m_stringArgs.size() >= 2) sourceDisplayName = GetDisplayNameByGamertag(packet->m_stringArgs[1]); if (stringArgsSize >= 2) sourceDisplayName = GetDisplayNameByGamertag(packet->m_stringArgs[1]);
switch(packet->m_messageType) switch(packet->m_messageType)
{ {
case ChatPacket::e_ChatCustom: case ChatPacket::e_ChatCustom:
message = (packet->m_stringArgs.size() >= 1) ? packet->m_stringArgs[0] : L""; if (stringArgsSize > 1) {
message = packet->m_stringArgs[0];
for (int i = 1; i < stringArgsSize; i++) { //skip the first string index, thats main message
message = replaceAll(message, L"{*ARGS_" + std::to_wstring(i) + L"*}", packet->m_stringArgs[i]);
}
std::wsmatch match;
while (std::regex_search(message, match, IDS_Pattern)) {
message = replaceAll(message, match[0], app.GetString(std::stoi(match[1].str())));
}
message = app.FormatHTMLString(m_userIndex, message);
} else if (packet->m_stringArgs.size() == 1) {
message = (packet->m_stringArgs.size() >= 1) ? packet->m_stringArgs[0] : L"";
}
break; break;
case ChatPacket::e_ChatBedOccupied: case ChatPacket::e_ChatBedOccupied:
message = app.GetString(IDS_TILE_BED_OCCUPIED); message = app.GetString(IDS_TILE_BED_OCCUPIED);
@ -1906,7 +1926,7 @@ void ClientConnection::handleChat(shared_ptr<ChatPacket> packet)
if(replacePlayer) if(replacePlayer)
{ {
message = replaceAll(message,L"{*PLAYER*}",playerDisplayName); message = replaceAll(message,L"{*PLAYER*}", playerDisplayName);
} }
if(replaceEntitySource) if(replaceEntitySource)
@ -1941,7 +1961,7 @@ void ClientConnection::handleChat(shared_ptr<ChatPacket> packet)
// flag that a message is a death message // flag that a message is a death message
bool bIsDeathMessage = (packet->m_messageType>=ChatPacket::e_ChatDeathInFire) && (packet->m_messageType<=ChatPacket::e_ChatDeathIndirectMagicItem); bool bIsDeathMessage = (packet->m_messageType>=ChatPacket::e_ChatDeathInFire) && (packet->m_messageType<=ChatPacket::e_ChatDeathIndirectMagicItem);
if( displayOnGui ) minecraft->gui->addMessage(message,m_userIndex, bIsDeathMessage); if( displayOnGui ) minecraft->gui->addMessage(message, m_userIndex, bIsDeathMessage);
} }
void ClientConnection::handleAnimate(shared_ptr<AnimatePacket> packet) void ClientConnection::handleAnimate(shared_ptr<AnimatePacket> packet)

View file

@ -23,6 +23,7 @@ UIScene_HUD::UIScene_HUD(int iPad, void *initData, UILayer *parentLayer) : UISce
for(unsigned int i = 0; i < CHAT_LINES_COUNT; ++i) for(unsigned int i = 0; i < CHAT_LINES_COUNT; ++i)
{ {
m_labelChatText[i].init(L""); m_labelChatText[i].init(L"");
IggyValueSetBooleanRS(m_labelChatText[i].getIggyValuePath(), 0, "m_bUseHtmlText", true);
} }
m_labelJukebox.init(L""); m_labelJukebox.init(L"");

View file

@ -11,7 +11,7 @@ private:
bool m_bSplitscreen; bool m_bSplitscreen;
protected: protected:
UIControl_Label m_labelChatText[CHAT_LINES_COUNT]; UIControl_HTMLLabel m_labelChatText[CHAT_LINES_COUNT];
UIControl_Label m_labelJukebox; UIControl_Label m_labelJukebox;
UIControl m_controlLabelBackground[CHAT_LINES_COUNT]; UIControl m_controlLabelBackground[CHAT_LINES_COUNT];
UIControl_Label m_labelDisplayName; UIControl_Label m_labelDisplayName;

View file

@ -678,7 +678,7 @@ void PlayerConnection::handleChat(shared_ptr<ChatPacket> packet)
handleCommand(message); handleCommand(message);
return; return;
} }
wstring formatted = L"<" + player->name + L"> " + message; wstring formatted = L"&lt;" + player->name + L"&gt; " + message;
server->getPlayers()->broadcastAll(shared_ptr<ChatPacket>(new ChatPacket(formatted))); server->getPlayers()->broadcastAll(shared_ptr<ChatPacket>(new ChatPacket(formatted)));
chatSpamTickCount += SharedConstants::TICKS_PER_SECOND; chatSpamTickCount += SharedConstants::TICKS_PER_SECOND;
if (chatSpamTickCount > SharedConstants::TICKS_PER_SECOND * 10) if (chatSpamTickCount > SharedConstants::TICKS_PER_SECOND * 10)