2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
|
|
|
#include "cClientHandle.h"
|
|
|
|
#include "cServer.h"
|
|
|
|
#include "cWorld.h"
|
|
|
|
#include "cPickup.h"
|
|
|
|
#include "cPluginManager.h"
|
|
|
|
#include "cPlayer.h"
|
|
|
|
#include "cInventory.h"
|
|
|
|
#include "cChestEntity.h"
|
|
|
|
#include "cSignEntity.h"
|
|
|
|
#include "cWindow.h"
|
|
|
|
#include "cCraftingWindow.h"
|
|
|
|
#include "cItem.h"
|
|
|
|
#include "cTorch.h"
|
|
|
|
#include "cDoors.h"
|
|
|
|
#include "cLadder.h"
|
2012-06-15 11:55:53 -04:00
|
|
|
#include "cVine.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
#include "cSign.h"
|
|
|
|
#include "cRedstone.h"
|
|
|
|
#include "cPiston.h"
|
|
|
|
#include "cBlockToPickup.h"
|
|
|
|
#include "cMonster.h"
|
|
|
|
#include "cChatColor.h"
|
|
|
|
#include "cSocket.h"
|
|
|
|
#include "cTimer.h"
|
2012-07-15 16:36:34 -04:00
|
|
|
#include "items/Item.h"
|
|
|
|
#include "blocks/Block.h"
|
2012-08-26 17:01:07 -04:00
|
|
|
#include "ChunkDataSerializer.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "cTracer.h"
|
|
|
|
#include "Vector3f.h"
|
|
|
|
#include "Vector3d.h"
|
|
|
|
|
|
|
|
#include "cSleep.h"
|
|
|
|
#include "cRoot.h"
|
|
|
|
|
|
|
|
#include "cBlockingTCPLink.h"
|
|
|
|
#include "cAuthenticator.h"
|
|
|
|
#include "MersenneTwister.h"
|
|
|
|
|
2012-08-27 13:31:16 -04:00
|
|
|
#include "Protocol125.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define AddPistonDir(x, y, z, dir, amount) switch (dir) { case 0: (y)-=(amount); break; case 1: (y)+=(amount); break;\
|
|
|
|
case 2: (z)-=(amount); break; case 3: (z)+=(amount); break;\
|
|
|
|
case 4: (x)-=(amount); break; case 5: (x)+=(amount); break; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// If the number of queued outgoing packets reaches this, the client will be kicked
|
|
|
|
#define MAX_OUTGOING_PACKETS 2000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define RECI_RAND_MAX (1.f/RAND_MAX)
|
|
|
|
inline int fRadRand(MTRand & r1, int a_BlockCoord)
|
|
|
|
{
|
|
|
|
return a_BlockCoord * 32 + (int)(16 * ((float)r1.rand() * RECI_RAND_MAX) * 16 - 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cClientHandle::s_ClientCount = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cClientHandle:
|
|
|
|
|
|
|
|
cClientHandle::cClientHandle(const cSocket & a_Socket, int a_ViewDistance)
|
|
|
|
: m_ViewDistance(a_ViewDistance)
|
|
|
|
, m_ProtocolVersion(MCS_PROTOCOL_VERSION)
|
|
|
|
, m_Socket(a_Socket)
|
2012-08-27 13:31:16 -04:00
|
|
|
, m_OutgoingData(64 KiB)
|
2012-06-14 09:06:06 -04:00
|
|
|
, m_bDestroyed(false)
|
|
|
|
, m_Player(NULL)
|
|
|
|
, m_bKicking(false)
|
|
|
|
, m_TimeLastPacket(cWorld::GetTime())
|
|
|
|
, m_bKeepThreadGoing(true)
|
|
|
|
, m_Ping(1000)
|
|
|
|
, m_State(csConnected)
|
|
|
|
, m_LastStreamedChunkX(0x7fffffff) // bogus chunk coords to force streaming upon login
|
|
|
|
, m_LastStreamedChunkZ(0x7fffffff)
|
|
|
|
, m_UniqueID(0)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol = new cProtocol125(this);
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
s_ClientCount++; // Not protected by CS because clients are always constructed from the same thread
|
|
|
|
m_UniqueID = s_ClientCount;
|
|
|
|
|
|
|
|
cTimer t1;
|
|
|
|
m_LastPingTime = t1.GetNowTime();
|
|
|
|
|
2012-08-27 13:31:16 -04:00
|
|
|
LOGD("New ClientHandle created at %p", this);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cClientHandle::~cClientHandle()
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
LOGD("Deleting client \"%s\" at %p", GetUsername().c_str(), this);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Remove from cSocketThreads, we're not to be called anymore:
|
|
|
|
cRoot::Get()->GetServer()->ClientDestroying(this);
|
|
|
|
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSChunkLists);
|
|
|
|
m_LoadedChunks.clear();
|
|
|
|
m_ChunksToSend.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_Player != NULL)
|
|
|
|
{
|
|
|
|
cWorld * World = m_Player->GetWorld();
|
|
|
|
if (!m_Username.empty() && (World != NULL))
|
|
|
|
{
|
|
|
|
// Send the Offline PlayerList packet:
|
2012-08-27 13:31:16 -04:00
|
|
|
World->BroadcastPlayerListItem(*m_Player, false, this);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Send the Chat packet:
|
2012-08-27 13:31:16 -04:00
|
|
|
AString Left(m_Username + " left the game!");
|
|
|
|
World->BroadcastChat(Left, this);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
if (World != NULL)
|
|
|
|
{
|
|
|
|
World->RemovePlayer(m_Player);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_Socket.IsValid())
|
|
|
|
{
|
2012-08-25 17:59:13 -04:00
|
|
|
if (!m_bKicking)
|
2012-06-16 04:53:38 -04:00
|
|
|
{
|
2012-08-25 17:59:13 -04:00
|
|
|
SendDisconnect("Server shut down? Kthnxbai");
|
2012-06-16 04:53:38 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_Player != NULL)
|
|
|
|
{
|
|
|
|
m_Player->Destroy();
|
|
|
|
m_Player = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Queue all remaining outgoing packets to cSocketThreads:
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
cCSLock Lock(m_CSOutgoingData);
|
|
|
|
AString Data;
|
|
|
|
m_OutgoingData.ReadAll(Data);
|
|
|
|
m_OutgoingData.CommitRead();
|
|
|
|
cRoot::Get()->GetServer()->WriteToClient(&m_Socket, Data);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Queue the socket to close as soon as it sends all outgoing data:
|
|
|
|
cRoot::Get()->GetServer()->QueueClientClose(&m_Socket);
|
|
|
|
|
|
|
|
// We need to remove the socket from SocketThreads because we own it and it gets destroyed after this destructor finishes
|
|
|
|
// TODO: The socket needs to stay alive, someone else has to own it
|
|
|
|
cRoot::Get()->GetServer()->RemoveClient(&m_Socket);
|
|
|
|
|
2012-08-27 13:31:16 -04:00
|
|
|
delete m_Protocol;
|
|
|
|
m_Protocol = NULL;
|
|
|
|
|
|
|
|
LOGD("ClientHandle at %p deleted", this);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::Destroy()
|
|
|
|
{
|
|
|
|
// Setting m_bDestroyed was moved to the bottom of Destroy(),
|
|
|
|
// otherwise the destructor may be called within another thread before the client is removed from chunks
|
|
|
|
// http://forum.mc-server.org/showthread.php?tid=366
|
|
|
|
|
|
|
|
if ((m_Player != NULL) && (m_Player->GetWorld() != NULL))
|
|
|
|
{
|
|
|
|
RemoveFromAllChunks();
|
|
|
|
m_Player->GetWorld()->RemoveClientFromChunkSender(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_bDestroyed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::Kick(const AString & a_Reason)
|
|
|
|
{
|
|
|
|
if (m_State >= csAuthenticating) // Don't log pings
|
|
|
|
{
|
|
|
|
LOG("Kicking user \"%s\" for \"%s\"", m_Username.c_str(), a_Reason.c_str());
|
|
|
|
}
|
2012-08-25 17:59:13 -04:00
|
|
|
SendDisconnect(a_Reason);
|
2012-06-14 09:06:06 -04:00
|
|
|
m_bKicking = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::Authenticate(void)
|
|
|
|
{
|
|
|
|
if (m_State != csAuthenticating)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT( m_Player == NULL );
|
|
|
|
|
|
|
|
// Spawn player (only serversided, so data is loaded)
|
|
|
|
m_Player = new cPlayer(this, GetUsername());
|
|
|
|
|
|
|
|
cWorld * World = cRoot::Get()->GetWorld(m_Player->GetLoadedWorldName());
|
|
|
|
if (World == NULL)
|
|
|
|
{
|
|
|
|
World = cRoot::Get()->GetDefaultWorld();
|
|
|
|
}
|
|
|
|
|
2012-08-27 13:31:16 -04:00
|
|
|
if (m_Player->GetGameMode() == eGameMode_NotSet)
|
|
|
|
{
|
2012-07-13 11:26:27 -04:00
|
|
|
m_Player->LoginSetGameMode(World->GetGameMode());
|
2012-08-27 13:31:16 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
m_Player->SetIP (m_Socket.GetIPString());
|
|
|
|
|
2012-06-15 11:55:53 -04:00
|
|
|
cRoot::Get()->GetPluginManager()->CallHook(cPluginManager::HOOK_PLAYER_SPAWN, 1, m_Player);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Return a server login packet
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendLogin(*m_Player);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Send Weather if raining:
|
|
|
|
if ((World->GetWeather() == 1) || (World->GetWeather() == 2))
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendWeather(World->GetWeather());
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send time
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendTimeUpdate(World->GetWorldTime());
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Send inventory
|
|
|
|
m_Player->GetInventory().SendWholeInventory(this);
|
|
|
|
|
|
|
|
// Send health
|
2012-07-17 08:02:03 -04:00
|
|
|
m_Player->SendHealth();
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
m_Player->Initialize(World);
|
|
|
|
StreamChunks();
|
|
|
|
m_State = csDownloadingWorld;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::StreamChunks(void)
|
|
|
|
{
|
|
|
|
if (m_State < csAuthenticating)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(m_Player != NULL);
|
|
|
|
|
|
|
|
int ChunkPosX = FAST_FLOOR_DIV(m_Player->GetPosX(), cChunkDef::Width);
|
|
|
|
int ChunkPosZ = FAST_FLOOR_DIV(m_Player->GetPosZ(), cChunkDef::Width);
|
|
|
|
if ((ChunkPosX == m_LastStreamedChunkX) && (ChunkPosZ == m_LastStreamedChunkZ))
|
|
|
|
{
|
|
|
|
// Already streamed for this position
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Remove all loaded chunks that are no longer in range; deferred to out-of-CS:
|
|
|
|
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 itr - m_LoadedChunks[]
|
|
|
|
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 itr - m_ChunksToSend[]
|
|
|
|
}
|
|
|
|
for (cChunkCoordsList::iterator itr = RemoveChunks.begin(); itr != RemoveChunks.end(); ++itr)
|
|
|
|
{
|
|
|
|
World->RemoveChunkClient(itr->m_ChunkX, itr->m_ChunkY, itr->m_ChunkZ, this);
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendUnloadChunk(itr->m_ChunkX, itr->m_ChunkZ);
|
2012-06-14 09:06:06 -04:00
|
|
|
} // for itr - RemoveChunks[]
|
|
|
|
|
|
|
|
// Add all chunks that are in range and not yet in m_LoadedChunks:
|
|
|
|
// Queue these smartly - from the center out to the edge
|
|
|
|
for (int d = 0; d <= m_ViewDistance; ++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)
|
|
|
|
{
|
|
|
|
StreamChunk(ChunkPosX + d, ZERO_CHUNK_Y, ChunkPosZ + i);
|
|
|
|
StreamChunk(ChunkPosX - d, ZERO_CHUNK_Y, ChunkPosZ + i);
|
|
|
|
} // for i
|
|
|
|
for (int i = -d + 1; i < d; ++i)
|
|
|
|
{
|
|
|
|
StreamChunk(ChunkPosX + i, ZERO_CHUNK_Y, ChunkPosZ + d);
|
|
|
|
StreamChunk(ChunkPosX + i, ZERO_CHUNK_Y, ChunkPosZ - d);
|
|
|
|
} // for i
|
|
|
|
} // for d
|
|
|
|
|
|
|
|
// Touch chunks GENERATEDISTANCE ahead to let them generate:
|
|
|
|
for (int d = m_ViewDistance + 1; d <= m_ViewDistance + GENERATEDISTANCE; ++d) // cycle through (square) distance, from nearest to furthest
|
|
|
|
{
|
|
|
|
// For each distance touch chunks in a hollow square centered around current position:
|
|
|
|
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 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);
|
|
|
|
} // for i
|
|
|
|
} // for d
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::StreamChunk(int a_ChunkX, int a_ChunkY, int a_ChunkZ)
|
|
|
|
{
|
|
|
|
cWorld * World = m_Player->GetWorld();
|
|
|
|
ASSERT(World != NULL);
|
|
|
|
|
|
|
|
if (World->AddChunkClient(a_ChunkX, a_ChunkY, a_ChunkZ, this))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSChunkLists);
|
|
|
|
m_LoadedChunks.push_back(cChunkCoords(a_ChunkX, a_ChunkY, a_ChunkZ));
|
|
|
|
m_ChunksToSend.push_back(cChunkCoords(a_ChunkX, a_ChunkY, a_ChunkZ));
|
|
|
|
}
|
|
|
|
World->SendChunkTo(a_ChunkX, a_ChunkY, a_ChunkZ, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Removes the client from all chunks. Used when switching worlds or destroying the player
|
|
|
|
void cClientHandle::RemoveFromAllChunks()
|
|
|
|
{
|
|
|
|
cWorld * World = m_Player->GetWorld();
|
|
|
|
if (World != NULL)
|
|
|
|
{
|
|
|
|
World->RemoveClientFromChunks(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSChunkLists);
|
|
|
|
m_LoadedChunks.clear();
|
|
|
|
m_ChunksToSend.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::HandlePing(void)
|
|
|
|
{
|
|
|
|
// Somebody tries to retrieve information about the server
|
|
|
|
AString Reply;
|
|
|
|
Printf(Reply, "%s%s%i%s%i",
|
|
|
|
cRoot::Get()->GetDefaultWorld()->GetDescription().c_str(),
|
|
|
|
cChatColor::Delimiter.c_str(),
|
|
|
|
cRoot::Get()->GetDefaultWorld()->GetNumPlayers(),
|
|
|
|
cChatColor::Delimiter.c_str(),
|
|
|
|
cRoot::Get()->GetDefaultWorld()->GetMaxPlayers()
|
|
|
|
);
|
|
|
|
Kick(Reply.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
void cClientHandle::HandleHandshake(const AString & a_Username)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
AStringVector UserData = StringSplit(a_Username, ";"); // "FakeTruth;localhost:25565"
|
|
|
|
if (UserData.empty())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
Kick("Did not receive username");
|
2012-06-14 09:06:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_Username = UserData[0];
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
LOGD("HANDSHAKE %s", m_Username.c_str());
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
if (cRoot::Get()->GetDefaultWorld()->GetNumPlayers() >= cRoot::Get()->GetDefaultWorld()->GetMaxPlayers())
|
|
|
|
{
|
|
|
|
Kick("The server is currently full :(-- Try again later");
|
|
|
|
return;
|
|
|
|
}
|
2012-08-27 13:31:16 -04:00
|
|
|
cRoot::Get()->GetServer()->BroadcastChat(m_Username + " is connecting.", this);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
SendHandshake(cRoot::Get()->GetServer()->GetServerID());
|
2012-08-27 13:31:16 -04:00
|
|
|
LOGD("User \"%s\" was sent a handshake", m_Username.c_str());
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
void cClientHandle::HandleLogin(int a_ProtocolVersion, const AString & a_Username)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
LOGD("LOGIN %s", a_Username.c_str());
|
|
|
|
if (a_ProtocolVersion < m_ProtocolVersion)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
Kick("Your client is outdated!");
|
|
|
|
return;
|
|
|
|
}
|
2012-08-18 05:56:28 -04:00
|
|
|
else if (a_ProtocolVersion > m_ProtocolVersion)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
Kick("Your client version is higher than the server!");
|
|
|
|
return;
|
|
|
|
}
|
2012-08-18 05:56:28 -04:00
|
|
|
if (m_Username.compare(a_Username) != 0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
LOGWARNING("Login Username (\"%s\") does not match Handshake username (\"%s\") for client @ \"%s\")",
|
|
|
|
a_Username.c_str(),
|
2012-06-14 09:06:06 -04:00
|
|
|
m_Username.c_str(),
|
|
|
|
m_Socket.GetIPString().c_str()
|
|
|
|
);
|
|
|
|
Kick("Hacked client"); // Don't tell them why we don't want them
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
if (cRoot::Get()->GetPluginManager()->CallHookLogin(this, a_ProtocolVersion, a_Username))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Schedule for authentication; until then, let them wait (but do not block)
|
|
|
|
m_State = csAuthenticating;
|
|
|
|
cRoot::Get()->GetAuthenticator().Authenticate(GetUniqueID(), GetUsername(), cRoot::Get()->GetServer()->GetServerID());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
void cClientHandle::HandleUnexpectedPacket(int a_PacketType)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
LOGWARNING(
|
|
|
|
"Invalid packet in state %d: 0x%02x from client \"%s\", username \"%s\"",
|
|
|
|
m_State,
|
2012-08-18 05:56:28 -04:00
|
|
|
a_PacketType,
|
2012-06-14 09:06:06 -04:00
|
|
|
m_Socket.GetIPString().c_str(),
|
|
|
|
m_Username.c_str()
|
|
|
|
);
|
|
|
|
Kick("Hacked client"); // Don't tell them why we don't like them
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
void cClientHandle::HandleMoveLookConfirm(double a_PosX, double a_PosY, double a_PosZ)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
Vector3d ReceivedPosition = Vector3d(a_PosX, a_PosY, a_PosZ);
|
2012-08-19 15:42:32 -04:00
|
|
|
// LOGD("Received MoveLook confirmation: {%0.2f %0.2f %0.2f}", a_PosX, a_PosY, a_PosZ);
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// Test the distance between points with a small/large enough value instead of comparing directly. Floating point inaccuracies might screw stuff up
|
|
|
|
double Dist = (ReceivedPosition - m_ConfirmPosition).SqrLength();
|
|
|
|
if (Dist < 1.0)
|
|
|
|
{
|
|
|
|
// Test
|
|
|
|
if (ReceivedPosition.Equals(m_ConfirmPosition))
|
|
|
|
{
|
|
|
|
LOGINFO("Exact position confirmed by client!");
|
|
|
|
}
|
|
|
|
m_State = csPlaying;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOGWARNING("Player \"%s\" sent a weird position confirmation %.2f blocks away, retrying", m_Username.c_str(), Dist);
|
|
|
|
m_ConfirmPosition = m_Player->GetPosition();
|
2012-08-19 15:42:32 -04:00
|
|
|
SendPlayerMoveLook();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
void cClientHandle::HandleCreativeInventory(short a_SlotNum, const cItem & a_HeldItem)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
// This is for creative Inventory changes
|
|
|
|
if (m_Player->GetGameMode() == 1)
|
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
m_Player->GetInventory().Clicked(a_SlotNum, false, false, a_HeldItem);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOGWARNING("Got a CreativeInventoryAction packet from user \"%s\" while not in creative mode. Ignoring.", m_Username.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
void cClientHandle::HandlePlayerPos(double a_PosX, double a_PosY, double a_PosZ, double a_Stance, bool a_IsOnGround)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-19 15:42:32 -04:00
|
|
|
/*
|
|
|
|
// TODO: Invalid stance check
|
|
|
|
if ((a_PosY >= a_Stance) || (a_Stance > a_PosY + 1.65))
|
|
|
|
{
|
|
|
|
LOGD("Invalid stance");
|
|
|
|
SendPlayerMoveLook();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// LOGD("recv player pos: {%0.2f %0.2f %0.2f}, ground: %d", a_PosX, a_PosY, a_PosZ, a_IsOnGround ? 1 : 0);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
m_Player->MoveTo(Pos);
|
2012-08-18 05:56:28 -04:00
|
|
|
m_Player->SetStance(a_Stance);
|
|
|
|
m_Player->SetTouchGround(a_IsOnGround);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
void cClientHandle::HandleBlockDig(int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
if (!CheckBlockInteractionsRate())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
LOGD("OnBlockDig: {%i, %i, %i}; Face: %i; Stat: %i",
|
|
|
|
a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_Status
|
2012-06-14 09:06:06 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
// Do we want plugins to disable tossing items? Probably no, so toss item before asking plugins for permission
|
2012-08-18 05:56:28 -04:00
|
|
|
if (a_Status == DIG_STATUS_DROP_HELD) // Drop held item
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
m_Player->TossItem(false);
|
|
|
|
return;
|
|
|
|
}
|
2012-07-15 16:36:34 -04:00
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
if (a_Status == DIG_STATUS_SHOOT_EAT)
|
2012-07-15 16:36:34 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
LOGINFO("BlockDig: Status SHOOT/EAT not implemented");
|
|
|
|
return;
|
2012-07-15 16:36:34 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
cWorld * World = m_Player->GetWorld();
|
|
|
|
BLOCKTYPE OldBlock;
|
|
|
|
NIBBLETYPE OldMeta;
|
|
|
|
World->GetBlockTypeMeta(a_BlockX, a_BlockY, a_BlockZ, OldBlock, OldMeta);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
if (cRoot::Get()->GetPluginManager()->CallHookBlockDig(m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_Status, OldBlock, OldMeta))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
// The plugin doesn't agree with the digging, replace the block on the client and quit:
|
2012-08-18 05:56:28 -04:00
|
|
|
World->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player);
|
2012-06-14 09:06:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bBroken = (
|
2012-08-18 05:56:28 -04:00
|
|
|
(a_Status == DIG_STATUS_FINISHED) ||
|
2012-06-14 09:06:06 -04:00
|
|
|
(g_BlockOneHitDig[(int)OldBlock]) ||
|
2012-08-18 05:56:28 -04:00
|
|
|
((a_Status == DIG_STATUS_STARTED) && (m_Player->GetGameMode() == 1))
|
2012-06-14 09:06:06 -04:00
|
|
|
);
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
cItem & Equipped = m_Player->GetInventory().GetEquippedItem();
|
|
|
|
cItemHandler * ItemHandler = cItemHandler::GetItemHandler(Equipped.m_ItemID);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
if (bBroken)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
ItemHandler->OnBlockDestroyed(World, m_Player, &Equipped, a_BlockX, a_BlockY, a_BlockZ);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
BlockHandler(OldBlock)->OnDestroyedByPlayer(World, m_Player, a_BlockX, a_BlockY, a_BlockZ);
|
|
|
|
World->DigBlock(a_BlockX, a_BlockY, a_BlockZ);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cBlockHandler * Handler = cBlockHandler::GetBlockHandler(OldBlock);
|
2012-08-19 06:44:19 -04:00
|
|
|
Handler->OnDigging(World, m_Player, a_BlockX, a_BlockY, a_BlockZ);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
ItemHandler->OnDiggingBlock(World, m_Player, &Equipped, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace);
|
2012-07-15 16:36:34 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
// Check for clickthrough-blocks:
|
|
|
|
int pX = a_BlockX;
|
|
|
|
unsigned char pY = a_BlockY;
|
|
|
|
int pZ = a_BlockZ;
|
|
|
|
AddDirection(pX, pY, pZ, a_BlockFace);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-07-15 16:36:34 -04:00
|
|
|
Handler = cBlockHandler::GetBlockHandler(World->GetBlock(pX, pY, pZ));
|
2012-08-18 05:56:28 -04:00
|
|
|
if (Handler->IsClickedThrough())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-19 06:44:19 -04:00
|
|
|
Handler->OnDigging(World, m_Player, pX, pY, pZ);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
void cClientHandle::HandleBlockPlace(int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, const cItem & a_HeldItem)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
if (!CheckBlockInteractionsRate())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cItem & Equipped = m_Player->GetInventory().GetEquippedItem();
|
|
|
|
|
2012-08-19 07:51:17 -04:00
|
|
|
if ((Equipped.m_ItemType != a_HeldItem.m_ItemType)) // Not valid
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
LOGWARN("Player %s tried to place a block that was not equipped (exp %d, got %d)",
|
2012-08-18 05:56:28 -04:00
|
|
|
m_Username.c_str(), Equipped.m_ItemType, a_HeldItem.m_ItemType
|
2012-06-14 09:06:06 -04:00
|
|
|
);
|
2012-08-18 05:56:28 -04:00
|
|
|
|
|
|
|
// 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 > -1)
|
|
|
|
{
|
|
|
|
AddDirection(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace);
|
|
|
|
m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player);
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
if (cRoot::Get()->GetPluginManager()->CallHookBlockPlace(m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_HeldItem))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
if (a_BlockFace > -1)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
AddDirection(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace);
|
|
|
|
m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2012-07-15 16:36:34 -04:00
|
|
|
|
|
|
|
cWorld * World = m_Player->GetWorld();
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
cBlockHandler *Handler = cBlockHandler::GetBlockHandler(World->GetBlock(a_BlockX, a_BlockY, a_BlockZ));
|
2012-08-06 16:10:16 -04:00
|
|
|
if (Handler->IsUseable())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-19 06:44:19 -04:00
|
|
|
Handler->OnUse(World, m_Player, a_BlockX, a_BlockY, a_BlockZ);
|
2012-08-06 16:10:16 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
cItemHandler * ItemHandler = cItemHandler::GetItemHandler(Equipped.m_ItemID);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
if (ItemHandler->OnItemUse(World, m_Player, &Equipped, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
// Nothing here :P
|
|
|
|
}
|
|
|
|
else if (ItemHandler->IsPlaceable())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
if (a_BlockFace < 0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-07-15 16:36:34 -04:00
|
|
|
// clicked in air
|
|
|
|
return;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
BLOCKTYPE ClickedBlock = World->GetBlock(a_BlockX, a_BlockY, a_BlockZ);
|
2012-07-15 16:36:34 -04:00
|
|
|
cBlockHandler *Handler = cBlockHandler::GetBlockHandler(ClickedBlock);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-07-15 16:36:34 -04:00
|
|
|
if(Handler->IgnoreBuildCollision())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
Handler->OnDestroyedByPlayer(World, m_Player, a_BlockX, a_BlockY, a_BlockZ);
|
|
|
|
World->FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_AIR, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AddDirection(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace);
|
|
|
|
// Check for Blocks not allowing placement on top
|
|
|
|
if ((a_BlockFace == BLOCK_FACE_TOP) && !Handler->AllowBlockOnTop())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
// Resend the old block
|
|
|
|
// Some times the client still places the block O.o
|
2012-07-15 16:36:34 -04:00
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
World->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player);
|
2012-06-14 09:06:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
int PlaceBlock = m_Player->GetWorld()->GetBlock(a_BlockX, a_BlockY, a_BlockZ);
|
2012-07-15 16:36:34 -04:00
|
|
|
if (!BlockHandler(PlaceBlock)->IgnoreBuildCollision())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
// Tried to place a block *into* another?
|
|
|
|
return; // Happens when you place a block aiming at side of block like torch or stem
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
2012-07-15 16:36:34 -04:00
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
cBlockHandler * NewBlock = BlockHandler(ItemHandler->GetBlockType());
|
2012-07-15 16:36:34 -04:00
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
// Cannot be placed on the side of an other block
|
|
|
|
if ((a_BlockFace != BLOCK_FACE_TOP) && !NewBlock->CanBePlacedOnSide())
|
|
|
|
{
|
2012-07-15 16:36:34 -04:00
|
|
|
return;
|
2012-08-18 05:56:28 -04:00
|
|
|
}
|
2012-07-15 16:36:34 -04:00
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
if (NewBlock->CanBePlacedAt(World, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
ItemHandler->PlaceBlock(World, m_Player, &m_Player->GetInventory().GetEquippedItem(), a_BlockX, a_BlockY, a_BlockZ, a_BlockFace);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
World->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); // Send the old block back to the player
|
2012-07-15 16:36:34 -04:00
|
|
|
return;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2012-07-15 16:36:34 -04:00
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
}
|
|
|
|
else if (ItemHandler->IsFood())
|
|
|
|
{
|
2012-07-15 16:36:34 -04:00
|
|
|
cItem Item;
|
|
|
|
Item.m_ItemID = Equipped.m_ItemID;
|
|
|
|
Item.m_ItemCount = 1;
|
2012-07-17 08:02:03 -04:00
|
|
|
if (ItemHandler->EatItem(m_Player, &Item))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-07-15 16:36:34 -04:00
|
|
|
ItemHandler->OnFoodEaten(World, m_Player, &Item);
|
|
|
|
m_Player->GetInventory().RemoveItem(Item);
|
|
|
|
return;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-18 06:38:15 -04:00
|
|
|
void cClientHandle::HandleChat(const AString & a_Message)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 06:38:15 -04:00
|
|
|
if (!cRoot::Get()->GetServer()->Command(*this, a_Message))
|
|
|
|
{
|
|
|
|
AString Msg;
|
|
|
|
Printf(Msg, "<%s%s%s> %s",
|
|
|
|
m_Player->GetColor().c_str(),
|
|
|
|
m_Player->GetName().c_str(),
|
|
|
|
cChatColor::White.c_str(),
|
|
|
|
a_Message.c_str()
|
|
|
|
);
|
|
|
|
m_Player->GetWorld()->BroadcastChat(Msg);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 07:51:17 -04:00
|
|
|
void cClientHandle::HandlePlayerLook(float a_Rotation, float a_Pitch, bool a_IsOnGround)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-19 07:51:17 -04:00
|
|
|
m_Player->SetRotation (a_Rotation);
|
|
|
|
m_Player->SetPitch (a_Pitch);
|
|
|
|
m_Player->SetTouchGround(a_IsOnGround);
|
2012-06-14 09:06:06 -04:00
|
|
|
m_Player->WrapRotation();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 07:51:17 -04:00
|
|
|
void cClientHandle::HandlePlayerMoveLook(double a_PosX, double a_PosY, double a_PosZ, double a_Stance, float a_Rotation, float a_Pitch, bool a_IsOnGround)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-19 15:42:32 -04:00
|
|
|
/*
|
|
|
|
// TODO: Invalid stance check
|
|
|
|
if ((a_PosY >= a_Stance) || (a_Stance > a_PosY + 1.65))
|
|
|
|
{
|
|
|
|
LOGD("Invalid stance");
|
|
|
|
SendPlayerMoveLook();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*/
|
2012-08-27 13:31:16 -04:00
|
|
|
if (m_State != csPlaying)
|
|
|
|
{
|
|
|
|
// Ignore this packet unles the player is fully in:
|
|
|
|
return;
|
|
|
|
}
|
2012-08-19 15:42:32 -04:00
|
|
|
|
2012-08-19 07:51:17 -04:00
|
|
|
m_Player->MoveTo(Vector3d(a_PosX, a_PosY, a_PosZ));
|
|
|
|
m_Player->SetStance (a_Stance);
|
|
|
|
m_Player->SetTouchGround(a_IsOnGround);
|
|
|
|
m_Player->SetRotation (a_Rotation);
|
|
|
|
m_Player->SetPitch (a_Pitch);
|
2012-06-14 09:06:06 -04:00
|
|
|
m_Player->WrapRotation();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 07:51:17 -04:00
|
|
|
void cClientHandle::HandleAnimation(char a_Animation)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-19 07:51:17 -04:00
|
|
|
m_Player->GetWorld()->BroadcastPlayerAnimation(*m_Player, a_Animation, this);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 07:51:17 -04:00
|
|
|
void cClientHandle::HandleSlotSelected(short a_SlotNum)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-19 07:51:17 -04:00
|
|
|
m_Player->GetInventory().SetEquippedSlot(a_SlotNum);
|
|
|
|
m_Player->GetWorld()->BroadcastEntityEquipment(*m_Player, 0, m_Player->GetInventory().GetEquippedItem(), this);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 07:51:17 -04:00
|
|
|
void cClientHandle::HandleWindowClose(char a_WindowID)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-19 07:51:17 -04:00
|
|
|
m_Player->CloseWindow(a_WindowID);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 07:51:17 -04:00
|
|
|
void cClientHandle::HandleWindowClick(char a_WindowID, short a_SlotNum, bool a_IsRightClick, bool a_IsShiftPressed, const cItem & a_HeldItem)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
if (a_WindowID == 0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-18 05:56:28 -04:00
|
|
|
m_Player->GetInventory().Clicked(a_SlotNum, a_IsRightClick, a_IsShiftPressed, a_HeldItem);
|
2012-06-14 09:06:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cWindow * Window = m_Player->GetWindow();
|
|
|
|
if (Window == NULL)
|
|
|
|
{
|
|
|
|
LOGWARNING("Player \"%s\" clicked in a non-existent window. Ignoring", m_Username.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-18 05:56:28 -04:00
|
|
|
Window->Clicked(*m_Player, a_WindowID, a_SlotNum, a_IsRightClick, a_IsShiftPressed, a_HeldItem);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
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
|
|
|
|
)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
cWorld * World = m_Player->GetWorld();
|
2012-08-19 15:42:32 -04:00
|
|
|
World->UpdateSign(a_BlockX, a_BlockY, a_BlockZ, a_Line1, a_Line2, a_Line3, a_Line4);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
void cClientHandle::HandleUseEntity(int a_TargetEntityID, bool a_IsLeftClick)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-19 15:42:32 -04:00
|
|
|
if (!a_IsLeftClick)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-19 15:42:32 -04:00
|
|
|
// TODO: we don't handle right-clicking yet
|
2012-06-14 09:06:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
class cDamageEntity : public cEntityCallback
|
|
|
|
{
|
|
|
|
virtual bool Item(cEntity * a_Entity) override
|
|
|
|
{
|
2012-08-19 15:42:32 -04:00
|
|
|
if (a_Entity->IsA("cPawn"))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-19 15:42:32 -04:00
|
|
|
reinterpret_cast<cPawn *>(a_Entity)->TakeDamage(Damage, Instigator);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
int Damage;
|
|
|
|
cEntity * Instigator;
|
|
|
|
} Callback;
|
|
|
|
|
|
|
|
Callback.Damage = 1; // TODO: Find proper damage from current item equipped
|
|
|
|
Callback.Instigator = m_Player;
|
|
|
|
|
|
|
|
cWorld * World = m_Player->GetWorld();
|
2012-08-19 15:42:32 -04:00
|
|
|
World->DoWithEntityByID(a_TargetEntityID, Callback);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::HandleRespawn(void)
|
|
|
|
{
|
|
|
|
m_Player->Respawn();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
void cClientHandle::HandleDisconnect(const AString & a_Reason)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-19 15:42:32 -04:00
|
|
|
LOGD("Received d/c packet from \"%s\" with reason \"%s\"", m_Username.c_str(), a_Reason.c_str());
|
|
|
|
if (!cRoot::Get()->GetPluginManager()->CallHookDisconnect(m_Player, a_Reason))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-19 15:42:32 -04:00
|
|
|
AString DisconnectMessage;
|
|
|
|
Printf(DisconnectMessage, "%s disconnected: %s", m_Username.c_str(), a_Reason.c_str());
|
|
|
|
m_Player->GetWorld()->BroadcastChat(DisconnectMessage, this);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
void cClientHandle::HandleKeepAlive(int a_KeepAliveID)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-19 15:42:32 -04:00
|
|
|
if (a_KeepAliveID == m_PingID)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
cTimer t1;
|
|
|
|
m_Ping = (short)((t1.GetNowTime() - m_PingStartTime) / 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-27 13:31:16 -04:00
|
|
|
void cClientHandle::SendData(const char * a_Data, int a_Size)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSOutgoingData);
|
|
|
|
if (!m_OutgoingData.Write(a_Data, a_Size))
|
|
|
|
{
|
|
|
|
// Client has too much outgoing data queued, drop them silently:
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify SocketThreads that we have something to write:
|
|
|
|
cRoot::Get()->GetServer()->NotifyClientWrite(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
bool cClientHandle::CheckBlockInteractionsRate(void)
|
|
|
|
{
|
|
|
|
ASSERT(m_Player != NULL);
|
|
|
|
ASSERT(m_Player->GetWorld() != NULL);
|
|
|
|
int LastActionCnt = m_Player->GetLastBlockActionCnt();
|
|
|
|
if ((m_Player->GetWorld()->GetTime() - m_Player->GetLastBlockActionTime()) < 0.1)
|
|
|
|
{
|
|
|
|
// Limit the number of block interactions per tick
|
|
|
|
m_Player->SetLastBlockActionTime(); //Player tried to interact with a block. Reset last block interation time.
|
|
|
|
m_Player->SetLastBlockActionCnt(LastActionCnt + 1);
|
|
|
|
if (m_Player->GetLastBlockActionCnt() > MAXBLOCKCHANGEINTERACTIONS)
|
|
|
|
{
|
|
|
|
// Kick if more than MAXBLOCKCHANGEINTERACTIONS per tick
|
|
|
|
LOGWARN("Player %s tried to interact with a block too quickly! (could indicate bot) Was Kicked.", m_Username.c_str());
|
|
|
|
Kick("You're a baaaaaad boy!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Player->SetLastBlockActionCnt(0); // Reset count
|
|
|
|
m_Player->SetLastBlockActionTime(); // Player tried to interact with a block. Reset last block interation time.
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::Tick(float a_Dt)
|
|
|
|
{
|
|
|
|
(void)a_Dt;
|
|
|
|
if (cWorld::GetTime() - m_TimeLastPacket > 30.f) // 30 seconds time-out
|
|
|
|
{
|
2012-08-25 17:59:13 -04:00
|
|
|
SendDisconnect("Nooooo!! You timed out! D: Come back!");
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// TODO: Cannot sleep in the tick thread!
|
|
|
|
cSleep::MilliSleep(1000); // Give packet some time to be received
|
|
|
|
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
cTimer t1;
|
|
|
|
// Send ping packet
|
|
|
|
if (m_LastPingTime + cClientHandle::PING_TIME_MS <= t1.GetNowTime())
|
|
|
|
{
|
|
|
|
m_PingID++;
|
|
|
|
m_PingStartTime = t1.GetNowTime();
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendKeepAlive(m_PingID);
|
2012-06-14 09:06:06 -04:00
|
|
|
m_LastPingTime = m_PingStartTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-17 06:18:07 -04:00
|
|
|
void cClientHandle::SendDisconnect(const AString & a_Reason)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
LOGD("Sending a DC");
|
|
|
|
m_Protocol->SendDisconnect(a_Reason);
|
2012-08-18 05:56:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendHandshake(const AString & a_ServerName)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendHandshake(a_ServerName);
|
2012-08-18 05:56:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendInventorySlot(int a_WindowID, short a_SlotNum, const cItem & a_Item)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendInventorySlot(a_WindowID, a_SlotNum, a_Item);
|
2012-08-18 06:38:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendChat(const AString & a_Message)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendChat(a_Message);
|
2012-08-17 06:18:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 07:51:17 -04:00
|
|
|
void cClientHandle::SendPlayerAnimation(const cPlayer & a_Player, char a_Animation)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendPlayerAnimation(a_Player, a_Animation);
|
2012-08-19 07:51:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum, const cItem & a_Item)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendEntityEquipment(a_Entity, a_SlotNum, a_Item);
|
2012-08-19 07:51:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendWindowOpen(char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendWindowOpen(a_WindowID, a_WindowType, a_WindowTitle, a_NumSlots);
|
2012-08-19 07:51:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendWindowClose(char a_WindowID)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendWindowClose(a_WindowID);
|
2012-08-19 07:51:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendWholeInventory(const cInventory & a_Inventory)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendWholeInventory(a_Inventory);
|
2012-08-19 07:51:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendWholeInventory(const cWindow & a_Window)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendWholeInventory(a_Window);
|
2012-08-19 07:51:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
void cClientHandle::SendTeleportEntity(const cEntity & a_Entity)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendTeleportEntity(a_Entity);
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-27 13:31:16 -04:00
|
|
|
void cClientHandle::SendPlayerListItem(const cPlayer & a_Player, bool a_IsOnline)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendPlayerListItem(a_Player, a_IsOnline);
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendPlayerPosition(void)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendPlayerPosition();
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendRelEntMoveLook(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
|
|
|
|
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendRelEntMoveLook(a_Entity, a_RelX, a_RelY, a_RelZ);
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendRelEntMove(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
|
|
|
|
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendRelEntMove(a_Entity, a_RelX, a_RelY, a_RelZ);
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendEntLook(const cEntity & a_Entity)
|
|
|
|
{
|
|
|
|
ASSERT(a_Entity.GetUniqueID() != m_Player->GetUniqueID()); // Must not send for self
|
|
|
|
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendEntLook(a_Entity);
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendEntHeadLook(const cEntity & a_Entity)
|
|
|
|
{
|
|
|
|
ASSERT(a_Entity.GetUniqueID() != m_Player->GetUniqueID()); // Must not send for self
|
|
|
|
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendEntHeadLook(a_Entity);
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendBlockAction(int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendBlockAction(a_BlockX, a_BlockY, a_BlockZ, a_Byte1, a_Byte2);
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendHealth(void)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendHealth();
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendRespawn(void)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendRespawn();
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-27 13:31:16 -04:00
|
|
|
void cClientHandle::SendGameMode(eGameMode a_GameMode)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendGameMode(a_GameMode);
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendDestroyEntity(const cEntity & a_Entity)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendDestroyEntity(a_Entity);
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
*/
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendPlayerMoveLook();
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-19 17:14:45 -04:00
|
|
|
void cClientHandle::SendEntityStatus(const cEntity & a_Entity, char a_Status)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendEntityStatus(a_Entity, a_Status);
|
2012-08-19 17:14:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendMetadata(const cPawn & a_Pawn)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendMetadata(a_Pawn);
|
2012-08-19 17:14:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendInventoryProgress(char a_WindowID, short a_ProgressBar, short a_Value)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendInventoryProgress(a_WindowID, a_ProgressBar, a_Value);
|
2012-08-19 17:14:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-24 03:58:26 -04:00
|
|
|
void cClientHandle::SendPlayerSpawn(const cPlayer & a_Player)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
if (a_Player.GetUniqueID() == m_Player->GetUniqueID())
|
|
|
|
{
|
|
|
|
// Do NOT send this packet to myself
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Protocol->SendPlayerSpawn(a_Player);
|
2012-08-24 03:58:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendPickupSpawn(const cPickup & a_Pickup)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendPickupSpawn(a_Pickup);
|
2012-08-24 03:58:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendSpawnMob(const cMonster & a_Mob)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendSpawnMob(a_Mob);
|
2012-08-24 03:58:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendUpdateSign(
|
|
|
|
a_BlockX, a_BlockY, a_BlockZ,
|
|
|
|
a_Line1, a_Line2, a_Line3, a_Line4
|
|
|
|
);
|
2012-08-24 03:58:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-24 05:49:00 -04:00
|
|
|
void cClientHandle::SendCollectPickup(const cPickup & a_Pickup, const cPlayer & a_Player)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendCollectPickup(a_Pickup, a_Player);
|
2012-08-24 05:49:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-25 13:52:08 -04:00
|
|
|
void cClientHandle::SendBlockChange(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendBlockChange(a_BlockX, a_BlockY, a_BlockZ, a_BlockType, a_BlockMeta);
|
2012-08-25 13:52:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendBlockChanges(int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendBlockChanges(a_ChunkX, a_ChunkZ, a_Changes);
|
2012-08-25 13:52:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendUnloadChunk(int a_ChunkX, int a_ChunkZ)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendUnloadChunk(a_ChunkX, a_ChunkZ);
|
2012-08-25 13:52:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-25 17:46:18 -04:00
|
|
|
void cClientHandle::SendWeather(eWeather a_Weather)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendWeather(a_Weather);
|
2012-08-25 17:46:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendTimeUpdate(Int64 a_WorldTime)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendTimeUpdate(a_WorldTime);
|
2012-08-25 17:46:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendThunderbolt(int a_BlockX, int a_BlockY, int a_BlockZ)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendThunderbolt(a_BlockX, a_BlockY, a_BlockZ);
|
2012-08-25 17:46:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-26 17:01:07 -04:00
|
|
|
void cClientHandle::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer)
|
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
// Check chunks being sent, erase them from m_ChunksToSend:
|
|
|
|
bool Found = false;
|
|
|
|
cCSLock Lock(m_CSChunkLists);
|
|
|
|
for (cChunkCoordsList::iterator itr = m_ChunksToSend.begin(); itr != m_ChunksToSend.end(); ++itr)
|
|
|
|
{
|
|
|
|
if ((itr->m_ChunkX == a_ChunkX) && (itr->m_ChunkZ == a_ChunkZ))
|
|
|
|
{
|
|
|
|
m_ChunksToSend.erase(itr);
|
|
|
|
|
|
|
|
// TODO: _X: Decouple this from packet sending, it creates a deadlock possibility
|
|
|
|
// -- postpone till Tick() instead, using a bool flag
|
|
|
|
CheckIfWorldDownloaded();
|
|
|
|
|
|
|
|
Found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // for itr - m_ChunksToSend[]
|
|
|
|
if (!Found)
|
|
|
|
{
|
|
|
|
// This just sometimes happens. If you have a reliably replicatable situation for this, go ahead and fix it
|
|
|
|
// It's not a big issue anyway, just means that some chunks may be compressed several times
|
|
|
|
// LOGD("Refusing to send chunk [%d, %d] to client \"%s\" at [%d, %d].", ChunkX, ChunkZ, m_Username.c_str(), m_Player->GetChunkX(), m_Player->GetChunkZ());
|
|
|
|
return;
|
|
|
|
}
|
2012-08-26 17:01:07 -04:00
|
|
|
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendChunkData(a_ChunkX, a_ChunkZ, a_Serializer);
|
2012-08-26 17:01:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
void cClientHandle::CheckIfWorldDownloaded(void)
|
|
|
|
{
|
|
|
|
if (m_State != csDownloadingWorld)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-08-25 13:52:08 -04:00
|
|
|
|
|
|
|
bool ShouldSendConfirm = false;
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSChunkLists);
|
|
|
|
ShouldSendConfirm = m_ChunksToSend.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ShouldSendConfirm)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
SendConfirmPosition();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SendConfirmPosition(void)
|
|
|
|
{
|
|
|
|
LOG("Spawning player \"%s\" at {%.2f, %.2f, %.2f}",
|
|
|
|
m_Username.c_str(), m_Player->GetPosX(), m_Player->GetPosY(), m_Player->GetPosZ()
|
|
|
|
);
|
|
|
|
|
|
|
|
m_State = csConfirmingPos;
|
|
|
|
|
2012-06-15 11:55:53 -04:00
|
|
|
if (!cRoot::Get()->GetPluginManager()->CallHook(cPluginManager::HOOK_PLAYER_JOIN, 1, m_Player))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
// Broadcast that this player has joined the game! Yay~
|
2012-08-27 13:31:16 -04:00
|
|
|
cRoot::Get()->GetServer()->BroadcastChat(m_Username + " joined the game!", this);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
m_ConfirmPosition = m_Player->GetPosition();
|
2012-08-19 15:42:32 -04:00
|
|
|
SendPlayerMoveLook();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const AString & cClientHandle::GetUsername(void) const
|
|
|
|
{
|
|
|
|
return m_Username;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SetViewDistance(int a_ViewDistance)
|
|
|
|
{
|
|
|
|
if (a_ViewDistance < MIN_VIEW_DISTANCE)
|
|
|
|
{
|
|
|
|
a_ViewDistance = MIN_VIEW_DISTANCE;
|
|
|
|
}
|
|
|
|
if (a_ViewDistance > MAX_VIEW_DISTANCE)
|
|
|
|
{
|
|
|
|
a_ViewDistance = MAX_VIEW_DISTANCE;
|
|
|
|
}
|
|
|
|
m_ViewDistance = a_ViewDistance;
|
|
|
|
|
|
|
|
// Need to re-stream chunks for the change to become apparent:
|
|
|
|
StreamChunks();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cClientHandle::WantsSendChunk(int a_ChunkX, int a_ChunkY, int a_ChunkZ)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSChunkLists);
|
|
|
|
return (std::find(m_ChunksToSend.begin(), m_ChunksToSend.end(), cChunkCoords(a_ChunkX, a_ChunkY, a_ChunkZ)) != m_ChunksToSend.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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, ZERO_CHUNK_Y, a_ChunkZ)) == m_ChunksToSend.end())
|
|
|
|
{
|
|
|
|
m_ChunksToSend.push_back(cChunkCoords(a_ChunkX, ZERO_CHUNK_Y, a_ChunkZ));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-27 13:31:16 -04:00
|
|
|
void cClientHandle::PacketBufferFull(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
// 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_Socket.GetIPString().c_str());
|
|
|
|
SendDisconnect("Server busy");
|
|
|
|
// TODO: QueueDestroy();
|
|
|
|
cSleep::MilliSleep(1000); // Give packet some time to be received
|
|
|
|
Destroy();
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
2012-08-27 13:31:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::PacketUnknown(unsigned char a_PacketType)
|
|
|
|
{
|
|
|
|
LOGERROR("Unknown packet type 0x%02x from client \"%s\" @ %s", a_PacketType, m_Username.c_str(), m_Socket.GetIPString().c_str());
|
|
|
|
|
|
|
|
AString Reason;
|
|
|
|
Printf(Reason, "[C->S] Unknown PacketID: 0x%02x", a_PacketType);
|
|
|
|
SendDisconnect(Reason);
|
|
|
|
// TODO: QueueDestroy();
|
|
|
|
cSleep::MilliSleep(1000); // Give packet some time to be received
|
|
|
|
Destroy();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-27 13:31:16 -04:00
|
|
|
void cClientHandle::PacketError(unsigned char a_PacketType)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-27 13:31:16 -04:00
|
|
|
LOGERROR("Protocol error while parsing packet type 0x%02x; disconnecting client \"%s\"", a_PacketType, m_Username.c_str());
|
|
|
|
SendDisconnect("Protocol error");
|
|
|
|
// TODO: QueueDestroy();
|
|
|
|
cSleep::MilliSleep(1000); // Give packet some time to be received
|
|
|
|
Destroy();
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
2012-08-27 13:31:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::DataReceived(const char * a_Data, int a_Size)
|
|
|
|
{
|
|
|
|
// Data is received from the client, hand it off to the protocol:
|
|
|
|
m_Protocol->DataReceived(a_Data, a_Size);
|
|
|
|
m_TimeLastPacket = cWorld::GetTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::GetOutgoingData(AString & a_Data)
|
|
|
|
{
|
|
|
|
// Data can be sent to client
|
|
|
|
m_OutgoingData.ReadAll(a_Data);
|
|
|
|
m_OutgoingData.CommitRead();
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Disconnect player after all packets have been sent
|
2012-08-27 13:31:16 -04:00
|
|
|
if (m_bKicking && a_Data.empty())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SocketClosed(void)
|
|
|
|
{
|
|
|
|
// The socket has been closed for any reason
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
/*
|
|
|
|
self->Destroy();
|
|
|
|
LOG("Client \"%s\" disconnected", GetLogName().c_str());
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 16:06:25 -04:00
|
|
|
|
|
|
|
|