From b5e898a608cfa0b872828db842e31ec2d624e598 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Mon, 3 Feb 2014 21:12:44 +0000 Subject: [PATCH] Server now handles join messages also * Revised as well hook documentation --- .../Plugins/APIDump/Hooks/OnDisconnect.lua | 16 ++++++--------- .../APIDump/Hooks/OnPlayerDestroyed.lua | 10 +++++++--- .../Plugins/APIDump/Hooks/OnPlayerJoined.lua | 8 ++++---- src/ClientHandle.cpp | 20 ++++++++++--------- src/Entities/Player.cpp | 8 +++++++- 5 files changed, 35 insertions(+), 27 deletions(-) diff --git a/MCServer/Plugins/APIDump/Hooks/OnDisconnect.lua b/MCServer/Plugins/APIDump/Hooks/OnDisconnect.lua index 496e0d751..a3301a8c6 100644 --- a/MCServer/Plugins/APIDump/Hooks/OnDisconnect.lua +++ b/MCServer/Plugins/APIDump/Hooks/OnDisconnect.lua @@ -5,13 +5,10 @@ return CalledWhen = "A player has explicitly disconnected.", DefaultFnName = "OnDisconnect", -- also used as pagename Desc = [[ - This hook is called when a client sends the disconnect packet and is about to be disconnected from - the server.

-

- Note that this callback is not called if the client drops the connection or is kicked by the - server.

-

- FIXME: There is no callback for "client destroying" that would be called in all circumstances.

+ This hook is called when a client is about to be disconnected from the server, for whatever reason. + +

Note that this hook will be removed after <1.7 protocol support is removed, as it was originally a hook for + the client sending the server a disconnect packet, which no longer happens.

]], Params = { @@ -19,9 +16,8 @@ return { Name = "Reason", Type = "string", Notes = "The reason that the client has sent in the disconnect packet" }, }, Returns = [[ - If the function returns false or no value, MCServer calls other plugins' callbacks for this event - and finally broadcasts a disconnect message to the player's world. If the function returns true, no - other plugins are called for this event and the disconnect message is not broadcast. In either case, + If the function returns false or no value, MCServer calls other plugins' callbacks for this event. + If the function returns true, no other plugins are called for this event. In either case, the player is disconnected. ]], }, -- HOOK_DISCONNECT diff --git a/MCServer/Plugins/APIDump/Hooks/OnPlayerDestroyed.lua b/MCServer/Plugins/APIDump/Hooks/OnPlayerDestroyed.lua index 8e503658c..dc033197a 100644 --- a/MCServer/Plugins/APIDump/Hooks/OnPlayerDestroyed.lua +++ b/MCServer/Plugins/APIDump/Hooks/OnPlayerDestroyed.lua @@ -2,17 +2,21 @@ return { HOOK_PLAYER_DESTROYED = { - CalledWhen = "A player is about to get destroyed.", + CalledWhen = "A player object is about to be destroyed.", DefaultFnName = "OnPlayerDestroyed", -- also used as pagename Desc = [[ - This function is called when a {{cPlayer|player}} is about to get destroyed. But the player isn't already destroyed. + This function is called before a {{cPlayer|player}} is about to be destroyed. + The player has disconnected for whatever reason and is no longer in the server. + If a plugin returns true, a leave message is not broadcast, and vice versa. + However, whatever the return value, the player object is removed from memory. ]], Params = { { Name = "Player", Type = "{{cPlayer}}", Notes = "The destroyed player" }, }, Returns = [[ - It's only for notification. Can't be returned. + If the function returns false or no value, other plugins' callbacks are called and a leave message is broadcast. + If the function returns true, no other callbacks are called for this event and no leave message appears. Either way the player is removed internally. ]], }, -- HOOK_PLAYER_DESTROYED } diff --git a/MCServer/Plugins/APIDump/Hooks/OnPlayerJoined.lua b/MCServer/Plugins/APIDump/Hooks/OnPlayerJoined.lua index 00805af7e..dcd16ed00 100644 --- a/MCServer/Plugins/APIDump/Hooks/OnPlayerJoined.lua +++ b/MCServer/Plugins/APIDump/Hooks/OnPlayerJoined.lua @@ -9,16 +9,16 @@ return enabled, this function is called after their name has been authenticated. It is called after {{OnLogin|HOOK_LOGIN}} and before {{OnPlayerSpawned|HOOK_PLAYER_SPAWNED}}, right after the player's entity is created, but not added to the world yet. The player is not yet visible to other players. - This is a notification-only event, plugins wishing to refuse player's entry should kick the player - using the {{cPlayer}}:Kick() function. + Returning true will block a join message from being broadcast, but otherwise, the player is still allowed to join. + Plugins wishing to refuse player's entry should kick the player using the {{cPlayer}}:Kick() function. ]], Params = { { Name = "Player", Type = "{{cPlayer}}", Notes = "The player who has joined the game" }, }, Returns = [[ - If the function returns false or no value, other plugins' callbacks are called. If the function - returns true, no other callbacks are called for this event. Either way the player is let in. + If the function returns false or no value, other plugins' callbacks are called and a join message is broadcast. If the function + returns true, no other callbacks are called for this event and a join message is not sent. Either way the player is let in. ]], }, -- HOOK_PLAYER_JOINED } diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index 6b61eaae8..b0eb27089 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -227,7 +227,13 @@ void cClientHandle::Authenticate(void) m_Player->SetIP (m_IPString); - cRoot::Get()->GetPluginManager()->CallHookPlayerJoined(*m_Player); + if (!cRoot::Get()->GetPluginManager()->CallHookPlayerJoined(*m_Player)) + { + AString JoinMessage; + AppendPrintf(JoinMessage, "%s[JOIN] %s%s has joined the game", cChatColor::Yellow.c_str(), cChatColor::White.c_str(), m_Username.c_str()); + cRoot::Get()->BroadcastChat(JoinMessage); + LOGINFO("Player %s has joined the game.", m_Username.c_str()); + } m_ConfirmPosition = m_Player->GetPosition(); @@ -910,7 +916,7 @@ void cClientHandle::HandleRightClick(int a_BlockX, int a_BlockY, int a_BlockZ, c cItemHandler * ItemHandler = cItemHandler::GetItemHandler(Equipped.m_ItemType); - if (ItemHandler->IsPlaceable()) + if (ItemHandler->IsPlaceable() && (a_BlockFace > -1)) { HandlePlaceBlock(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, *ItemHandler); } @@ -1330,13 +1336,9 @@ void cClientHandle::HandleRespawn(void) void cClientHandle::HandleDisconnect(const AString & a_Reason) { 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)) - { - AString DisconnectMessage; - Printf(DisconnectMessage, "%s[LEAVE] %s%s has left the game", cChatColor::Yellow.c_str(), cChatColor::White.c_str(), m_Username.c_str()); - cRoot::Get()->BroadcastChat(DisconnectMessage); - LOGINFO("Player %s has left the game.", m_Username.c_str()); - } + + cRoot::Get()->GetPluginManager()->CallHookDisconnect(m_Player, a_Reason); + m_HasSentDC = true; Destroy(); } diff --git a/src/Entities/Player.cpp b/src/Entities/Player.cpp index 8c37fdc8d..a1c942c20 100644 --- a/src/Entities/Player.cpp +++ b/src/Entities/Player.cpp @@ -130,7 +130,13 @@ cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName) cPlayer::~cPlayer(void) { - cRoot::Get()->GetPluginManager()->CallHookPlayerDestroyed(*this); + if (!cRoot::Get()->GetPluginManager()->CallHookPlayerDestroyed(*this)) + { + AString DisconnectMessage; + AppendPrintf(DisconnectMessage, "%s[LEAVE] %s%s has left the game", cChatColor::Yellow.c_str(), cChatColor::White.c_str(), GetClientHandle()->GetUsername().c_str()); + cRoot::Get()->BroadcastChat(DisconnectMessage); + LOGINFO("Player %s has left the game.", GetClientHandle()->GetUsername().c_str()); + } LOGD("Deleting cPlayer \"%s\" at %p, ID %d", m_PlayerName.c_str(), this, GetUniqueID());