Mineserver
A rewrite of Minecraft 1.8.9 in C++ !
Loading...
Searching...
No Matches
commands.h
Go to the documentation of this file.
1
11#ifndef MINESERVER_COMMANDS_H
12#define MINESERVER_COMMANDS_H
13
14#include <unordered_map>
15#include <memory>
16#include <functional>
17#include <string>
18#include <regex>
19#include <types/chatmessage.h>
20
29{
30public:
38 {
39 CONSOLE = 0,
40 PLAYER,
41 COMMAND_BLOCK
42 };
43
48 virtual void sendMessage(const ChatMessage &message) = 0;
55 static void loadLua(lua_State *state, const char *namespaceName);
56};
57
58#ifndef DOXYGEN_IGNORE_THIS
59template <>
60struct luabridge::Stack<ISender::SenderType>
61 : luabridge::Enum<ISender::SenderType,
62 ISender::SenderType::CONSOLE,
63 ISender::SenderType::PLAYER,
64 ISender::SenderType::COMMAND_BLOCK>
65{
66};
67#endif
68
75struct Command
76{
88 std::string name;
106 std::string usage;
113 std::string description;
114
121 typedef std::function<void(const ISender::SenderType, ISender &, const std::vector<std::string> &)> HandlerType;
129};
130
138{
139private:
140 std::unordered_map<std::string, Command> commands;
141
142 static CommandsManager *instance;
143
144public:
155
165 void addCommand(const std::string &name,
166 Command::HandlerType handler,
167 const std::string &usage = "",
168 const std::string &description = "");
169
175 const std::unordered_map<std::string, Command> &getCommands() const
176 {
177 return commands;
178 }
179
215 CallCommandError callCommand(ISender::SenderType type, ISender *sender, std::string commandString);
216
223 static void loadLua(lua_State *state, const char *namespaceName);
224
231 {
232 return *instance;
233 }
234};
235
236#ifndef DOXYGEN_IGNORE_THIS
237template <>
238struct luabridge::Stack<CommandsManager::CallCommandError>
239 : luabridge::Enum<CommandsManager::CallCommandError,
240 CommandsManager::CallCommandError::NONE,
241 CommandsManager::CallCommandError::FORMAT,
242 CommandsManager::CallCommandError::COMMAND_NOT_FOUND,
243 CommandsManager::CallCommandError::RUNTIME_ERROR>
244{
245};
246#endif
247
248#endif // MINESERVER_COMMANDS_H
The file containing minecraft chat message implementation.
Minecraft Chat Message implementation.
Definition chatmessage.h:28
Commands Manager.
Definition commands.h:138
void addCommand(const std::string &name, Command::HandlerType handler, const std::string &usage="", const std::string &description="")
Adds a command.
Definition commands.cpp:25
CommandsManager()
Construct a new Commands Manager object.
Definition commands.cpp:7
static void loadLua(lua_State *state, const char *namespaceName)
Register Lua things.
Definition commands.cpp:97
~CommandsManager()
Destroy the Commands Manager object.
Definition commands.cpp:17
CallCommandError
Call command return code.
Definition commands.h:185
@ COMMAND_NOT_FOUND
Command was not found.
Definition commands.h:200
@ NONE
No errors.
Definition commands.h:190
@ FORMAT
Errors while parsing the command.
Definition commands.h:195
@ RUNTIME_ERROR
Error while running the command.
Definition commands.h:205
CallCommandError callCommand(ISender::SenderType type, ISender *sender, std::string commandString)
Calls a command.
Definition commands.cpp:50
static CommandsManager & inst()
Gets Commands Manager instance.
Definition commands.h:230
const std::unordered_map< std::string, Command > & getCommands() const
Get all registered commands.
Definition commands.h:175
Sender Interface.
Definition commands.h:29
virtual void sendMessage(const ChatMessage &message)=0
Sends a message to the sender.
static void loadLua(lua_State *state, const char *namespaceName)
Register Lua things.
Definition commands.cpp:115
SenderType
Sender type.
Definition commands.h:38
Command struct.
Definition commands.h:76
std::function< void(const ISender::SenderType, ISender &, const std::vector< std::string > &) HandlerType)
Function handler type.
Definition commands.h:121
std::string usage
The usage of the command.
Definition commands.h:106
std::string description
The description of the command.
Definition commands.h:113
std::string name
The name of the command.
Definition commands.h:88
HandlerType handler
Command handler.
Definition commands.h:128