swap 'action' and 'mode' on_inventory_interact arguments & minor refactor

This commit is contained in:
MihailRis 2026-03-16 19:52:24 +03:00
parent 1f7696e972
commit c435c6080a
6 changed files with 75 additions and 41 deletions

View file

@ -222,7 +222,7 @@ function on_hud_close(playerid: int)
Called on world close (before saving) Called on world close (before saving)
```lua ```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. Triggered when a player interacts with inventory slots.

View file

@ -231,7 +231,7 @@ function on_hud_close(playerid: int)
Вызывается при выходе из мира, перед его сохранением. Вызывается при выходе из мира, перед его сохранением.
```lua ```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)
``` ```
Вызывается при взаимодействии игрока с любым слотом Вызывается при взаимодействии игрока с любым слотом

View file

@ -350,68 +350,87 @@ bool SlotView::isHighlighted() const {
void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) { void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) {
const auto& input = gui.getInput(); const auto& input = gui.getInput();
const int mode = PRIMARY; const auto mode = InteractionMode::PRIMARY;
int action; auto action = InteractionAction::UNDEFINED;
if (layout.taking && input.pressed(Keycode::LEFT_SHIFT)) { if (layout.taking && input.pressed(Keycode::LEFT_SHIFT)) {
action = SHARE; action = InteractionAction::SHARE;
if (layout.shareFunc) { if (layout.shareFunc) {
layout.shareFunc(layout.index, stack); layout.shareFunc(layout.index, stack);
} }
if (layout.updateFunc) { if (layout.updateFunc) {
layout.updateFunc(layout.index, stack); layout.updateFunc(layout.index, stack);
} }
scripting::on_inventory_interact(inventoryId, layout.index, mode, action); scripting::on_inventory_interact(
inventoryId,
layout.index,
static_cast<int>(action),
static_cast<int>(mode)
);
return; return;
} }
if (!layout.itemSource && stack.accepts(grabbed) && layout.placing) { if (!layout.itemSource && stack.accepts(grabbed) && layout.placing) {
action = PUT; action = InteractionAction::PUT;
stack.move(grabbed, *content->getIndices()); stack.move(grabbed, *content->getIndices());
} else { } else {
if (layout.itemSource) { if (layout.itemSource) {
if (grabbed.isEmpty()) { if (grabbed.isEmpty()) {
action = TAKE; action = InteractionAction::TAKE;
grabbed.set(stack); grabbed.set(stack);
} else { } else {
action = PUT; action = InteractionAction::PUT;
grabbed.clear(); grabbed.clear();
} }
} else if (grabbed.isEmpty()) { } else if (grabbed.isEmpty()) {
if (layout.taking) { if (layout.taking) {
action = TAKE; action = InteractionAction::TAKE;
std::swap(grabbed, stack); std::swap(grabbed, stack);
} }
} else if (layout.taking && layout.placing) { } else if (layout.taking && layout.placing) {
action = PUT; action = InteractionAction::PUT;
std::swap(grabbed, stack); 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<int>(action),
static_cast<int>(mode)
);
}
} }
void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) { void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) {
const auto& input = gui.getInput();
const int mode = SECONDARY;
int action = PUT;
if (layout.rightClick) { if (layout.rightClick) {
layout.rightClick(inventoryId, stack); layout.rightClick(inventoryId, stack);
if (layout.updateFunc) { if (layout.updateFunc) {
layout.updateFunc(layout.index, stack); layout.updateFunc(layout.index, stack);
} }
scripting::on_inventory_interact(inventoryId, layout.index, mode, PUT); scripting::on_inventory_interact(
inventoryId,
layout.index,
static_cast<int>(InteractionAction::PUT),
static_cast<int>(InteractionMode::SECONDARY)
);
return; return;
} }
if (layout.itemSource) { if (layout.itemSource) {
scripting::on_inventory_interact(inventoryId, layout.index, mode, TAKE); scripting::on_inventory_interact(
inventoryId,
layout.index,
static_cast<int>(InteractionAction::TAKE),
static_cast<int>(InteractionMode::SECONDARY)
);
return; return;
} }
auto action = InteractionAction::PUT;
if (grabbed.isEmpty()) { if (grabbed.isEmpty()) {
if (!stack.isEmpty() && layout.taking) { if (!stack.isEmpty() && layout.taking) {
action = TAKE; action = InteractionAction::TAKE;
grabbed.set(std::move(stack)); grabbed.set(std::move(stack));
int halfremain = stack.getCount() / 2; int halfremain = stack.getCount() / 2;
grabbed.setCount(stack.getCount() - halfremain); grabbed.setCount(stack.getCount() - halfremain);
@ -422,19 +441,28 @@ void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) {
stack = ItemStack(0, 0); stack = ItemStack(0, 0);
} }
} }
scripting::on_inventory_interact(inventoryId, layout.index, mode, action); scripting::on_inventory_interact(
inventoryId,
layout.index,
static_cast<int>(action),
static_cast<int>(InteractionMode::SECONDARY)
);
return; return;
} }
if (!layout.placing) { if (!layout.placing) {
scripting::on_inventory_interact(inventoryId, layout.index, mode, PUT); scripting::on_inventory_interact(
inventoryId,
layout.index,
static_cast<int>(InteractionAction::PUT),
static_cast<int>(InteractionMode::SECONDARY)
);
return; return;
} }
auto& stackDef = content->getIndices()->items.require(stack.getItemId()); auto& stackDef = content->getIndices()->items.require(stack.getItemId());
if (stack.isEmpty()) { if (stack.isEmpty()) {
action = PUT;
itemcount_t count = grabbed.getCount(); itemcount_t count = grabbed.getCount();
stack.set(std::move(grabbed)); stack.set(std::move(grabbed));
stack.setCount(1); stack.setCount(1);
@ -444,12 +472,18 @@ void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) {
grabbed = ItemStack(stack.getItemId(), count - 1); grabbed = ItemStack(stack.getItemId(), count - 1);
} }
} else if (stack.accepts(grabbed) && stack.getCount() < stackDef.stackSize) { } else if (stack.accepts(grabbed) && stack.getCount() < stackDef.stackSize) {
action = PUT;
stack.setCount(stack.getCount() + 1); stack.setCount(stack.getCount() + 1);
grabbed.setCount(grabbed.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<int>(InteractionAction::PUT),
static_cast<int>(InteractionMode::SECONDARY)
);
} }
void SlotView::clicked(Mousecode button) { 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)); setColor(glm::vec4(0, 0, 0, 0.0f));
} }
InventoryView::~InventoryView() { InventoryView::~InventoryView() = default;
}
std::shared_ptr<SlotView> InventoryView::addSlot(const SlotLayout& layout) { std::shared_ptr<SlotView> InventoryView::addSlot(const SlotLayout& layout) {
uint width = InventoryView::SLOT_SIZE + layout.padding; uint width = InventoryView::SLOT_SIZE + layout.padding;

View file

@ -56,17 +56,6 @@ namespace gui {
ItemStack stack {}; ItemStack stack {};
std::wstring countStr; std::wstring countStr;
} cache; } cache;
enum InteractionMode {
PRIMARY = 0,
SECONDARY = 1
};
enum InteractionAction {
PUT = 0,
TAKE = 1,
SHARE = 2,
};
const Content* content = nullptr; const Content* content = nullptr;
SlotLayout layout; SlotLayout layout;
@ -122,6 +111,18 @@ namespace gui {
size_t getIndex() const; size_t getIndex() const;
static inline std::string EXCHANGE_SLOT_NAME = "exchange-slot"; 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 { class InventoryView final : public gui::Container {

View file

@ -108,7 +108,7 @@ namespace scripting {
void on_inventory_open(const Player* player, const Inventory& inventory); void on_inventory_open(const Player* player, const Inventory& inventory);
void on_inventory_closed(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); void on_player_tick(Player* player, int tps);

View file

@ -93,7 +93,7 @@ void scripting::on_frontend_close() {
scripting::post_processing = nullptr; 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(); auto L = lua::get_main_state();
for (auto& pack : content_control->getAllContentPacks()) { for (auto& pack : content_control->getAllContentPacks()) {
lua::emit_event( lua::emit_event(
@ -102,8 +102,8 @@ void scripting::on_inventory_interact(int invid, int slot, int mode, int action)
[&](lua::State* L) { [&](lua::State* L) {
lua::pushinteger(L, invid); lua::pushinteger(L, invid);
lua::pushinteger(L, slot); lua::pushinteger(L, slot);
lua::pushinteger(L, mode);
lua::pushinteger(L, action); lua::pushinteger(L, action);
lua::pushinteger(L, mode);
return 4; return 4;
} }
); );