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
38
39 void disableModCommands(std::string_view modName);
40
41 char const* addText(CommandHandle&, std::string_view);
42
43 [[nodiscard]] CommandRegistry& getRegistry() const;
44
45public:
46 LLNDAPI static CommandRegistrar& getInstance();
47
48 void clear();
49
50 LLNDAPI CommandHandle& getOrCreateCommand(
51 std::string const& name,
52 std::string const& description = {},
53 CommandPermissionLevel requirement = CommandPermissionLevel::Any,
54 CommandFlag flag = CommandFlagValue::NotCheat,
55 std::weak_ptr<mod::Mod> mod = mod::NativeMod::current()
56 );
57
58 LLAPI bool hasEnum(std::string const& name);
59
60 LLAPI bool tryRegisterEnum(
61 std::string const& name,
62 std::vector<std::pair<std::string, uint64>> values,
64 CommandRegistry::ParseFunction parser
65 );
66 LLAPI bool addEnumValues(
67 std::string const& name,
68 std::vector<std::pair<std::string, uint64>> values,
70 );
71
72 LLAPI bool tryRegisterRuntimeEnum(std::string const& name, std::vector<std::pair<std::string, uint64>> values);
73 LLAPI bool addRuntimeEnumValues(std::string const& name, std::vector<std::pair<std::string, uint64>> values);
74
75 LLAPI bool hasSoftEnum(std::string const& name);
76
77 LLAPI bool tryRegisterSoftEnum(std::string const& name, std::vector<std::string> values);
78
79 LLAPI bool addSoftEnumValues(std::string const& name, std::vector<std::string> values);
80
81 LLAPI bool removeSoftEnumValues(std::string const& name, std::vector<std::string> values);
82
83 LLAPI bool setSoftEnumValues(std::string const& name, std::vector<std::string> values);
84
85 template <concepts::Require<std::is_enum> T>
86 inline bool tryRegisterEnum() {
87 static std::vector<std::pair<std::string, uint64>> values{[] {
88 std::vector<std::pair<std::string, uint64>> vals;
89 if constexpr (magic_enum::enum_count<T>() > 0) {
90 magic_enum::enum_for_each<T>([&](T enumVal) {
91 vals.emplace_back(magic_enum::enum_name(enumVal), (uint64)enumVal);
92 });
93 }
94 return vals;
95 }()};
96 return tryRegisterEnum(::ll::command::enum_name_v<T>, values, Bedrock::type_id<CommandRegistry, T>(), &CommandRegistry::parse<T>);
97 }
98 template <concepts::Require<std::is_enum> T>
99 inline bool tryRegisterRuntimeEnum() {
100 static std::vector<std::pair<std::string, uint64>> values{[] {
101 std::vector<std::pair<std::string, uint64>> vals;
102 if constexpr (magic_enum::enum_count<T>() > 0) {
103 magic_enum::enum_for_each<T>([&](T enumVal) {
104 vals.emplace_back(magic_enum::enum_name(enumVal), (uint64)enumVal);
105 });
106 }
107 return vals;
108 }()};
109 return tryRegisterRuntimeEnum(::ll::command::enum_name_v<T>, values);
110 }
111
112 template <concepts::Specializes<SoftEnum> T>
113 inline bool tryRegisterSoftEnum() {
114 static std::vector<std::string> values{[] {
115 std::vector<std::string> vals;
116 using enum_type = remove_soft_enum_t<T>;
117 if constexpr (magic_enum::enum_count<enum_type>() > 0) {
118 magic_enum::enum_for_each<enum_type>([&](enum_type enumVal) {
119 vals.emplace_back(magic_enum::enum_name(enumVal));
120 });
121 }
122 return vals;
123 }()};
124 return tryRegisterSoftEnum(::ll::command::enum_name_v<T>, values);
125 }
126};
127} // namespace ll::command
Definition typeid_t.h:25
Definition CommandRegistry.h:44
Definition CommandHandle.h:13
Definition CommandRegistrar.h:32
Definition CommandFlag.h:43