Skip to content

Commit

Permalink
Add Player:onInventoryUpdate(item, slot, equip) (#3944)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evil Puncker authored and Mateuzkl committed Aug 2, 2024
1 parent a4295a4 commit 0c3d470
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions data/events/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<event class="Player" method="onLoseExperience" enabled="0" />
<event class="Player" method="onGainSkillTries" enabled="1" />
<event class="Player" method="onWrapItem" enabled="1" />
<event class="Player" method="onInventoryUpdate" enabled="1" />

<!-- Monster methods -->
<event class="Monster" method="onDropLoot" enabled="1" />
Expand Down
6 changes: 6 additions & 0 deletions data/events/scripts/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,9 @@ function Player:onWrapItem(item)
end
end
end

function Player:onInventoryUpdate(item, slot, equip)
if hasEventCallback(EVENT_CALLBACK_ONINVENTORYUPDATE) then
EventCallback(EVENT_CALLBACK_ONINVENTORYUPDATE, self, item, slot, equip)
end
end
6 changes: 4 additions & 2 deletions data/scripts/lib/event_callbacks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ EVENT_CALLBACK_ONGAINEXPERIENCE = 24
EVENT_CALLBACK_ONLOSEEXPERIENCE = 25
EVENT_CALLBACK_ONGAINSKILLTRIES = 26
EVENT_CALLBACK_ONWRAPITEM = 27
EVENT_CALLBACK_ONINVENTORYUPDATE = 28
-- Monster
EVENT_CALLBACK_ONDROPLOOT = 28
EVENT_CALLBACK_ONSPAWN = 29
EVENT_CALLBACK_ONDROPLOOT = 29
EVENT_CALLBACK_ONSPAWN = 30
-- last (for correct table counting)
EVENT_CALLBACK_LAST = EVENT_CALLBACK_ONSPAWN

Expand Down Expand Up @@ -65,6 +66,7 @@ local callbacks = {
["onLoseExperience"] = EVENT_CALLBACK_ONLOSEEXPERIENCE,
["onGainSkillTries"] = EVENT_CALLBACK_ONGAINSKILLTRIES,
["onWrapItem"] = EVENT_CALLBACK_ONWRAPITEM,
["onInventoryUpdate"] = EVENT_CALLBACK_ONINVENTORYUPDATE,
-- Monster
["onDropLoot"] = EVENT_CALLBACK_ONDROPLOOT,
["onSpawn"] = EVENT_CALLBACK_ONSPAWN
Expand Down
32 changes: 32 additions & 0 deletions src/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ bool Events::load()
info.playerOnGainSkillTries = event;
} else if (methodName == "onWrapItem") {
info.playerOnWrapItem = event;
} else if (methodName == "onInventoryUpdate") {
info.playerOnInventoryUpdate = event;
} else {
std::cout << "[Warning - Events::load] Unknown player method: " << methodName << std::endl;
}
Expand Down Expand Up @@ -988,6 +990,36 @@ void Events::eventPlayerOnWrapItem(Player* player, Item* item)
scriptInterface.callVoidFunction(2);
}

void Events::eventPlayerOnInventoryUpdate(Player* player, Item* item, slots_t slot, bool equip)
{
// Player:onInventoryUpdate(item, slot, equip)
if (info.playerOnInventoryUpdate == -1) {
return;
}

if (!scriptInterface.reserveScriptEnv()) {
std::cout << "[Error - Events::eventPlayerOnInventoryUpdate] Call stack overflow" << std::endl;
return;
}

ScriptEnvironment* env = scriptInterface.getScriptEnv();
env->setScriptId(info.playerOnInventoryUpdate, &scriptInterface);

lua_State* L = scriptInterface.getLuaState();
scriptInterface.pushFunction(info.playerOnInventoryUpdate);

LuaScriptInterface::pushUserdata<Player>(L, player);
LuaScriptInterface::setMetatable(L, -1, "Player");

LuaScriptInterface::pushUserdata<Item>(L, item);
LuaScriptInterface::setItemMetatable(L, -1, item);

lua_pushnumber(L, slot);
LuaScriptInterface::pushBoolean(L, equip);

scriptInterface.callVoidFunction(4);
}

void Events::eventMonsterOnDropLoot(Monster* monster, Container* corpse)
{
// Monster:onDropLoot(corpse)
Expand Down
2 changes: 2 additions & 0 deletions src/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Events
int32_t playerOnLoseExperience = -1;
int32_t playerOnGainSkillTries = -1;
int32_t playerOnWrapItem = -1;
int32_t playerOnInventoryUpdate = -1;

// Monster
int32_t monsterOnDropLoot = -1;
Expand Down Expand Up @@ -91,6 +92,7 @@ class Events
void eventPlayerOnLoseExperience(Player* player, uint64_t& exp);
void eventPlayerOnGainSkillTries(Player* player, skills_t skill, uint64_t& tries);
void eventPlayerOnWrapItem(Player* player, Item* item);
void eventPlayerOnInventoryUpdate(Player* player, Item* item, slots_t slot, bool equip);

// Monster
void eventMonsterOnDropLoot(Monster* monster, Container* corpse);
Expand Down
2 changes: 2 additions & 0 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3011,6 +3011,7 @@ void Player::postAddNotification(Thing* thing, const Cylinder* oldParent, int32_
if (link == LINK_OWNER) {
//calling movement scripts
g_moveEvents->onPlayerEquip(this, thing->getItem(), static_cast<slots_t>(index), false);
g_events->eventPlayerOnInventoryUpdate(this, thing->getItem(), static_cast<slots_t>(index), true);
}

bool requireListUpdate = false;
Expand Down Expand Up @@ -3065,6 +3066,7 @@ void Player::postRemoveNotification(Thing* thing, const Cylinder* newParent, int
if (link == LINK_OWNER) {
//calling movement scripts
g_moveEvents->onPlayerDeEquip(this, thing->getItem(), static_cast<slots_t>(index));
g_events->eventPlayerOnInventoryUpdate(this, thing->getItem(), static_cast<slots_t>(index), false);
}

bool requireListUpdate = false;
Expand Down

0 comments on commit 0c3d470

Please sign in to comment.