LeviLamina
Loading...
Searching...
No Matches
DynamicListener.h
1#pragma once
2
3#include "ll/api/event/Event.h"
4#include "ll/api/event/ListenerBase.h"
5
6#include "mc/nbt/CompoundTag.h"
7
8namespace ll::event {
10public:
11 using callback_fn = std::function<void(CompoundTag&)>;
12
13 explicit DynamicListener(
14 callback_fn fn,
15 EventPriority priority = EventPriority::Normal,
16 std::weak_ptr<mod::Mod> mod = mod::NativeMod::current()
17 )
18 : ListenerBase(priority, std::move(mod)),
19 callback(std::move(fn)) {}
20
21 ~DynamicListener() override = default;
22
23 void call(Event& event) override {
24 CompoundTag data{};
25 event.serialize(data);
26 callback(data);
27 event.deserialize(data);
28 }
29
30 static std::shared_ptr<DynamicListener> create(
31 callback_fn fn,
32 EventPriority priority = EventPriority::Normal,
33 std::weak_ptr<mod::Mod> mod = mod::NativeMod::current()
34 ) {
35 return std::make_shared<DynamicListener>(std::move(fn), priority, std::move(mod));
36 }
37
38private:
39 callback_fn callback;
40};
41} // namespace ll::event
Definition CompoundTag.h:13
Definition DynamicListener.h:9
Definition Event.h:14
Definition ListenerBase.h:24