From 25a319bb2355d95db4daf3ab2f91024e7326b42c Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Mon, 16 Jun 2025 18:10:44 +0200 Subject: [PATCH] comments --- doc/lua_api.md | 9 ++++++--- src/client/game_formspec.cpp | 24 +++++++++++++----------- src/client/game_formspec.h | 4 ++-- src/client/localplayer.h | 1 + 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/doc/lua_api.md b/doc/lua_api.md index 7350b140c..91e988690 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -6873,7 +6873,7 @@ Formspec * `playername`: name of player to show formspec * `formname`: name passed to `on_player_receive_fields` callbacks. * It should follow the `"modname:"` naming convention. - * If empty: Shows an inventory formspec. Use + * If empty: Shows a custom, temporary inventory formspec. Use `ObjectRef:set_inventory_formspec` to change it for future opens. Supported if server AND client are both of version >= 5.13.0. * `formspec`: formspec to display @@ -8654,9 +8654,12 @@ child will follow movement and rotation of that bone. * Returns `nil` if no attribute found. * `get_meta()`: Returns metadata associated with the player (a PlayerMetaRef). * `set_inventory_formspec(formspec)` - * Redefine player's inventory form. This sends an update to the player. - * Should usually be called in `on_joinplayer` + * Redefines the player's inventory formspec. + * Should usually be called at least once in the `on_joinplayer` callback. * If `formspec` is `""`, the player's inventory is disabled. + * If the inventory formspec is currently open on the client, it is + updated immediately. + * See also: `core.register_on_player_receive_fields` * `get_inventory_formspec()`: returns a formspec string * `set_formspec_prepend(formspec)`: * the formspec string will be added to every formspec shown to the user, diff --git a/src/client/game_formspec.cpp b/src/client/game_formspec.cpp index ddca7003c..e9c3a1659 100644 --- a/src/client/game_formspec.cpp +++ b/src/client/game_formspec.cpp @@ -321,33 +321,35 @@ void GameFormSpec::showPlayerInventory(const std::string *fs_override) infostream << "Game: Launching inventory" << std::endl; - PlayerInventoryFormSource *fs_src = new PlayerInventoryFormSource(m_client); + auto fs_src = std::make_unique(m_client); InventoryLocation inventoryloc; inventoryloc.setCurrentPlayer(); - if (fs_override) + if (fs_override) { + // Temporary overwrite for this specific formspec. player->inventory_formspec_override = *fs_override; - else + } else { + // Show the regular inventory formspec player->inventory_formspec_override.clear(); - - if (m_client->modsLoaded() && m_client->getScript()->on_inventory_open(m_client->getInventory(inventoryloc))) { - delete fs_src; - return; } - if (fs_src->getForm().empty() || (fs_override && fs_override->empty())) { - delete fs_src; + // If preventedd by Client-Side Mods + if (m_client->modsLoaded() && m_client->getScript()->on_inventory_open(m_client->getInventory(inventoryloc))) + return; + + // Empty formspec -> do not show. + if (fs_src->getForm().empty()) return; - } TextDest *txt_dst = new TextDestPlayerInventory(m_client); GUIFormSpecMenu::create(m_formspec, m_client, m_rendering_engine->get_gui_env(), - &m_input->joystick, fs_src, txt_dst, m_client->getFormspecPrepend(), + &m_input->joystick, fs_src.get(), txt_dst, m_client->getFormspecPrepend(), m_client->getSoundManager()); m_formspec->setFormSpec(fs_src->getForm(), inventoryloc); + fs_src.release(); // owned by GUIFormSpecMenu } #define SIZE_TAG "size[11,5.5,true]" // Fixed size (ignored in touchscreen mode) diff --git a/src/client/game_formspec.h b/src/client/game_formspec.h index 022b8a55b..8ad3059af 100644 --- a/src/client/game_formspec.h +++ b/src/client/game_formspec.h @@ -34,8 +34,8 @@ struct GameFormSpec // Currently only used for the in-game settings menu. void showPauseMenuFormSpec(const std::string &formspec, const std::string &formname); void showNodeFormspec(const std::string &formspec, const v3s16 &nodepos); - /// If ` fs_override`: Uses `player->inventory_formspec`. - /// If `!fs_override`: Uses a temporary formspec until an update is received. + /// If `!fs_override`: Uses `player->inventory_formspec`. + /// If ` fs_override`: Uses a temporary formspec until an update is received. void showPlayerInventory(const std::string *fs_override); void showDeathFormspecLegacy(); // Shows the hardcoded "main" pause menu. diff --git a/src/client/localplayer.h b/src/client/localplayer.h index 047a95372..2f108dcce 100644 --- a/src/client/localplayer.h +++ b/src/client/localplayer.h @@ -99,6 +99,7 @@ public: std::string hotbar_image = ""; std::string hotbar_selected_image = ""; + /// Temporary player inventory formspec. Empty value = feature inactive. std::string inventory_formspec_override; video::SColor light_color = video::SColor(255, 255, 255, 255);