Skip to content

Commit ea315d9

Browse files
committed
fix: fix onTick
1 parent 2eab54a commit ea315d9

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

src/api/EventAPI.cpp

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
#include "api/PlayerAPI.h"
55
#include "engine/EngineOwnData.h"
66
#include "engine/GlobalShareData.h"
7+
#include "ll/api/chrono/GameChrono.h"
78
#include "ll/api/event/EventBus.h"
9+
#include "ll/api/schedule/Scheduler.h"
10+
#include "ll/api/schedule/Task.h"
811
#include "ll/api/service/Bedrock.h"
912
#include "main/Global.hpp"
1013
#include <exception>
1114
#include <list>
15+
#include <shared_mutex>
1216

1317
#include "ll/api/event/EventBus.h"
1418
#include "ll/api/event/player/PlayerChatEvent.h"
@@ -1449,36 +1453,35 @@ void InitBasicEventListeners() {
14491453
// return true;
14501454
// });
14511455

1452-
// // 植入tick
1453-
// Schedule::repeat(
1454-
// []() {
1455-
// #ifndef LLSE_BACKEND_NODEJS
1456-
// try {
1457-
// std::list<ScriptEngine *> tmpList;
1458-
// {
1459-
// SRWLockSharedHolder lock(globalShareData->engineListLock);
1460-
// // low efficiency
1461-
// tmpList = globalShareData->globalEngineList;
1462-
// }
1463-
// for (auto engine : tmpList) {
1464-
// if (EngineManager::isValid(engine) &&
1465-
// EngineManager::getEngineType(engine) == LLSE_BACKEND_TYPE)
1466-
// {
1467-
// EngineScope enter(engine);
1468-
// engine->messageQueue()->loopQueue(
1469-
// script::utils::MessageQueue::LoopType::kLoopOnce);
1470-
// }
1471-
// }
1472-
// } catch (...) {
1473-
// logger.error("Error occurred in Engine Message Loop!");
1474-
// logger.error("Uncaught Exception Detected!");
1475-
// }
1476-
// #endif
1477-
// // Call tick event
1478-
// IF_LISTENED(EVENT_TYPES::onTick) { CallEvent(EVENT_TYPES::onTick);
1479-
// } IF_LISTENED_END(EVENT_TYPES::onTick);
1480-
// },
1481-
// 1);
1456+
// 植入tick
1457+
ll::schedule::ServerTimeScheduler scheduler;
1458+
scheduler.add<ll::schedule::RepeatTask>(ll::chrono::ticks(1), []() {
1459+
#ifndef LLSE_BACKEND_NODEJS
1460+
try {
1461+
std::list<ScriptEngine *> tmpList;
1462+
{
1463+
std::shared_lock<std::shared_mutex> lock(
1464+
globalShareData->engineListLock);
1465+
// low efficiency
1466+
tmpList = globalShareData->globalEngineList;
1467+
}
1468+
for (auto engine : tmpList) {
1469+
if (EngineManager::isValid(engine) &&
1470+
EngineManager::getEngineType(engine) == LLSE_BACKEND_TYPE) {
1471+
EngineScope enter(engine);
1472+
engine->messageQueue()->loopQueue(
1473+
script::utils::MessageQueue::LoopType::kLoopOnce);
1474+
}
1475+
}
1476+
} catch (...) {
1477+
logger.error("Error occurred in Engine Message Loop!");
1478+
logger.error("Uncaught Exception Detected!");
1479+
}
1480+
#endif
1481+
// Call tick event
1482+
IF_LISTENED(EVENT_TYPES::onTick) { CallEvent(EVENT_TYPES::onTick); }
1483+
IF_LISTENED_END(EVENT_TYPES::onTick);
1484+
});
14821485
}
14831486

14841487
/* onTurnLectern // 由于还是不能拦截掉书,暂时注释

0 commit comments

Comments
 (0)