image/svg+xml
void cClientHandle::Kick(const AString & a_Reason) { if (m_State >= csAuthenticating) { LOG("Kicking user \"%s\" for \"%s\"", m_Username.c_str(), StripColorCodes(a_Reason).c_str()); } SendDisconnect(a_Reason); } void cClientHandle::Authenticate(void) { if (m_State != csAuthenticating) { return; } ASSERT( m_Player == NULL ); m_Player = new cPlayer(this, GetUsername()); cWorld * World = cRoot::Get()->GetWorld(m_Player->GetLoadedWorldName()); if (World == NULL) { World = cRoot::Get()->GetDefaultWorld(); } if (m_Player->GetGameMode() == eGameMode_NotSet) { m_Player->LoginSetGameMode(World->GetGameMode()); } m_Player->SetIP (m_IPString); cRoot::Get()->GetPluginManager()->CallHookPlayerJoined(*m_Player); m_ConfirmPosition = m_Player->GetPosition(); m_Protocol->SendLogin(*m_Player, *World); if ((World->GetWeather() == 1) || (World->GetWeather() == 2)) { m_Protocol->SendWeather(World->GetWeather()); } m_Protocol->SendTimeUpdate(World->GetWorldAge(), World->GetTimeOfDay()); m_Protocol->SendWholeInventory(*m_Player->GetWindow()); m_Player->SendHealth(); m_Player->SendExperience(); SendGameMode(m_Player->GetGameMode()); m_Player->Initialize(World); m_State = csAuthenticated; m_Player->GetWorld()->BroadcastSpawnEntity(*m_Player, this); cRoot::Get()->GetPluginManager()->CallHookPlayerSpawned(*m_Player); } void cClientHandle::StreamChunks(void) { if ((m_State < csAuthenticated) || (m_State >= csDestroying)) { return; } ASSERT(m_Player != NULL); int ChunkPosX = FAST_FLOOR_DIV((int)m_Player->GetPosX(), cChunkDef::Width); int ChunkPosZ = FAST_FLOOR_DIV((int)m_Player->GetPosZ(), cChunkDef::Width); if ((ChunkPosX == m_LastStreamedChunkX) && (ChunkPosZ == m_LastStreamedChunkZ)) { return; } m_LastStreamedChunkX = ChunkPosX; m_LastStreamedChunkZ = ChunkPosZ; LOGD("Streaming chunks centered on [%d, %d], view distance %d", ChunkPosX, ChunkPosZ, m_ViewDistance); cWorld * World = m_Player->GetWorld(); ASSERT(World != NULL); cChunkCoordsList RemoveChunks; { cCSLock Lock(m_CSChunkLists); for (cChunkCoordsList::iterator itr = m_LoadedChunks.begin(); itr != m_LoadedChunks.end();) { int RelX = (*itr).m_ChunkX - ChunkPosX; int RelZ = (*itr).m_ChunkZ - ChunkPosZ; if ((RelX > m_ViewDistance) || (RelX < -m_ViewDistance) || (RelZ > m_ViewDistance) || (RelZ < -m_ViewDistance)) { RemoveChunks.push_back(*itr); itr = m_LoadedChunks.erase(itr); } else { ++itr; } } for (cChunkCoordsList::iterator itr = m_ChunksToSend.begin(); itr != m_ChunksToSend.end();) { int RelX = (*itr).m_ChunkX - ChunkPosX; int RelZ = (*itr).m_ChunkZ - ChunkPosZ; if ((RelX > m_ViewDistance) || (RelX < -m_ViewDistance) || (RelZ > m_ViewDistance) || (RelZ < -m_ViewDistance)) { itr = m_ChunksToSend.erase(itr); } else { ++itr; } } } for (cChunkCoordsList::iterator itr = RemoveChunks.begin(); itr != RemoveChunks.end(); ++itr) { World->RemoveChunkClient(itr->m_ChunkX, itr->m_ChunkZ, this); m_Protocol->SendUnloadChunk(itr->m_ChunkX, itr->m_ChunkZ); } for (int d = 0; d <= m_ViewDistance; ++d) { for (int i = -d; i <= d; ++i) { StreamChunk(ChunkPosX + d, ChunkPosZ + i); StreamChunk(ChunkPosX - d, ChunkPosZ + i); } for (int i = -d + 1; i < d; ++i) { StreamChunk(ChunkPosX + i, ChunkPosZ + d); StreamChunk(ChunkPosX + i, ChunkPosZ - d); } } for (int d = m_ViewDistance + 1; d <= m_ViewDistance + GENERATEDISTANCE; ++d) { for (int i = -d; i <= d; ++i) { World->TouchChunk(ChunkPosX + d, ZERO_CHUNK_Y, ChunkPosZ + i); World->TouchChunk(ChunkPosX - d, ZERO_CHUNK_Y, ChunkPosZ + i); } for (int i = -d + 1; i < d; ++i) { World->TouchChunk(ChunkPosX + i, ZERO_CHUNK_Y, ChunkPosZ + d); World->TouchChunk(ChunkPosX + i, ZERO_CHUNK_Y, ChunkPosZ - d); } } } void cClientHandle::StreamChunk(int a_ChunkX, int a_ChunkZ) { if (m_State >= csDestroying) { return; } cWorld * World = m_Player->GetWorld(); ASSERT(World != NULL); if (World->AddChunkClient(a_ChunkX, a_ChunkZ, this)) { { cCSLock Lock(m_CSChunkLists); m_LoadedChunks.push_back(cChunkCoords(a_ChunkX, ZERO_CHUNK_Y, a_ChunkZ)); m_ChunksToSend.push_back(cChunkCoords(a_ChunkX, ZERO_CHUNK_Y, a_ChunkZ)); } World->SendChunkTo(a_ChunkX, a_ChunkZ, this); } } void cClientHandle::RemoveFromAllChunks() { cWorld * World = m_Player->GetWorld(); if (World != NULL) { World->RemoveClientFromChunks(this); } { cCSLock Lock(m_CSChunkLists); m_LoadedChunks.clear(); m_ChunksToSend.clear(); m_LastStreamedChunkX = 0x7fffffff; m_LastStreamedChunkZ = 0x7fffffff; } } void cClientHandle::HandlePing(void) { AString Reply; Printf(Reply, "%s%s%i%s%i", cRoot::Get()->GetServer()->GetDescription().c_str(), cChatColor::Delimiter.c_str(), cRoot::Get()->GetServer()->GetNumPlayers(), cChatColor::Delimiter.c_str(), cRoot::Get()->GetServer()->GetMaxPlayers() ); Kick(Reply.c_str()); } bool cClientHandle::HandleLogin(int a_ProtocolVersion, const AString & a_Username) { LOGD("LOGIN %s", a_Username.c_str()); m_Username = a_Username; if (cRoot::Get()->GetPluginManager()->CallHookLogin(this, a_ProtocolVersion, a_Username)) { Destroy(); return false; } m_State = csAuthenticating; cRoot::Get()->GetAuthenticator().Authenticate(GetUniqueID(), GetUsername(), m_Protocol->GetAuthServerID()); return true; } void cClientHandle::HandleCreativeInventory(short a_SlotNum, const cItem & a_HeldItem) { if (!m_Player->IsGameModeCreative()) { LOGWARNING("Got a CreativeInventoryAction packet from user \"%s\" while not in creative mode. Ignoring.", m_Username.c_str()); return; } if (m_Player->GetWindow()->GetWindowType() != cWindow::wtInventory) { LOGWARNING("Got a CreativeInventoryAction packet from user \"%s\" while not in the inventory window. Ignoring.", m_Username.c_str()); return; } m_Player->GetWindow()->Clicked(*m_Player, 0, a_SlotNum, (a_SlotNum >= 0) ? caLeftClick : caLeftClickOutside, a_HeldItem); } void cClientHandle::HandlePlayerPos(double a_PosX, double a_PosY, double a_PosZ, double a_Stance, bool a_IsOnGround) { if ((m_Player == NULL) || (m_State != csPlaying)) { return; } /* if ((a_PosY >= a_Stance) || (a_Stance > a_PosY + 1.65)) { LOGD("Invalid stance"); SendPlayerMoveLook(); return; } */ Vector3d Pos(a_PosX, a_PosY, a_PosZ); if ((m_Player->GetPosition() - Pos).SqrLength() > 100 * 100) { LOGD("Too far away (%0.2f), \"repairing\" the client", (m_Player->GetPosition() - Pos).Length()); SendPlayerMoveLook(); return; } if ((a_PosY > m_Player->GetPosY()) && !a_IsOnGround && m_Player->IsOnGround()) { if (!m_Player->IsSwimming()) { m_Player->AddFoodExhaustion(m_Player->IsSprinting() ? 0.8 : 0.2); } } m_Player->MoveTo(Pos); m_Player->SetStance(a_Stance); m_Player->SetTouchGround(a_IsOnGround); } void cClientHandle::HandleLeftClick(int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status) { LOGD("HandleLeftClick: {%i, %i, %i}; Face: %i; Stat: %i", a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_Status ); cPluginManager * PlgMgr = cRoot::Get()->GetPluginManager(); if (PlgMgr->CallHookPlayerLeftClick(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_Status)) { m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_ I write myown server