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

fix: eventcallbacks which returns a ReturnValue #2532

Merged
15 changes: 12 additions & 3 deletions src/creatures/combat/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "items/weapons/weapons.hpp"
#include "map/spectators.hpp"
#include "lib/metrics/metrics.hpp"
#include "lua/callbacks/event_callback.hpp"
#include "lua/callbacks/events_callbacks.hpp"

int32_t Combat::getLevelFormula(std::shared_ptr<Player> player, const std::shared_ptr<Spell> wheelSpell, const CombatDamage &damage) const {
if (!player) {
Expand Down Expand Up @@ -269,8 +271,11 @@ ReturnValue Combat::canDoCombat(std::shared_ptr<Creature> caster, std::shared_pt
}
}
}

return g_events().eventCreatureOnAreaCombat(caster, tile, aggressive);
ReturnValue ret = g_events().eventCreatureOnAreaCombat(caster, tile, aggressive);
if (ret == RETURNVALUE_NOERROR) {
ret = g_callbacks().checkCallbackWithReturnValue(EventCallback_t::creatureOnTargetCombat, &EventCallback::creatureOnAreaCombat, caster, tile, aggressive);
}
return ret;
}

bool Combat::isInPvpZone(std::shared_ptr<Creature> attacker, std::shared_ptr<Creature> target) {
Expand Down Expand Up @@ -405,7 +410,11 @@ ReturnValue Combat::canDoCombat(std::shared_ptr<Creature> attacker, std::shared_
}
}
}
return g_events().eventCreatureOnTargetCombat(attacker, target);
ReturnValue ret = g_events().eventCreatureOnTargetCombat(attacker, target);
if (ret == RETURNVALUE_NOERROR) {
ret = g_callbacks().checkCallbackWithReturnValue(EventCallback_t::creatureOnTargetCombat, &EventCallback::creatureOnTargetCombat, attacker, target);
}
return ret;
}

void Combat::setPlayerCombatValues(formulaType_t newFormulaType, double newMina, double newMinb, double newMaxa, double newMaxb) {
Expand Down
24 changes: 23 additions & 1 deletion src/lua/callbacks/events_callbacks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,29 @@ class EventsCallbacks {
}
}
}

/**
* @brief Checks if all registered callbacks of the specified event type succeed.
* @param eventType The type of event to check.
* @param callbackFunc Function pointer to the callback method.
* @param args Variadic arguments to pass to the callback function.
* @return ReturnValue enum.
*/
template <typename CallbackFunc, typename... Args>
ReturnValue checkCallbackWithReturnValue(EventCallback_t eventType, CallbackFunc callbackFunc, Args &&... args) {
for (const auto &callback : getCallbacksByType(eventType)) {
auto argsCopy = std::make_tuple(args...);
if (callback && callback->isLoadedCallback()) {
ReturnValue callbackResult = std::apply(
[&callback, &callbackFunc](auto &&... args) {
return ((*callback).*callbackFunc)(std::forward<decltype(args)>(args)...);
},
argsCopy
);
return callbackResult;
lamonato29 marked this conversation as resolved.
Show resolved Hide resolved
}
}
return RETURNVALUE_NOERROR;
}
/**
* @brief Checks if all registered callbacks of the specified event type succeed.
* @param eventType The type of event to check.
Expand Down
Loading