diff --git a/doc/en/scripting/events.md b/doc/en/scripting/events.md index b78c791a5..504bde532 100644 --- a/doc/en/scripting/events.md +++ b/doc/en/scripting/events.md @@ -222,7 +222,7 @@ function on_hud_close(playerid: int) Called on world close (before saving) ```lua -function on_inventory_interact(invid: int, slot: int, mode: int, action: int) +function on_inventory_interact(invid: int, slot: int, action: int, mode: int) ``` Triggered when a player interacts with inventory slots. diff --git a/doc/ru/scripting/events.md b/doc/ru/scripting/events.md index d0cf88a24..116c73551 100644 --- a/doc/ru/scripting/events.md +++ b/doc/ru/scripting/events.md @@ -231,7 +231,7 @@ function on_hud_close(playerid: int) Вызывается при выходе из мира, перед его сохранением. ```lua -function on_inventory_interact(invid: int, slot: int, mode: int, action: int) +function on_inventory_interact(invid: int, slot: int, action: int, mode: int) ``` Вызывается при взаимодействии игрока с любым слотом diff --git a/src/graphics/ui/elements/InventoryView.cpp b/src/graphics/ui/elements/InventoryView.cpp index 6429f710f..144d80397 100644 --- a/src/graphics/ui/elements/InventoryView.cpp +++ b/src/graphics/ui/elements/InventoryView.cpp @@ -350,68 +350,87 @@ bool SlotView::isHighlighted() const { void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) { const auto& input = gui.getInput(); - const int mode = PRIMARY; - int action; + const auto mode = InteractionMode::PRIMARY; + auto action = InteractionAction::UNDEFINED; if (layout.taking && input.pressed(Keycode::LEFT_SHIFT)) { - action = SHARE; + action = InteractionAction::SHARE; if (layout.shareFunc) { layout.shareFunc(layout.index, stack); } if (layout.updateFunc) { layout.updateFunc(layout.index, stack); } - scripting::on_inventory_interact(inventoryId, layout.index, mode, action); + scripting::on_inventory_interact( + inventoryId, + layout.index, + static_cast(action), + static_cast(mode) + ); return; } if (!layout.itemSource && stack.accepts(grabbed) && layout.placing) { - action = PUT; + action = InteractionAction::PUT; stack.move(grabbed, *content->getIndices()); } else { if (layout.itemSource) { if (grabbed.isEmpty()) { - action = TAKE; + action = InteractionAction::TAKE; grabbed.set(stack); } else { - action = PUT; + action = InteractionAction::PUT; grabbed.clear(); } } else if (grabbed.isEmpty()) { if (layout.taking) { - action = TAKE; + action = InteractionAction::TAKE; std::swap(grabbed, stack); } } else if (layout.taking && layout.placing) { - action = PUT; + action = InteractionAction::PUT; std::swap(grabbed, stack); } } - scripting::on_inventory_interact(inventoryId, layout.index, mode, action); + if (action != InteractionAction::UNDEFINED) { + scripting::on_inventory_interact( + inventoryId, + layout.index, + static_cast(action), + static_cast(mode) + ); + } } void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) { - const auto& input = gui.getInput(); - const int mode = SECONDARY; - int action = PUT; - if (layout.rightClick) { layout.rightClick(inventoryId, stack); if (layout.updateFunc) { layout.updateFunc(layout.index, stack); } - scripting::on_inventory_interact(inventoryId, layout.index, mode, PUT); + scripting::on_inventory_interact( + inventoryId, + layout.index, + static_cast(InteractionAction::PUT), + static_cast(InteractionMode::SECONDARY) + ); return; } if (layout.itemSource) { - scripting::on_inventory_interact(inventoryId, layout.index, mode, TAKE); + scripting::on_inventory_interact( + inventoryId, + layout.index, + static_cast(InteractionAction::TAKE), + static_cast(InteractionMode::SECONDARY) + ); return; } + auto action = InteractionAction::PUT; if (grabbed.isEmpty()) { if (!stack.isEmpty() && layout.taking) { - action = TAKE; + action = InteractionAction::TAKE; grabbed.set(std::move(stack)); int halfremain = stack.getCount() / 2; grabbed.setCount(stack.getCount() - halfremain); @@ -422,19 +441,28 @@ void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) { stack = ItemStack(0, 0); } } - scripting::on_inventory_interact(inventoryId, layout.index, mode, action); + scripting::on_inventory_interact( + inventoryId, + layout.index, + static_cast(action), + static_cast(InteractionMode::SECONDARY) + ); return; } if (!layout.placing) { - scripting::on_inventory_interact(inventoryId, layout.index, mode, PUT); + scripting::on_inventory_interact( + inventoryId, + layout.index, + static_cast(InteractionAction::PUT), + static_cast(InteractionMode::SECONDARY) + ); return; } auto& stackDef = content->getIndices()->items.require(stack.getItemId()); if (stack.isEmpty()) { - action = PUT; itemcount_t count = grabbed.getCount(); stack.set(std::move(grabbed)); stack.setCount(1); @@ -444,12 +472,18 @@ void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) { grabbed = ItemStack(stack.getItemId(), count - 1); } } else if (stack.accepts(grabbed) && stack.getCount() < stackDef.stackSize) { - action = PUT; stack.setCount(stack.getCount() + 1); grabbed.setCount(grabbed.getCount() - 1); + } else { + return; } - - scripting::on_inventory_interact(inventoryId, layout.index, mode, action); + + scripting::on_inventory_interact( + inventoryId, + layout.index, + static_cast(InteractionAction::PUT), + static_cast(InteractionMode::SECONDARY) + ); } void SlotView::clicked(Mousecode button) { @@ -514,8 +548,7 @@ InventoryView::InventoryView(GUI& gui) : Container(gui, glm::vec2()) { setColor(glm::vec4(0, 0, 0, 0.0f)); } -InventoryView::~InventoryView() { -} +InventoryView::~InventoryView() = default; std::shared_ptr InventoryView::addSlot(const SlotLayout& layout) { uint width = InventoryView::SLOT_SIZE + layout.padding; diff --git a/src/graphics/ui/elements/InventoryView.hpp b/src/graphics/ui/elements/InventoryView.hpp index 03e328815..30320f3ae 100644 --- a/src/graphics/ui/elements/InventoryView.hpp +++ b/src/graphics/ui/elements/InventoryView.hpp @@ -56,17 +56,6 @@ namespace gui { ItemStack stack {}; std::wstring countStr; } cache; - - enum InteractionMode { - PRIMARY = 0, - SECONDARY = 1 - }; - - enum InteractionAction { - PUT = 0, - TAKE = 1, - SHARE = 2, - }; const Content* content = nullptr; SlotLayout layout; @@ -122,6 +111,18 @@ namespace gui { size_t getIndex() const; static inline std::string EXCHANGE_SLOT_NAME = "exchange-slot"; + + enum class InteractionMode { + PRIMARY = 0, + SECONDARY = 1 + }; + + enum class InteractionAction { + PUT = 0, + TAKE = 1, + SHARE = 2, + UNDEFINED, + }; }; class InventoryView final : public gui::Container { diff --git a/src/logic/scripting/scripting.hpp b/src/logic/scripting/scripting.hpp index 26bd43ac5..db27e8873 100644 --- a/src/logic/scripting/scripting.hpp +++ b/src/logic/scripting/scripting.hpp @@ -108,7 +108,7 @@ namespace scripting { void on_inventory_open(const Player* player, const Inventory& inventory); void on_inventory_closed(const Player* player, const Inventory& inventory); - void on_inventory_interact(int invid, int slot, int mode, int action); + void on_inventory_interact(int invid, int slot, int action, int mode); void on_player_tick(Player* player, int tps); diff --git a/src/logic/scripting/scripting_hud.cpp b/src/logic/scripting/scripting_hud.cpp index 995c2e7a5..59b928d21 100644 --- a/src/logic/scripting/scripting_hud.cpp +++ b/src/logic/scripting/scripting_hud.cpp @@ -93,7 +93,7 @@ void scripting::on_frontend_close() { scripting::post_processing = nullptr; } -void scripting::on_inventory_interact(int invid, int slot, int mode, int action) { +void scripting::on_inventory_interact(int invid, int slot, int action, int mode) { auto L = lua::get_main_state(); for (auto& pack : content_control->getAllContentPacks()) { lua::emit_event( @@ -102,8 +102,8 @@ void scripting::on_inventory_interact(int invid, int slot, int mode, int action) [&](lua::State* L) { lua::pushinteger(L, invid); lua::pushinteger(L, slot); - lua::pushinteger(L, mode); lua::pushinteger(L, action); + lua::pushinteger(L, mode); return 4; } );