Skip to content

Commit 3381f99

Browse files
committed
feat(scripting/commands): RegisterRawAlias
1 parent ec41782 commit 3381f99

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

src/commands/CommandsManager.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,32 @@ Command *CommandsManager::FetchCommand(std::string cmd)
6060
return this->commands.at(cmd);
6161
}
6262

63-
void CommandsManager::RegisterCommand(std::string plugin_name, std::string cmd, Command *command)
63+
void CommandsManager::RegisterCommand(std::string plugin_name, std::string cmd, Command *command, bool registerRaw)
6464
{
65-
if (this->commands.find(cmd) != this->commands.end())
66-
return;
6765

68-
this->commands.insert({cmd, command});
69-
this->commandAliases.insert({cmd, {}});
66+
if (!registerRaw)
67+
{
68+
if (this->commands.find(cmd) != this->commands.end())
69+
return;
70+
71+
this->commands.insert({cmd, command});
72+
this->commandAliases.insert({cmd, {}});
7073

71-
if (this->commandsByPlugin.find(plugin_name) == this->commandsByPlugin.end())
72-
this->commandsByPlugin.insert({plugin_name, {}});
74+
if (this->commandsByPlugin.find(plugin_name) == this->commandsByPlugin.end())
75+
this->commandsByPlugin.insert({plugin_name, {}});
76+
77+
this->commandsByPlugin[plugin_name].push_back(cmd);
78+
}
7379

74-
this->commandsByPlugin[plugin_name].push_back(cmd);
80+
if (!registerRaw)
81+
cmd = "sw_" + cmd;
7582

7683
if (conCommandCreated.find(cmd) == conCommandCreated.end())
7784
{
7885
conCommandCreated.insert({cmd, true});
7986

8087
ConCommandRefAbstract commandRef;
81-
new ConCommand(&commandRef, ("sw_" + cmd).c_str(), commandsCallback, "Swiftly Command", (1 << 25) | (1 << 0) | (1 << 24));
88+
new ConCommand(&commandRef, cmd.c_str(), commandsCallback, "Swiftly Command", (1 << 25) | (1 << 0) | (1 << 24));
8289
}
8390
}
8491

@@ -109,7 +116,7 @@ static void commandsCallback(const CCommandContext &context, const CCommand &arg
109116
CCommand tokenizedArgs;
110117
tokenizedArgs.Tokenize(args.GetCommandString());
111118

112-
std::string commandName = (tokenizedArgs[0] + 3);
119+
std::string commandName = (starts_with(tokenizedArgs[0], "sw_") ? (tokenizedArgs[0] + 3) : tokenizedArgs[0]);
113120

114121
std::vector<std::string> argsplit;
115122
for (int i = 1; i < tokenizedArgs.ArgC(); i++)

src/commands/CommandsManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CommandsManager
2323
int HandleCommand(Player *player, std::string text);
2424

2525
Command *FetchCommand(std::string cmd);
26-
void RegisterCommand(std::string plugin_name, std::string cmd, Command *command);
26+
void RegisterCommand(std::string plugin_name, std::string cmd, Command *command, bool registerRaw = false);
2727
void UnregisterCommand(std::string cmd);
2828

2929
std::vector<std::string> FetchCommandsByPlugin(std::string plugin_name)

src/plugins/core/scripting.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ class PluginCommands
310310
void RegisterCommand(std::string commandName, void *callback);
311311
void UnregisterCommand(std::string commandName);
312312

313+
void RegisterRawAlias(std::string commandName, std::string aliasName);
313314
void RegisterAlias(std::string commandName, std::string aliasName);
314315
void UnregisterAlias(std::string aliasName);
315316

src/plugins/core/scripting/commands.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ void PluginCommands::RegisterAlias(std::string commandName, std::string aliasNam
2929
g_commandsManager->RegisterCommand(command->GetPluginName(), aliasName, command);
3030
}
3131

32+
void PluginCommands::RegisterRawAlias(std::string commandName, std::string aliasName)
33+
{
34+
Command *command = g_commandsManager->FetchCommand(commandName);
35+
if (!command)
36+
return;
37+
38+
g_commandsManager->RegisterCommand(command->GetPluginName(), aliasName, command, true);
39+
}
40+
3241
void PluginCommands::UnregisterAlias(std::string aliasName)
3342
{
3443
UnregisterCommand(aliasName);

src/plugins/lua/scripting/commands.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ void SetupLuaCommands(LuaPlugin *plugin, lua_State *state)
77
.addConstructor<void (*)(std::string)>()
88
.addFunction("Register", &PluginCommands::RegisterCommandLua)
99
.addFunction("Unregister", &PluginCommands::UnregisterCommand)
10+
.addFunction("RegisterRawAlias", &PluginCommands::RegisterRawAlias)
1011
.addFunction("RegisterAlias", &PluginCommands::RegisterAlias)
1112
.addFunction("UnregisterAlias", &PluginCommands::UnregisterAlias)
1213
.endClass();

0 commit comments

Comments
 (0)