Skip to content

Commit f0df07b

Browse files
committed
feat: level callbacks
1 parent 03a7382 commit f0df07b

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

src-client/.gitkeep

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "mc/client/game/ClientInstance.h"
2+
3+
#include "ll/api/memory/Hook.h"
4+
5+
#include "pc/mod/PrimordialCore.h"
6+
#include "pc/callback/CallbackManager.hpp"
7+
8+
using namespace pc;
9+
10+
LL_AUTO_TYPE_INSTANCE_HOOK(
11+
ClientInstanceOnLevelExitHook,
12+
HookPriority::Normal,
13+
::ClientInstance,
14+
&::ClientInstance::$onLevelExit,
15+
void
16+
) {
17+
CallbackManager::getInstance().invokeCallback("OnClientLevelExit");
18+
return origin();
19+
}
20+
21+
LL_AUTO_TYPE_INSTANCE_HOOK(
22+
ClientInstanceOnLevelCorruptHook,
23+
HookPriority::Normal,
24+
::ClientInstance,
25+
&::ClientInstance::$onLevelCorrupt,
26+
void
27+
) {
28+
CallbackManager::getInstance().invokeCallback("OnClientLevelCorrupt");
29+
return origin();
30+
}

src/pc/callback/LevelCallbacks.cpp

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
#include "mc/world/level/Level.h"
1+
#include "magic_enum/magic_enum.hpp"
2+
3+
#include "mc/world/level/Level.h"
24
#include "mc/world/level/Tick.h"
5+
#include "mc/world/Minecraft.h"
6+
#include "mc/server/ServerInstance.h"
37

48
#include "ll/api/memory/Hook.h"
59

@@ -9,12 +13,35 @@
913

1014
using namespace pc;
1115

16+
LL_AUTO_TYPE_INSTANCE_HOOK(
17+
LevelInitializeHook,
18+
HookPriority::Normal,
19+
::Level,
20+
&::Level::$initialize,
21+
bool,
22+
::std::string const& levelName,
23+
::LevelSettings const& levelSettings,
24+
::Experiments const& experiments,
25+
::std::string const* levelId,
26+
::std::optional<::std::reference_wrapper<
27+
::std::unordered_map<::std::string, ::std::unique_ptr<::BiomeJsonDocumentGlue::ResolvedBiomeData>>>>
28+
biomeIdToResolvedData
29+
) {
30+
if (isClientSide()) {
31+
CallbackManager::getInstance().invokeCallback("OnClientLevelInitialize", levelName);
32+
} else {
33+
CallbackManager::getInstance().invokeCallback("OnServerLevelInitialize", levelName);
34+
}
35+
return origin(levelName, levelSettings, experiments, levelId, biomeIdToResolvedData);
36+
}
37+
1238
LL_AUTO_TYPE_INSTANCE_HOOK(
1339
LevelTickHook,
1440
HookPriority::Normal,
1541
::Level,
1642
&::Level::$tick,
17-
void) {
43+
void
44+
) {
1845
{
1946
Game::EventScope scope;
2047
if (isClientSide()) {
@@ -35,3 +62,30 @@ LL_AUTO_TYPE_INSTANCE_HOOK(
3562
}
3663
return origin();
3764
}
65+
66+
LL_AUTO_TYPE_INSTANCE_HOOK(
67+
MinecraftStartLeaveGameHook,
68+
HookPriority::Normal,
69+
::Minecraft,
70+
&::Minecraft::startLeaveGame,
71+
void,
72+
bool stopNetwork
73+
) {
74+
if (getLevel()->isClientSide()) {
75+
CallbackManager::getInstance().invokeCallback("OnClientStartLeaveGame", stopNetwork);
76+
} else {
77+
CallbackManager::getInstance().invokeCallback("OnServerStartLeaveGame", stopNetwork);
78+
}
79+
return origin(stopNetwork);
80+
}
81+
82+
LL_AUTO_TYPE_INSTANCE_HOOK(
83+
ServerInstanceOnLevelCorruptHook,
84+
HookPriority::Normal,
85+
::ServerInstance,
86+
&::ServerInstance::$onLevelCorrupt,
87+
void
88+
) {
89+
CallbackManager::getInstance().invokeCallback("OnServerLevelCorrupt");
90+
return origin();
91+
}

src/pc/mod/PrimordialCore.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
#include "ll/api/mod/NativeMod.h"
44

5+
#include "pc/Macro.h"
6+
57
namespace pc {
68

79
class PrimordialCore {
810

911
public:
10-
static PrimordialCore& getInstance();
12+
PCAPI static PrimordialCore& getInstance();
1113

1214
PrimordialCore() : mSelf(*ll::mod::NativeMod::current()) {}
1315

0 commit comments

Comments
 (0)