Skip to content

Commit c679521

Browse files
committed
fix: duplicate event and improve returns
1 parent 7b82064 commit c679521

2 files changed

Lines changed: 26 additions & 20 deletions

File tree

data-otservbr-global/scripts/quests/soul_war/globalevent-ebb_and_flow_change_maps.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ local function loadMapEmpty()
3737
SoulWarQuest.ebbAndFlow.setLoadedEmptyMap(true)
3838
SoulWarQuest.ebbAndFlow.setActive(false)
3939

40-
local updatePlayers = EventCallback("UpdatePlayersEmptyEbbFlowMap")
40+
local updatePlayers = EventCallback("UpdatePlayersEmptyEbbFlowMap", true)
4141
function updatePlayers.mapOnLoad(mapPath)
4242
if mapPath ~= SoulWarQuest.ebbAndFlow.mapsPath.empty then
4343
return
@@ -95,7 +95,7 @@ local function loadMapInundate()
9595
SoulWarQuest.ebbAndFlow.setLoadedEmptyMap(false)
9696
SoulWarQuest.ebbAndFlow.setActive(true)
9797

98-
local updatePlayers = EventCallback("UpdatePlayersInundateEbbFlowMap")
98+
local updatePlayers = EventCallback("UpdatePlayersInundateEbbFlowMap", true)
9999
function updatePlayers.mapOnLoad(mapPath)
100100
if mapPath ~= SoulWarQuest.ebbAndFlow.mapsPath.inundate then
101101
return

src/lua/callbacks/events_callbacks.hpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,13 @@ class EventsCallbacks {
7676
template <typename CallbackFunc, typename... Args>
7777
void executeCallback(EventCallback_t eventType, CallbackFunc callbackFunc, Args &&... args) {
7878
auto it = m_callbacks.find(eventType);
79-
if (it != m_callbacks.end()) {
80-
for (const auto &entry : it->second) {
81-
if (entry.callback && entry.callback->isLoadedCallback()) {
82-
std::invoke(callbackFunc, *entry.callback, args...);
83-
}
79+
if (it == m_callbacks.end()) {
80+
return;
81+
}
82+
83+
for (const auto &entry : it->second) {
84+
if (entry.callback && entry.callback->isLoadedCallback()) {
85+
std::invoke(callbackFunc, *entry.callback, args...);
8486
}
8587
}
8688
}
@@ -95,13 +97,15 @@ class EventsCallbacks {
9597
template <typename CallbackFunc, typename... Args>
9698
ReturnValue checkCallbackWithReturnValue(EventCallback_t eventType, CallbackFunc callbackFunc, Args &&... args) {
9799
auto it = m_callbacks.find(eventType);
98-
if (it != m_callbacks.end()) {
99-
for (const auto &entry : it->second) {
100-
if (entry.callback && entry.callback->isLoadedCallback()) {
101-
ReturnValue callbackResult = std::invoke(callbackFunc, *entry.callback, args...);
102-
if (callbackResult != RETURNVALUE_NOERROR) {
103-
return callbackResult;
104-
}
100+
if (it == m_callbacks.end()) {
101+
return RETURNVALUE_NOERROR;
102+
}
103+
104+
for (const auto &entry : it->second) {
105+
if (entry.callback && entry.callback->isLoadedCallback()) {
106+
ReturnValue callbackResult = std::invoke(callbackFunc, *entry.callback, args...);
107+
if (callbackResult != RETURNVALUE_NOERROR) {
108+
return callbackResult;
105109
}
106110
}
107111
}
@@ -119,12 +123,14 @@ class EventsCallbacks {
119123
bool checkCallback(EventCallback_t eventType, CallbackFunc callbackFunc, Args &&... args) {
120124
bool allCallbacksSucceeded = true;
121125
auto it = m_callbacks.find(eventType);
122-
if (it != m_callbacks.end()) {
123-
for (const auto &entry : it->second) {
124-
if (entry.callback && entry.callback->isLoadedCallback()) {
125-
bool callbackResult = std::invoke(callbackFunc, *entry.callback, args...);
126-
allCallbacksSucceeded &= callbackResult;
127-
}
126+
if (it == m_callbacks.end()) {
127+
return allCallbacksSucceeded;
128+
}
129+
130+
for (const auto &entry : it->second) {
131+
if (entry.callback && entry.callback->isLoadedCallback()) {
132+
bool callbackResult = std::invoke(callbackFunc, *entry.callback, args...);
133+
allCallbacksSucceeded &= callbackResult;
128134
}
129135
}
130136
return allCallbacksSucceeded;

0 commit comments

Comments
 (0)