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/Expected.h"
13#include "ll/api/base/Macro.h"
14#include "ll/api/base/StdInt.h"
15#include "ll/api/command/EnumName.h"
16#include "ll/api/command/SoftEnum.h"
17#include "ll/api/mod/ModManagerRegistry.h"
18#include "ll/api/mod/NativeMod.h"
19
20#include "mc/deps/core/utility/typeid_t.h"
21#include "mc/server/commands/CommandFlag.h"
22#include "mc/server/commands/CommandOutput.h"
23#include "mc/server/commands/CommandPermissionLevel.h"
24#include "mc/server/commands/CommandRegistry.h"
25#include "mc/server/commands/CurrentCmdVersion.h"
26
27namespace ll::mod {
28class Mod;
29}
30
31namespace ll::command {
32
33class CommandHandle;
34
35class CommandRegistrar {
36 friend CommandHandle;
37 struct Impl;
38 std::unique_ptr<Impl> impl;
39 bool isClient;
40
41 CommandRegistrar(bool isClient);
42
43 void disableModCommands(std::string_view modName);
44
45 char const* addText(CommandHandle&, std::string_view);
46
47 [[nodiscard]] CommandRegistry& getRegistry() const;
48
49public:
50 LLNDAPI static CommandRegistrar& getInstance(bool isClientSide);
51#ifdef LL_PLAT_C
52 [[nodiscard]] static CommandRegistrar& getClientInstance() { return getInstance(true); }
53#endif
54 [[nodiscard]] static CommandRegistrar& getServerInstance() { return getInstance(false); }
55
56 void clear();
57
58 LLNDAPI CommandHandle& getOrCreateCommand(
59 std::string const& name,
60 std::string const& description = {},
61 CommandPermissionLevel requirement = CommandPermissionLevel::Any,
62 CommandFlag flag = CommandFlagValue::NotCheat,
63 std::weak_ptr<mod::Mod> mod = mod::NativeMod::current()
64 );
65
66 LLNDAPI Expected<std::unique_ptr<::Command>> compileCommand(
67 std::string_view commandStr,
68 ::CommandOrigin& origin,
69 ::CurrentCmdVersion version = CurrentCmdVersion::Latest
70 ) const noexcept;
71
72 LLAPI CommandOutput executeCommand(
73 std::string_view commandStr,
74 ::CommandOrigin& origin,
75 ::CommandOutputType outputType = CommandOutputType::AllOutput,
76 ::CurrentCmdVersion version = CurrentCmdVersion::Latest
77 ) const noexcept;
78
79 LLAPI bool hasEnum(std::string const& name);
80
81 LLAPI bool tryRegisterEnum(
82 std::string const& name,
83 std::vector<std::pair<std::string, uint64>> values,
85 CommandRegistry::ParseFunction parser
86 );
87 LLAPI bool addEnumValues(
88 std::string const& name,
89 std::vector<std::pair<std::string, uint64>> values,
91 );
92
93 LLAPI bool tryRegisterRuntimeEnum(std::string const& name, std::vector<std::pair<std::string, uint64>> values);
94 LLAPI bool addRuntimeEnumValues(std::string const& name, std::vector<std::pair<std::string, uint64>> values);
95
96 LLAPI bool hasSoftEnum(std::string const& name);
97
98 LLAPI bool tryRegisterSoftEnum(std::string const& name, std::vector<std::string> values);
99
100 LLAPI bool addSoftEnumValues(std::string const& name, std::vector<std::string> values);
101
102 LLAPI bool removeSoftEnumValues(std::string const& name, std::vector<std::string> values);
103
104 LLAPI bool setSoftEnumValues(std::string const& name, std::vector<std::string> values);
105
106 template <concepts::Require<std::is_enum> T>
107 inline bool tryRegisterEnum() {
108 static std::vector<std::pair<std::string, uint64>> values{[] {
109 std::vector<std::pair<std::string, uint64>> vals;
110 if constexpr (magic_enum::enum_count<T>() > 0) {
111 magic_enum::enum_for_each<T>([&](T enumVal) {
112 vals.emplace_back(magic_enum::enum_name(enumVal), (uint64)enumVal);
113 });
114 }
115 return vals;
116 }()};
117 return tryRegisterEnum(
118 ::ll::command::enum_name_v<T>,
119 values,
120 Bedrock::type_id<CommandRegistry, T>(),
121 &CommandRegistry::parse<T>
122 );
123 }
124 template <concepts::Require<std::is_enum> T>
125 inline bool tryRegisterRuntimeEnum() {
126 static std::vector<std::pair<std::string, uint64>> values{[] {
127 std::vector<std::pair<std::string, uint64>> vals;
128 if constexpr (magic_enum::enum_count<T>() > 0) {
129 magic_enum::enum_for_each<T>([&](T enumVal) {
130 vals.emplace_back(magic_enum::enum_name(enumVal), (uint64)enumVal);
131 });
132 }
133 return vals;
134 }()};
135 return tryRegisterRuntimeEnum(::ll::command::enum_name_v<T>, values);
136 }
137
138 template <concepts::Specializes<SoftEnum> T>
139 inline bool tryRegisterSoftEnum() {
140 static std::vector<std::string> values{[] {
141 std::vector<std::string> vals;
142 using enum_type = remove_soft_enum_t<T>;
143 if constexpr (magic_enum::enum_count<enum_type>() > 0) {
144 magic_enum::enum_for_each<enum_type>([&](enum_type enumVal) {
145 vals.emplace_back(magic_enum::enum_name(enumVal));
146 });
147 }
148 return vals;
149 }()};
150 return tryRegisterSoftEnum(::ll::command::enum_name_v<T>, values);
151 }
152};
153} // namespace ll::command
Definition typeid_t.h:25
Definition CommandOrigin.h:32
Definition CommandOutput.h:20
Definition CommandRegistry.h:50
Definition CommandHandle.h:13
Definition Mod.h:17
Definition CommandFlag.h:43