summaryrefslogtreecommitdiffstats
path: root/src/ClientHandle.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/ClientHandle.cpp')
-rw-r--r--src/ClientHandle.cpp177
1 files changed, 150 insertions, 27 deletions
diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp
index c8ccc1cf5..6ca43da6f 100644
--- a/src/ClientHandle.cpp
+++ b/src/ClientHandle.cpp
@@ -381,8 +381,8 @@ void cClientHandle::Authenticate(const AString & a_Name, const AString & a_UUID,
// Send player list items
SendPlayerListAddPlayer(*m_Player);
- World->BroadcastPlayerListAddPlayer(*m_Player);
- World->SendPlayerList(m_Player);
+ cRoot::Get()->BroadcastPlayerListsAddPlayer(*m_Player);
+ cRoot::Get()->SendPlayerLists(m_Player);
m_Player->Initialize(*World);
m_State = csAuthenticated;
@@ -440,9 +440,9 @@ bool cClientHandle::StreamNextChunk(void)
int RangeX, RangeZ = 0;
cChunkDef::BlockToChunk(FloorC(Vector.x), FloorC(Vector.z), RangeX, RangeZ);
- for (size_t X = 0; X < 7; X++)
+ for (int X = 0; X < 7; X++)
{
- for (size_t Z = 0; Z < 7; Z++)
+ for (int Z = 0; Z < 7; Z++)
{
int ChunkX = RangeX + ((X >= 4) ? (3 - X) : X);
int ChunkZ = RangeZ + ((Z >= 4) ? (3 - Z) : Z);
@@ -456,7 +456,7 @@ bool cClientHandle::StreamNextChunk(void)
// If the chunk already loading / loaded -> skip
if (
- (std::find(m_ChunksToSend.begin(), m_ChunksToSend.end(), Coords) != m_ChunksToSend.end()) ||
+ (m_ChunksToSend.find(Coords) != m_ChunksToSend.end()) ||
(std::find(m_LoadedChunks.begin(), m_LoadedChunks.end(), Coords) != m_LoadedChunks.end())
)
{
@@ -494,7 +494,7 @@ bool cClientHandle::StreamNextChunk(void)
// If the chunk already loading / loaded -> skip
if (
- (std::find(m_ChunksToSend.begin(), m_ChunksToSend.end(), Coords) != m_ChunksToSend.end()) ||
+ (m_ChunksToSend.find(Coords) != m_ChunksToSend.end()) ||
(std::find(m_LoadedChunks.begin(), m_LoadedChunks.end(), Coords) != m_LoadedChunks.end())
)
{
@@ -541,7 +541,7 @@ void cClientHandle::UnloadOutOfRangeChunks(void)
}
}
- for (cChunkCoordsList::iterator itr = m_ChunksToSend.begin(); itr != m_ChunksToSend.end();)
+ for (auto itr = m_ChunksToSend.begin(); itr != m_ChunksToSend.end();)
{
int DiffX = Diff((*itr).m_ChunkX, ChunkPosX);
int DiffZ = Diff((*itr).m_ChunkZ, ChunkPosZ);
@@ -583,7 +583,7 @@ void cClientHandle::StreamChunk(int a_ChunkX, int a_ChunkZ, cChunkSender::eChunk
{
cCSLock Lock(m_CSChunkLists);
m_LoadedChunks.push_back(cChunkCoords(a_ChunkX, a_ChunkZ));
- m_ChunksToSend.push_back(cChunkCoords(a_ChunkX, a_ChunkZ));
+ m_ChunksToSend.emplace(a_ChunkX, a_ChunkZ);
}
World->SendChunkTo(a_ChunkX, a_ChunkZ, a_Priority, this);
}
@@ -1475,7 +1475,7 @@ void cClientHandle::HandleChat(const AString & a_Message)
Msg.AddTextPart(AString("<") + m_Player->GetName() + "> ", Color);
Msg.ParseText(Message);
Msg.UnderlineUrls();
- m_Player->GetWorld()->BroadcastChat(Msg);
+ cRoot::Get()->BroadcastChat(Msg);
}
@@ -1902,7 +1902,7 @@ void cClientHandle::Tick(float a_Dt)
if (m_TicksSinceLastPacket > 600) // 30 seconds time-out
{
SendDisconnect("Nooooo!! You timed out! D: Come back!");
- Destroy();
+ return;
}
if (m_Player == nullptr)
@@ -2013,7 +2013,6 @@ void cClientHandle::ServerTick(float a_Dt)
if (m_TicksSinceLastPacket > 600) // 30 seconds
{
SendDisconnect("Nooooo!! You timed out! D: Come back!");
- Destroy();
}
}
@@ -2114,6 +2113,64 @@ void cClientHandle::SendChat(const cCompositeChat & a_Message)
+void cClientHandle::SendChatAboveActionBar(const AString & a_Message, eMessageType a_ChatPrefix, const AString & a_AdditionalData)
+{
+ cWorld * World = GetPlayer()->GetWorld();
+ if (World == nullptr)
+ {
+ World = cRoot::Get()->GetWorld(GetPlayer()->GetLoadedWorldName());
+ if (World == nullptr)
+ {
+ World = cRoot::Get()->GetDefaultWorld();
+ }
+ }
+
+ AString Message = FormatMessageType(World->ShouldUseChatPrefixes(), a_ChatPrefix, a_AdditionalData);
+ m_Protocol->SendChatAboveActionBar(Message.append(a_Message));
+}
+
+
+
+
+
+void cClientHandle::SendChatAboveActionBar(const cCompositeChat & a_Message)
+{
+ m_Protocol->SendChatAboveActionBar(a_Message);
+}
+
+
+
+
+
+void cClientHandle::SendChatSystem(const AString & a_Message, eMessageType a_ChatPrefix, const AString & a_AdditionalData)
+{
+ cWorld * World = GetPlayer()->GetWorld();
+ if (World == nullptr)
+ {
+ World = cRoot::Get()->GetWorld(GetPlayer()->GetLoadedWorldName());
+ if (World == nullptr)
+ {
+ World = cRoot::Get()->GetDefaultWorld();
+ }
+ }
+
+ AString Message = FormatMessageType(World->ShouldUseChatPrefixes(), a_ChatPrefix, a_AdditionalData);
+ m_Protocol->SendChatSystem(Message.append(a_Message));
+}
+
+
+
+
+
+void cClientHandle::SendChatSystem(const cCompositeChat & a_Message)
+{
+ m_Protocol->SendChatSystem(a_Message);
+}
+
+
+
+
+
void cClientHandle::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer)
{
ASSERT(m_Player != nullptr);
@@ -2122,15 +2179,12 @@ void cClientHandle::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerializ
bool Found = false;
{
cCSLock Lock(m_CSChunkLists);
- for (cChunkCoordsList::iterator itr = m_ChunksToSend.begin(); itr != m_ChunksToSend.end(); ++itr)
+ auto itr = m_ChunksToSend.find(cChunkCoords{a_ChunkX, a_ChunkZ});
+ if (itr != m_ChunksToSend.end())
{
- if ((itr->m_ChunkX == a_ChunkX) && (itr->m_ChunkZ == a_ChunkZ))
- {
- m_ChunksToSend.erase(itr);
- Found = true;
- break;
- }
- } // for itr - m_ChunksToSend[]
+ m_ChunksToSend.erase(itr);
+ Found = true;
+ }
}
if (!Found)
{
@@ -2182,6 +2236,8 @@ void cClientHandle::SendDestroyEntity(const cEntity & a_Entity)
void cClientHandle::SendDisconnect(const AString & a_Reason)
{
+ // Destruction (Destroy()) is called when the client disconnects, not when a disconnect packet (or anything else) is sent
+ // Otherwise, the cClientHandle instance is can be unexpectedly removed from the associated player - Core/#142
if (!m_HasSentDC)
{
LOGD("Sending a DC: \"%s\"", StripColorCodes(a_Reason).c_str());
@@ -2329,6 +2385,15 @@ void cClientHandle::SendHealth(void)
+void cClientHandle::SendHideTitle(void)
+{
+ m_Protocol->SendHideTitle();
+}
+
+
+
+
+
void cClientHandle::SendInventorySlot(char a_WindowID, short a_SlotNum, const cItem & a_Item)
{
m_Protocol->SendInventorySlot(a_WindowID, a_SlotNum, a_Item);
@@ -2532,6 +2597,15 @@ void cClientHandle::SendRemoveEntityEffect(const cEntity & a_Entity, int a_Effec
+void cClientHandle::SendResetTitle()
+{
+ m_Protocol->SendResetTitle();
+}
+
+
+
+
+
void cClientHandle::SendRespawn(eDimension a_Dimension, bool a_ShouldIgnoreDimensionChecks)
{
m_Protocol->SendRespawn(a_Dimension, a_ShouldIgnoreDimensionChecks);
@@ -2586,6 +2660,42 @@ void cClientHandle::SendDisplayObjective(const AString & a_Objective, cScoreboar
+void cClientHandle::SendSetSubTitle(const cCompositeChat & a_SubTitle)
+{
+ m_Protocol->SendSetSubTitle(a_SubTitle);
+}
+
+
+
+
+
+void cClientHandle::SendSetRawSubTitle(const AString & a_SubTitle)
+{
+ m_Protocol->SendSetRawSubTitle(a_SubTitle);
+}
+
+
+
+
+
+void cClientHandle::SendSetTitle(const cCompositeChat & a_Title)
+{
+ m_Protocol->SendSetTitle(a_Title);
+}
+
+
+
+
+
+void cClientHandle::SendSetRawTitle(const AString & a_Title)
+{
+ m_Protocol->SendSetRawTitle(a_Title);
+}
+
+
+
+
+
void cClientHandle::SendSoundEffect(const AString & a_SoundName, double a_X, double a_Y, double a_Z, float a_Volume, float a_Pitch)
{
m_Protocol->SendSoundEffect(a_SoundName, a_X, a_Y, a_Z, a_Volume, a_Pitch);
@@ -2676,6 +2786,15 @@ void cClientHandle::SendThunderbolt(int a_BlockX, int a_BlockY, int a_BlockZ)
+void cClientHandle::SendTitleTimes(int a_FadeInTicks, int a_DisplayTicks, int a_FadeOutTicks)
+{
+ m_Protocol->SendTitleTimes(a_FadeInTicks, a_DisplayTicks, a_FadeOutTicks);
+}
+
+
+
+
+
void cClientHandle::SendTimeUpdate(Int64 a_WorldAge, Int64 a_TimeOfDay, bool a_DoDaylightCycle)
{
m_Protocol->SendTimeUpdate(a_WorldAge, a_TimeOfDay, a_DoDaylightCycle);
@@ -2828,7 +2947,7 @@ bool cClientHandle::WantsSendChunk(int a_ChunkX, int a_ChunkZ)
}
cCSLock Lock(m_CSChunkLists);
- return (std::find(m_ChunksToSend.begin(), m_ChunksToSend.end(), cChunkCoords(a_ChunkX, a_ChunkZ)) != m_ChunksToSend.end());
+ return m_ChunksToSend.find(cChunkCoords(a_ChunkX, a_ChunkZ)) != m_ChunksToSend.end();
}
@@ -2844,9 +2963,9 @@ void cClientHandle::AddWantedChunk(int a_ChunkX, int a_ChunkZ)
LOGD("Adding chunk [%d, %d] to wanted chunks for client %p", a_ChunkX, a_ChunkZ, this);
cCSLock Lock(m_CSChunkLists);
- if (std::find(m_ChunksToSend.begin(), m_ChunksToSend.end(), cChunkCoords(a_ChunkX, a_ChunkZ)) == m_ChunksToSend.end())
+ if (m_ChunksToSend.find(cChunkCoords(a_ChunkX, a_ChunkZ)) == m_ChunksToSend.end())
{
- m_ChunksToSend.push_back(cChunkCoords(a_ChunkX, a_ChunkZ));
+ m_ChunksToSend.emplace(a_ChunkX, a_ChunkZ);
}
}
@@ -2859,7 +2978,6 @@ void cClientHandle::PacketBufferFull(void)
// Too much data in the incoming queue, the server is probably too busy, kick the client:
LOGERROR("Too much data in queue for client \"%s\" @ %s, kicking them.", m_Username.c_str(), m_IPString.c_str());
SendDisconnect("Server busy");
- Destroy();
}
@@ -2873,7 +2991,6 @@ void cClientHandle::PacketUnknown(UInt32 a_PacketType)
AString Reason;
Printf(Reason, "Unknown [C->S] PacketType: 0x%x", a_PacketType);
SendDisconnect(Reason);
- Destroy();
}
@@ -2884,7 +3001,6 @@ void cClientHandle::PacketError(UInt32 a_PacketType)
{
LOGERROR("Protocol error while parsing packet type 0x%02x; disconnecting client \"%s\"", a_PacketType, m_Username.c_str());
SendDisconnect("Protocol error");
- Destroy();
}
@@ -2894,13 +3010,20 @@ void cClientHandle::PacketError(UInt32 a_PacketType)
void cClientHandle::SocketClosed(void)
{
// The socket has been closed for any reason
-
+
if (!m_Username.empty()) // Ignore client pings
{
LOGD("Client %s @ %s disconnected", m_Username.c_str(), m_IPString.c_str());
cRoot::Get()->GetPluginManager()->CallHookDisconnect(*this, "Player disconnected");
}
-
+ if ((m_State < csDestroying) && (m_Player != nullptr))
+ {
+ cWorld * World = m_Player->GetWorld();
+ if (World != nullptr)
+ {
+ World->RemovePlayer(m_Player, true); // Must be called before cPlayer::Destroy() as otherwise cChunk tries to delete the player, and then we do it again
+ }
+ }
Destroy();
}