#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "Chunk.h" #include "ClientHandle.h" #include "Server.h" #include "World.h" #include "Chunk.h" #include "Entities/Pickup.h" #include "Bindings/PluginManager.h" #include "Entities/Player.h" #include "Inventory.h" #include "EffectID.h" #include "BlockEntities/BeaconEntity.h" #include "BlockEntities/ChestEntity.h" #include "BlockEntities/CommandBlockEntity.h" #include "BlockEntities/SignEntity.h" #include "UI/Window.h" #include "UI/AnvilWindow.h" #include "UI/BeaconWindow.h" #include "UI/EnchantingWindow.h" #include "Item.h" #include "Mobs/Monster.h" #include "ChatColor.h" #include "Items/ItemHandler.h" #include "Blocks/BlockHandler.h" #include "Blocks/BlockBed.h" #include "Blocks/ChunkInterface.h" #include "BlockInServerPluginInterface.h" #include "Root.h" #include "Protocol/Authenticator.h" #include "Protocol/ProtocolRecognizer.h" #include "CompositeChat.h" #include "Items/ItemSword.h" #include "polarssl/md5.h" /** Maximum number of explosions to send this tick, server will start dropping if exceeded */ #define MAX_EXPLOSIONS_PER_TICK 20 /** Maximum number of block change interactions a player can perform per tick - exceeding this causes a kick */ #define MAX_BLOCK_CHANGE_INTERACTIONS 20 /** The interval for sending pings to clients. Vanilla sends one ping every 1 second. */ static const std::chrono::milliseconds PING_TIME_MS = std::chrono::milliseconds(1000); int cClientHandle::s_ClientCount = 0; //////////////////////////////////////////////////////////////////////////////// // cClientHandle: cClientHandle::cClientHandle(const AString & a_IPString, int a_ViewDistance) : m_CurrentViewDistance(a_ViewDistance), m_RequestedViewDistance(a_ViewDistance), m_IPString(a_IPString), m_Protocol(this), m_Player(nullptr), // In order to catch when client leaves before authentication m_TicksSinceLastPacket(0), m_Ping(1000), m_PingID(1), m_BlockDigAnimStage(-1), m_BlockDigAnimSpeed(0), m_BlockDigAnimX(0), m_BlockDigAnimY(256), // Invalid Y, so that the coords don't get picked up m_BlockDigAnimZ(0), m_HasStartedDigging(false), m_LastDigBlockX(0), m_LastDigBlockY(256), // Invalid Y, so that the coords don't get picked up m_LastDigBlockZ(0), m_State(eState::csConnected), m_ShouldRefreshSentChunks(true), m_NumExplosionsThisTick(0), m_NumBlockChangeInteractionsThisTick(0), m_UniqueID(0), m_Locale("en_GB"), m_LastPlacedSign(0, -1, 0), m_ProtocolVersion(0) { s_ClientCount++; // Not protected by CS because clients are always constructed from the same thread m_UniqueID = s_ClientCount; m_PingStartTime = std::chrono::steady_clock::now(); LOGD("New ClientHandle created at %p", static_cast(this)); } cClientHandle::~cClientHandle() { ASSERT(m_State == eState::csDestroyed); // Has Destroy() been called? // Note: don't handle player destruction here because we don't own them, so problems arise during shutdown LOGD("Deletied client \"%s\" at %p", GetUsername().c_str(), static_cast(this)); } void cClientHandle::Destroy(void) { ASSERT(cRoot::Get()->GetServer()->IsInTickThread()); if (!SetState(eState::csDestroying)) { // Already destroyed. return; } { cCSLock Lock(m_CSLink); m_Link.reset(); } if (m_Player != nullptr) { cWorld * World = m_Player->GetWorld(); // Upon Destroy, the world pointer of a valid player SHALL NOT be null. // Guaranteed by Destroy and Authenticate being called in the same thread ASSERT(World != nullptr); // We do not leak a player object as Authenticate checks to see if Destroy was called before creating a player World->QueueTask( [this](cWorld & a_World) { if (m_Player->GetParentChunk() == nullptr) { // Player not yet initialised; calling Destroy is not valid LOGWARN("ParentChunk null"); return; } if (!cRoot::Get()->GetPluginManager()->CallHookPlayerDestroyed(*m_Player)) { cRoot::Get()->BroadcastChatLeave(Printf("%s has left the game", m_Player->GetName().c_str())); LOGINFO("Player %s has left the game", m_Player->GetName().c_str()); } a_World.RemovePlayer(m_Player); a_World.BroadcastPlayerListRemovePlayer(*m_Player); m_Player->Destroy(); } ); } RemoveFromWorld(); LOGD("%s: client %p, \"%s\"", __FUNCTION__, static_cast(this), m_Username.c_str()); SetState(eState::csDestroyed); } bool cClientHandle::SetState(eState a_State) { // Obtain state at a point in time to compare to a_State auto ExpectedState = m_State.load(); while ( // Ensure that an exchange will be performed validly (ExpectedState < a_State) && // Ensure that the current state matches what we have just verified // If so, set the desired state // Else, update ExpectedState to be the current value in preparation for another check and loop !m_State.compare_exchange_weak(ExpectedState, a_State) ) { continue; } return (ExpectedState < a_State); } void cClientHandle::GenerateOfflineUUID(void) { m_UUID = GenerateOfflineUUID(m_Username); } AString cClientHandle::FormatChatPrefix(bool ShouldAppendChatPrefixes, AString a_ChatPrefixS, AString m_Color1, AString m_Color2) { if (ShouldAppendChatPrefixes) { return Printf("%s[%s] %s", m_Color1.c_str(), a_ChatPrefixS.c_str(), m_Color2.c_str()); } else { return Printf("%s", m_Color1.c_str()); } } AString cClientHandle::FormatMessageType(bool ShouldAppendChatPrefixes, eMessageType a_ChatPrefix, const AString &a_AdditionalData) { switch (a_ChatPrefix) { case mtCustom: return ""; case mtFailure: return FormatChatPrefix(ShouldAppendChatPrefixes, "INFO", cChatColor::Rose, cChatColor::White); case mtInformation: return FormatChatPrefix(ShouldAppendChatPrefixes, "INFO", cChatColor::Yellow, cChatColor::White); case mtSuccess: return FormatChatPrefix(ShouldAppendChatPrefixes, "INFO", cChatColor::Green, cChatColor::White); case mtWarning: return FormatChatPrefix(ShouldAppendChatPrefixes, "WARN", cChatColor::Rose, cChatColor::White); case mtFatal: return FormatChatPrefix(ShouldAppendChatPrefixes, "FATAL", cChatColor::Red, cChatColor::White); case mtDeath: return FormatChatPrefix(ShouldAppendChatPrefixes, "DEATH", cChatColor::Gray, cChatColor::White); case mtJoin: return FormatChatPrefix(ShouldAppendChatPrefixes, "JOIN", cChatColor::Yellow, cChatColor::White); case mtLeave: return FormatChatPrefix(ShouldAppendChatPrefixes, "LEAVE", cChatColor::Yellow, cChatColor::White); case mtPrivateMessage: { if (ShouldAppendChatPrefixes) { return Printf("%s[MSG: %s] %s%s", cChatColor::LightBlue, a_AdditionalData.c_str(), cChatColor::White, cChatColor::Italic); } else { return Printf("%s: %s", a_AdditionalData.c_str(), cChatColor::LightBlue); } } } ASSERT(!"Unhandled chat prefix type!"); #ifndef __clang__ return ""; #endif } AString cClientHandle::GenerateOfflineUUID(const AString & a_Username) { // Online UUIDs are always version 4 (random) // We use Version 3 (MD5 hash) UUIDs for the offline UUIDs // This guarantees that they will never collide with an online UUID and can be distinguished. // Proper format for a version 3 UUID is: // xxxxxxxx-xxxx-3xxx-yxxx-xxxxxxxxxxxx where x is any hexadecimal digit and y is one of 8, 9, A, or B // Note that we generate a short UUID (without the dashes) // First make the username lowercase: AString lcUsername = StrToLower(a_Username); // Generate an md5 checksum, and use it as base for the ID: unsigned char MD5[16]; md5(reinterpret_cast(lcUsername.c_str()), lcUsername.length(), MD5); MD5[6] &= 0x0f; // Need to trim to 4 bits only... MD5[8] &= 0x0f; // ... otherwise %01x overflows into two chars return Printf("%02x%02x%02x%02x%02x%02x3%01x%02x8%01x%02x%02x%02x%02x%02x%02x%02x", MD5[0], MD5[1], MD5[2], MD5[3], MD5[4], MD5[5], MD5[6], MD5[7], MD5[8], MD5[9], MD5[10], MD5[11], MD5[12], MD5[13], MD5[14], MD5[15] ); } bool cClientHandle::IsUUIDOnline(const AString & a_UUID) { // Online UUIDs are always version 4 (random) // We use Version 3 (MD5 hash) UUIDs for the offline UUIDs // This guarantees that they will never collide with an online UUID and can be distinguished. // The version-specifying char is at pos #12 of raw UUID, pos #14 in dashed-UUID. switch (a_UUID.size()) { case 32: { // This is the UUID format without dashes, the version char is at pos #12: return (a_UUID[12] == '4'); } case 36: { // This is the UUID format with dashes, the version char is at pos #14: return (a_UUID[14] == '4'); } } return false; } void cClientHandle::Kick(const AString & a_Reason) { if (IsLoggedIn()) // Don't log pings { LOGINFO("Kicking player %s for \"%s\"", m_Username.c_str(), StripColorCodes(a_Reason).c_str()); } SendDisconnect(a_Reason); } void cClientHandle::Authenticate(const AString & a_Name, const AString & a_UUID, const Json::Value & a_Properties) { ASSERT(cRoot::Get()->GetServer()->IsInTickThread()); if (m_State != eState::csAuthenticating) { // TODO: is this necessary? return; } m_Username = a_Name; // Only assign UUID and properties if not already pre-assigned (BungeeCord sends those in the Handshake packet): if (m_UUID.empty()) { m_UUID = a_UUID; } if (m_Properties.empty()) { m_Properties = a_Properties; } // Send login success (if the protocol supports it): m_Protocol.SendLoginSuccess(); // Obtain a strong reference to ourselves auto Client = shared_from_this(); // Spawn player (only serversided, so data is loaded) auto Player = cpp14::make_unique(Client, m_Username); m_Player = Player.get(); cWorld * World = cRoot::Get()->GetWorld(m_Player->GetLoadedWorldName()); if (World == nullptr) { World = cRoot::Get()->GetDefaultWorld(); } if (m_Player->GetGameMode() == eGameMode_NotSet) { m_Player->LoginSetGameMode(World->GetGameMode()); } m_Player->SetIP(m_IPString); // So Destroy always has a valid world for a valid player // Additionally, plugins expect world to be set m_Player->SetWorld(World); cpp14::move_on_copy_wrapper PlayerPtr(std::move(Player)); World->QueueTask( [World, Player = m_Player, PlayerPtr, Client](cWorld & a_World) mutable { // We're in the task to create the player - any other QueueTask will execute in the next tick // So, check to see cClientHandle::Destroy has not been called (State >= IsDestroying where State is monotonic) if (Client->IsDestroying() || Client->IsDestroyed()) { // Destroy called - proceeding now means we may create a player object that's never deleted LOGWARN("Destroyed"); return; } if (!cRoot::Get()->GetPluginManager()->CallHookPlayerJoined(*Player)) { cRoot::Get()->BroadcastChatJoin(Printf("%s has joined the game", Client->GetUsername().c_str())); LOGINFO("Player %s has joined the game", Client->GetUsername().c_str()); } // Return a server login packet Client->GetProtocol().SendLogin(*Player, *World); // Send Weather if raining: if ((World->GetWeather() == 1) || (World->GetWeather() == 2)) { Client->GetProtocol().SendWeather(World->GetWeather()); } // Send time: Client->GetProtocol().SendTimeUpdate(World->GetWorldAge(), World->GetTimeOfDay(), World->IsDaylightCycleEnabled()); // Send contents of the inventory window Client->GetProtocol().SendWholeInventory(*Player->GetWindow()); // Send health Client->GetProtocol().SendHealth(); // Send experience Client->GetProtocol().SendExperience(); // Send player list items cRoot::Get()->BroadcastPlayerListsAddPlayer(*Player); cRoot::Get()->SendPlayerLists(Player); // Note: cEntity::Initialize takes ownership of the player object Player->Initialize(std::move(PlayerPtr.value), *World); World->AddPlayer(Player); Client->SetState(cClientHandle::eState::csAuthenticated); // Query player team Player->UpdateTeam(); // Send scoreboard data World->GetScoreBoard().SendTo(*Client); // Send statistics Client->GetProtocol().SendStatistics(Player->GetStatManager()); cRoot::Get()->GetPluginManager()->CallHookPlayerSpawned(*Player); } ); // Delay the first ping until the client "settles down" // This should fix #889, "BadCast exception, cannot convert bit to fm" error in client m_PingStartTime = std::chrono::steady_clock::now() + std::chrono::seconds(3); // Send the first KeepAlive packet in 3 seconds } void cClientHandle::StreamAllChunks(void) { ASSERT((m_State >= eState::csAuthenticated) || (m_State < eState::csDestroying)); ASSERT(m_Player != nullptr); if (m_Player->IsChangingWorlds()) { // Make a best-effort attempt at bailing. return; } int ChunkPosX = m_Player->GetChunkX(); int ChunkPosZ = m_Player->GetChunkZ(); std::vector> ChunksToSend = { { cChunkCoords(ChunkPosX, ChunkPosZ), cChunkSender::E_CHUNK_PRIORITY_HIGH } }; // Get the look vector and normalize it. Vector3d Position = m_Player->GetEyePosition(); Vector3d LookVector = m_Player->GetLookVector(); LookVector.Normalize(); // High priority: Load the chunks that are in the view-direction of the player (with a radius of 3) for (int Range = 0; Range < m_CurrentViewDistance; Range++) { Vector3d Vector = Position + LookVector * cChunkDef::Width * Range; // Get the chunk from the x / z coords. int RangeX, RangeZ = 0; cChunkDef::BlockToChunk(FloorC(Vector.x), FloorC(Vector.z), RangeX, RangeZ); for (int X = 0; X < 7; X++) { for (int Z = 0; Z < 7; Z++) { int ChunkX = RangeX + ((X >= 4) ? (3 - X) : X); int ChunkZ = RangeZ + ((Z >= 4) ? (3 - Z) : Z); cChunkCoords Coords(ChunkX, ChunkZ); // Checks if the chunk is in distance if ((Diff(ChunkX, ChunkPosX) > m_CurrentViewDistance) || (Diff(ChunkZ, ChunkPosZ) > m_CurrentViewDistance)) { continue; } // Send the chunk; StreamChunk queues a task which will abort if it has been called for the chunk already (we are part of the chunk's clientlist) ChunksToSend.emplace_back( std::make_pair( cChunkCoords(ChunkX, ChunkZ), (Range <= 2) ? cChunkSender::E_CHUNK_PRIORITY_HIGH : cChunkSender::E_CHUNK_PRIORITY_MEDIUM ) ); } } } // Low priority: Add all chunks that are in range. (From the center out to the edge) for (int d = 0; d <= m_CurrentViewDistance; ++d) // cycle through (square) distance, from nearest to furthest { // For each distance add chunks in a hollow square centered around current position: for (int i = -d; i <= d; ++i) { ChunksToSend.emplace_back(std::make_pair(cChunkCoords(ChunkPosX + d, ChunkPosZ + i), cChunkSender::E_CHUNK_PRIORITY_LOW)); ChunksToSend.emplace_back(std::make_pair(cChunkCoords(ChunkPosX - d, ChunkPosZ + i), cChunkSender::E_CHUNK_PRIORITY_LOW)); } for (int i = -d + 1; i < d; ++i) { ChunksToSend.emplace_back(std::make_pair(cChunkCoords(ChunkPosZ + i, ChunkPosZ + d), cChunkSender::E_CHUNK_PRIORITY_LOW)); ChunksToSend.emplace_back(std::make_pair(cChunkCoords(ChunkPosZ + i, ChunkPosZ - d), cChunkSender::E_CHUNK_PRIORITY_LOW)); } } m_Player->GetWorld()->QueueTask( [ChunksToSend = std::move(ChunksToSend), ClientHandle = shared_from_this()](cWorld & a_World) { for (const auto & Chunk : ChunksToSend) { if (a_World.AddChunkClient(Chunk.first.m_ChunkX, Chunk.first.m_ChunkZ, ClientHandle)) { a_World.GetChunkSender().QueueSendChunkTo(Chunk.first.m_ChunkX, Chunk.first.m_ChunkZ, Chunk.second, ClientHandle); // Clarification: std::partition, which cWorld::TickQueuedTasks uses, doesn't guarantee ordering // However, we won't end up with a situation where a RemoveChunkClient ends up before an AddChunkClient for a chunk // because UnloadChunk / RemoveChunkClient is based on our m_LoadedChunks list, which is only updated when this task to add a client has finished. } } } ); } void cClientHandle::UnloadOutOfRangeChunks(void) { if (m_LoadedChunks.empty()) { return; } int ChunkPosX = FAST_FLOOR_DIV(static_cast(m_Player->GetPosX()), cChunkDef::Width); int ChunkPosZ = FAST_FLOOR_DIV(static_cast(m_Player->GetPosZ()), cChunkDef::Width); for (auto EraseItr = m_LoadedChunks.begin(); EraseItr != m_LoadedChunks.end();) { int DiffX = Diff(EraseItr->first.m_ChunkX, ChunkPosX); int DiffZ = Diff(EraseItr->first.m_ChunkZ, ChunkPosZ); if ( (DiffX <= m_CurrentViewDistance) && (DiffZ <= m_CurrentViewDistance) && (m_Player->GetWorld() == &EraseItr->second.get()) ) { ++EraseItr; continue; } EraseItr->second.get().QueueTask( [ChunkCoordinates = EraseItr->first, ClientHandle = shared_from_this()](cWorld & a_World) { a_World.RemoveChunkClient(ChunkCoordinates.m_ChunkX, ChunkCoordinates.m_ChunkZ, ClientHandle); } ); m_Protocol.SendUnloadChunk(EraseItr->first.m_ChunkX, EraseItr->first.m_ChunkZ); EraseItr = m_LoadedChunks.erase(EraseItr); } } void cClientHandle::HandleNPCTrade(int a_SlotNum) { // TODO LOGWARNING("%s: Not implemented yet", __FUNCTION__); } bool cClientHandle::HandleLogin(UInt32 a_ProtocolVersion, const AString & a_Username) { // If the protocol version hasn't been set yet, set it now: if (m_ProtocolVersion == 0) { m_ProtocolVersion = a_ProtocolVersion; } m_Username = a_Username; // Let the plugins know about this event, they may refuse the player: if (cRoot::Get()->GetPluginManager()->CallHookLogin(*this, a_ProtocolVersion, a_Username)) { Destroy(); return false; } // Schedule for authentication; until then, let the player wait (but do not block) SetState(eState::csAuthenticating); cRoot::Get()->GetAuthenticator().Authenticate(GetUniqueID(), GetUsername(), m_Protocol.GetAuthServerID()); return true; } void cClientHandle::HandleCreativeInventory(Int16 a_SlotNum, const cItem & a_HeldItem, eClickAction a_ClickAction) { // This is for creative Inventory changes 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_ClickAction, a_HeldItem); } void cClientHandle::HandleEnchantItem(UInt8 a_WindowID, UInt8 a_Enchantment) { if (a_Enchantment > 2) { LOGWARNING("%s attempt to crash the server with invalid enchanting selection (%u)!", GetUsername().c_str(), a_Enchantment); Kick("Invalid enchanting!"); return; } if ( (m_Player->GetWindow() == nullptr) || (m_Player->GetWindow()->GetWindowID() != a_WindowID) || (m_Player->GetWindow()->GetWindowType() != cWindow::wtEnchantment) ) { return; } cEnchantingWindow * Window = reinterpret_cast(m_Player->GetWindow()); cItem Item = *Window->m_SlotArea->GetSlot(0, *m_Player); // Make a copy of the item short BaseEnchantmentLevel = Window->GetPropertyValue(a_Enchantment); if (Item.EnchantByXPLevels(BaseEnchantmentLevel)) { if (m_Player->IsGameModeCreative() || m_Player->DeltaExperience(-m_Player->XpForLevel(BaseEnchantmentLevel)) >= 0) { Window->m_SlotArea->SetSlot(0, *m_Player, Item); Window->SendSlot(*m_Player, Window->m_SlotArea, 0); Window->BroadcastWholeWindow(); Window->SetProperty(0, 0, *m_Player); Window->SetProperty(1, 0, *m_Player); Window->SetProperty(2, 0, *m_Player); } } } void cClientHandle::HandlePlayerAbilities(bool a_CanFly, bool a_IsFlying, float FlyingSpeed, float WalkingSpeed) { UNUSED(FlyingSpeed); // Ignore the client values for these UNUSED(WalkingSpeed); m_Player->SetCanFly(a_CanFly); m_Player->SetFlying(a_IsFlying); } void cClientHandle::HandlePlayerPos(double a_PosX, double a_PosY, double a_PosZ, double a_Stance, bool a_IsOnGround) { if (m_State != eState::csPlaying) { // The client hasn't been spawned yet and sends nonsense, we know better return; } if (m_Player->IsFrozen()) { // Ignore client-side updates if the player is frozen return; } Vector3d NewPosition(a_PosX, a_PosY, a_PosZ); Vector3d OldPosition = m_Player->GetPosition(); // If the player has moved too far, "repair" them: if ((OldPosition - NewPosition).SqrLength() > 100 * 100) { LOGD("Too far away (%0.2f), \"repairing\" the client", (OldPosition - NewPosition).Length()); SendPlayerMoveLook(); return; } if (cRoot::Get()->GetPluginManager()->CallHookPlayerMoving(*m_Player, OldPosition, NewPosition)) { SendPlayerMoveLook(); return; } // TODO: should do some checks to see if player is not moving through terrain // TODO: Official server refuses position packets too far away from each other, kicking "hacked" clients; we should, too if (cChunkDef::BlockToChunk(NewPosition) != cChunkDef::BlockToChunk(m_Player->GetLastPosition())) { m_ShouldRefreshSentChunks = true; } auto PreviousIsOnGround = m_Player->IsOnGround(); m_Player->SetPosition(NewPosition); m_Player->SetStance(a_Stance); m_Player->SetTouchGround(a_IsOnGround); m_Player->UpdateMovementStats(NewPosition - OldPosition, PreviousIsOnGround); } void cClientHandle::HandlePluginMessage(const AString & a_Channel, const AString & a_Message) { if (a_Channel == "REGISTER") { if (HasPluginChannel(a_Channel)) { SendPluginMessage("UNREGISTER", a_Channel); return; // Can't register again if already taken - kinda defeats the point of plugin messaging! } RegisterPluginChannels(BreakApartPluginChannels(a_Message)); } else if (a_Channel == "UNREGISTER") { UnregisterPluginChannels(BreakApartPluginChannels(a_Message)); } else if (!HasPluginChannel(a_Channel)) { // Ignore if client sent something but didn't register the channel first LOGD("Player %s sent a plugin message on channel \"%s\", but didn't REGISTER it first", GetUsername().c_str(), a_Channel.c_str()); SendPluginMessage("UNREGISTER", a_Channel); return; } cPluginManager::Get()->CallHookPluginMessage(*this, a_Channel, a_Message); } AStringVector cClientHandle::BreakApartPluginChannels(const AString & a_PluginChannels) { // Break the string on each NUL character. // Note that StringSplit() doesn't work on this because NUL is a special char - string terminator size_t len = a_PluginChannels.size(); size_t first = 0; AStringVector res; for (size_t i = 0; i < len; i++) { if (a_PluginChannels[i] != 0) { continue; } if (i > first) { res.push_back(a_PluginChannels.substr(first, i - first)); } first = i + 1; } // for i - a_PluginChannels[] if (first < len) { res.push_back(a_PluginChannels.substr(first, len - first)); } return res; } void cClientHandle::RegisterPluginChannels(const AStringVector & a_ChannelList) { for (AStringVector::const_iterator itr = a_ChannelList.begin(), end = a_ChannelList.end(); itr != end; ++itr) { m_PluginChannels.insert(*itr); } // for itr - a_ChannelList[] } void cClientHandle::UnregisterPluginChannels(const AStringVector & a_ChannelList) { for (AStringVector::const_iterator itr = a_ChannelList.begin(), end = a_ChannelList.end(); itr != end; ++itr) { m_PluginChannels.erase(*itr); } // for itr - a_ChannelList[] } void cClientHandle::HandleBeaconSelection(int a_PrimaryEffect, int a_SecondaryEffect) { cWindow * Window = m_Player->GetWindow(); if ((Window == nullptr) || (Window->GetWindowType() != cWindow::wtBeacon)) { return; } cBeaconWindow * BeaconWindow = reinterpret_cast(Window); if (Window->GetSlot(*m_Player, 0)->IsEmpty()) { return; } cEntityEffect::eType PrimaryEffect = cEntityEffect::effNoEffect; if ((a_PrimaryEffect >= 0) && (a_PrimaryEffect <= static_cast(cEntityEffect::effSaturation))) { PrimaryEffect = static_cast(a_PrimaryEffect); } cEntityEffect::eType SecondaryEffect = cEntityEffect::effNoEffect; if ((a_SecondaryEffect >= 0) && (a_SecondaryEffect <= static_cast(cEntityEffect::effSaturation))) { SecondaryEffect = static_cast(a_SecondaryEffect); } Window->SetSlot(*m_Player, 0, cItem()); BeaconWindow->GetBeaconEntity()->SetPrimaryEffect(PrimaryEffect); // Valid effect check. Vanilla don't check this, but we do it :) if ( (SecondaryEffect == cEntityEffect::effNoEffect) || (SecondaryEffect == cEntityEffect::effRegeneration) || (SecondaryEffect == BeaconWindow->GetBeaconEntity()->GetPrimaryEffect()) ) { BeaconWindow->GetBeaconEntity()->SetSecondaryEffect(SecondaryEffect); } else { BeaconWindow->GetBeaconEntity()->SetSecondaryEffect(cEntityEffect::effNoEffect); } m_Player->CloseWindow(true); } void cClientHandle::HandleCommandBlockBlockChange(int a_BlockX, int a_BlockY, int a_BlockZ, const AString & a_NewCommand) { if (a_NewCommand.empty()) { Kick("Command block string unexpectedly empty - hacked client?"); return; } cWorld * World = m_Player->GetWorld(); if (World->AreCommandBlocksEnabled()) { World->SetCommandBlockCommand(a_BlockX, a_BlockY, a_BlockZ, a_NewCommand); SendChat("Successfully set command block command", mtSuccess); } else { SendChat("Command blocks are not enabled on this server", mtFailure); } } void cClientHandle::HandleCommandBlockEntityChange(UInt32 a_EntityID, const AString & a_NewCommand) { // TODO LOGWARNING("%s: Not implemented yet", __FUNCTION__); } void cClientHandle::HandleAnvilItemName(const AString & a_ItemName) { if ((m_Player->GetWindow() == nullptr) || (m_Player->GetWindow()->GetWindowType() != cWindow::wtAnvil)) { return; } if (a_ItemName.length() <= 30) { reinterpret_cast(m_Player->GetWindow())->SetRepairedItemName(a_ItemName, m_Player); } } void cClientHandle::HandleLeftClick(int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, UInt8 a_Status) { LOGD("HandleLeftClick: {%i, %i, %i}; Face: %i; Stat: %i", a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_Status ); if ((a_Status == DIG_STATUS_STARTED) || (a_Status == DIG_STATUS_FINISHED)) { if (a_BlockFace == BLOCK_FACE_NONE) { return; } /* Check for clickthrough-blocks: When the user breaks a fire block, the client send the wrong block location. We must find the right block with the face direction. */ int BlockX = a_BlockX; int BlockY = a_BlockY; int BlockZ = a_BlockZ; AddFaceDirection(BlockX, BlockY, BlockZ, a_BlockFace); if ((BlockY < 0) || (BlockY >= cChunkDef::Height)) { return; } if (cBlockInfo::GetHandler(m_Player->GetWorld()->GetBlock(BlockX, BlockY, BlockZ))->IsClickedThrough()) { a_BlockX = BlockX; a_BlockY = BlockY; a_BlockZ = BlockZ; } if ( ((Diff(m_Player->GetPosX(), static_cast(a_BlockX)) > 6) || (Diff(m_Player->GetPosY(), static_cast(a_BlockY)) > 6) || (Diff(m_Player->GetPosZ(), static_cast(a_BlockZ)) > 6)) ) { m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); return; } } auto PlgMgr = cRoot::Get()->GetPluginManager(); if (m_Player->IsFrozen() || PlgMgr->CallHookPlayerLeftClick(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, static_cast(a_Status))) { // A plugin doesn't agree with the action, replace the block on the client and quit: m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); SendPlayerPosition(); // Prevents the player from falling through the block that was temporarily broken client side. return; } switch (a_Status) { case DIG_STATUS_DROP_HELD: // Drop held item { if (PlgMgr->CallHookPlayerTossingItem(*m_Player)) { // A plugin doesn't agree with the tossing. The plugin itself is responsible for handling the consequences (possible inventory mismatch) return; } m_Player->TossEquippedItem(); return; } case DIG_STATUS_SHOOT_EAT: { cItemHandler * ItemHandler = cItemHandler::GetItemHandler(m_Player->GetEquippedItem()); if (ItemHandler->IsFood() || ItemHandler->IsDrinkable(m_Player->GetEquippedItem().m_ItemDamage)) { m_Player->AbortEating(); return; } else { if (PlgMgr->CallHookPlayerShooting(*m_Player)) { // A plugin doesn't agree with the action. The plugin itself is responsible for handling the consequences (possible inventory mismatch) return; } ItemHandler->OnItemShoot(m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace); } return; } case DIG_STATUS_STARTED: { BLOCKTYPE OldBlock; NIBBLETYPE OldMeta; m_Player->GetWorld()->GetBlockTypeMeta(a_BlockX, a_BlockY, a_BlockZ, OldBlock, OldMeta); HandleBlockDigStarted(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, OldBlock, OldMeta); return; } case DIG_STATUS_FINISHED: { BLOCKTYPE OldBlock; NIBBLETYPE OldMeta; m_Player->GetWorld()->GetBlockTypeMeta(a_BlockX, a_BlockY, a_BlockZ, OldBlock, OldMeta); HandleBlockDigFinished(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, OldBlock, OldMeta); return; } case DIG_STATUS_CANCELLED: { // Block breaking cancelled by player FinishDigAnimation(); return; } case DIG_STATUS_DROP_STACK: { if (PlgMgr->CallHookPlayerTossingItem(*m_Player)) { // A plugin doesn't agree with the tossing. The plugin itself is responsible for handling the consequences (possible inventory mismatch) return; } m_Player->TossEquippedItem(64); // Toss entire slot - if there aren't enough items, the maximum will be ejected return; } case DIG_STATUS_SWAP_ITEM_IN_HAND: { // TODO: Not yet implemented return; } default: { ASSERT(!"Unhandled DIG_STATUS"); return; } } // switch (a_Status) } void cClientHandle::HandleBlockDigStarted(int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, BLOCKTYPE a_OldBlock, NIBBLETYPE a_OldMeta) { if ( m_HasStartedDigging && (a_BlockX == m_LastDigBlockX) && (a_BlockY == m_LastDigBlockY) && (a_BlockZ == m_LastDigBlockZ) ) { // It is a duplicate packet, drop it right away return; } if ( m_Player->IsGameModeCreative() && ItemCategory::IsSword(m_Player->GetInventory().GetEquippedItem().m_ItemType) && (m_Player->GetWorld()->GetBlock(a_BlockX, a_BlockY, a_BlockZ) != E_BLOCK_FIRE) ) { // Players can't destroy blocks with a sword in the hand. return; } if ( (Diff(m_Player->GetPosX(), static_cast(a_BlockX)) > 6) || (Diff(m_Player->GetPosY(), static_cast(a_BlockY)) > 6) || (Diff(m_Player->GetPosZ(), static_cast(a_BlockZ)) > 6) ) { m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); return; } // Set the last digging coords to the block being dug, so that they can be checked in DIG_FINISHED to avoid dig / aim bug in the client: m_HasStartedDigging = true; m_LastDigBlockX = a_BlockX; m_LastDigBlockY = a_BlockY; m_LastDigBlockZ = a_BlockZ; if ( (m_Player->IsGameModeCreative()) || // In creative mode, digging is done immediately cBlockInfo::IsOneHitDig(a_OldBlock) // One-hit blocks get destroyed immediately, too ) { HandleBlockDigFinished(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_OldBlock, a_OldMeta); return; } // Start dig animation // TODO: calculate real animation speed // TODO: Send animation packets even without receiving any other packets m_BlockDigAnimSpeed = 10; m_BlockDigAnimX = a_BlockX; m_BlockDigAnimY = a_BlockY; m_BlockDigAnimZ = a_BlockZ; m_BlockDigAnimStage = 0; m_Player->GetWorld()->BroadcastBlockBreakAnimation(static_cast(m_UniqueID), m_BlockDigAnimX, m_BlockDigAnimY, m_BlockDigAnimZ, 0, this); cWorld * World = m_Player->GetWorld(); cChunkInterface ChunkInterface(World->GetChunkMap()); cBlockHandler * Handler = cBlockInfo::GetHandler(a_OldBlock); Handler->OnDigging(ChunkInterface, *World, m_Player, a_BlockX, a_BlockY, a_BlockZ); cItemHandler * ItemHandler = cItemHandler::GetItemHandler(m_Player->GetEquippedItem()); ItemHandler->OnDiggingBlock(World, m_Player, m_Player->GetEquippedItem(), a_BlockX, a_BlockY, a_BlockZ, a_BlockFace); } void cClientHandle::HandleBlockDigFinished(int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, BLOCKTYPE a_OldBlock, NIBBLETYPE a_OldMeta) { if ( !m_HasStartedDigging || // Hasn't received the DIG_STARTED packet (m_LastDigBlockX != a_BlockX) || // DIG_STARTED has had different pos (m_LastDigBlockY != a_BlockY) || (m_LastDigBlockZ != a_BlockZ) ) { LOGD("Prevented a dig / aim bug in the client (finish {%d, %d, %d} vs start {%d, %d, %d}, HSD: %s)", a_BlockX, a_BlockY, a_BlockZ, m_LastDigBlockX, m_LastDigBlockY, m_LastDigBlockZ, (m_HasStartedDigging ? "True" : "False") ); return; } FinishDigAnimation(); if (!m_Player->IsGameModeCreative()) { if (a_OldBlock == E_BLOCK_BEDROCK) { Kick("You can't break a bedrock!"); return; } if (a_OldBlock == E_BLOCK_BARRIER) { Kick("You can't break a barrier!"); return; } } cWorld * World = m_Player->GetWorld(); cItemHandler * ItemHandler = cItemHandler::GetItemHandler(m_Player->GetEquippedItem()); if (cRoot::Get()->GetPluginManager()->CallHookPlayerBreakingBlock(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_OldBlock, a_OldMeta)) { // A plugin doesn't agree with the breaking. Bail out. Send the block back to the client, so that it knows: m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); SendPlayerPosition(); // Prevents the player from falling through the block that was temporarily broken client side. return; } if (a_OldBlock == E_BLOCK_AIR) { LOGD("Dug air - what the function?"); return; } m_Player->AddFoodExhaustion(0.025); ItemHandler->OnBlockDestroyed(World, m_Player, m_Player->GetEquippedItem(), a_BlockX, a_BlockY, a_BlockZ); // The ItemHandler is also responsible for spawning the pickups cChunkInterface ChunkInterface(World->GetChunkMap()); BlockHandler(a_OldBlock)->OnDestroyedByPlayer(ChunkInterface, *World, m_Player, a_BlockX, a_BlockY, a_BlockZ); World->BroadcastSoundParticleEffect(EffectID::PARTICLE_SMOKE, a_BlockX, a_BlockY, a_BlockZ, a_OldBlock, this); // This call would remove the water, placed from the ice block handler if (a_OldBlock != E_BLOCK_ICE) { World->DigBlock(a_BlockX, a_BlockY, a_BlockZ); } cRoot::Get()->GetPluginManager()->CallHookPlayerBrokenBlock(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_OldBlock, a_OldMeta); } void cClientHandle::FinishDigAnimation() { if (!m_HasStartedDigging) // Hasn't received the DIG_STARTED packet { return; } m_HasStartedDigging = false; if (m_BlockDigAnimStage != -1) { // End dig animation m_BlockDigAnimStage = -1; // It seems that 10 ends block animation m_Player->GetWorld()->BroadcastBlockBreakAnimation(static_cast(m_UniqueID), m_LastDigBlockX, m_LastDigBlockY, m_LastDigBlockZ, 10, this); } m_BlockDigAnimX = -1; m_BlockDigAnimY = -1; m_BlockDigAnimZ = -1; } void cClientHandle::HandleRightClick(int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, const cItem & a_HeldItem) { // TODO: Rewrite this function // Distance from the block's center to the player's eye height double dist = (Vector3d(a_BlockX, a_BlockY, a_BlockZ) + Vector3d(0.5, 0.5, 0.5) - m_Player->GetEyePosition()).Length(); LOGD("HandleRightClick: {%d, %d, %d}, face %d, HeldItem: %s; dist: %.02f", a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, ItemToFullString(a_HeldItem).c_str(), dist ); // Check the reach distance: // _X 2014-11-25: I've maxed at 5.26 with a Survival client and 5.78 with a Creative client in my tests double maxDist = m_Player->IsGameModeCreative() ? 5.78 : 5.26; bool AreRealCoords = (dist <= maxDist); cWorld * World = m_Player->GetWorld(); if ( (a_BlockFace != BLOCK_FACE_NONE) && // The client is interacting with a specific block IsValidBlock(a_HeldItem.m_ItemType) && !AreRealCoords ) { AddFaceDirection(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace); if ((a_BlockX != -1) && (a_BlockY >= 0) && (a_BlockZ != -1)) { World->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); if (a_BlockY < cChunkDef::Height - 1) { World->SendBlockTo(a_BlockX, a_BlockY + 1, a_BlockZ, m_Player); // 2 block high things } if (a_BlockY > 0) { World->SendBlockTo(a_BlockX, a_BlockY - 1, a_BlockZ, m_Player); // 2 block high things } } m_Player->GetInventory().SendEquippedSlot(); return; } if (!AreRealCoords) { a_BlockFace = BLOCK_FACE_NONE; } auto PlgMgr = cRoot::Get()->GetPluginManager(); if (m_Player->IsFrozen() || PlgMgr->CallHookPlayerRightClick(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ)) { // A plugin doesn't agree with the action, replace the block on the client and quit: if (AreRealCoords) { cChunkInterface ChunkInterface(World->GetChunkMap()); BLOCKTYPE BlockType = World->GetBlock(a_BlockX, a_BlockY, a_BlockZ); cBlockHandler * BlockHandler = cBlockInfo::GetHandler(BlockType); BlockHandler->OnCancelRightClick(ChunkInterface, *World, m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace); if (a_BlockFace != BLOCK_FACE_NONE) { AddFaceDirection(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace); World->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); if (a_BlockY < cChunkDef::Height - 1) { World->SendBlockTo(a_BlockX, a_BlockY + 1, a_BlockZ, m_Player); // 2 block high things } if (a_BlockY > 1) { World->SendBlockTo(a_BlockX, a_BlockY - 1, a_BlockZ, m_Player); // 2 block high things } m_Player->GetInventory().SendEquippedSlot(); } } return; } const cItem & Equipped = m_Player->GetInventory().GetEquippedItem(); if ((Equipped.m_ItemType != a_HeldItem.m_ItemType) && (a_HeldItem.m_ItemType != -1)) { // Only compare ItemType, not meta (torches have different metas) // The -1 check is there because sometimes the client sends -1 instead of the held item // Ref.: https://forum.cuberite.org/thread-549-post-4502.html#pid4502 LOGWARN("Player %s tried to place a block that was not equipped (exp %d, got %d)", m_Username.c_str(), Equipped.m_ItemType, a_HeldItem.m_ItemType ); // Let's send the current world block to the client, so that it can immediately "let the user know" that they haven't placed the block if (a_BlockFace != BLOCK_FACE_NONE) { AddFaceDirection(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace); World->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); } return; } if (AreRealCoords) { BLOCKTYPE BlockType; NIBBLETYPE BlockMeta; World->GetBlockTypeMeta(a_BlockX, a_BlockY, a_BlockZ, BlockType, BlockMeta); cBlockHandler * BlockHandler = cBlockInfo::GetHandler(BlockType); if (BlockHandler->IsUseable() && !m_Player->IsCrouched()) { if (!PlgMgr->CallHookPlayerUsingBlock(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, BlockType, BlockMeta)) { cChunkInterface ChunkInterface(World->GetChunkMap()); if (BlockHandler->OnUse(ChunkInterface, *World, m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ)) { // block use was successful, we're done PlgMgr->CallHookPlayerUsedBlock(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, BlockType, BlockMeta); return; } } } } short EquippedDamage = Equipped.m_ItemDamage; cItemHandler * ItemHandler = cItemHandler::GetItemHandler(Equipped.m_ItemType); if (ItemHandler->IsPlaceable() && (a_BlockFace != BLOCK_FACE_NONE)) { if (!ItemHandler->OnPlayerPlace(*World, *m_Player, Equipped, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ)) { // Placement failed, bail out return; } } else if ((ItemHandler->IsFood() || ItemHandler->IsDrinkable(EquippedDamage))) { if ( (m_Player->IsSatiated() || m_Player->IsGameModeCreative()) && // Only creative or hungry players can eat ItemHandler->IsFood() && (Equipped.m_ItemType != E_ITEM_GOLDEN_APPLE) // Golden apple is a special case, it is used instead of eaten ) { // The player is satiated or in creative, and trying to eat return; } m_Player->StartEating(); if (m_Player->IsFrozen() || PlgMgr->CallHookPlayerEating(*m_Player)) { // A plugin won't let us eat, abort (send the proper packets to the client, too): m_Player->AbortEating(); } } else { if (m_Player->IsFrozen() || PlgMgr->CallHookPlayerUsingItem(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ)) { // A plugin doesn't agree with using the item, abort return; } cBlockInServerPluginInterface PluginInterface(*World); ItemHandler->OnItemUse(World, m_Player, PluginInterface, Equipped, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace); PlgMgr->CallHookPlayerUsedItem(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ); } } void cClientHandle::HandleChat(const AString & a_Message) { // We no longer need to postpone message processing, because the messages already arrive in the Tick thread // If a command, perform it: AString Message(a_Message); if (cRoot::Get()->GetServer()->Command(*this, Message)) { return; } // Not a command, broadcast as a message: cCompositeChat Msg; AString Color = m_Player->GetColor(); if (Color.length() == 3) { Color = AString("@") + Color[2]; } else { Color.clear(); } Msg.AddTextPart(AString("<") + m_Player->GetName() + "> ", Color); Msg.ParseText(Message); Msg.UnderlineUrls(); cRoot::Get()->BroadcastChat(Msg); } void cClientHandle::HandlePlayerLook(float a_Rotation, float a_Pitch, bool a_IsOnGround) { if (m_State != eState::csPlaying) { return; } m_Player->SetYaw (a_Rotation); m_Player->SetHeadYaw (a_Rotation); m_Player->SetPitch (a_Pitch); m_Player->SetTouchGround(a_IsOnGround); } void cClientHandle::HandlePlayerMoveLook(double a_PosX, double a_PosY, double a_PosZ, double a_Stance, float a_Rotation, float a_Pitch, bool a_IsOnGround) { HandlePlayerPos(a_PosX, a_PosY, a_PosZ, a_Stance, a_IsOnGround); HandlePlayerLook(a_Rotation, a_Pitch, a_IsOnGround); } void cClientHandle::HandleAnimation(int a_Animation) { if (cPluginManager::Get()->CallHookPlayerAnimation(*m_Player, a_Animation)) { // Plugin disagrees, bail out return; } m_Player->GetWorld()->BroadcastEntityAnimation(*m_Player, static_cast(a_Animation), this); } void cClientHandle::HandleSlotSelected(Int16 a_SlotNum) { m_Player->GetInventory().SetEquippedSlotNum(a_SlotNum); m_Player->GetWorld()->BroadcastEntityEquipment(*m_Player, 0, m_Player->GetInventory().GetEquippedItem(), this); } void cClientHandle::HandleSteerVehicle(float a_Forward, float a_Sideways) { m_Player->SteerVehicle(a_Forward, a_Sideways); } void cClientHandle::HandleWindowClose(UInt8 a_WindowID) { m_Player->CloseWindowIfID(static_cast(a_WindowID)); } void cClientHandle::HandleWindowClick(UInt8 a_WindowID, Int16 a_SlotNum, eClickAction a_ClickAction, const cItem & a_HeldItem) { LOGD("WindowClick: WinID %d, SlotNum %d, action: %s, Item %s x %d", a_WindowID, a_SlotNum, ClickActionToString(a_ClickAction), ItemToString(a_HeldItem).c_str(), a_HeldItem.m_ItemCount ); cWindow * Window = m_Player->GetWindow(); if (Window == nullptr) { LOGWARNING("Player \"%s\" clicked in a non-existent window. Ignoring", m_Username.c_str()); return; } Window->Clicked(*m_Player, a_WindowID, a_SlotNum, a_ClickAction, a_HeldItem); } void cClientHandle::HandleUpdateSign( int a_BlockX, int a_BlockY, int a_BlockZ, const AString & a_Line1, const AString & a_Line2, const AString & a_Line3, const AString & a_Line4 ) { if (m_LastPlacedSign.Equals(Vector3i(a_BlockX, a_BlockY, a_BlockZ))) { m_LastPlacedSign.Set(0, -1, 0); m_Player->GetWorld()->QueueTask( [=](cWorld & a_World) { a_World.SetSignLines(a_BlockX, a_BlockY, a_BlockZ, a_Line1, a_Line2, a_Line3, a_Line4, m_Player); } ); } } void cClientHandle::HandleUseEntity(UInt32 a_TargetEntityID, bool a_IsLeftClick) { // TODO: Let plugins interfere via a hook // If it is a right click, call the entity's OnRightClicked() handler: if (!a_IsLeftClick) { class cRclkEntity : public cEntityCallback { cPlayer & m_Player; virtual bool Item(cEntity * a_Entity) override { if (cPluginManager::Get()->CallHookPlayerRightClickingEntity(m_Player, *a_Entity)) { return false; } a_Entity->OnRightClicked(m_Player); return false; } public: cRclkEntity(cPlayer & a_Player) : m_Player(a_Player) {} } Callback (*m_Player); cWorld * World = m_Player->GetWorld(); World->DoWithEntityByID(a_TargetEntityID, Callback); return; } // If it is a left click, attack the entity: class cDamageEntity : public cEntityCallback { public: cPlayer * m_Me; cDamageEntity(cPlayer * a_Player) : m_Me(a_Player) { } virtual bool Item(cEntity * a_Entity) override { if (!a_Entity->GetWorld()->IsPVPEnabled()) { // PVP is disabled, disallow players hurting other players: if (a_Entity->IsPlayer()) { // Player is hurting another player which is not allowed when PVP is disabled so ignore it return true; } } a_Entity->TakeDamage(*m_Me); m_Me->AddFoodExhaustion(0.3); if (a_Entity->IsPawn()) { m_Me->NotifyNearbyWolves(static_cast(a_Entity), true); } return true; } } Callback(m_Player); cWorld * World = m_Player->GetWorld(); World->DoWithEntityByID(a_TargetEntityID, Callback); } void cClientHandle::HandleRespawn(void) { m_Player->Respawn(); cRoot::Get()->GetPluginManager()->CallHookPlayerSpawned(*m_Player); } void cClientHandle::HandleKeepAlive(UInt32 a_KeepAliveID) { if (a_KeepAliveID == m_PingID) { m_Ping = std::chrono::steady_clock::now() - m_PingStartTime; } } bool cClientHandle::CheckMultiLogin(const AString & a_Username) { // If the multilogin is allowed, skip this check entirely: if ((cRoot::Get()->GetServer()->DoesAllowMultiLogin())) { return true; } // Check if the username is connected already: if (cRoot::Get()->GetServer()->IsPlayerInQueue(a_Username)) { Kick("A player of the username is already logged in"); return false; } return true; } bool cClientHandle::HandleHandshake(const AString & a_Username) { if (a_Username.length() > 16) { Kick("Your username is too long(>16 characters)"); return false; } if (!cRoot::Get()->GetPluginManager()->CallHookHandshake(*this, a_Username)) { if (cRoot::Get()->GetServer()->GetNumPlayers() >= cRoot::Get()->GetServer()->GetMaxPlayers()) { Kick("The server is currently full :(-- Try again later"); return false; } } return CheckMultiLogin(a_Username); } void cClientHandle::HandleEntityCrouch(UInt32 a_EntityID, bool a_IsCrouching) { if (a_EntityID != m_Player->GetUniqueID()) { // We should only receive entity actions from the entity that is performing the action return; } m_Player->SetCrouch(a_IsCrouching); } void cClientHandle::HandleEntityLeaveBed(UInt32 a_EntityID) { if (a_EntityID != m_Player->GetUniqueID()) { // We should only receive entity actions from the entity that is performing the action return; } cChunkInterface Interface(m_Player->GetWorld()->GetChunkMap()); cBlockBedHandler::SetBedOccupationState(Interface, m_Player->GetLastBedPos(), false); m_Player->SetIsInBed(false); } void cClientHandle::HandleEntitySprinting(UInt32 a_EntityID, bool a_IsSprinting) { if (a_EntityID != m_Player->GetUniqueID()) { // We should only receive entity actions from the entity that is performing the action return; } m_Player->SetSprint(a_IsSprinting); } void cClientHandle::HandleUnmount(void) { m_Player->Detach(); } void cClientHandle::HandleTabCompletion(const AString & a_Text) { AStringVector Results; // Get player name completions. if (cRoot::Get()->GetServer()->ShouldAllowMultiWorldTabCompletion()) { Results = cRoot::Get()->GetPlayerTabCompletionMultiWorld(a_Text); } else { m_Player->GetWorld()->TabCompleteUserName(a_Text, Results); } // Get command completions cRoot::Get()->GetPluginManager()->TabCompleteCommand(a_Text, Results, m_Player); if (Results.empty()) { return; } // Sort and send results. std::sort(Results.begin(), Results.end()); SendTabCompletionResults(Results); } void cClientHandle::SendData(const char * a_Data, size_t a_Size) { cCSLock Lock(m_CSOutgoingData); m_OutgoingData.append(a_Data, a_Size); } void cClientHandle::RemoveFromWorld(void) { ASSERT(cRoot::Get()->GetServer()->IsInTickThread()); // Remove all associated chunks: for (const auto & Chunk : m_LoadedChunks) { Chunk.second.get().QueueTask( [ChunkCoordinates = Chunk.first, ClientHandle = shared_from_this()](cWorld & a_World) { a_World.RemoveChunkClient(ChunkCoordinates.m_ChunkX, ChunkCoordinates.m_ChunkZ, ClientHandle); } ); GetProtocol().SendUnloadChunk(Chunk.first.m_ChunkX, Chunk.first.m_ChunkZ); } m_LoadedChunks.clear(); } bool cClientHandle::EnforceBlockInteractionsRate(void) { ASSERT(cRoot::Get()->GetServer()->IsInTickThread()); m_NumBlockChangeInteractionsThisTick++; if (m_NumBlockChangeInteractionsThisTick > MAX_BLOCK_CHANGE_INTERACTIONS) { Kick("Too many blocks were destroyed per unit time - hacked client?"); return false; } return true; } void cClientHandle::ProcessQueuedIncomingData(void) { decltype(m_IncomingData) IncomingData; { cCSLock Lock(m_CSIncomingData); std::swap(IncomingData, m_IncomingData); } if (!IncomingData.empty()) { m_Protocol.DataReceived(IncomingData.data(), IncomingData.size()); } } void cClientHandle::ProcessDataCommitQueue(void) { if (m_DataCommitQueue.empty()) { return; } if ((m_Player == nullptr) || (m_Player->GetWorld() == nullptr)) { Kick("Illegal packet sent - player not spawned yet - hacked client?"); return; } decltype(m_DataCommitQueue) DataCommitTasks; std::swap(DataCommitTasks, m_DataCommitQueue); m_Player->GetWorld()->QueueTask( [DataCommitTasks = std::move(DataCommitTasks), Player = m_Player](cWorld & a_World) { if (Player->IsChangingWorlds() || (Player->GetWorld() != &a_World)) { // IsChangingWorlds is true: cPlayer::m_World may be updated without warning - cannot handle this. // cPlayer::m_World is unequal to a_World: player has completed world change - deal with after effect by bailing return; } for (const auto & Task : DataCommitTasks) { Task(); } } ); } void cClientHandle::ProcessQueuedOutgoingData(void) { decltype(m_OutgoingData) OutgoingData; { cCSLock Lock(m_CSOutgoingData); std::swap(OutgoingData, m_OutgoingData); } if (!OutgoingData.empty()) { { cCSLock Lock(m_CSLink); // All possible operations which reset m_Link MUST cause the clienthandle to cease to be ticked by the server ASSERT(m_Link != nullptr); m_Link->Send(OutgoingData); } } } void cClientHandle::Tick(float a_Dt) { ProcessQueuedIncomingData(); ProcessQueuedOutgoingData(); ProcessDataCommitQueue(); m_TicksSinceLastPacket += 1; if (m_TicksSinceLastPacket > 600) // 30 seconds time-out { SendDisconnect("Nooooo!! You timed out! D: Come back!"); return; } if (m_Player == nullptr) { return; } // Send a ping packet: if ((m_PingStartTime + PING_TIME_MS <= std::chrono::steady_clock::now())) { m_PingID++; m_PingStartTime = std::chrono::steady_clock::now(); m_Protocol.SendKeepAlive(m_PingID); } if (m_ShouldRefreshSentChunks && (m_State >= eState::csAuthenticated) && (m_State < eState::csDestroying)) { StreamAllChunks(); UnloadOutOfRangeChunks(); m_ShouldRefreshSentChunks = false; } // Handle block break animation: if (m_BlockDigAnimStage > -1) { int lastAnimVal = m_BlockDigAnimStage; m_BlockDigAnimStage += static_cast(m_BlockDigAnimSpeed * a_Dt); if (m_BlockDigAnimStage > 9000) { m_BlockDigAnimStage = 9000; } if (m_BlockDigAnimStage / 1000 != lastAnimVal / 1000) { m_Player->GetWorld()->BroadcastBlockBreakAnimation(static_cast(m_UniqueID), m_BlockDigAnimX, m_BlockDigAnimY, m_BlockDigAnimZ, static_cast(m_BlockDigAnimStage / 1000), this); } } // Reset explosion & block change counters: m_NumExplosionsThisTick = 0; m_NumBlockChangeInteractionsThisTick = 0; } void cClientHandle::SendAttachEntity(const cEntity & a_Entity, const cEntity & a_Vehicle) { m_Protocol.SendAttachEntity(a_Entity, a_Vehicle); } void cClientHandle::SendBlockAction(int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType) { m_Protocol.SendBlockAction(a_BlockX, a_BlockY, a_BlockZ, a_Byte1, a_Byte2, a_BlockType); } void cClientHandle::SendBlockBreakAnim(UInt32 a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) { m_Protocol.SendBlockBreakAnim(a_EntityID, a_BlockX, a_BlockY, a_BlockZ, a_Stage); } void cClientHandle::SendBlockChange(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) { GetProtocol().SendBlockChange(a_BlockX, a_BlockY, a_BlockZ, a_BlockType, a_BlockMeta); } void cClientHandle::SendBlockChanges(int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) { GetProtocol().SendBlockChanges(a_ChunkX, a_ChunkZ, a_Changes); } void cClientHandle::SendChat(const AString & a_Message, eMessageType a_ChatPrefix, const AString & a_AdditionalData) { bool ShouldUsePrefixes = m_Player->GetWorld()->ShouldUseChatPrefixes(); AString Message = FormatMessageType(ShouldUsePrefixes, a_ChatPrefix, a_AdditionalData); m_Protocol.SendChat(Message.append(a_Message), ctChatBox, ShouldUsePrefixes); } void cClientHandle::SendChat(const cCompositeChat & a_Message) { bool ShouldUsePrefixes = m_Player->GetWorld()->ShouldUseChatPrefixes(); m_Protocol.SendChat(a_Message, ctChatBox, ShouldUsePrefixes); } void cClientHandle::SendChatAboveActionBar(const AString & a_Message, eMessageType a_ChatPrefix, const AString & a_AdditionalData) { AString Message = FormatMessageType(m_Player->GetWorld()->ShouldUseChatPrefixes(), a_ChatPrefix, a_AdditionalData); m_Protocol.SendChat(Message.append(a_Message), ctAboveActionBar); } void cClientHandle::SendChatAboveActionBar(const cCompositeChat & a_Message) { m_Protocol.SendChat(a_Message, ctAboveActionBar, m_Player->GetWorld()->ShouldUseChatPrefixes()); } void cClientHandle::SendChatSystem(const AString & a_Message, eMessageType a_ChatPrefix, const AString & a_AdditionalData) { auto ShouldUsePrefixes = m_Player->GetWorld()->ShouldUseChatPrefixes(); AString Message = FormatMessageType(ShouldUsePrefixes, a_ChatPrefix, a_AdditionalData); m_Protocol.SendChat(Message.append(a_Message), ctSystem, ShouldUsePrefixes); } void cClientHandle::SendChatSystem(const cCompositeChat & a_Message) { m_Protocol.SendChat(a_Message, ctSystem, m_Player->GetWorld()->ShouldUseChatPrefixes()); } void cClientHandle::SendChunkData(cWorld & a_ChunkSenderWorld, const cChunkCoords & a_ChunkCoordinates, cChunkDataSerializer & a_Serializer) { m_Protocol.SendChunkData(a_ChunkCoordinates.m_ChunkX, a_ChunkCoordinates.m_ChunkZ, a_Serializer); cRoot::Get()->GetServer()->QueueTask( [&a_ChunkSenderWorld, a_ChunkCoordinates, ClientHandle = shared_from_this()] { // Add the chunk to the list of chunks sent to the player: auto Result = ClientHandle->m_LoadedChunks.emplace(a_ChunkCoordinates, std::ref(a_ChunkSenderWorld)); if (Result.second || (&Result.first->second.get() != &a_ChunkSenderWorld)) { auto Player = ClientHandle->GetPlayer(); if ( (Player->GetChunkX() == a_ChunkCoordinates.m_ChunkX) && (Player->GetChunkZ() == a_ChunkCoordinates.m_ChunkZ) ) { if (ClientHandle->m_State == eState::csAuthenticated) { ClientHandle->SendPlayerMoveLook(); ClientHandle->SetState(eState::csPlaying); } Player->GetWorld()->QueueTask( [Player](cWorld & a_World) { auto SearchingPosition = Player->GetPosition(); if (SearchingPosition.y < 0) { SearchingPosition.y = 0; } while (SearchingPosition.y < cChunkDef::Height - 2) { // If we find a position with enough space for the player if (a_World.GetBlock(SearchingPosition.x, SearchingPosition.y, SearchingPosition.z) == E_BLOCK_AIR) { if (a_World.GetBlock(SearchingPosition.x, SearchingPosition.y + 1, SearchingPosition.z) == E_BLOCK_AIR) { Player->TeleportToCoords(SearchingPosition); break; } else { SearchingPosition.y += 2; } } else { SearchingPosition.y++; } } } ); } Result.first->second.get().QueueTask( [a_ChunkCoordinates, ClientHandle](cWorld & a_World) { a_World.RemoveChunkClient(a_ChunkCoordinates.m_ChunkX, a_ChunkCoordinates.m_ChunkZ, ClientHandle); } ); Result.first->second = std::ref(a_ChunkSenderWorld); } } ); } void cClientHandle::SendCollectEntity(const cEntity & a_Entity, const cPlayer & a_Player) { m_Protocol.SendCollectEntity(a_Entity, a_Player); } void cClientHandle::SendDestroyEntity(const cEntity & a_Entity) { m_Protocol.SendDestroyEntity(a_Entity); } void cClientHandle::SendDetachEntity(const cEntity & a_Entity, const cEntity & a_PreviousVehicle) { m_Protocol.SendDetachEntity(a_Entity, a_PreviousVehicle); } void cClientHandle::SendDisconnect(const AString & a_Reason) { // Lemma 1: All outgoing data is queued to be sent on the server tick thread. // Lemma 2: All link-closing actions will set state to destroyed. // Lemma 3: A call to Destroy() is guaranteed to prevent the send queue from being processed. // Corollary: Prevents situations where the client was signalled to disconnect - Kick() - and suddenly a wild packet appears (#31) // Note: Kick packets are sent explicitly by flushing the queue; TODO: probably fine? LOGD("Sending a DC: \"%s\"", StripColorCodes(a_Reason).c_str()); { cCSLock Lock(m_CSLink); m_OutgoingData.clear(); m_Protocol.SendDisconnect(a_Reason); ProcessQueuedOutgoingData(); } cRoot::Get()->GetServer()->QueueTask( [ClientHandle = shared_from_this()] { ClientHandle->Destroy(); } ); } void cClientHandle::SendEditSign(int a_BlockX, int a_BlockY, int a_BlockZ) { m_LastPlacedSign.Set(a_BlockX, a_BlockY, a_BlockZ); m_Protocol.SendEditSign(a_BlockX, a_BlockY, a_BlockZ); } void cClientHandle::SendEntityEffect(const cEntity & a_Entity, int a_EffectID, int a_Amplifier, short a_Duration) { m_Protocol.SendEntityEffect(a_Entity, a_EffectID, a_Amplifier, a_Duration); } void cClientHandle::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum, const cItem & a_Item) { m_Protocol.SendEntityEquipment(a_Entity, a_SlotNum, a_Item); } void cClientHandle::SendEntityHeadLook(const cEntity & a_Entity) { ASSERT(a_Entity.GetUniqueID() != m_Player->GetUniqueID()); // Must not send for self m_Protocol.SendEntityHeadLook(a_Entity); } void cClientHandle::SendEntityLook(const cEntity & a_Entity) { ASSERT(a_Entity.GetUniqueID() != m_Player->GetUniqueID()); // Must not send for self m_Protocol.SendEntityLook(a_Entity); } void cClientHandle::SendEntityMetadata(const cEntity & a_Entity) { m_Protocol.SendEntityMetadata(a_Entity); } void cClientHandle::SendEntityRelMove(const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) { ASSERT(a_Entity.GetUniqueID() != m_Player->GetUniqueID()); // Must not send for self m_Protocol.SendEntityRelMove(a_Entity, a_RelX, a_RelY, a_RelZ); } void cClientHandle::SendEntityRelMoveLook(const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ) { ASSERT(a_Entity.GetUniqueID() != m_Player->GetUniqueID()); // Must not send for self m_Protocol.SendEntityRelMoveLook(a_Entity, a_RelX, a_RelY, a_RelZ); } void cClientHandle::SendEntityStatus(const cEntity & a_Entity, char a_Status) { m_Protocol.SendEntityStatus(a_Entity, a_Status); } void cClientHandle::SendEntityVelocity(const cEntity & a_Entity) { m_Protocol.SendEntityVelocity(a_Entity); } void cClientHandle::SendExplosion(double a_BlockX, double a_BlockY, double a_BlockZ, float a_Radius, const cVector3iArray & a_BlocksAffected, const Vector3d & a_PlayerMotion) { if (m_NumExplosionsThisTick > MAX_EXPLOSIONS_PER_TICK) { LOGD("Dropped an explosion!"); return; } // Update the statistics: m_NumExplosionsThisTick++; m_Protocol.SendExplosion(a_BlockX, a_BlockY, a_BlockZ, a_Radius, a_BlocksAffected, a_PlayerMotion); } void cClientHandle::SendGameMode(eGameMode a_GameMode) { m_Protocol.SendGameMode(a_GameMode); } void cClientHandle::SendHealth(void) { m_Protocol.SendHealth(); } 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); } void cClientHandle::SendMapData(const cMap & a_Map, int a_DataStartX, int a_DataStartY) { m_Protocol.SendMapData(a_Map, a_DataStartX, a_DataStartY); } void cClientHandle::SendParticleEffect(const AString & a_ParticleName, float a_SrcX, float a_SrcY, float a_SrcZ, float a_OffsetX, float a_OffsetY, float a_OffsetZ, float a_ParticleData, int a_ParticleAmount) { m_Protocol.SendParticleEffect(a_ParticleName, a_SrcX, a_SrcY, a_SrcZ, a_OffsetX, a_OffsetY, a_OffsetZ, a_ParticleData, a_ParticleAmount); } void cClientHandle::SendParticleEffect(const AString & a_ParticleName, const Vector3f a_Src, const Vector3f a_Offset, float a_ParticleData, int a_ParticleAmount, std::array a_Data) { m_Protocol.SendParticleEffect(a_ParticleName, a_Src, a_Offset, a_ParticleData, a_ParticleAmount, a_Data); } void cClientHandle::SendPickupSpawn(const cPickup & a_Pickup) { m_Protocol.SendPickupSpawn(a_Pickup); } void cClientHandle::SendPaintingSpawn(const cPainting & a_Painting) { m_Protocol.SendPaintingSpawn(a_Painting); } void cClientHandle::SendEntityAnimation(const cEntity & a_Entity, char a_Animation) { m_Protocol.SendEntityAnimation(a_Entity, a_Animation); } void cClientHandle::SendPlayerAbilities() { m_Protocol.SendPlayerAbilities(); } void cClientHandle::SendPlayerListAddPlayer(const cPlayer & a_Player) { m_Protocol.SendPlayerListAddPlayer(a_Player); } void cClientHandle::SendPlayerListRemovePlayer(const cPlayer & a_Player) { m_Protocol.SendPlayerListRemovePlayer(a_Player); } void cClientHandle::SendPlayerListUpdateGameMode(const cPlayer & a_Player) { m_Protocol.SendPlayerListUpdateGameMode(a_Player); } void cClientHandle::SendPlayerListUpdatePing(const cPlayer & a_Player) { m_Protocol.SendPlayerListUpdatePing(a_Player); } void cClientHandle::SendPlayerListUpdateDisplayName(const cPlayer & a_Player, const AString & a_CustomName) { m_Protocol.SendPlayerListUpdateDisplayName(a_Player, a_CustomName); } void cClientHandle::SendPlayerMaxSpeed(void) { m_Protocol.SendPlayerMaxSpeed(); } void cClientHandle::SendPlayerMoveLook(void) { /* LOGD("Sending PlayerMoveLook: {%0.2f, %0.2f, %0.2f}, stance %0.2f, OnGround: %d", m_Player->GetPosX(), m_Player->GetPosY(), m_Player->GetPosZ(), m_Player->GetStance(), m_Player->IsOnGround() ? 1 : 0 ); */ m_Protocol.SendPlayerMoveLook(); } void cClientHandle::SendPlayerPosition(void) { m_Protocol.SendPlayerPosition(); } void cClientHandle::SendPlayerSpawn(const cPlayer & a_Player) { if (a_Player.GetUniqueID() == m_Player->GetUniqueID()) { // Do NOT send this packet to myself return; } LOGD("Spawning player \"%s\" on client \"%s\" @ %s", a_Player.GetName().c_str(), m_Player->GetName().c_str(), GetIPString().c_str() ); m_Protocol.SendPlayerSpawn(a_Player); } void cClientHandle::SendPluginMessage(const AString & a_Channel, const AString & a_Message) { m_Protocol.SendPluginMessage(a_Channel, a_Message); } void cClientHandle::SendRemoveEntityEffect(const cEntity & a_Entity, int a_EffectID) { m_Protocol.SendRemoveEntityEffect(a_Entity, a_EffectID); } void cClientHandle::SendResetTitle() { m_Protocol.SendResetTitle(); } void cClientHandle::SendRespawn(eDimension a_Dimension, bool a_ShouldIgnoreDimensionChecks) { m_Protocol.SendRespawn(a_Dimension, a_ShouldIgnoreDimensionChecks); } void cClientHandle::SendExperience(void) { m_Protocol.SendExperience(); } void cClientHandle::SendExperienceOrb(const cExpOrb & a_ExpOrb) { m_Protocol.SendExperienceOrb(a_ExpOrb); } void cClientHandle::SendScoreboardObjective(const AString & a_Name, const AString & a_DisplayName, Byte a_Mode) { m_Protocol.SendScoreboardObjective(a_Name, a_DisplayName, a_Mode); } void cClientHandle::SendScoreUpdate(const AString & a_Objective, const AString & a_Player, cObjective::Score a_Score, Byte a_Mode) { m_Protocol.SendScoreUpdate(a_Objective, a_Player, a_Score, a_Mode); } void cClientHandle::SendDisplayObjective(const AString & a_Objective, cScoreboard::eDisplaySlot a_Display) { m_Protocol.SendDisplayObjective(a_Objective, a_Display); } 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); } void cClientHandle::SendSoundParticleEffect(const EffectID a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data) { m_Protocol.SendSoundParticleEffect(a_EffectID, a_SrcX, a_SrcY, a_SrcZ, a_Data); } void cClientHandle::SendSpawnFallingBlock(const cFallingBlock & a_FallingBlock) { m_Protocol.SendSpawnFallingBlock(a_FallingBlock); } void cClientHandle::SendSpawnMob(const cMonster & a_Mob) { m_Protocol.SendSpawnMob(a_Mob); } void cClientHandle::SendSpawnObject(const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) { m_Protocol.SendSpawnObject(a_Entity, a_ObjectType, a_ObjectData, a_Yaw, a_Pitch); } void cClientHandle::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleType, char a_VehicleSubType) // VehicleSubType is specific to Minecarts { m_Protocol.SendSpawnVehicle(a_Vehicle, a_VehicleType, a_VehicleSubType); } void cClientHandle::SendStatistics(const cStatManager & a_Manager) { m_Protocol.SendStatistics(a_Manager); } void cClientHandle::SendTabCompletionResults(const AStringVector & a_Results) { m_Protocol.SendTabCompletionResults(a_Results); } void cClientHandle::SendTeleportEntity(const cEntity & a_Entity) { m_Protocol.SendTeleportEntity(a_Entity); } void cClientHandle::SendThunderbolt(int a_BlockX, int a_BlockY, int a_BlockZ) { m_Protocol.SendThunderbolt(a_BlockX, a_BlockY, 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); } void cClientHandle::SendUpdateBlockEntity(cBlockEntity & a_BlockEntity) { m_Protocol.SendUpdateBlockEntity(a_BlockEntity); } void cClientHandle::SendUpdateSign( int a_BlockX, int a_BlockY, int a_BlockZ, const AString & a_Line1, const AString & a_Line2, const AString & a_Line3, const AString & a_Line4 ) { m_Protocol.SendUpdateSign( a_BlockX, a_BlockY, a_BlockZ, a_Line1, a_Line2, a_Line3, a_Line4 ); } void cClientHandle::SendUseBed(const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ) { m_Protocol.SendUseBed(a_Entity, a_BlockX, a_BlockY, a_BlockZ); } void cClientHandle::SendWeather(eWeather a_Weather) { m_Protocol.SendWeather(a_Weather); } void cClientHandle::SendWholeInventory(const cWindow & a_Window) { m_Protocol.SendWholeInventory(a_Window); } void cClientHandle::SendWindowClose(const cWindow & a_Window) { m_Protocol.SendWindowClose(a_Window); } void cClientHandle::SendWindowOpen(const cWindow & a_Window) { m_Protocol.SendWindowOpen(a_Window); } void cClientHandle::SendWindowProperty(const cWindow & a_Window, short a_Property, short a_Value) { m_Protocol.SendWindowProperty(a_Window, a_Property, a_Value); } const AString & cClientHandle::GetUsername(void) const { return m_Username; } void cClientHandle::SetUsername( const AString & a_Username) { m_Username = a_Username; } void cClientHandle::SetViewDistance(int a_ViewDistance) { ASSERT(cRoot::Get()->GetServer()->IsInTickThread()); ASSERT(m_Player != nullptr); m_RequestedViewDistance = a_ViewDistance; LOGD("%s is requesting ViewDistance of %d!", GetUsername().c_str(), m_RequestedViewDistance); cWorld * World = m_Player->GetWorld(); World->QueueTask( [a_ViewDistance, ClientHandle = shared_from_this()](cWorld & a_World) { ClientHandle->m_CurrentViewDistance = Clamp(a_ViewDistance, cClientHandle::MIN_VIEW_DISTANCE, a_World.GetMaxViewDistance()); } ); } bool cClientHandle::HasPluginChannel(const AString & a_PluginChannel) { return (m_PluginChannels.find(a_PluginChannel) != m_PluginChannels.end()); } 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"); } void cClientHandle::PacketUnknown(UInt32 a_PacketType) { LOGERROR("Unknown packet type 0x%x from client \"%s\" @ %s", a_PacketType, m_Username.c_str(), m_IPString.c_str()); AString Reason; Printf(Reason, "Unknown [C->S] PacketType: 0x%x", a_PacketType); SendDisconnect(Reason); } 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"); } void cClientHandle::SocketClosed(void) { // The socket has been closed for any reason cRoot::Get()->GetServer()->QueueTask( [ClientHandle = shared_from_this()] { if (!ClientHandle->GetUsername().empty()) // Ignore client pings { LOGD("Client %s @ %s disconnected", ClientHandle->GetUsername().c_str(), ClientHandle->GetIPString().c_str()); cRoot::Get()->GetPluginManager()->CallHookDisconnect(*ClientHandle, "Player disconnected"); } ClientHandle->Destroy(); } ); } void cClientHandle::OnLinkCreated(cTCPLinkPtr a_Link) { cCSLock Lock(m_CSLink); m_Link = a_Link; } void cClientHandle::OnReceivedData(const char * a_Data, size_t a_Length) { // Reset the timeout: m_TicksSinceLastPacket = 0; // Queue the incoming data to be processed in the tick thread: cCSLock Lock(m_CSIncomingData); m_IncomingData.append(a_Data, a_Length); } void cClientHandle::OnRemoteClosed(void) { SocketClosed(); } void cClientHandle::OnError(int a_ErrorCode, const AString & a_ErrorMsg) { LOGD("An error has occurred on client link for %s @ %s: %d (%s). Client disconnected.", m_Username.c_str(), m_IPString.c_str(), a_ErrorCode, a_ErrorMsg.c_str() ); SocketClosed(); }