1
0

recover hotbar selected slot after reconnect (#4249)

1. implement protocol message SendHeldItemChange
2. add save / load inventory equipped item slot in JSON
3. send held item slot message after player connect to server

Fixes #4189
This commit is contained in:
changyong guo 2018-07-23 06:23:33 +08:00 committed by peterbell10
parent 7b431bed51
commit 3e802932a6
12 changed files with 79 additions and 0 deletions

View File

@ -417,6 +417,9 @@ void cClientHandle::FinishAuthenticate(const AString & a_Name, const cUUID & a_U
// Send experience
m_Player->SendExperience();
// Send hotbar active slot
m_Player->SendHotbarActiveSlot();
// Send player list items
SendPlayerListAddPlayer(*m_Player);
cRoot::Get()->BroadcastPlayerListsAddPlayer(*m_Player);
@ -2635,6 +2638,15 @@ void cClientHandle::SendHealth(void)
void cClientHandle::SendHeldItemChange(int a_ItemIndex)
{
m_Protocol->SendHeldItemChange(a_ItemIndex);
}
void cClientHandle::SendHideTitle(void)
{
m_Protocol->SendHideTitle();

View File

@ -176,6 +176,7 @@ public: // tolua_export
void SendExplosion (double a_BlockX, double a_BlockY, double a_BlockZ, float a_Radius, const cVector3iArray & a_BlocksAffected, const Vector3d & a_PlayerMotion);
void SendGameMode (eGameMode a_GameMode);
void SendHealth (void);
void SendHeldItemChange (int a_ItemIndex);
void SendHideTitle (void); // tolua_export
void SendInventorySlot (char a_WindowID, short a_SlotNum, const cItem & a_Item);
void SendLeashEntity (const cEntity & a_Entity, const cEntity & a_EntityLeashedTo);

View File

@ -743,6 +743,18 @@ void cPlayer::SendHealth(void)
void cPlayer::SendHotbarActiveSlot(void)
{
if (m_ClientHandle != nullptr)
{
m_ClientHandle->SendHeldItemChange(m_Inventory.GetEquippedSlotNum());
}
}
void cPlayer::SendExperience(void)
{
if (m_ClientHandle != nullptr)
@ -2212,6 +2224,10 @@ bool cPlayer::LoadFromFile(const AString & a_FileName, cWorldPtr & a_World)
}
m_Inventory.LoadFromJson(root["inventory"]);
int equippedSlotNum = root.get("equippedItemSlot", 0).asInt();
m_Inventory.SetEquippedSlotNum(equippedSlotNum);
cEnderChestEntity::LoadFromJson(root["enderchestinventory"], m_EnderChestContents);
m_LoadedWorldName = root.get("world", "world").asString();
@ -2302,6 +2318,7 @@ bool cPlayer::SaveToDisk()
root["position"] = JSON_PlayerPosition;
root["rotation"] = JSON_PlayerRotation;
root["inventory"] = JSON_Inventory;
root["equippedItemSlot"] = m_Inventory.GetEquippedSlotNum();
root["enderchestinventory"] = JSON_EnderChestInventory;
root["health"] = m_Health;
root["xpTotal"] = m_LifetimeTotalXp;

View File

@ -431,6 +431,9 @@ public:
void SendHealth(void);
// Send current active hotbar slot
void SendHotbarActiveSlot(void);
void SendExperience(void);
/** In UI windows, get the item that the player is dragging */

View File

@ -41,6 +41,7 @@ UInt32 cProtocol_1_9_0::GetPacketId(eOutgoingPackets a_Packet)
case sendExplosion: return 0x1c;
case sendGameMode: return 0x1e;
case sendHealth: return 0x3e;
case sendHeldItemChange: return 0x37;
case sendInventorySlot: return 0x16;
case sendJoinGame: return 0x23;
case sendKeepAlive: return 0x1f;
@ -124,6 +125,7 @@ UInt32 cProtocol_1_12::GetPacketId(eOutgoingPackets a_Packet)
case sendEntityVelocity: return 0x3d;
case sendExperience: return 0x3f;
case sendHealth: return 0x40;
case sendHeldItemChange: return 0x39;
case sendLeashEntity: return 0x3c;
case sendPlayerMaxSpeed: return 0x4d;
case sendRemoveEntityEffect: return 0x32;
@ -161,6 +163,7 @@ UInt32 cProtocol_1_12_1::GetPacketId(eOutgoingPackets a_Packet)
case sendEntityVelocity: return 0x3e;
case sendExperience: return 0x40;
case sendHealth: return 0x41;
case sendHeldItemChange: return 0x3a;
case sendLeashEntity: return 0x3d;
case sendPlayerList: return 0x2e;
case sendPlayerAbilities: return 0x2c;

View File

@ -89,6 +89,7 @@ public:
sendExplosion,
sendGameMode,
sendHealth,
sendHeldItemChange,
sendInventorySlot,
sendJoinGame,
sendKeepAlive,
@ -160,6 +161,7 @@ public:
virtual void SendExplosion (double a_BlockX, double a_BlockY, double a_BlockZ, float a_Radius, const cVector3iArray & a_BlocksAffected, const Vector3d & a_PlayerMotion) = 0;
virtual void SendGameMode (eGameMode a_GameMode) = 0;
virtual void SendHealth (void) = 0;
virtual void SendHeldItemChange (int a_ItemIndex) = 0;
virtual void SendHideTitle (void) = 0;
virtual void SendInventorySlot (char a_WindowID, short a_SlotNum, const cItem & a_Item) = 0;
virtual void SendKeepAlive (UInt32 a_PingID) = 0;

View File

@ -428,6 +428,17 @@ void cProtocolRecognizer::SendHealth(void)
void cProtocolRecognizer::SendHeldItemChange(int a_ItemIndex)
{
ASSERT(m_Protocol != nullptr);
m_Protocol->SendHeldItemChange(a_ItemIndex);
}
void cProtocolRecognizer::SendHideTitle(void)
{
ASSERT(m_Protocol != nullptr);

View File

@ -85,6 +85,7 @@ public:
virtual void SendExplosion (double a_BlockX, double a_BlockY, double a_BlockZ, float a_Radius, const cVector3iArray & a_BlocksAffected, const Vector3d & a_PlayerMotion) override;
virtual void SendGameMode (eGameMode a_GameMode) override;
virtual void SendHealth (void) override;
virtual void SendHeldItemChange (int a_ItemIndex) override;
virtual void SendHideTitle (void) override;
virtual void SendInventorySlot (char a_WindowID, short a_SlotNum, const cItem & a_Item) override;
virtual void SendKeepAlive (UInt32 a_PingID) override;

View File

@ -592,6 +592,19 @@ void cProtocol_1_8_0::SendHealth(void)
void cProtocol_1_8_0::SendHeldItemChange(int a_ItemIndex)
{
ASSERT((a_ItemIndex >= 0) && (a_ItemIndex <= 8)); // Valid check
cPacketizer Pkt(*this, 0x09); // Held item change
cPlayer * Player = m_Client->GetPlayer();
Pkt.WriteBEInt8(static_cast<Int8>(Player->GetInventory().GetEquippedSlotNum()));
}
void cProtocol_1_8_0::SendHideTitle(void)
{
ASSERT(m_State == 3); // In game mode?

View File

@ -77,6 +77,7 @@ public:
virtual void SendExplosion (double a_BlockX, double a_BlockY, double a_BlockZ, float a_Radius, const cVector3iArray & a_BlocksAffected, const Vector3d & a_PlayerMotion) override;
virtual void SendGameMode (eGameMode a_GameMode) override;
virtual void SendHealth (void) override;
virtual void SendHeldItemChange (int a_ItemIndex) override;
virtual void SendHideTitle (void) override;
virtual void SendInventorySlot (char a_WindowID, short a_SlotNum, const cItem & a_Item) override;
virtual void SendKeepAlive (UInt32 a_PingID) override;

View File

@ -636,6 +636,20 @@ void cProtocol_1_9_0::SendHealth(void)
void cProtocol_1_9_0::SendHeldItemChange(int a_ItemIndex)
{
ASSERT((a_ItemIndex >= 0) && (a_ItemIndex <= 8)); // Valid check
cPacketizer Pkt(*this, GetPacketId(sendHeldItemChange)); // Held item change
cPlayer * Player = m_Client->GetPlayer();
Pkt.WriteBEInt8(static_cast<Int8>(Player->GetInventory().GetEquippedSlotNum()));
}
void cProtocol_1_9_0::SendHideTitle(void)
{
ASSERT(m_State == 3); // In game mode?

View File

@ -83,6 +83,7 @@ public:
virtual void SendExplosion (double a_BlockX, double a_BlockY, double a_BlockZ, float a_Radius, const cVector3iArray & a_BlocksAffected, const Vector3d & a_PlayerMotion) override;
virtual void SendGameMode (eGameMode a_GameMode) override;
virtual void SendHealth (void) override;
virtual void SendHeldItemChange (int a_ItemIndex) override;
virtual void SendHideTitle (void) override;
virtual void SendInventorySlot (char a_WindowID, short a_SlotNum, const cItem & a_Item) override;
virtual void SendKeepAlive (UInt32 a_PingID) override;