diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client.cpp | 10 | ||||
-rw-r--r-- | src/clientserver.h | 8 | ||||
-rw-r--r-- | src/game.cpp | 28 | ||||
-rw-r--r-- | src/player.cpp | 7 | ||||
-rw-r--r-- | src/player.h | 2 | ||||
-rw-r--r-- | src/scriptapi.cpp | 28 | ||||
-rw-r--r-- | src/server.cpp | 32 | ||||
-rw-r--r-- | src/server.h | 2 |
8 files changed, 109 insertions, 8 deletions
diff --git a/src/client.cpp b/src/client.cpp index 1327feb1f..fdfb49c84 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -1688,6 +1688,16 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) } infostream<<std::endl; } + else if(command == TOCLIENT_INVENTORY_FORMSPEC) + { + std::string datastring((char*)&data[2], datasize-2); + std::istringstream is(datastring, std::ios_base::binary); + + // Store formspec in LocalPlayer + Player *player = m_env.getLocalPlayer(); + assert(player != NULL); + player->inventory_formspec = deSerializeLongString(is); + } else { infostream<<"Client: Ignoring unknown command " diff --git a/src/clientserver.h b/src/clientserver.h index 559fb5b15..07e7b831d 100644 --- a/src/clientserver.h +++ b/src/clientserver.h @@ -58,6 +58,7 @@ with this program; if not, write to the Free Software Foundation, Inc., PROTOCOL_VERSION 11: TileDef in ContentFeatures Nodebox drawtype + Added after a release: TOCLIENT_INVENTORY_FORMSPEC */ #define PROTOCOL_VERSION 11 @@ -308,6 +309,13 @@ enum ToClientCommand u16 len u8[len] privilege */ + + TOCLIENT_INVENTORY_FORMSPEC = 0x42, + /* + u16 command + u32 len + u8[len] formspec + */ }; enum ToServerCommand diff --git a/src/game.cpp b/src/game.cpp index 98ffac3d5..b29d2d646 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -144,6 +144,22 @@ public: v3s16 m_p; }; +class PlayerInventoryFormSource: public IFormSource +{ +public: + PlayerInventoryFormSource(Client *client): + m_client(client) + { + } + std::string getForm() + { + LocalPlayer* player = m_client->getEnv().getLocalPlayer(); + return player->inventory_formspec; + } + + Client *m_client; +}; + /* Hotbar draw routine */ @@ -1463,14 +1479,10 @@ void the_game( InventoryLocation inventoryloc; inventoryloc.setCurrentPlayer(); - menu->setFormSpec( - "invsize[8,7.5;]" - //"image[1,0.6;1,2;player.png]" - "list[current_player;main;0,3.5;8,4;]" - "list[current_player;craft;3,0;3,3;]" - "list[current_player;craftpreview;7,1;1,1;]" - , inventoryloc); - + PlayerInventoryFormSource *src = new PlayerInventoryFormSource(&client); + assert(src); + menu->setFormSpec(src->getForm(), inventoryloc); + menu->setFormSource(new PlayerInventoryFormSource(&client)); menu->drop(); } else if(input->wasKeyDown(EscapeKey)) diff --git a/src/player.cpp b/src/player.cpp index 999d842fa..6dd90e957 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -48,6 +48,13 @@ Player::Player(IGameDef *gamedef): inventory.addList("craft", 9); inventory.addList("craftpreview", 1); inventory.addList("craftresult", 1); + + // Can be redefined via Lua + inventory_formspec = "invsize[8,7.5;]" + //"image[1,0.6;1,2;player.png]" + "list[current_player;main;0,3.5;8,4;]" + "list[current_player;craft;3,0;3,3;]" + "list[current_player;craftpreview;7,1;1,1;]"; } Player::~Player() diff --git a/src/player.h b/src/player.h index c3ccc960d..352b93e88 100644 --- a/src/player.h +++ b/src/player.h @@ -157,6 +157,8 @@ public: u16 peer_id; + std::string inventory_formspec; + protected: IGameDef *m_gamedef; diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index f06485458..25af2d105 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -2864,6 +2864,32 @@ private: return 1; } + // set_inventory_formspec(self, formspec) + static int l_set_inventory_formspec(lua_State *L) + { + ObjectRef *ref = checkobject(L, 1); + Player *player = getplayer(ref); + if(player == NULL) return 0; + std::string formspec = luaL_checkstring(L, 2); + + player->inventory_formspec = formspec; + get_server(L)->reportInventoryFormspecModified(player->getName()); + lua_pushboolean(L, true); + return 1; + } + + // get_inventory_formspec(self) -> formspec + static int l_get_inventory_formspec(lua_State *L) + { + ObjectRef *ref = checkobject(L, 1); + Player *player = getplayer(ref); + if(player == NULL) return 0; + + std::string formspec = player->inventory_formspec; + lua_pushlstring(L, formspec.c_str(), formspec.size()); + return 1; + } + public: ObjectRef(ServerActiveObject *object): m_object(object) @@ -2960,6 +2986,8 @@ const luaL_reg ObjectRef::methods[] = { method(ObjectRef, get_look_dir), method(ObjectRef, get_look_pitch), method(ObjectRef, get_look_yaw), + method(ObjectRef, set_inventory_formspec), + method(ObjectRef, get_inventory_formspec), {0,0} }; diff --git a/src/server.cpp b/src/server.cpp index f8eaaf9c3..d703f7d9b 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2243,6 +2243,9 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) // Send privileges SendPlayerPrivileges(peer_id); + // Send inventory formspec + SendPlayerInventoryFormspec(peer_id); + // Send inventory UpdateCrafting(peer_id); SendInventory(peer_id); @@ -3573,6 +3576,9 @@ void Server::SendPlayerPrivileges(u16 peer_id) { Player *player = m_env->getPlayer(peer_id); assert(player); + if(player->peer_id == PEER_ID_INEXISTENT) + return; + std::set<std::string> privs; scriptapi_get_auth(m_lua, player->getName(), NULL, &privs); @@ -3591,6 +3597,24 @@ void Server::SendPlayerPrivileges(u16 peer_id) m_con.Send(peer_id, 0, data, true); } +void Server::SendPlayerInventoryFormspec(u16 peer_id) +{ + Player *player = m_env->getPlayer(peer_id); + assert(player); + if(player->peer_id == PEER_ID_INEXISTENT) + return; + + std::ostringstream os(std::ios_base::binary); + writeU16(os, TOCLIENT_INVENTORY_FORMSPEC); + os<<serializeLongString(player->inventory_formspec); + + // Make data buffer + std::string s = os.str(); + SharedBuffer<u8> data((u8*)s.c_str(), s.size()); + // Send as reliable + m_con.Send(peer_id, 0, data, true); +} + s32 Server::playSound(const SimpleSoundSpec &spec, const ServerSoundParams ¶ms) { @@ -4357,6 +4381,14 @@ void Server::reportPrivsModified(const std::string &name) } } +void Server::reportInventoryFormspecModified(const std::string &name) +{ + Player *player = m_env->getPlayer(name.c_str()); + if(!player) + return; + SendPlayerInventoryFormspec(player->peer_id); +} + // Saves g_settings to configpath given at initialization void Server::saveConfig() { diff --git a/src/server.h b/src/server.h index 4d93ae6f3..36c243ccd 100644 --- a/src/server.h +++ b/src/server.h @@ -505,6 +505,7 @@ public: std::set<std::string> getPlayerEffectivePrivs(const std::string &name); bool checkPriv(const std::string &name, const std::string &priv); void reportPrivsModified(const std::string &name=""); // ""=all + void reportInventoryFormspecModified(const std::string &name); // Saves g_settings to configpath given at initialization void saveConfig(); @@ -602,6 +603,7 @@ private: void SendPlayerHP(u16 peer_id); void SendMovePlayer(u16 peer_id); void SendPlayerPrivileges(u16 peer_id); + void SendPlayerInventoryFormspec(u16 peer_id); /* Send a node removal/addition event to all clients except ignore_id. Additionally, if far_players!=NULL, players further away than |