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)
```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.

View file

@ -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)
```
Вызывается при взаимодействии игрока с любым слотом

View file

@ -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<int>(action),
static_cast<int>(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<int>(action),
static_cast<int>(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<int>(InteractionAction::PUT),
static_cast<int>(InteractionMode::SECONDARY)
);
return;
}
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;
}
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<int>(action),
static_cast<int>(InteractionMode::SECONDARY)
);
return;
}
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;
}
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<int>(InteractionAction::PUT),
static_cast<int>(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<SlotView> InventoryView::addSlot(const SlotLayout& layout) {
uint width = InventoryView::SLOT_SIZE + layout.padding;

View file

@ -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 {

View file

@ -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);

View file

@ -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;
}
);