Skip to content

Commit

Permalink
fix: grabbed item is deleted on inventory close
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Dec 19, 2024
1 parent 9ef1849 commit 2787f2f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/frontend/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "window/Window.hpp"
#include "world/Level.hpp"
#include "world/World.hpp"
#include "debug/Logger.hpp"

#include <assert.h>
#include <memory>
Expand All @@ -56,6 +57,8 @@

using namespace gui;

static debug::Logger logger("hud");

bool Hud::showGeneratorMinimap = false;

// implemented in debug_panel.cpp
Expand Down Expand Up @@ -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<SlotView>(
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<InventoryView>(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;
Expand Down
3 changes: 3 additions & 0 deletions src/frontend/hud.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 2787f2f

Please sign in to comment.