1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-11 17:51:04 +00:00

Notify other players of wielded item change

This commit is contained in:
Giuseppe Bilotta 2011-08-11 07:02:57 +02:00
parent 88a9bae160
commit 371af9c241
4 changed files with 110 additions and 0 deletions

View file

@ -2157,6 +2157,9 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
UpdateCrafting(peer->id);
SendInventory(peer->id);
// Send player items to all players
SendPlayerItems();
// Send HP
{
Player *player = m_env.getPlayer(peer_id);
@ -3388,6 +3391,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
u16 item = readU16(&data[2]);
player->wieldItem(item);
SendWieldedItem(player);
}
else
{
@ -3673,6 +3677,60 @@ void Server::SendInventory(u16 peer_id)
m_con.Send(peer_id, 0, data, true);
}
std::string getWieldedItemString(const Player *player)
{
const InventoryItem *item = player->getWieldItem();
if (item == NULL)
return std::string("");
std::ostringstream os(std::ios_base::binary);
item->serialize(os);
return os.str();
}
void Server::SendWieldedItem(const Player* player)
{
DSTACK(__FUNCTION_NAME);
assert(player);
std::ostringstream os(std::ios_base::binary);
writeU16(os, TOCLIENT_PLAYERITEM);
writeU16(os, 1);
writeU16(os, player->peer_id);
os<<serializeString(getWieldedItemString(player));
// Make data buffer
std::string s = os.str();
SharedBuffer<u8> data((u8*)s.c_str(), s.size());
m_con.SendToAll(0, data, true);
}
void Server::SendPlayerItems()
{
DSTACK(__FUNCTION_NAME);
std::ostringstream os(std::ios_base::binary);
core::list<Player *> players = m_env.getPlayers(true);
writeU16(os, TOCLIENT_PLAYERITEM);
writeU16(os, players.size());
core::list<Player *>::Iterator i;
for(i = players.begin(); i != players.end(); ++i)
{
Player *p = *i;
writeU16(os, p->peer_id);
os<<serializeString(getWieldedItemString(p));
}
// Make data buffer
std::string s = os.str();
SharedBuffer<u8> data((u8*)s.c_str(), s.size());
m_con.SendToAll(0, data, true);
}
void Server::SendChatMessage(u16 peer_id, const std::wstring &message)
{
DSTACK(__FUNCTION_NAME);