From bf838238e45f4ba96c0653f77cf1bf79e6c13c78 Mon Sep 17 00:00:00 2001 From: "mtilden@gmail.com" Date: Mon, 26 Dec 2011 01:07:35 +0000 Subject: - Make Color was using 2 extra characters which took 2 characters off the 16 max (including color codes) for scoreboard display - Added xC9 PlayerListItem packet and added code for player names to be added and removed from the scoreboard (need a catch-all for client disconnects: crashes, timeouts, etc) - Changed wid wording to a_WindowType git-svn-id: http://mc-server.googlecode.com/svn/trunk@113 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- VC2010/MCServer.vcxproj | 2 ++ VC2010/MCServer.vcxproj.filters | 9 +++++++++ source/PacketID.h | 1 + source/cChatColor.cpp | 2 +- source/cClientHandle.cpp | 11 +++++++++++ source/cPlayer.cpp | 19 +++++++++++++++++-- source/cPlayer.h | 2 +- source/packets/cPacket_PlayerListItem.cpp | 27 +++++++++++++++++++++++++++ source/packets/cPacket_PlayerListItem.h | 21 +++++++++++++++++++++ source/packets/cPacket_WindowClose.h | 2 +- 10 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 source/packets/cPacket_PlayerListItem.cpp create mode 100644 source/packets/cPacket_PlayerListItem.h diff --git a/VC2010/MCServer.vcxproj b/VC2010/MCServer.vcxproj index 36229f4b6..04f73eafa 100644 --- a/VC2010/MCServer.vcxproj +++ b/VC2010/MCServer.vcxproj @@ -316,6 +316,7 @@ + @@ -470,6 +471,7 @@ + diff --git a/VC2010/MCServer.vcxproj.filters b/VC2010/MCServer.vcxproj.filters index 02a305eb0..a7a523f6c 100644 --- a/VC2010/MCServer.vcxproj.filters +++ b/VC2010/MCServer.vcxproj.filters @@ -418,6 +418,9 @@ {71574b1c-a518-4a17-92c1-e3ea340f73bc} + + {b690d7b6-3697-4d91-bab3-21fd652986be} + @@ -835,6 +838,9 @@ cEntity\cPawn\cMonster\Personalities\Passive + + Packets\cPacket_PlayerListItem + @@ -1287,6 +1293,9 @@ cEntity\cPawn\cMonster\Personalities\Passive + + Packets\cPacket_PlayerListItem + diff --git a/source/PacketID.h b/source/PacketID.h index f19d7a795..7fc068b53 100644 --- a/source/PacketID.h +++ b/source/PacketID.h @@ -52,6 +52,7 @@ enum ENUM_PACKET_ID E_INVENTORY_PROGRESS= 0x69, E_CREATE_INVENTORY_ACTION = 0x6B, E_UPDATE_SIGN = 0x82, + E_PLAYER_LIST_ITEM = 0xC9, E_PING = 0xfe, E_DISCONNECT = 0xff, }; diff --git a/source/cChatColor.cpp b/source/cChatColor.cpp index a0f7813c5..4f955dcb7 100644 --- a/source/cChatColor.cpp +++ b/source/cChatColor.cpp @@ -21,5 +21,5 @@ const std::string cChatColor::White = cChatColor::Color + "f"; const std::string cChatColor::MakeColor( char a_Color ) { - return cChatColor::Color + a_Color; + return cChatColor::Delimiter + a_Color; } \ No newline at end of file diff --git a/source/cClientHandle.cpp b/source/cClientHandle.cpp index 902e3df2f..c9d1de1ce 100644 --- a/source/cClientHandle.cpp +++ b/source/cClientHandle.cpp @@ -65,6 +65,7 @@ #include "packets/cPacket_13.h" #include "packets/cPacket_UpdateSign.h" #include "packets/cPacket_Ping.h" +#include "packets/cPacket_PlayerListItem.h" #ifndef _WIN32 @@ -155,6 +156,7 @@ cClientHandle::cClientHandle(const cSocket & a_Socket) m_pState->PacketMap[E_UPDATE_SIGN] = new cPacket_UpdateSign; m_pState->PacketMap[E_RESPAWN] = new cPacket_Respawn; m_pState->PacketMap[E_PING] = new cPacket_Ping; + m_pState->PacketMap[E_PLAYER_LIST_ITEM] = new cPacket_PlayerListItem; memset( m_LoadedChunks, 0x00, sizeof(cChunk*)*VIEWDISTANCE*VIEWDISTANCE ); @@ -1161,6 +1163,15 @@ void cClientHandle::HandlePacket( cPacket* a_Packet ) { cPacket_Chat DisconnectMessage( m_pState->Username + " disconnected: " + PacketData->m_Reason ); cRoot::Get()->GetServer()->Broadcast( DisconnectMessage ); + cWorld::PlayerList PlayerList = cRoot::Get()->GetWorld()->GetAllPlayers(); + for( cWorld::PlayerList::iterator itr = PlayerList.begin(); itr != PlayerList.end(); ++itr ) + { + cPacket_PlayerListItem PlayerList; + PlayerList.m_PlayerName = GetUsername(); + PlayerList.m_Online = false; + PlayerList.m_Ping = (short)5; + (*itr)->GetClientHandle()->Send( PlayerList ); + } } Destroy(); return; diff --git a/source/cPlayer.cpp b/source/cPlayer.cpp index d13e1e5aa..fd7a09a6b 100644 --- a/source/cPlayer.cpp +++ b/source/cPlayer.cpp @@ -29,6 +29,7 @@ #include "packets/cPacket_Metadata.h" #include "packets/cPacket_Chat.h" #include "packets/cPacket_NewInvalidState.h" +#include "packets/cPacket_PlayerListItem.h" #include "Vector3d.h" #include "Vector3f.h" @@ -232,6 +233,20 @@ void cPlayer::Tick(float a_Dt) if(e_EPMetaState == BURNING){ InStateBurning(a_Dt); } + + // Send Player List + cWorld::PlayerList PlayerList = cRoot::Get()->GetWorld()->GetAllPlayers(); + for( cWorld::PlayerList::iterator itr = PlayerList.begin(); itr != PlayerList.end(); ++itr ) + { + if ((*itr) && (*itr)->GetClientHandle() && !((*itr)->GetClientHandle()->IsDestroyed())) { + cPacket_PlayerListItem PlayerList; + PlayerList.m_PlayerName = GetColor() + GetName(); + PlayerList.m_Online = true; + PlayerList.m_Ping = (short)5; + (*itr)->GetClientHandle()->Send( PlayerList ); + } + } + } void cPlayer::InStateBurning(float a_Dt) { @@ -406,10 +421,10 @@ void cPlayer::OpenWindow( cWindow* a_Window ) m_CurrentWindow = a_Window; } -void cPlayer::CloseWindow(char wID = -1) +void cPlayer::CloseWindow(char a_WindowType) { if( m_CurrentWindow ) m_CurrentWindow->Close( *this ); - if (wID == 0) { + if (a_WindowType == 0) { if(GetInventory().GetWindow()->GetDraggingItem() && GetInventory().GetWindow()->GetDraggingItem()->m_ItemCount > 0) { LOG("Player holds item! Dropping it..."); diff --git a/source/cPlayer.h b/source/cPlayer.h index c8bd6c473..e25fb1b07 100644 --- a/source/cPlayer.h +++ b/source/cPlayer.h @@ -46,7 +46,7 @@ public: cWindow* GetWindow() { return m_CurrentWindow; } void OpenWindow( cWindow* a_Window ); - void CloseWindow(char wID); + void CloseWindow(char a_WindowType); cClientHandle* GetClientHandle() { return m_ClientHandle; } //tolua_export void SetClientHandle( cClientHandle* a_Client ) { m_ClientHandle = a_Client; } diff --git a/source/packets/cPacket_PlayerListItem.cpp b/source/packets/cPacket_PlayerListItem.cpp new file mode 100644 index 000000000..b0726dc8a --- /dev/null +++ b/source/packets/cPacket_PlayerListItem.cpp @@ -0,0 +1,27 @@ +#include "cPacket_PlayerListItem.h" + +bool cPacket_PlayerListItem::Parse( cSocket & a_Socket ) +{ + m_Socket = a_Socket; + if (!ReadString(m_PlayerName)) return false; + if (!ReadBool(m_Online)) return false; + if (!ReadShort(m_Ping)) return false; + return true; +} + +bool cPacket_PlayerListItem::Send( cSocket & a_Socket ) +{ + m_PlayerName = m_PlayerName.substr(0,16); + unsigned int TotalSize = c_Size + m_PlayerName.size()*sizeof(short); + char* Message = new char[TotalSize]; + + unsigned int i = 0; + AppendByte((char)m_PacketID, Message, i); + AppendString16(m_PlayerName, Message, i); + AppendBool(m_Online, Message, i); + AppendShort(m_Ping, Message, i); + + bool RetVal = !cSocket::IsSocketError( SendData( a_Socket, Message, TotalSize, 0 ) ); + delete [] Message; + return RetVal; +} diff --git a/source/packets/cPacket_PlayerListItem.h b/source/packets/cPacket_PlayerListItem.h new file mode 100644 index 000000000..a4e10eb74 --- /dev/null +++ b/source/packets/cPacket_PlayerListItem.h @@ -0,0 +1,21 @@ +#pragma once + +#include "cPacket.h" +#include "PacketID.h" + +class cPacket_PlayerListItem : public cPacket +{ +public: + cPacket_PlayerListItem() { m_PacketID = E_PLAYER_LIST_ITEM; } + + bool Parse(cSocket & a_Socket); + bool Send(cSocket & a_Socket); + + virtual cPacket* Clone() const { return new cPacket_PlayerListItem(*this); } + + std::string m_PlayerName; // Supports chat coloring, limited to 16 characters. + bool m_Online; + short m_Ping; + + static const unsigned int c_Size = 6; // Minimal size ( 6 + string ) +}; \ No newline at end of file diff --git a/source/packets/cPacket_WindowClose.h b/source/packets/cPacket_WindowClose.h index 1940eec0a..4784586a2 100644 --- a/source/packets/cPacket_WindowClose.h +++ b/source/packets/cPacket_WindowClose.h @@ -14,7 +14,7 @@ public: bool Parse(cSocket & a_Socket); bool Send(cSocket & a_Socket); - char m_Close; // 1 = close + char m_Close; // m_Close == cWindow WindowType number static const unsigned int c_Size = 1 + 1; }; \ No newline at end of file -- cgit v1.2.3