LeviLamina
Loading...
Searching...
No Matches
CommandRegistrar.h
1#pragma once
2
3#include <memory>
4#include <string>
5#include <string_view>
6#include <type_traits>
7#include <utility>
8#include <vector>
9
10#include "magic_enum/magic_enum_all.hpp" // IWYU pragma: keep
11
12#include "ll/api/base/Macro.h"
13#include "ll/api/base/StdInt.h"
14#include "ll/api/command/EnumName.h"
15#include "ll/api/command/SoftEnum.h"
16#include "ll/api/mod/ModManagerRegistry.h"
17#include "ll/api/mod/NativeMod.h"
18
19#include "mc/deps/core/utility/typeid_t.h"
20#include "mc/server/commands/CommandFlag.h"
21#include "mc/server/commands/CommandPermissionLevel.h"
22#include "mc/server/commands/CommandRegistry.h"
23
24namespace ll::mod {
25class Mod;
26}
27
28namespace ll::command {
29
30class CommandHandle;
31
33 friend CommandHandle;
34 struct Impl;
35 std::unique_ptr<Impl> impl;
36 bool isClient;
37
38 CommandRegistrar(bool isClient);
39
40 void disableModCommands(std::string_view modName);
41
42 char const* addText(CommandHandle&, std::string_view);
43
44 [[nodiscard]] CommandRegistry& getRegistry() const;
45
46public:
47 LLNDAPI static CommandRegistrar& getInstance(bool isClientSide);
48
49 void clear();
50
51 LLNDAPI CommandHandle& getOrCreateCommand(
52 std::string const& name,
53 std::string const& description = {},
54 CommandPermissionLevel requirement = CommandPermissionLevel::Any,
55 CommandFlag flag = CommandFlagValue::NotCheat,
56 std::weak_ptr<mod::Mod> mod = mod::NativeMod::current()
57 );
58
59 LLAPI bool hasEnum(std::string const& name);
60
61 LLAPI bool tryRegisterEnum(
62 std::string const& name,
63 std::vector<std::pair<std::string, uint64>> values,
64 Bedrock::typeid_t<CommandRegistry> type,
65 CommandRegistry::ParseFunction parser
66 );
67 LLAPI bool addEnumValues(
68 std::string const& name,
69 std::vector<std::pair<std::string, uint64>> values,
70 Bedrock::typeid_t<CommandRegistry> type
71 );
72
73 LLAPI bool tryRegisterRuntimeEnum(std::string const& name, std::vector<std::pair<std::string, uint64>> values);
74 LLAPI bool addRuntimeEnumValues(std::string const& name, std::vector<std::pair<std::string, uint64>> values);
75
76 LLAPI bool hasSoftEnum(std::string const& name);
77
78 LLAPI bool tryRegisterSoftEnum(std::string const& name, std::vector<std::string> values);
79
80 LLAPI bool addSoftEnumValues(std::string const& name, std::vector<std::string> values);
81
82 LLAPI bool removeSoftEnumValues(std::string const& name, std::vector<std::string> values);
83
84 LLAPI bool setSoftEnumValues(std::string const& name, std::vector<std::string> values);
85
86 template <concepts::Require<std::is_enum> T>
87 inline bool tryRegisterEnum() {
88 static std::vector<std::pair<std::string, uint64>> values{[] {
89 std::vector<std::pair<std::string, uint64>> vals;
90 if constexpr (magic_enum::enum_count<T>() > 0) {
91 magic_enum::enum_for_each<T>([&](T enumVal) {
92 vals.emplace_back(magic_enum::enum_name(enumVal), (uint64)enumVal);
93 });
94 }
95 return vals;
96 }()};
97 return tryRegisterEnum(
98 ::ll::command::enum_name_v<T>,
99 values,
100 Bedrock::type_id<CommandRegistry, T>(),
101 &CommandRegistry::parse<T>
102 );
103 }
104 template <concepts::Require<std::is_enum> T>
105 inline bool tryRegisterRuntimeEnum() {
106 static std::vector<std::pair<std::string, uint64>> values{[] {
107 std::vector<std::pair<std::string, uint64>> vals;
108 if constexpr (magic_enum::enum_count<T>() > 0) {
109 magic_enum::enum_for_each<T>([&](T enumVal) {
110 vals.emplace_back(magic_enum::enum_name(enumVal), (uint64)enumVal);
111 });
112 }
113 return vals;
114 }()};
115 return tryRegisterRuntimeEnum(::ll::command::enum_name_v<T>, values);
116 }
117
118 template <concepts::Specializes<SoftEnum> T>
119 inline bool tryRegisterSoftEnum() {
120 static std::vector<std::string> values{[] {
121 std::vector<std::string> vals;
122 using enum_type = remove_soft_enum_t<T>;
123 if constexpr (magic_enum::enum_count<enum_type>() > 0) {
124 magic_enum::enum_for_each<enum_type>([&](enum_type enumVal) {
125 vals.emplace_back(magic_enum::enum_name(enumVal));
126 });
127 }
128 return vals;
129 }()};
130 return tryRegisterSoftEnum(::ll::command::enum_name_v<T>, values);
131 }
132};
133} // namespace ll::command
Definition CommandHandle.h:13
Definition CommandRegistrar.h:32