2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "ClientHandle.h"
|
|
|
|
#include "Server.h"
|
|
|
|
#include "World.h"
|
|
|
|
#include "Pickup.h"
|
|
|
|
#include "PluginManager.h"
|
|
|
|
#include "Player.h"
|
|
|
|
#include "Inventory.h"
|
2013-05-28 15:12:47 -04:00
|
|
|
#include "BlockEntities/ChestEntity.h"
|
|
|
|
#include "BlockEntities/SignEntity.h"
|
2012-09-23 13:19:34 -04:00
|
|
|
#include "UI/Window.h"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "Item.h"
|
|
|
|
#include "Doors.h"
|
|
|
|
#include "Piston.h"
|
2012-09-23 16:53:08 -04:00
|
|
|
#include "Mobs/Monster.h"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "ChatColor.h"
|
2012-09-23 17:23:33 -04:00
|
|
|
#include "OSSupport/Socket.h"
|
|
|
|
#include "OSSupport/Timer.h"
|
2012-09-29 09:59:32 -04:00
|
|
|
#include "Items/ItemHandler.h"
|
|
|
|
#include "Blocks/BlockHandler.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "Tracer.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
#include "Vector3f.h"
|
|
|
|
#include "Vector3d.h"
|
|
|
|
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "Root.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "Authenticator.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
#include "MersenneTwister.h"
|
|
|
|
|
2012-09-23 16:03:26 -04:00
|
|
|
#include "Protocol/ProtocolRecognizer.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
|
|
|
|
|
2013-06-18 15:32:31 -04:00
|
|
|
/// How many explosions per single game tick are allowed
|
|
|
|
static const int MAX_EXPLOSIONS_PER_TICK = 100;
|
|
|
|
|
|
|
|
/// How many explosions in the recent history are allowed
|
|
|
|
static const int MAX_RUNNING_SUM_EXPLOSIONS = cClientHandle::NUM_CHECK_EXPLOSIONS_TICKS * MAX_EXPLOSIONS_PER_TICK / 8;
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#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:
|
|
|
|
|
2012-09-25 04:23:19 -04:00
|
|
|
cClientHandle::cClientHandle(const cSocket * a_Socket, int a_ViewDistance)
|
2012-06-14 09:06:06 -04:00
|
|
|
: m_ViewDistance(a_ViewDistance)
|
2012-09-25 04:23:19 -04:00
|
|
|
, m_IPString(a_Socket->GetIPString())
|
2012-08-27 13:31:16 -04:00
|
|
|
, m_OutgoingData(64 KiB)
|
2012-06-14 09:06:06 -04:00
|
|
|
, m_Player(NULL)
|
2013-01-05 14:05:15 -05:00
|
|
|
, m_HasSentDC(false)
|
2012-11-01 17:38:20 -04:00
|
|
|
, m_TimeSinceLastPacket(0)
|
2012-06-14 09:06:06 -04:00
|
|
|
, m_bKeepThreadGoing(true)
|
|
|
|
, m_Ping(1000)
|
2012-08-31 07:40:54 -04:00
|
|
|
, m_PingID(1)
|
2012-06-14 09:06:06 -04:00
|
|
|
, m_State(csConnected)
|
|
|
|
, m_LastStreamedChunkX(0x7fffffff) // bogus chunk coords to force streaming upon login
|
|
|
|
, m_LastStreamedChunkZ(0x7fffffff)
|
2012-08-31 12:26:03 -04:00
|
|
|
, m_ShouldCheckDownloaded(false)
|
2012-06-14 09:06:06 -04:00
|
|
|
, m_UniqueID(0)
|
2013-01-11 23:46:01 -05:00
|
|
|
, m_BlockDigAnimStage(-1)
|
|
|
|
, m_HasStartedDigging(false)
|
2013-06-18 15:32:31 -04:00
|
|
|
, m_CurrentExplosionTick(0)
|
|
|
|
, m_RunningSumExplosions(0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-30 05:56:59 -04:00
|
|
|
m_Protocol = new cProtocolRecognizer(this);
|
2012-08-27 13:31:16 -04:00
|
|
|
|
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-11-11 09:00:58 -05:00
|
|
|
ASSERT(m_State == csDestroyed); // Has Destroy() been called?
|
|
|
|
|
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);
|
2013-04-13 17:02:10 -04:00
|
|
|
m_Player->Destroy();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-04-13 17:02:10 -04:00
|
|
|
delete m_Player;
|
|
|
|
m_Player = NULL;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-01-05 14:05:15 -05:00
|
|
|
if (!m_HasSentDC)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-09-25 04:23:19 -04:00
|
|
|
SendDisconnect("Server shut down? Kthnxbai");
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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();
|
2012-09-25 04:23:19 -04:00
|
|
|
cRoot::Get()->GetServer()->WriteToClient(this, Data);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Queue the socket to close as soon as it sends all outgoing data:
|
2012-09-25 04:23:19 -04:00
|
|
|
cRoot::Get()->GetServer()->QueueClientClose(this);
|
|
|
|
cRoot::Get()->GetServer()->RemoveClient(this);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
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
|
|
|
|
|
2012-11-11 09:00:58 -05:00
|
|
|
m_State = csDestroying;
|
2012-06-14 09:06:06 -04:00
|
|
|
if ((m_Player != NULL) && (m_Player->GetWorld() != NULL))
|
|
|
|
{
|
|
|
|
RemoveFromAllChunks();
|
|
|
|
m_Player->GetWorld()->RemoveClientFromChunkSender(this);
|
|
|
|
}
|
2012-11-11 09:00:58 -05:00
|
|
|
m_State = csDestroyed;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-09-25 04:23:19 -04:00
|
|
|
m_Player->SetIP (m_IPString);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-01-11 23:46:01 -05:00
|
|
|
cRoot::Get()->GetPluginManager()->CallHookPlayerJoined(*m_Player);
|
2012-09-23 12:54:03 -04:00
|
|
|
|
|
|
|
m_ConfirmPosition = m_Player->GetPosition();
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Return a server login packet
|
2012-09-02 08:09:58 -04:00
|
|
|
m_Protocol->SendLogin(*m_Player, *World);
|
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-11-01 17:38:20 -04:00
|
|
|
m_Protocol->SendTimeUpdate(World->GetWorldAge(), World->GetTimeOfDay());
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Send inventory
|
2012-09-20 09:25:54 -04:00
|
|
|
m_Player->GetInventory().SendWholeInventory(*this);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Send health
|
2012-07-17 08:02:03 -04:00
|
|
|
m_Player->SendHealth();
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-07-05 17:02:08 -04:00
|
|
|
// Send gamemode (1.6.1 movementSpeed):
|
|
|
|
SendGameMode(m_Player->GetGameMode());
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
m_Player->Initialize(World);
|
|
|
|
StreamChunks();
|
|
|
|
m_State = csDownloadingWorld;
|
2012-09-23 12:39:12 -04:00
|
|
|
|
|
|
|
// Broadcast this player's spawning to all other players in the same chunk
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Player->GetWorld()->BroadcastSpawnEntity(*m_Player, this);
|
2012-10-17 08:19:20 -04:00
|
|
|
|
2013-01-11 23:46:01 -05:00
|
|
|
cRoot::Get()->GetPluginManager()->CallHookPlayerSpawned(*m_Player);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::StreamChunks(void)
|
|
|
|
{
|
2012-11-11 09:00:58 -05:00
|
|
|
if ((m_State < csAuthenticating) || (m_State >= csDestroying))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(m_Player != NULL);
|
|
|
|
|
2013-02-27 05:01:20 -05:00
|
|
|
int ChunkPosX = FAST_FLOOR_DIV((int)m_Player->GetPosX(), cChunkDef::Width);
|
|
|
|
int ChunkPosZ = FAST_FLOOR_DIV((int)m_Player->GetPosZ(), cChunkDef::Width);
|
2012-06-14 09:06:06 -04:00
|
|
|
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)
|
|
|
|
{
|
2013-04-13 17:02:10 -04:00
|
|
|
World->RemoveChunkClient(itr->m_ChunkX, 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)
|
|
|
|
{
|
2013-04-13 17:02:10 -04:00
|
|
|
StreamChunk(ChunkPosX + d, ChunkPosZ + i);
|
|
|
|
StreamChunk(ChunkPosX - d, ChunkPosZ + i);
|
2012-06-14 09:06:06 -04:00
|
|
|
} // for i
|
|
|
|
for (int i = -d + 1; i < d; ++i)
|
|
|
|
{
|
2013-04-13 17:02:10 -04:00
|
|
|
StreamChunk(ChunkPosX + i, ChunkPosZ + d);
|
|
|
|
StreamChunk(ChunkPosX + i, ChunkPosZ - d);
|
2012-06-14 09:06:06 -04:00
|
|
|
} // 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-13 17:02:10 -04:00
|
|
|
void cClientHandle::StreamChunk(int a_ChunkX, int a_ChunkZ)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-11-11 09:00:58 -05:00
|
|
|
if (m_State >= csDestroying)
|
|
|
|
{
|
|
|
|
// Don't stream chunks to clients that are being destroyed
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
cWorld * World = m_Player->GetWorld();
|
|
|
|
ASSERT(World != NULL);
|
|
|
|
|
2013-04-13 17:02:10 -04:00
|
|
|
if (World->AddChunkClient(a_ChunkX, a_ChunkZ, this))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSChunkLists);
|
2013-04-13 17:02:10 -04:00
|
|
|
m_LoadedChunks.push_back(cChunkCoords(a_ChunkX, ZERO_CHUNK_Y, a_ChunkZ));
|
|
|
|
m_ChunksToSend.push_back(cChunkCoords(a_ChunkX, ZERO_CHUNK_Y, a_ChunkZ));
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-04-13 17:02:10 -04:00
|
|
|
World->SendChunkTo(a_ChunkX, a_ChunkZ, this);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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();
|
2013-07-03 03:47:35 -04:00
|
|
|
|
|
|
|
// Also reset the LastStreamedChunk coords to bogus coords,
|
|
|
|
// so that all chunks are streamed in subsequent StreamChunks() call (FS #407)
|
|
|
|
m_LastStreamedChunkX = 0x7fffffff;
|
|
|
|
m_LastStreamedChunkZ = 0x7fffffff;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-30 04:19:19 -04:00
|
|
|
bool 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());
|
2012-08-30 04:19:19 -04:00
|
|
|
m_Username = a_Username;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
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();
|
2012-08-30 04:19:19 -04:00
|
|
|
return false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Schedule for authentication; until then, let them wait (but do not block)
|
|
|
|
m_State = csAuthenticating;
|
2012-09-06 13:36:59 -04:00
|
|
|
cRoot::Get()->GetAuthenticator().Authenticate(GetUniqueID(), GetUsername(), m_Protocol->GetAuthServerID());
|
2012-08-30 04:19:19 -04:00
|
|
|
return true;
|
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
|
2012-09-20 09:25:54 -04:00
|
|
|
if (m_Player->GetGameMode() != eGameMode_Creative)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-09-20 09:25:54 -04:00
|
|
|
LOGWARNING("Got a CreativeInventoryAction packet from user \"%s\" while not in creative mode. Ignoring.", m_Username.c_str());
|
|
|
|
return;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2012-09-20 09:25:54 -04:00
|
|
|
if (m_Player->GetWindow()->GetWindowType() != cWindow::Inventory)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-09-20 09:25:54 -04:00
|
|
|
LOGWARNING("Got a CreativeInventoryAction packet from user \"%s\" while not in the inventory window. Ignoring.", m_Username.c_str());
|
|
|
|
return;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2012-09-20 09:25:54 -04:00
|
|
|
|
2013-05-08 05:45:07 -04:00
|
|
|
m_Player->GetWindow()->Clicked(*m_Player, 0, a_SlotNum, (a_SlotNum >= 0) ? caLeftClick : caLeftClickOutside, a_HeldItem);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2013-07-12 16:01:25 -04:00
|
|
|
|
|
|
|
// If a jump just started, process food exhaustion:
|
|
|
|
if ((a_PosY > m_Player->GetPosY()) && !a_IsOnGround && m_Player->IsOnGround())
|
|
|
|
{
|
|
|
|
m_Player->AddFoodExhaustion(m_Player->IsSprinting() ? 0.8 : 0.2);
|
|
|
|
}
|
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-01-11 23:46:01 -05:00
|
|
|
void cClientHandle::HandleLeftClick(int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-01-11 23:46:01 -05:00
|
|
|
LOGD("HandleLeftClick: {%i, %i, %i}; Face: %i; Stat: %i",
|
|
|
|
a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_Status
|
|
|
|
);
|
|
|
|
|
|
|
|
cPluginManager * PlgMgr = cRoot::Get()->GetPluginManager();
|
|
|
|
if (PlgMgr->CallHookPlayerLeftClick(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_Status))
|
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
if (!CheckBlockInteractionsRate())
|
|
|
|
{
|
2013-01-11 23:46:01 -05:00
|
|
|
// Too many interactions per second, simply ignore. Probably a hacked client, so don't even send bak the block
|
2012-06-14 09:06:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-11 23:46:01 -05:00
|
|
|
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->TossItem(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DIG_STATUS_SHOOT_EAT:
|
|
|
|
{
|
|
|
|
cItemHandler * ItemHandler = cItemHandler::GetItemHandler(m_Player->GetEquippedItem());
|
|
|
|
if (ItemHandler->IsFood())
|
|
|
|
{
|
|
|
|
if (PlgMgr->CallHookPlayerEating(*m_Player))
|
|
|
|
{
|
|
|
|
// A plugin doesn't agree with the action. The plugin itself is responsible for handling the consequences (possible inventory mismatch)
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOGINFO("%s: Status SHOOT / EAT not implemented", __FUNCTION__);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-01-13 07:59:48 -05:00
|
|
|
case DIG_STATUS_CANCELLED:
|
|
|
|
{
|
|
|
|
// Block breaking cancelled by player
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-11 23:46:01 -05:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
ASSERT(!"Unhandled DIG_STATUS");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} // switch (a_Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-01-11 23:46:01 -05:00
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::HandleBlockDigStarted(int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_OldBlock, NIBBLETYPE a_OldMeta)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
m_HasStartedDigging &&
|
|
|
|
(a_BlockX == m_LastDigBlockX) &&
|
|
|
|
(a_BlockY == m_LastDigBlockY) &&
|
|
|
|
(a_BlockZ == m_LastDigBlockZ)
|
|
|
|
)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-01-11 23:46:01 -05:00
|
|
|
// It is a duplicate packet, drop it right away
|
2012-06-14 09:06:06 -04:00
|
|
|
return;
|
|
|
|
}
|
2013-01-11 23:46:01 -05:00
|
|
|
|
|
|
|
if (cRoot::Get()->GetPluginManager()->CallHookPlayerBreakingBlock(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_OldBlock, a_OldMeta))
|
2012-07-15 16:36:34 -04:00
|
|
|
{
|
2013-01-11 23:46:01 -05:00
|
|
|
// 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);
|
2012-08-18 05:56:28 -04:00
|
|
|
return;
|
2012-07-15 16:36:34 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-01-11 23:46:01 -05:00
|
|
|
// 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;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-02-14 16:47:57 -05:00
|
|
|
if (
|
|
|
|
(m_Player->GetGameMode() == eGameMode_Creative) || // In creative mode, digging is done immediately
|
|
|
|
g_BlockOneHitDig[a_OldBlock] // One-hit blocks get destroyed immediately, too
|
|
|
|
)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-01-11 23:46:01 -05:00
|
|
|
HandleBlockDigFinished(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_OldBlock, a_OldMeta);
|
2012-06-14 09:06:06 -04:00
|
|
|
return;
|
|
|
|
}
|
2012-11-12 06:10:01 -05:00
|
|
|
|
2013-01-11 23:46:01 -05:00
|
|
|
// 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(m_UniqueID, m_BlockDigAnimX, m_BlockDigAnimY, m_BlockDigAnimZ, 0, this);
|
|
|
|
|
|
|
|
cWorld * World = m_Player->GetWorld();
|
|
|
|
|
|
|
|
cBlockHandler * Handler = cBlockHandler::GetBlockHandler(a_OldBlock);
|
|
|
|
Handler->OnDigging(World, m_Player, a_BlockX, a_BlockY, a_BlockZ);
|
|
|
|
|
|
|
|
cItemHandler * ItemHandler = cItemHandler::GetItemHandler(m_Player->GetEquippedItem());
|
2013-05-19 14:22:37 -04:00
|
|
|
ItemHandler->OnDiggingBlock(World, m_Player, m_Player->GetEquippedItem(), a_BlockX, a_BlockY, a_BlockZ, a_BlockFace);
|
2013-01-11 23:46:01 -05:00
|
|
|
|
|
|
|
// Check for clickthrough-blocks:
|
|
|
|
if (a_BlockFace != BLOCK_FACE_NONE)
|
|
|
|
{
|
|
|
|
int pX = a_BlockX;
|
|
|
|
int pY = a_BlockY;
|
|
|
|
int pZ = a_BlockZ;
|
|
|
|
AddFaceDirection(pX, pY, pZ, a_BlockFace);
|
|
|
|
|
|
|
|
Handler = cBlockHandler::GetBlockHandler(World->GetBlock(pX, pY, pZ));
|
|
|
|
|
|
|
|
// 2013_01_05 _X: This looks weird
|
|
|
|
// Why do we ask the block "behind" the one being clicked if it is clicked through? Shouldn't we ask the primary block instead?
|
|
|
|
if (Handler->IsClickedThrough())
|
|
|
|
{
|
|
|
|
Handler->OnDigging(World, m_Player, pX, pY, pZ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-11-12 06:10:01 -05:00
|
|
|
|
2013-01-11 23:46:01 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::HandleBlockDigFinished(int a_BlockX, int a_BlockY, int a_BlockZ, char 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)
|
|
|
|
)
|
2012-09-25 05:54:36 -04:00
|
|
|
{
|
2013-01-11 23:46:01 -05:00
|
|
|
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
|
|
|
|
);
|
|
|
|
return;
|
2012-09-25 05:54:36 -04:00
|
|
|
}
|
2013-01-11 23:46:01 -05:00
|
|
|
|
|
|
|
m_HasStartedDigging = false;
|
|
|
|
if (m_BlockDigAnimStage != -1)
|
2012-09-25 05:54:36 -04:00
|
|
|
{
|
|
|
|
// End dig animation
|
2013-01-11 23:46:01 -05:00
|
|
|
m_BlockDigAnimStage = -1;
|
2012-09-25 05:54:36 -04:00
|
|
|
// It seems that 10 ends block animation
|
2013-01-11 23:46:01 -05:00
|
|
|
m_Player->GetWorld()->BroadcastBlockBreakAnimation(m_UniqueID, m_BlockDigAnimX, m_BlockDigAnimY, m_BlockDigAnimZ, 10, this);
|
2012-09-25 05:54:36 -04:00
|
|
|
}
|
|
|
|
|
2013-01-11 23:46:01 -05:00
|
|
|
cItemHandler * ItemHandler = cItemHandler::GetItemHandler(m_Player->GetEquippedItem());
|
2012-09-25 05:54:36 -04:00
|
|
|
|
2013-01-11 23:46:01 -05:00
|
|
|
if (a_OldBlock == E_BLOCK_AIR)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-01-11 23:46:01 -05:00
|
|
|
LOGD("Digged air? wtf?");
|
|
|
|
return;
|
2012-08-18 05:56:28 -04:00
|
|
|
}
|
2013-01-11 23:46:01 -05:00
|
|
|
|
|
|
|
cWorld * World = m_Player->GetWorld();
|
2013-05-19 14:22:37 -04:00
|
|
|
ItemHandler->OnBlockDestroyed(World, m_Player, m_Player->GetEquippedItem(), a_BlockX, a_BlockY, a_BlockZ);
|
2013-01-11 23:46:01 -05:00
|
|
|
|
|
|
|
BlockHandler(a_OldBlock)->OnDestroyedByPlayer(World, m_Player, a_BlockX, a_BlockY, a_BlockZ);
|
|
|
|
World->BroadcastSoundParticleEffect(2001, a_BlockX * 8, a_BlockY * 8, a_BlockZ * 8, a_OldBlock, this);
|
|
|
|
World->DigBlock(a_BlockX, a_BlockY, a_BlockZ);
|
2012-10-06 16:53:08 -04:00
|
|
|
|
2013-01-11 23:46:01 -05:00
|
|
|
cRoot::Get()->GetPluginManager()->CallHookPlayerBrokenBlock(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_OldBlock, a_OldMeta);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-01-11 23:46:01 -05:00
|
|
|
void cClientHandle::HandleRightClick(int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, const cItem & a_HeldItem)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-01-11 23:46:01 -05:00
|
|
|
LOGD("HandleRightClick: {%d, %d, %d}, face %d, HeldItem: %s",
|
2012-11-18 16:50:05 -05:00
|
|
|
a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, ItemToFullString(a_HeldItem).c_str()
|
2012-09-07 16:53:37 -04:00
|
|
|
);
|
|
|
|
|
2013-01-11 23:46:01 -05:00
|
|
|
cPluginManager * PlgMgr = cRoot::Get()->GetPluginManager();
|
|
|
|
if (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 (a_BlockFace > -1)
|
|
|
|
{
|
|
|
|
AddFaceDirection(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace);
|
|
|
|
m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
if (!CheckBlockInteractionsRate())
|
|
|
|
{
|
2012-11-18 16:50:05 -05:00
|
|
|
LOGD("Too many block interactions, aborting placement");
|
2012-06-14 09:06:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-19 14:22:37 -04:00
|
|
|
const cItem & Equipped = m_Player->GetInventory().GetEquippedItem();
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-09-07 16:53:37 -04:00
|
|
|
if ((Equipped.m_ItemType != a_HeldItem.m_ItemType) && (a_HeldItem.m_ItemType != -1))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-09-07 16:53:37 -04:00
|
|
|
// 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
|
|
|
|
// ( http://forum.mc-server.org/showthread.php?tid=549&pid=4502#pid4502 )
|
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)
|
|
|
|
{
|
2013-01-11 23:46:01 -05:00
|
|
|
AddFaceDirection(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace);
|
2012-08-18 05:56:28 -04:00
|
|
|
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
|
|
|
|
2013-01-11 23:46:01 -05:00
|
|
|
BLOCKTYPE BlockType;
|
|
|
|
NIBBLETYPE BlockMeta;
|
|
|
|
World->GetBlockTypeMeta(a_BlockX, a_BlockY, a_BlockZ, BlockType, BlockMeta);
|
|
|
|
cBlockHandler * Handler = cBlockHandler::GetBlockHandler(BlockType);
|
2012-12-19 16:19:36 -05:00
|
|
|
|
2013-06-18 16:15:29 -04:00
|
|
|
if (Handler->IsUseable() && !m_Player->IsCrouched())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-01-11 23:46:01 -05:00
|
|
|
if (PlgMgr->CallHookPlayerUsingBlock(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, BlockType, BlockMeta))
|
|
|
|
{
|
|
|
|
// A plugin doesn't agree with using the block, abort
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Handler->OnUse(World, m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ);
|
|
|
|
PlgMgr->CallHookPlayerUsedBlock(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, BlockType, BlockMeta);
|
|
|
|
return;
|
2012-08-06 16:10:16 -04:00
|
|
|
}
|
2013-01-11 23:46:01 -05:00
|
|
|
|
|
|
|
cItemHandler * ItemHandler = cItemHandler::GetItemHandler(Equipped.m_ItemType);
|
|
|
|
|
2013-06-29 11:45:46 -04:00
|
|
|
if (ItemHandler->IsPlaceable())
|
2012-08-06 16:10:16 -04:00
|
|
|
{
|
2013-01-11 23:46:01 -05:00
|
|
|
HandlePlaceBlock(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, *ItemHandler);
|
|
|
|
}
|
|
|
|
else if (ItemHandler->IsFood())
|
|
|
|
{
|
|
|
|
cItem Item;
|
|
|
|
Item.m_ItemType = Equipped.m_ItemType;
|
|
|
|
Item.m_ItemCount = 1;
|
|
|
|
if (ItemHandler->EatItem(m_Player, &Item))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-01-11 23:46:01 -05:00
|
|
|
ItemHandler->OnFoodEaten(World, m_Player, &Item);
|
2013-05-24 03:30:39 -04:00
|
|
|
m_Player->GetInventory().RemoveOneEquippedItem();
|
2013-01-11 23:46:01 -05:00
|
|
|
return;
|
2012-08-18 05:56:28 -04:00
|
|
|
}
|
2013-01-11 23:46:01 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (PlgMgr->CallHookPlayerUsingItem(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-01-11 23:46:01 -05:00
|
|
|
// A plugin doesn't agree with using the item, abort
|
|
|
|
return;
|
|
|
|
}
|
2013-05-19 14:22:37 -04:00
|
|
|
ItemHandler->OnItemUse(World, m_Player, Equipped, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace);
|
2013-01-11 23:46:01 -05:00
|
|
|
PlgMgr->CallHookPlayerUsedItem(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ);
|
|
|
|
}
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
2012-07-15 16:36:34 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
2013-01-11 23:46:01 -05:00
|
|
|
void cClientHandle::HandlePlaceBlock(int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, cItemHandler & a_ItemHandler)
|
|
|
|
{
|
|
|
|
if (a_BlockFace < 0)
|
|
|
|
{
|
|
|
|
// Clicked in air
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cWorld * World = m_Player->GetWorld();
|
|
|
|
|
|
|
|
// Check if the block ignores build collision (water, grass etc.):
|
|
|
|
BLOCKTYPE ClickedBlock = World->GetBlock(a_BlockX, a_BlockY, a_BlockZ);
|
|
|
|
cBlockHandler * Handler = cBlockHandler::GetBlockHandler(ClickedBlock);
|
|
|
|
if (Handler->DoesIgnoreBuildCollision())
|
|
|
|
{
|
|
|
|
Handler->OnDestroyedByPlayer(World, m_Player, a_BlockX, a_BlockY, a_BlockZ);
|
|
|
|
// World->FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_AIR, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AddFaceDirection(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace);
|
|
|
|
// Check for Blocks not allowing placement on top
|
|
|
|
if ((a_BlockFace == BLOCK_FACE_TOP) && !Handler->DoesAllowBlockOnTop())
|
|
|
|
{
|
|
|
|
// Resend the old block
|
|
|
|
// Some times the client still places the block O.o
|
|
|
|
World->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player);
|
|
|
|
return;
|
|
|
|
}
|
2012-07-15 16:36:34 -04:00
|
|
|
|
|
|
|
|
2013-01-11 23:46:01 -05:00
|
|
|
BLOCKTYPE PlaceBlock = World->GetBlock(a_BlockX, a_BlockY, a_BlockZ);
|
|
|
|
if (!BlockHandler(PlaceBlock)->DoesIgnoreBuildCollision())
|
2012-08-18 05:56:28 -04:00
|
|
|
{
|
2013-01-11 23:46:01 -05: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
|
|
|
}
|
|
|
|
}
|
2013-01-11 23:46:01 -05:00
|
|
|
|
|
|
|
BLOCKTYPE BlockType;
|
|
|
|
NIBBLETYPE BlockMeta;
|
|
|
|
if (!a_ItemHandler.GetPlacementBlockTypeMeta(World, m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, BlockType, BlockMeta))
|
|
|
|
{
|
|
|
|
// Handler refused the placement, send that information back to the client:
|
|
|
|
World->SendBlockTo(a_BlockX, a_BlockY, a_BlockY, m_Player);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cBlockHandler * NewBlock = BlockHandler(BlockType);
|
|
|
|
|
|
|
|
if ((a_BlockFace != BLOCK_FACE_TOP) && !NewBlock->CanBePlacedOnSide())
|
|
|
|
{
|
|
|
|
// Cannot be placed on the side of an other block
|
|
|
|
World->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cRoot::Get()->GetPluginManager()->CallHookPlayerPlacingBlock(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, BlockType, BlockMeta))
|
|
|
|
{
|
|
|
|
// A plugin doesn't agree with placing the block, revert the block on the client:
|
|
|
|
World->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The actual block placement:
|
|
|
|
World->SetBlock(a_BlockX, a_BlockY, a_BlockZ, BlockType, BlockMeta);
|
2013-05-24 03:30:39 -04:00
|
|
|
if (m_Player->GetGameMode() != gmCreative)
|
2013-01-11 23:46:01 -05:00
|
|
|
{
|
2013-05-24 03:30:39 -04:00
|
|
|
m_Player->GetInventory().RemoveOneEquippedItem();
|
2013-01-11 23:46:01 -05:00
|
|
|
}
|
|
|
|
NewBlock->OnPlacedByPlayer(World, m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, BlockType, BlockMeta);
|
|
|
|
|
|
|
|
// Step sound with 0.8f pitch is used as block placement sound
|
|
|
|
World->BroadcastSoundEffect(NewBlock->GetStepSound(),a_BlockX * 8, a_BlockY * 8, a_BlockZ * 8, 1.0f, 0.8f);
|
|
|
|
cRoot::Get()->GetPluginManager()->CallHookPlayerPlacedBlock(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, BlockType, BlockMeta);
|
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
|
|
|
{
|
2013-07-03 03:47:35 -04:00
|
|
|
// We need to process messages in the Tick thread, to avoid deadlocks resulting from player-commands being processed
|
|
|
|
// in the SocketThread and waiting for acquiring the ChunkMap CS with Plugin CS locked
|
|
|
|
|
|
|
|
cCSLock Lock(m_CSMessages);
|
|
|
|
m_PendingMessages.push_back(a_Message);
|
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);
|
2013-04-02 02:48:31 -04:00
|
|
|
m_Player->SetHeadYaw (a_Rotation);
|
2012-08-19 07:51:17 -04:00
|
|
|
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-28 17:59:49 -04:00
|
|
|
switch (m_State)
|
2012-08-27 13:31:16 -04:00
|
|
|
{
|
2012-08-28 17:59:49 -04:00
|
|
|
case csPlaying:
|
|
|
|
{
|
|
|
|
m_Player->MoveTo(Vector3d(a_PosX, a_PosY, a_PosZ));
|
|
|
|
m_Player->SetStance (a_Stance);
|
|
|
|
m_Player->SetTouchGround(a_IsOnGround);
|
2013-04-02 02:48:31 -04:00
|
|
|
m_Player->SetHeadYaw (a_Rotation);
|
2012-08-28 17:59:49 -04:00
|
|
|
m_Player->SetRotation (a_Rotation);
|
|
|
|
m_Player->SetPitch (a_Pitch);
|
|
|
|
m_Player->WrapRotation();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case csDownloadingWorld:
|
|
|
|
{
|
|
|
|
Vector3d ReceivedPosition = Vector3d(a_PosX, a_PosY, a_PosZ);
|
|
|
|
// LOGD("Received MoveLook confirmation: {%0.2f %0.2f %0.2f}", a_PosX, a_PosY, a_PosZ);
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
if (ReceivedPosition.Equals(m_ConfirmPosition))
|
|
|
|
{
|
|
|
|
LOGINFO("Exact position confirmed by client!");
|
|
|
|
}
|
|
|
|
m_State = csPlaying;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-30 05:56:59 -04:00
|
|
|
LOGWARNING("Player \"%s\" sent a weird position confirmation %.2f blocks away, retrying", m_Username.c_str(), sqrt(Dist));
|
2012-09-23 12:25:32 -04:00
|
|
|
LOGD(" Expected pos: {%0.2f, %0.2f, %0.2f}", m_ConfirmPosition.x, m_ConfirmPosition.y, m_ConfirmPosition.z);
|
|
|
|
LOGD(" Received pos: {%0.2f, %0.2f, %0.2f}", a_PosX, a_PosY, a_PosZ);
|
2012-08-28 17:59:49 -04:00
|
|
|
m_ConfirmPosition = m_Player->GetPosition();
|
|
|
|
SendPlayerMoveLook();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-08-27 13:31:16 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2013-05-19 14:22:37 -04:00
|
|
|
m_Player->GetInventory().SetEquippedSlotNum(a_SlotNum);
|
2012-08-19 07:51:17 -04:00
|
|
|
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
|
|
|
{
|
2013-05-30 15:34:09 -04:00
|
|
|
m_Player->CloseWindowIfID(a_WindowID);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-08 05:45:07 -04:00
|
|
|
void cClientHandle::HandleWindowClick(char a_WindowID, short a_SlotNum, eClickAction a_ClickAction, const cItem & a_HeldItem)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-05-08 05:45:07 -04:00
|
|
|
LOGD("WindowClick: WinID %d, SlotNum %d, action: %s, Item %s x %d",
|
|
|
|
a_WindowID, a_SlotNum, ClickActionToString(a_ClickAction),
|
2012-09-20 09:25:54 -04:00
|
|
|
ItemToString(a_HeldItem).c_str(), a_HeldItem.m_ItemCount
|
|
|
|
);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
cWindow * Window = m_Player->GetWindow();
|
|
|
|
if (Window == NULL)
|
|
|
|
{
|
|
|
|
LOGWARNING("Player \"%s\" clicked in a non-existent window. Ignoring", m_Username.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-08 05:45:07 -04:00
|
|
|
Window->Clicked(*m_Player, a_WindowID, a_SlotNum, a_ClickAction, 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-09-01 17:44:18 -04:00
|
|
|
World->UpdateSign(a_BlockX, a_BlockY, a_BlockZ, a_Line1, a_Line2, a_Line3, a_Line4, m_Player);
|
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
|
|
|
{
|
2013-03-03 14:05:11 -05:00
|
|
|
// TODO: Let plugins interfere via a hook
|
|
|
|
|
2013-07-12 16:01:25 -04:00
|
|
|
// If it is a right click, call the entity's OnRightClicked() handler:
|
2012-08-19 15:42:32 -04:00
|
|
|
if (!a_IsLeftClick)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-03 14:05:11 -05:00
|
|
|
class cRclkEntity : public cEntityCallback
|
|
|
|
{
|
|
|
|
cPlayer & m_Player;
|
|
|
|
virtual bool Item(cEntity * a_Entity) override
|
|
|
|
{
|
|
|
|
a_Entity->OnRightClicked(m_Player);
|
2013-07-12 16:01:25 -04:00
|
|
|
return false;
|
2013-03-03 14:05:11 -05:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
cRclkEntity(cPlayer & a_Player) : m_Player(a_Player) {}
|
|
|
|
} Callback (*m_Player);
|
|
|
|
|
|
|
|
cWorld * World = m_Player->GetWorld();
|
|
|
|
World->DoWithEntityByID(a_TargetEntityID, Callback);
|
2012-06-14 09:06:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-12 16:01:25 -04:00
|
|
|
// If it is a left click, attack the entity:
|
2012-06-14 09:06:06 -04:00
|
|
|
class cDamageEntity : public cEntityCallback
|
|
|
|
{
|
|
|
|
virtual bool Item(cEntity * a_Entity) override
|
|
|
|
{
|
2012-11-16 11:03:56 -05:00
|
|
|
if (!a_Entity->GetWorld()->IsPVPEnabled())
|
2012-10-10 15:46:12 -04:00
|
|
|
{
|
2013-07-12 16:01:25 -04:00
|
|
|
// PVP is disabled, disallow players hurting other players:
|
|
|
|
if (a_Entity->IsPlayer())
|
2012-10-10 15:46:12 -04:00
|
|
|
{
|
|
|
|
// Player is hurting another player which is not allowed when PVP is disabled so ignore it
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2013-07-12 16:01:25 -04:00
|
|
|
a_Entity->TakeDamage(*m_Attacker);
|
|
|
|
return false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
public:
|
2012-12-21 06:04:08 -05:00
|
|
|
cPawn * m_Attacker;
|
2012-06-14 09:06:06 -04:00
|
|
|
} Callback;
|
|
|
|
|
2012-12-21 06:04:08 -05:00
|
|
|
Callback.m_Attacker = m_Player;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
cWorld * World = m_Player->GetWorld();
|
2013-07-12 16:01:25 -04:00
|
|
|
if (World->DoWithEntityByID(a_TargetEntityID, Callback))
|
|
|
|
{
|
|
|
|
// Any kind of an attack implies food exhaustion
|
|
|
|
m_Player->AddFoodExhaustion(0.3);
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::HandleRespawn(void)
|
|
|
|
{
|
2012-10-17 08:19:20 -04:00
|
|
|
if( m_Player == NULL )
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
return;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
m_Player->Respawn();
|
2013-01-11 23:46:01 -05:00
|
|
|
cRoot::Get()->GetPluginManager()->CallHookPlayerSpawned(*m_Player);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2013-04-13 17:02:10 -04:00
|
|
|
m_HasSentDC = true;
|
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-09-05 16:30:27 -04:00
|
|
|
bool cClientHandle::HandleHandshake(const AString & a_Username)
|
|
|
|
{
|
|
|
|
if (!cRoot::Get()->GetPluginManager()->CallHookHandshake(this, a_Username))
|
|
|
|
{
|
|
|
|
if (cRoot::Get()->GetDefaultWorld()->GetNumPlayers() >= cRoot::Get()->GetDefaultWorld()->GetMaxPlayers())
|
|
|
|
{
|
|
|
|
Kick("The server is currently full :(-- Try again later");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-09-29 16:43:42 -04:00
|
|
|
void cClientHandle::HandleEntityAction(int a_EntityID, char a_ActionID)
|
|
|
|
{
|
|
|
|
if( a_EntityID != m_Player->GetUniqueID() )
|
|
|
|
{
|
|
|
|
// We should only receive entity actions from the entity that is performing the action
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-04 10:18:03 -04:00
|
|
|
switch (a_ActionID)
|
2012-09-29 16:43:42 -04:00
|
|
|
{
|
2013-06-04 10:18:03 -04:00
|
|
|
case 1: // crouch
|
|
|
|
{
|
|
|
|
m_Player->SetCrouch(true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2: // uncrouch
|
|
|
|
{
|
|
|
|
m_Player->SetCrouch(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3: // Leave bed
|
|
|
|
{
|
|
|
|
m_Player->GetWorld()->BroadcastPlayerAnimation(*m_Player, 3);
|
|
|
|
break;
|
|
|
|
}
|
2013-07-07 10:13:43 -04:00
|
|
|
case 4: // Start sprinting
|
|
|
|
{
|
|
|
|
m_Player->SetSprint(true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 5: // Stop sprinting
|
|
|
|
{
|
|
|
|
m_Player->SetSprint(false);
|
|
|
|
SendPlayerMaxSpeed();
|
|
|
|
break;
|
|
|
|
}
|
2012-09-29 16:43:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-05 17:11:06 -04:00
|
|
|
void cClientHandle::HandleUnmount(void)
|
|
|
|
{
|
|
|
|
if (m_Player == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_Player->Detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-27 13:31:16 -04:00
|
|
|
void cClientHandle::SendData(const char * a_Data, int a_Size)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSOutgoingData);
|
2012-09-06 04:33:43 -04:00
|
|
|
|
|
|
|
// _X 2012_09_06: We need an overflow buffer, usually when streaming the initial chunks
|
|
|
|
if (m_OutgoingDataOverflow.empty())
|
|
|
|
{
|
|
|
|
// No queued overflow data; if this packet fits into the ringbuffer, put it in, otherwise put it in the overflow buffer:
|
|
|
|
int CanFit = m_OutgoingData.GetFreeSpace();
|
|
|
|
if (CanFit > a_Size)
|
|
|
|
{
|
|
|
|
CanFit = a_Size;
|
|
|
|
}
|
|
|
|
if (CanFit > 0)
|
|
|
|
{
|
|
|
|
m_OutgoingData.Write(a_Data, CanFit);
|
|
|
|
}
|
|
|
|
if (a_Size > CanFit)
|
|
|
|
{
|
|
|
|
m_OutgoingDataOverflow.append(a_Data + CanFit, a_Size - CanFit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2012-08-27 13:31:16 -04:00
|
|
|
{
|
2012-09-06 04:33:43 -04:00
|
|
|
// There is a queued overflow. Append to it, then send as much from its front as possible
|
|
|
|
m_OutgoingDataOverflow.append(a_Data, a_Size);
|
|
|
|
int CanFit = m_OutgoingData.GetFreeSpace();
|
|
|
|
if (CanFit > 128)
|
|
|
|
{
|
|
|
|
// No point in moving the data over if it's not large enough - too much effort for too little an effect
|
|
|
|
m_OutgoingData.Write(m_OutgoingDataOverflow.data(), CanFit);
|
|
|
|
m_OutgoingDataOverflow.erase(0, CanFit);
|
|
|
|
}
|
2012-08-27 13:31:16 -04:00
|
|
|
}
|
2012-09-06 04:33:43 -04:00
|
|
|
} // Lock(m_CSOutgoingData)
|
2012-08-27 13:31:16 -04:00
|
|
|
|
|
|
|
// 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);
|
2012-11-01 17:38:20 -04:00
|
|
|
/*
|
|
|
|
// TODO: _X 2012_11_01: This needs a total re-thinking and rewriting
|
2012-06-14 09:06:06 -04:00
|
|
|
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.
|
|
|
|
}
|
2012-11-01 17:38:20 -04:00
|
|
|
*/
|
2012-06-14 09:06:06 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::Tick(float a_Dt)
|
|
|
|
{
|
2012-11-01 17:38:20 -04:00
|
|
|
m_TimeSinceLastPacket += a_Dt;
|
|
|
|
if (m_TimeSinceLastPacket > 30000.f) // 30 seconds time-out
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-25 17:59:13 -04:00
|
|
|
SendDisconnect("Nooooo!! You timed out! D: Come back!");
|
2012-06-14 09:06:06 -04:00
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
|
2012-08-31 12:26:03 -04:00
|
|
|
if ((m_State == csDownloadingWorld) && m_ShouldCheckDownloaded)
|
|
|
|
{
|
|
|
|
CheckIfWorldDownloaded();
|
|
|
|
m_ShouldCheckDownloaded = false;
|
|
|
|
}
|
|
|
|
|
2013-07-03 03:47:35 -04:00
|
|
|
// Send a ping packet:
|
2012-06-14 09:06:06 -04:00
|
|
|
cTimer t1;
|
2012-08-30 05:56:59 -04:00
|
|
|
if (
|
|
|
|
(m_Player != NULL) && // Is logged in?
|
|
|
|
(m_LastPingTime + cClientHandle::PING_TIME_MS <= t1.GetNowTime())
|
|
|
|
)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
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-09-25 05:54:36 -04:00
|
|
|
|
|
|
|
// Handle block break animation:
|
2013-01-11 23:46:01 -05:00
|
|
|
if ((m_Player != NULL) && (m_BlockDigAnimStage > -1))
|
2012-09-25 05:54:36 -04:00
|
|
|
{
|
2013-01-11 23:46:01 -05:00
|
|
|
int lastAnimVal = m_BlockDigAnimStage;
|
|
|
|
m_BlockDigAnimStage += (int)(m_BlockDigAnimSpeed * a_Dt);
|
|
|
|
if (m_BlockDigAnimStage > 9000)
|
2012-09-25 05:54:36 -04:00
|
|
|
{
|
2013-01-11 23:46:01 -05:00
|
|
|
m_BlockDigAnimStage = 9000;
|
2012-09-25 05:54:36 -04:00
|
|
|
}
|
2013-01-11 23:46:01 -05:00
|
|
|
if (m_BlockDigAnimStage / 1000 != lastAnimVal / 1000)
|
2012-09-25 05:54:36 -04:00
|
|
|
{
|
2013-01-11 23:46:01 -05:00
|
|
|
m_Player->GetWorld()->BroadcastBlockBreakAnimation(m_UniqueID, m_BlockDigAnimX, m_BlockDigAnimY, m_BlockDigAnimZ, (char)(m_BlockDigAnimStage / 1000), this);
|
2012-09-25 05:54:36 -04:00
|
|
|
}
|
|
|
|
}
|
2013-06-18 15:32:31 -04:00
|
|
|
|
|
|
|
// Update the explosion statistics:
|
|
|
|
m_CurrentExplosionTick = (m_CurrentExplosionTick + 1) % ARRAYCOUNT(m_NumExplosionsPerTick);
|
|
|
|
m_RunningSumExplosions -= m_NumExplosionsPerTick[m_CurrentExplosionTick];
|
|
|
|
m_NumExplosionsPerTick[m_CurrentExplosionTick] = 0;
|
2013-07-03 03:47:35 -04:00
|
|
|
|
|
|
|
// Process the queued messages:
|
|
|
|
ProcessPendingMessages();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendAttachEntity(const cEntity & a_Entity, const cEntity * a_Vehicle)
|
2012-08-17 06:18:07 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendAttachEntity(a_Entity, a_Vehicle);
|
2012-08-18 05:56:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendBlockAction(int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType)
|
2012-08-18 05:56:28 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendBlockAction(a_BlockX, a_BlockY, a_BlockZ, a_Byte1, a_Byte2, a_BlockType);
|
2012-08-18 06:38:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendBlockBreakAnim(int a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage)
|
2012-08-18 06:38:15 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendBlockBreakAnim(a_EntityID, a_BlockX, a_BlockY, a_BlockZ, a_Stage);
|
2012-08-17 06:18:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendBlockChange(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
|
2012-08-19 07:51:17 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendBlockChange(a_BlockX, a_BlockY, a_BlockZ, a_BlockType, a_BlockMeta);
|
2012-08-19 07:51:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendBlockChanges(int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes)
|
2012-08-19 07:51:17 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendBlockChanges(a_ChunkX, a_ChunkZ, a_Changes);
|
2012-08-19 07:51:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendChat(const AString & a_Message)
|
2012-08-19 07:51:17 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendChat(a_Message);
|
2012-08-19 07:51:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer)
|
2012-08-19 07:51:17 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -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);
|
|
|
|
|
|
|
|
// Make the tick thread check if all the needed chunks have been downloaded
|
|
|
|
// -- needed to offload this from here due to a deadlock possibility
|
|
|
|
m_ShouldCheckDownloaded = true;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Protocol->SendChunkData(a_ChunkX, a_ChunkZ, a_Serializer);
|
2012-08-19 07:51:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendCollectPickup(const cPickup & a_Pickup, const cPlayer & a_Player)
|
2012-08-19 07:51:17 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendCollectPickup(a_Pickup, a_Player);
|
2012-08-19 07:51:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendDestroyEntity(const cEntity & a_Entity)
|
2012-08-19 07:51:17 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendDestroyEntity(a_Entity);
|
2012-08-19 07:51:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendDisconnect(const AString & a_Reason)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
if (!m_HasSentDC)
|
|
|
|
{
|
2013-07-08 09:00:09 -04:00
|
|
|
LOGD("Sending a DC: \"%s\"", StripColorCodes(a_Reason).c_str());
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendDisconnect(a_Reason);
|
|
|
|
m_HasSentDC = true;
|
|
|
|
}
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum, const cItem & a_Item)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendEntityEquipment(a_Entity, a_SlotNum, a_Item);
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendEntityHeadLook(const cEntity & a_Entity)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
ASSERT(a_Entity.GetUniqueID() != m_Player->GetUniqueID()); // Must not send for self
|
|
|
|
|
|
|
|
m_Protocol->SendEntityHeadLook(a_Entity);
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendEntityLook(const cEntity & a_Entity)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
|
|
|
ASSERT(a_Entity.GetUniqueID() != m_Player->GetUniqueID()); // Must not send for self
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendEntityLook(a_Entity);
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendEntityMetadata(const cEntity & a_Entity)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendEntityMetadata(a_Entity);
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendEntityRelMove(const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
|
|
|
ASSERT(a_Entity.GetUniqueID() != m_Player->GetUniqueID()); // Must not send for self
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendEntityRelMove(a_Entity, a_RelX, a_RelY, a_RelZ);
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendEntityRelMoveLook(const cEntity & a_Entity, char a_RelX, char a_RelY, char a_RelZ)
|
2013-03-17 22:51:55 -04:00
|
|
|
{
|
|
|
|
ASSERT(a_Entity.GetUniqueID() != m_Player->GetUniqueID()); // Must not send for self
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
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);
|
2013-03-17 22:51:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendEntityVelocity(const cEntity & a_Entity)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
|
|
|
ASSERT(a_Entity.GetUniqueID() != m_Player->GetUniqueID()); // Must not send for self
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendEntityVelocity(a_Entity);
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendExplosion(double a_BlockX, double a_BlockY, double a_BlockZ, float a_Radius, const cVector3iArray & a_BlocksAffected, const Vector3d & a_PlayerMotion)
|
2013-03-03 14:05:11 -05:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
if (
|
|
|
|
(m_NumExplosionsPerTick[m_CurrentExplosionTick] > MAX_EXPLOSIONS_PER_TICK) || // Too many explosions in this tick
|
|
|
|
(m_RunningSumExplosions > MAX_RUNNING_SUM_EXPLOSIONS) // Too many explosions in the recent history
|
|
|
|
)
|
|
|
|
{
|
|
|
|
LOGD("Dropped %u explosions", a_BlocksAffected.size());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the statistics:
|
|
|
|
m_NumExplosionsPerTick[m_CurrentExplosionTick] += a_BlocksAffected.size();
|
|
|
|
m_RunningSumExplosions += a_BlocksAffected.size();
|
|
|
|
|
|
|
|
m_Protocol->SendExplosion(a_BlockX, a_BlockY, a_BlockZ, a_Radius, a_BlocksAffected, a_PlayerMotion);
|
2013-03-03 14:05:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendGameMode(eGameMode a_GameMode)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendGameMode(a_GameMode);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendInventoryProgress(char a_WindowID, short a_ProgressBar, short a_Value)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendInventoryProgress(a_WindowID, a_ProgressBar, a_Value);
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendInventorySlot(char a_WindowID, short a_SlotNum, const cItem & a_Item)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendInventorySlot(a_WindowID, a_SlotNum, a_Item);
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendPickupSpawn(const cPickup & a_Pickup)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendPickupSpawn(a_Pickup);
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendPlayerAnimation(const cPlayer & a_Player, char a_Animation)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendPlayerAnimation(a_Player, a_Animation);
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendPlayerListItem(const cPlayer & a_Player, bool a_IsOnline)
|
2012-08-19 17:14:45 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendPlayerListItem(a_Player, a_IsOnline);
|
2012-08-19 17:14:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendPlayerMaxSpeed(void)
|
2013-04-17 22:42:45 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendPlayerMaxSpeed();
|
2013-04-17 22:42:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendPlayerMoveLook(void)
|
2012-08-19 17:14:45 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
/*
|
|
|
|
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();
|
2012-08-19 17:14:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendPlayerPosition(void)
|
2012-08-19 17:14:45 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendPlayerPosition();
|
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;
|
|
|
|
}
|
|
|
|
|
2012-11-16 10:56:41 -05:00
|
|
|
LOGD("Spawning player \"%s\" on client \"%s\" @ %s",
|
2012-09-23 12:25:32 -04:00
|
|
|
a_Player.GetName().c_str(), GetPlayer()->GetName().c_str(), GetIPString().c_str()
|
|
|
|
);
|
|
|
|
|
2012-08-27 13:31:16 -04:00
|
|
|
m_Protocol->SendPlayerSpawn(a_Player);
|
2012-08-24 03:58:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendRespawn(void)
|
2012-08-24 03:58:26 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendRespawn();
|
2012-08-24 03:58:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendSoundEffect(const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch)
|
2012-12-26 04:12:00 -05:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendSoundEffect(a_SoundName, a_SrcX, a_SrcY, a_SrcZ, a_Volume, a_Pitch);
|
2012-12-26 04:12:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data)
|
2012-08-24 03:58:26 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendSoundParticleEffect(a_EffectID, a_SrcX, a_SrcY, a_SrcZ, a_Data);
|
2012-08-24 03:58:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendSpawnFallingBlock(const cFallingBlock & a_FallingBlock)
|
2012-08-24 03:58:26 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendSpawnFallingBlock(a_FallingBlock);
|
2012-08-24 03:58:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendSpawnMob(const cMonster & a_Mob)
|
2012-08-24 05:49:00 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendSpawnMob(a_Mob);
|
2012-08-24 05:49:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendSpawnObject(const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch)
|
2012-08-25 13:52:08 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendSpawnObject(a_Entity, a_ObjectType, a_ObjectData, a_Yaw, a_Pitch);
|
2012-08-25 13:52:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleType)
|
2012-08-25 13:52:08 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendSpawnVehicle(a_Vehicle, a_VehicleType);
|
2012-08-25 13:52:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendTeleportEntity(const cEntity & a_Entity)
|
2012-08-25 13:52:08 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendTeleportEntity(a_Entity);
|
2012-08-25 13:52:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendThunderbolt(int a_BlockX, int a_BlockY, int a_BlockZ)
|
2012-08-25 17:46:18 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendThunderbolt(a_BlockX, a_BlockY, a_BlockZ);
|
2012-08-25 17:46:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-01 17:38:20 -04:00
|
|
|
void cClientHandle::SendTimeUpdate(Int64 a_WorldAge, Int64 a_TimeOfDay)
|
2012-08-25 17:46:18 -04:00
|
|
|
{
|
2012-11-01 17:38:20 -04:00
|
|
|
m_Protocol->SendTimeUpdate(a_WorldAge, a_TimeOfDay);
|
2012-08-25 17:46:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendUnloadChunk(int a_ChunkX, int a_ChunkZ)
|
2012-08-25 17:46:18 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendUnloadChunk(a_ChunkX, a_ChunkZ);
|
2012-08-25 17:46:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -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-09-11 08:01:34 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendUpdateSign(
|
|
|
|
a_BlockX, a_BlockY, a_BlockZ,
|
|
|
|
a_Line1, a_Line2, a_Line3, a_Line4
|
|
|
|
);
|
2012-09-11 08:01:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendUseBed(const cEntity & a_Entity, int a_BlockX, int a_BlockY, int a_BlockZ )
|
2012-10-21 03:46:28 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendUseBed(a_Entity, a_BlockX, a_BlockY, a_BlockZ);
|
2012-10-21 03:46:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendWeather(eWeather a_Weather)
|
2012-09-25 05:54:36 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendWeather(a_Weather);
|
2012-09-25 05:54:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendWindowClose(const cWindow & a_Window)
|
2012-09-29 16:43:42 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendWindowClose(a_Window);
|
2012-09-29 16:43:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
|
|
|
|
void cClientHandle::SendWindowOpen(char a_WindowID, char a_WindowType, const AString & a_WindowTitle, char a_NumSlots)
|
2012-09-30 12:37:44 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendWindowOpen(a_WindowID, a_WindowType, a_WindowTitle, a_NumSlots);
|
2012-09-30 12:37:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-09-29 16:43:42 -04:00
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendWholeInventory(const cInventory & a_Inventory)
|
2013-02-18 11:48:50 -05:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendWholeInventory(a_Inventory);
|
2013-02-18 11:48:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-07 09:06:06 -04:00
|
|
|
void cClientHandle::SendWholeInventory(const cWindow & a_Window)
|
2012-08-26 17:01:07 -04:00
|
|
|
{
|
2013-07-07 09:06:06 -04:00
|
|
|
m_Protocol->SendWholeInventory(a_Window);
|
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;
|
|
|
|
|
2013-01-11 23:46:01 -05:00
|
|
|
if (!cRoot::Get()->GetPluginManager()->CallHookPlayerJoined(*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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-18 17:54:56 -04:00
|
|
|
void cClientHandle::SetUsername( const AString & a_Username )
|
|
|
|
{
|
|
|
|
m_Username = a_Username;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
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)
|
|
|
|
{
|
2012-11-11 09:00:58 -05:00
|
|
|
if (m_State >= csDestroying)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
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)
|
|
|
|
{
|
2012-11-11 09:00:58 -05:00
|
|
|
if (m_State >= csDestroying)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-03 03:47:35 -04:00
|
|
|
void cClientHandle::ProcessPendingMessages(void)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
AString Message;
|
|
|
|
|
|
|
|
// Extract one message from the PendingMessages buffer:
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSMessages);
|
|
|
|
if (m_PendingMessages.empty())
|
|
|
|
{
|
|
|
|
// No more messages in the buffer, bail out
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Message = m_PendingMessages.front();
|
|
|
|
m_PendingMessages.pop_front();
|
|
|
|
} // Lock(m_CSMessages)
|
|
|
|
|
|
|
|
// If a command, perform it:
|
|
|
|
if (cRoot::Get()->GetServer()->Command(*this, Message))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not a command, broadcast as a simple message:
|
|
|
|
AString Msg;
|
|
|
|
Printf(Msg, "<%s%s%s> %s",
|
|
|
|
m_Player->GetColor().c_str(),
|
|
|
|
m_Player->GetName().c_str(),
|
|
|
|
cChatColor::White.c_str(),
|
|
|
|
Message.c_str()
|
|
|
|
);
|
|
|
|
m_Player->GetWorld()->BroadcastChat(Msg);
|
|
|
|
} // while (true)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
2012-09-25 04:23:19 -04:00
|
|
|
LOGERROR("Too much data in queue for client \"%s\" @ %s, kicking them.", m_Username.c_str(), m_IPString.c_str());
|
2012-08-27 13:31:16 -04:00
|
|
|
SendDisconnect("Server busy");
|
|
|
|
Destroy();
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
2012-08-27 13:31:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::PacketUnknown(unsigned char a_PacketType)
|
|
|
|
{
|
2012-09-25 04:23:19 -04:00
|
|
|
LOGERROR("Unknown packet type 0x%02x from client \"%s\" @ %s", a_PacketType, m_Username.c_str(), m_IPString.c_str());
|
2012-08-27 13:31:16 -04:00
|
|
|
|
|
|
|
AString Reason;
|
|
|
|
Printf(Reason, "[C->S] Unknown PacketID: 0x%02x", a_PacketType);
|
|
|
|
SendDisconnect(Reason);
|
|
|
|
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");
|
|
|
|
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:
|
2013-06-15 11:29:20 -04:00
|
|
|
if ((m_Player != NULL) && (m_Player->GetWorld() != NULL))
|
|
|
|
{
|
2013-06-16 04:51:05 -04:00
|
|
|
/*
|
|
|
|
_X: Lock the world, so that plugins reacting to protocol events have already the chunkmap locked.
|
|
|
|
There was a possibility of a deadlock between SocketThreads and TickThreads, resulting from each
|
|
|
|
holding one CS an requesting the other one (ChunkMap CS vs Plugin CS) (FS #375). To break this, it's
|
|
|
|
sufficient to break any of the four Coffman conditions for a deadlock. We'll solve this by requiring
|
|
|
|
the ChunkMap CS for all SocketThreads operations before they lock the PluginCS - thus creating a kind
|
|
|
|
of a lock hierarchy. However, this incurs a performance penalty, we're de facto locking the chunkmap
|
|
|
|
for each incoming packet. A better, but more involved solutin would be to lock the chunkmap only when
|
|
|
|
the incoming packet really has a plugin CS lock request.
|
|
|
|
Also, it is still possible for a packet to slip through - when a player still doesn't have their world
|
|
|
|
assigned and several packets arrive at once.
|
|
|
|
*/
|
2013-06-15 11:29:20 -04:00
|
|
|
cWorld::cLock(*m_Player->GetWorld());
|
|
|
|
|
|
|
|
m_Protocol->DataReceived(a_Data, a_Size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Protocol->DataReceived(a_Data, a_Size);
|
|
|
|
}
|
2012-11-01 17:38:20 -04:00
|
|
|
m_TimeSinceLastPacket = 0;
|
2012-08-27 13:31:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::GetOutgoingData(AString & a_Data)
|
|
|
|
{
|
|
|
|
// Data can be sent to client
|
2012-09-04 15:05:35 -04:00
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSOutgoingData);
|
|
|
|
m_OutgoingData.ReadAll(a_Data);
|
|
|
|
m_OutgoingData.CommitRead();
|
2012-09-06 04:33:43 -04:00
|
|
|
a_Data.append(m_OutgoingDataOverflow);
|
|
|
|
m_OutgoingDataOverflow.clear();
|
2012-09-04 15:05:35 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Disconnect player after all packets have been sent
|
2013-01-05 14:05:15 -05:00
|
|
|
if (m_HasSentDC && a_Data.empty())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cClientHandle::SocketClosed(void)
|
|
|
|
{
|
|
|
|
// The socket has been closed for any reason
|
|
|
|
|
2012-09-25 04:23:19 -04:00
|
|
|
LOG("Client \"%s\" @ %s disconnected", m_Username.c_str(), m_IPString.c_str());
|
|
|
|
Destroy();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 16:06:25 -04:00
|
|
|
|
|
|
|
|