diff --git a/src/graphics/ui/elements/InventoryView.cpp b/src/graphics/ui/elements/InventoryView.cpp index 333777857..614d4c913 100644 --- a/src/graphics/ui/elements/InventoryView.cpp +++ b/src/graphics/ui/elements/InventoryView.cpp @@ -371,35 +371,7 @@ void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) { action = InteractionAction::PUT; stack.move(grabbed, indices); } else { - if (layout.itemSource) { - if (grabbed.isEmpty()) { - action = InteractionAction::TAKE; - grabbed.set(stack); - if (input.pressed(Keycode::LEFT_CONTROL)) { - grabbed.maximizeCount(*content->getIndices()); - } - } else { - // TODO: refactor to reduce nesting - if (grabbed.accepts(stack)) { - auto& def = indices.items.require(stack.getItemId()); - if (grabbed.getCount() < def.stackSize) { - action = InteractionAction::TAKE; - grabbed.setCount(grabbed.getCount() + 1); - } - } else { - action = InteractionAction::PUT; - grabbed.clear(); - } - } - } else if (grabbed.isEmpty()) { - if (layout.taking) { - action = InteractionAction::TAKE; - std::swap(grabbed, stack); - } - } else if (layout.taking && layout.placing) { - action = InteractionAction::PUT; - std::swap(grabbed, stack); - } + actIfCannotPut(stack, grabbed, action); } if (action != InteractionAction::UNDEFINED) { @@ -412,6 +384,39 @@ void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) { } } +void SlotView::actIfCannotPut(ItemStack& stack, ItemStack& grabbed, InteractionAction& action) { + const auto& input = gui.getInput(); + auto indices = *content->getIndices(); + if (layout.itemSource) { + if (grabbed.isEmpty()) { + action = InteractionAction::TAKE; + grabbed.set(stack); + if (input.pressed(Keycode::LEFT_CONTROL)) { + grabbed.maximizeCount(*content->getIndices()); + } + } else { + if (grabbed.accepts(stack)) { + auto& def = indices.items.require(stack.getItemId()); + if (grabbed.getCount() < def.stackSize) { + action = InteractionAction::TAKE; + grabbed.setCount(grabbed.getCount() + 1); + } + } else { + action = InteractionAction::PUT; + grabbed.clear(); + } + } + } else if (grabbed.isEmpty()) { + if (layout.taking) { + action = InteractionAction::TAKE; + std::swap(grabbed, stack); + } + } else if (layout.taking && layout.placing) { + action = InteractionAction::PUT; + std::swap(grabbed, stack); + } +} + void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) { if (layout.rightClick) { layout.rightClick(inventoryId, stack); diff --git a/src/graphics/ui/elements/InventoryView.hpp b/src/graphics/ui/elements/InventoryView.hpp index 243d8b6b5..fbfe3eee9 100644 --- a/src/graphics/ui/elements/InventoryView.hpp +++ b/src/graphics/ui/elements/InventoryView.hpp @@ -123,6 +123,8 @@ namespace gui { SHARE = 2, UNDEFINED, }; + private: + void actIfCannotPut(ItemStack& stack, ItemStack& grabbed, InteractionAction& action); }; class InventoryView final : public gui::Container { diff --git a/src/logic/scripting/lua/libs/libinventory.cpp b/src/logic/scripting/lua/libs/libinventory.cpp index 20f090a01..9d11d66c6 100644 --- a/src/logic/scripting/lua/libs/libinventory.cpp +++ b/src/logic/scripting/lua/libs/libinventory.cpp @@ -1,5 +1,3 @@ -#include - #include "content/Content.hpp" #include "items/Inventories.hpp" #include "items/ItemStack.hpp"