From 2fbf5f4250b161418341e0f3c9cd7a1fb0cdd5f4 Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Sun, 22 Oct 2023 15:31:29 +0200 Subject: [PATCH] CSM: Fix duplicate player names (#13910) --- src/chat.cpp | 2 +- src/chat.h | 2 +- src/client/client.h | 2 +- src/client/clientenvironment.h | 15 +++++++-------- src/gui/guiChatConsole.cpp | 2 +- src/script/lua_api/l_client.cpp | 7 +++---- src/terminal_chat_console.cpp | 4 ++-- src/terminal_chat_console.h | 3 ++- 8 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/chat.cpp b/src/chat.cpp index 7bbaa8016..a704996fc 100644 --- a/src/chat.cpp +++ b/src/chat.cpp @@ -575,7 +575,7 @@ void ChatPrompt::historyNext() } } -void ChatPrompt::nickCompletion(const std::list& names, bool backwards) +void ChatPrompt::nickCompletion(const std::set &names, bool backwards) { // Two cases: // (a) m_nick_completion_start == m_nick_completion_end == 0 diff --git a/src/chat.h b/src/chat.h index 049cabcd1..444d3bede 100644 --- a/src/chat.h +++ b/src/chat.h @@ -190,7 +190,7 @@ public: void historyNext(); // Nick completion - void nickCompletion(const std::list& names, bool backwards); + void nickCompletion(const std::set &names, bool backwards); // Update console size and reformat the visible portion of the prompt void reformat(u32 cols); diff --git a/src/client/client.h b/src/client/client.h index 1bec65279..4c49301ce 100644 --- a/src/client/client.h +++ b/src/client/client.h @@ -288,7 +288,7 @@ public: // Send the item number 'item' as player item to the server void setPlayerItem(u16 item); - const std::list &getConnectedPlayerNames() + const std::set &getConnectedPlayerNames() { return m_env.getPlayerNames(); } diff --git a/src/client/clientenvironment.h b/src/client/clientenvironment.h index fbf08aecd..ba0fdb50a 100644 --- a/src/client/clientenvironment.h +++ b/src/client/clientenvironment.h @@ -20,10 +20,9 @@ with this program; if not, write to the Free Software Foundation, Inc., #pragma once #include "environment.h" -#include -#include "clientobject.h" -#include "util/numeric.h" -#include "activeobjectmgr.h" +#include "util/numeric.h" // IntervalLimiter +#include "activeobjectmgr.h" // client::ActiveObjectMgr +#include class ClientSimpleObject; class ClientMap; @@ -135,9 +134,9 @@ public: std::vector &objects ); - const std::list &getPlayerNames() { return m_player_names; } - void addPlayerName(const std::string &name) { m_player_names.push_back(name); } - void removePlayerName(const std::string &name) { m_player_names.remove(name); } + const std::set &getPlayerNames() { return m_player_names; } + void addPlayerName(const std::string &name) { m_player_names.insert(name); } + void removePlayerName(const std::string &name) { m_player_names.erase(name); } void updateCameraOffset(const v3s16 &camera_offset) { m_camera_offset = camera_offset; } v3s16 getCameraOffset() const { return m_camera_offset; } @@ -156,7 +155,7 @@ private: std::vector m_simple_objects; std::queue m_client_event_queue; IntervalLimiter m_active_object_light_update_interval; - std::list m_player_names; + std::set m_player_names; v3s16 m_camera_offset; u64 m_frame_time = 0; u64 m_frame_dtime = 0; diff --git a/src/gui/guiChatConsole.cpp b/src/gui/guiChatConsole.cpp index 986a9a530..219a4b4f9 100644 --- a/src/gui/guiChatConsole.cpp +++ b/src/gui/guiChatConsole.cpp @@ -645,7 +645,7 @@ bool GUIChatConsole::OnEvent(const SEvent& event) { // Tab or Shift-Tab pressed // Nick completion - std::list names = m_client->getConnectedPlayerNames(); + auto names = m_client->getConnectedPlayerNames(); bool backwards = event.KeyInput.Shift; prompt.nickCompletion(names, backwards); return true; diff --git a/src/script/lua_api/l_client.cpp b/src/script/lua_api/l_client.cpp index 0f148070b..8d7eb0984 100644 --- a/src/script/lua_api/l_client.cpp +++ b/src/script/lua_api/l_client.cpp @@ -150,13 +150,12 @@ int ModApiClient::l_get_player_names(lua_State *L) if (checkCSMRestrictionFlag(CSM_RF_READ_PLAYERINFO)) return 0; - const std::list &plist = getClient(L)->getConnectedPlayerNames(); + auto plist = getClient(L)->getConnectedPlayerNames(); lua_createtable(L, plist.size(), 0); int newTable = lua_gettop(L); int index = 1; - std::list::const_iterator iter; - for (iter = plist.begin(); iter != plist.end(); ++iter) { - lua_pushstring(L, (*iter).c_str()); + for (const std::string &name : plist) { + lua_pushstring(L, name.c_str()); lua_rawseti(L, newTable, index); index++; } diff --git a/src/terminal_chat_console.cpp b/src/terminal_chat_console.cpp index b12261c3b..47c9c1d03 100644 --- a/src/terminal_chat_console.cpp +++ b/src/terminal_chat_console.cpp @@ -323,10 +323,10 @@ void TerminalChatConsole::step(int ch) ChatEvent *evt = m_chat_interface->outgoing_queue.pop_frontNoEx(); switch (evt->type) { case CET_NICK_REMOVE: - m_nicks.remove(((ChatEventNick *)evt)->nick); + m_nicks.erase(((ChatEventNick *)evt)->nick); break; case CET_NICK_ADD: - m_nicks.push_back(((ChatEventNick *)evt)->nick); + m_nicks.insert(((ChatEventNick *)evt)->nick); break; case CET_CHAT: complete_redraw_needed = true; diff --git a/src/terminal_chat_console.h b/src/terminal_chat_console.h index eae7c6b22..825c76ef4 100644 --- a/src/terminal_chat_console.h +++ b/src/terminal_chat_console.h @@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "threading/thread.h" #include "util/container.h" #include "log.h" +#include #include @@ -103,7 +104,7 @@ private: u8 m_utf8_bytes_to_wait = 0; std::string m_pending_utf8_bytes; - std::list m_nicks; + std::set m_nicks; int m_cols; int m_rows;