From 2787f2fc5495004f6029644ed5221f3abfc0c68f Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 19 Dec 2024 22:44:54 +0300 Subject: [PATCH] fix: grabbed item is deleted on inventory close --- src/frontend/hud.cpp | 28 ++++++++++++++++++++++++++++ src/frontend/hud.hpp | 3 +++ 2 files changed, 31 insertions(+) diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index 21111e18..01252c4c 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -47,6 +47,7 @@ #include "window/Window.hpp" #include "world/Level.hpp" #include "world/World.hpp" +#include "debug/Logger.hpp" #include #include @@ -56,6 +57,8 @@ using namespace gui; +static debug::Logger logger("hud"); + bool Hud::showGeneratorMinimap = false; // implemented in debug_panel.cpp @@ -485,7 +488,32 @@ void Hud::openPermanent(UiDocument* doc) { add(HudElement(hud_element_mode::permanent, doc, doc->getRoot(), false)); } +void Hud::dropExchangeSlot() { + auto slotView = std::dynamic_pointer_cast( + gui->get(SlotView::EXCHANGE_SLOT_NAME) + ); + if (slotView == nullptr) { + return; + } + ItemStack& stack = slotView->getStack(); + + auto indices = frontend.getLevel().content->getIndices(); + if (auto invView = std::dynamic_pointer_cast(blockUI)) { + invView->getInventory()->move(stack, indices); + } + if (stack.isEmpty()) { + return; + } + player->getInventory()->move(stack, indices); + if (!stack.isEmpty()) { + logger.warning() << "discard item [" << stack.getItemId() << ":" + << stack.getCount(); + stack.clear(); + } +} + void Hud::closeInventory() { + dropExchangeSlot(); gui->remove(SlotView::EXCHANGE_SLOT_NAME); exchangeSlot = nullptr; exchangeSlotInv = nullptr; diff --git a/src/frontend/hud.hpp b/src/frontend/hud.hpp index 5594664a..ad834955 100644 --- a/src/frontend/hud.hpp +++ b/src/frontend/hud.hpp @@ -128,6 +128,9 @@ class Hud : public util::ObjectsKeeper { void updateHotbarControl(); void cleanup(); + /// @brief Perform exchange slot removal when it's not empty. + void dropExchangeSlot(); + void showExchangeSlot(); void updateWorldGenDebugVisualization(); public: