Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add event callback to "on rotate item" and simplify callbacks #1370

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data-otservbr-global/scripts/eventcallbacks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Event callbacks are available for several categories of game entities, such as `
- `(void)` `playerOnStorageUpdate`
- `(void)` `playerOnCombat`
- `(void)` `playerOnInventoryUpdate`
- `(bool)` `playerOnRotateItem`
- `(void)` `monsterOnDropLoot`
- `(void)` `monsterOnSpawn`
- `(void)` `npcOnSpawn`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
local callback = EventCallback()

function callback.playerOnRotateItem(player, item, position)
if item:getActionId() == 100 then
player:sendCancelMessage(RETURNVALUE_NOTPOSSIBLE)
return false
end

return true
end

callback:register()
47 changes: 5 additions & 42 deletions data/libs/functions/revscriptsys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,53 +99,16 @@ do
end

-- Sets a custom __newindex behavior for the EventCallback class's metatable. It dynamically maps certain keys to predefined callback methods within the EventCallback class. When a key matching a method name is added, it triggers the associated function, sets the event type, and logs the registration. This allows for flexible, runtime assignment of various event handlers through Lua scripts.
local eventCallbacks = Game.getEventCallbacks()
do
local function EventCallbackNewIndex(self, key, value)
local keyToFunctionMap = {
creatureOnChangeOutfit = self.creatureOnChangeOutfit,
creatureOnAreaCombat = self.creatureOnAreaCombat,
creatureOnTargetCombat = self.creatureOnTargetCombat,
creatureOnHear = self.creatureOnHear,
creatureOnDrainHealth = self.creatureOnDrainHealth,
partyOnJoin = self.partyOnJoin,
partyOnLeave = self.partyOnLeave,
partyOnDisband = self.partyOnDisband,
partyOnShareExperience = self.partyOnShareExperience,
playerOnBrowseField = self.playerOnBrowseField,
playerOnLook = self.playerOnLook,
playerOnLookInBattleList = self.playerOnLookInBattleList,
playerOnLookInTrade = self.playerOnLookInTrade,
playerOnLookInShop = self.playerOnLookInShop,
playerOnMoveItem = self.playerOnMoveItem,
playerOnItemMoved = self.playerOnItemMoved,
playerOnChangeZone = self.playerOnChangeZone,
playerOnChangeHazard = self.playerOnChangeHazard,
playerOnMoveCreature = self.playerOnMoveCreature,
playerOnReportRuleViolation = self.playerOnReportRuleViolation,
playerOnReportBug = self.playerOnReportBug,
playerOnTurn = self.playerOnTurn,
playerOnTradeRequest = self.playerOnTradeRequest,
playerOnTradeAccept = self.playerOnTradeAccept,
playerOnGainExperience = self.playerOnGainExperience,
playerOnLoseExperience = self.playerOnLoseExperience,
playerOnGainSkillTries = self.playerOnGainSkillTries,
playerOnRemoveCount = self.playerOnRemoveCount,
playerOnRequestQuestLog = self.playerOnRequestQuestLog,
playerOnRequestQuestLine = self.playerOnRequestQuestLine,
playerOnStorageUpdate = self.playerOnStorageUpdate,
playerOnCombat = self.playerOnCombat,
playerOnInventoryUpdate = self.playerOnInventoryUpdate,
monsterOnDropLoot = self.monsterOnDropLoot,
monsterOnSpawn = self.monsterOnSpawn,
npcOnSpawn = self.npcOnSpawn,
}

local func = keyToFunctionMap[key]
if func then
local func = eventCallbacks[key]
if func and type(func) == "function" then
Spdlog.debug("[Registering EventCallback: " .. key)
func(self, value)
self:type(key)
else
rawset(self, key, value)
reportError("Invalid EventCallback with name: ".. tostring(key))
end
end
rawgetmetatable("EventCallback").__newindex = EventCallbackNewIndex
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/monsters/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,7 @@ void Monster::dropLoot(Container* corpse, Creature*) {
}
}
g_events().eventMonsterOnDropLoot(this, corpse);
g_callbacks().executeCallback(EventCallback_t::MonsterOnDropLoot, &EventCallback::monsterOnDropLoot, this, corpse);
g_callbacks().executeCallback(EventCallback_t::monsterOnDropLoot, &EventCallback::monsterOnDropLoot, this, corpse);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/creatures/monsters/spawns/spawn_monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ bool SpawnMonster::spawnMonster(uint32_t spawnMonsterId, MonsterType* monsterTyp
spawnedMonsterMap.insert(spawned_pair(spawnMonsterId, monster));
spawnMonsterMap[spawnMonsterId].lastSpawn = OTSYS_TIME();
g_events().eventMonsterOnSpawn(monster, pos);
g_callbacks().executeCallback(EventCallback_t::MonsterOnSpawn, &EventCallback::monsterOnSpawn, monster, pos);
g_callbacks().executeCallback(EventCallback_t::monsterOnSpawn, &EventCallback::monsterOnSpawn, monster, pos);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/creatures/npcs/spawns/spawn_npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ bool SpawnNpc::spawnNpc(uint32_t spawnId, NpcType* npcType, const Position &pos,
spawnNpcMap[spawnId].lastSpawnNpc = OTSYS_TIME();

g_events().eventNpcOnSpawn(npc, pos);
g_callbacks().executeCallback(EventCallback_t::NpcOnSpawn, &EventCallback::npcOnSpawn, npc, pos);
g_callbacks().executeCallback(EventCallback_t::npcOnSpawn, &EventCallback::npcOnSpawn, npc, pos);
return true;
}

Expand Down
8 changes: 4 additions & 4 deletions src/creatures/players/grouping/party.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void Party::disband() {
return;
}

if (!g_callbacks().checkCallback(EventCallback_t::PartyOnDisband, &EventCallback::partyOnDisband, this)) {
if (!g_callbacks().checkCallback(EventCallback_t::partyOnDisband, &EventCallback::partyOnDisband, this)) {
return;
}

Expand Down Expand Up @@ -84,7 +84,7 @@ bool Party::leaveParty(Player* player) {
return false;
}

if (!g_callbacks().checkCallback(EventCallback_t::PartyOnLeave, &EventCallback::partyOnLeave, this, player)) {
if (!g_callbacks().checkCallback(EventCallback_t::partyOnLeave, &EventCallback::partyOnLeave, this, player)) {
return false;
}

Expand Down Expand Up @@ -186,7 +186,7 @@ bool Party::joinParty(Player &player) {
return false;
}

if (!g_callbacks().checkCallback(EventCallback_t::PartyOnJoin, &EventCallback::partyOnJoin, this, &player)) {
if (!g_callbacks().checkCallback(EventCallback_t::partyOnJoin, &EventCallback::partyOnJoin, this, &player)) {
return false;
}

Expand Down Expand Up @@ -389,7 +389,7 @@ bool Party::setSharedExperience(Player* player, bool newSharedExpActive) {
void Party::shareExperience(uint64_t experience, Creature* target /* = nullptr*/) {
uint64_t shareExperience = experience;
g_events().eventPartyOnShareExperience(this, shareExperience);
g_callbacks().executeCallback(EventCallback_t::PartyOnShareExperience, &EventCallback::partyOnShareExperience, this, shareExperience);
g_callbacks().executeCallback(EventCallback_t::partyOnShareExperience, &EventCallback::partyOnShareExperience, this, shareExperience);

for (Player* member : memberList) {
member->onGainSharedExperience(shareExperience, target);
Expand Down
22 changes: 11 additions & 11 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ void Player::addSkillAdvance(skills_t skill, uint64_t count) {
}

g_events().eventPlayerOnGainSkillTries(this, skill, count);
g_callbacks().executeCallback(EventCallback_t::PlayerOnGainSkillTries, &EventCallback::playerOnGainSkillTries, this, skill, count);
g_callbacks().executeCallback(EventCallback_t::playerOnGainSkillTries, &EventCallback::playerOnGainSkillTries, this, skill, count);
if (count == 0) {
return;
}
Expand Down Expand Up @@ -818,7 +818,7 @@ void Player::addStorageValue(const uint32_t key, const int32_t value, const bool
if (!isLogin) {
auto currentFrameTime = g_dispatcher().getDispatcherCycle();
g_events().eventOnStorageUpdate(this, key, value, getStorageValue(key), currentFrameTime);
g_callbacks().executeCallback(EventCallback_t::PlayerOnStorageUpdate, &EventCallback::playerOnStorageUpdate, this, key, value, getStorageValue(key), currentFrameTime);
g_callbacks().executeCallback(EventCallback_t::playerOnStorageUpdate, &EventCallback::playerOnStorageUpdate, this, key, value, getStorageValue(key), currentFrameTime);
}
} else {
storageMap.erase(key);
Expand Down Expand Up @@ -1629,12 +1629,12 @@ void Player::onChangeZone(ZoneType_t zone) {
sendIcons();
g_events().eventPlayerOnChangeZone(this, zone);

g_callbacks().executeCallback(EventCallback_t::PlayerOnChangeZone, &EventCallback::playerOnChangeZone, this, zone);
g_callbacks().executeCallback(EventCallback_t::playerOnChangeZone, &EventCallback::playerOnChangeZone, this, zone);
}

void Player::onChangeHazard(bool isHazard) {
g_events().eventPlayerOnChangeHazard(this, isHazard);
g_callbacks().executeCallback(EventCallback_t::PlayerOnChangeHazard, &EventCallback::playerOnChangeHazard, this, isHazard);
g_callbacks().executeCallback(EventCallback_t::playerOnChangeHazard, &EventCallback::playerOnChangeHazard, this, isHazard);
sendIcons();
}

Expand Down Expand Up @@ -2110,7 +2110,7 @@ void Player::addManaSpent(uint64_t amount) {
}

g_events().eventPlayerOnGainSkillTries(this, SKILL_MAGLEVEL, amount);
g_callbacks().executeCallback(EventCallback_t::PlayerOnGainSkillTries, &EventCallback::playerOnGainSkillTries, this, SKILL_MAGLEVEL, amount);
g_callbacks().executeCallback(EventCallback_t::playerOnGainSkillTries, &EventCallback::playerOnGainSkillTries, this, SKILL_MAGLEVEL, amount);
if (amount == 0) {
return;
}
Expand Down Expand Up @@ -2166,7 +2166,7 @@ void Player::addExperience(Creature* target, uint64_t exp, bool sendText /* = fa
return;
}

g_callbacks().executeCallback(EventCallback_t::PlayerOnGainExperience, &EventCallback::playerOnGainExperience, this, target, exp, rawExp);
g_callbacks().executeCallback(EventCallback_t::playerOnGainExperience, &EventCallback::playerOnGainExperience, this, target, exp, rawExp);

g_events().eventPlayerOnGainExperience(this, target, exp, rawExp);
if (exp == 0) {
Expand Down Expand Up @@ -2272,7 +2272,7 @@ void Player::removeExperience(uint64_t exp, bool sendText /* = false*/) {
}

g_events().eventPlayerOnLoseExperience(this, exp);
g_callbacks().executeCallback(EventCallback_t::PlayerOnLoseExperience, &EventCallback::playerOnLoseExperience, this, exp);
g_callbacks().executeCallback(EventCallback_t::playerOnLoseExperience, &EventCallback::playerOnLoseExperience, this, exp);
if (exp == 0) {
return;
}
Expand Down Expand Up @@ -2568,7 +2568,7 @@ void Player::death(Creature* lastHitCreature) {
// Level loss
uint64_t expLoss = static_cast<uint64_t>(experience * deathLossPercent);
g_events().eventPlayerOnLoseExperience(this, expLoss);
g_callbacks().executeCallback(EventCallback_t::PlayerOnLoseExperience, &EventCallback::playerOnLoseExperience, this, expLoss);
g_callbacks().executeCallback(EventCallback_t::playerOnLoseExperience, &EventCallback::playerOnLoseExperience, this, expLoss);

sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are dead.");
std::ostringstream lostExp;
Expand Down Expand Up @@ -5694,7 +5694,7 @@ bool Player::addOfflineTrainingTries(skills_t skill, uint64_t tries) {
oldPercentToNextLevel = static_cast<long double>(manaSpent * 100) / nextReqMana;

g_events().eventPlayerOnGainSkillTries(this, SKILL_MAGLEVEL, tries);
g_callbacks().executeCallback(EventCallback_t::PlayerOnGainSkillTries, &EventCallback::playerOnGainSkillTries, this, SKILL_MAGLEVEL, tries);
g_callbacks().executeCallback(EventCallback_t::playerOnGainSkillTries, &EventCallback::playerOnGainSkillTries, this, SKILL_MAGLEVEL, tries);

uint32_t currMagLevel = magLevel;
while ((manaSpent + tries) >= nextReqMana) {
Expand Down Expand Up @@ -5749,7 +5749,7 @@ bool Player::addOfflineTrainingTries(skills_t skill, uint64_t tries) {
oldPercentToNextLevel = static_cast<long double>(skills[skill].tries * 100) / nextReqTries;

g_events().eventPlayerOnGainSkillTries(this, skill, tries);
g_callbacks().executeCallback(EventCallback_t::PlayerOnGainSkillTries, &EventCallback::playerOnGainSkillTries, this, skill, tries);
g_callbacks().executeCallback(EventCallback_t::playerOnGainSkillTries, &EventCallback::playerOnGainSkillTries, this, skill, tries);
uint32_t currSkillLevel = skills[skill].level;

while ((skills[skill].tries + tries) >= nextReqTries) {
Expand Down Expand Up @@ -6747,7 +6747,7 @@ bool Player::saySpell(
tmpPlayer->onCreatureSay(this, type, text);
if (this != tmpPlayer) {
g_events().eventCreatureOnHear(tmpPlayer, this, text, type);
g_callbacks().executeCallback(EventCallback_t::CreatureOnHear, &EventCallback::creatureOnHear, tmpPlayer, this, text, type);
g_callbacks().executeCallback(EventCallback_t::creatureOnHear, &EventCallback::creatureOnHear, tmpPlayer, this, text, type);
}
}
return true;
Expand Down
Loading