| 256 | } |
| 257 | |
| 258 | bool EndstoneCommandMap::registerCommand(std::shared_ptr<Command> command) |
| 259 | { |
| 260 | std::lock_guard lock(mutex_); |
| 261 | auto ®istry = getHandle().getRegistry(); |
| 262 | |
| 263 | if (!command) { |
| 264 | server_.getLogger().error("Unable to register a null command."); |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | // Check if the command name is available |
| 269 | auto name = command->getName(); |
| 270 | if (getCommand(name) != nullptr) { |
| 271 | server_.getLogger().error("Unable to register command '{}' as it already exists.", name); |
| 272 | return false; // the name was registered and is not an alias, we don't replace it |
| 273 | } |
| 274 | |
| 275 | // Check for available aliases |
| 276 | std::vector<std::string> pending_aliases; |
| 277 | for (const auto &alias : command->getAliases()) { |
| 278 | if (getCommand(alias) == nullptr) { |
| 279 | pending_aliases.push_back(alias); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // Check for available usages |
| 284 | auto usages = command->getUsages(); |
| 285 | if (usages.empty()) { |
| 286 | usages.push_back("/" + name); |
| 287 | } |
| 288 | std::vector<std::vector<CommandParameterData>> pending_param_data; |
| 289 | for (const auto &usage : usages) { |
| 290 | auto parser = CommandUsageParser(usage); |
| 291 | auto result = parser.parse(); |
| 292 | if (!result.has_value()) { |
| 293 | server_.getLogger().error("Error occurred when parsing usage '{}'. {}", usage, result.error()); |
| 294 | continue; |
| 295 | } |
| 296 | |
| 297 | auto &[command_name, parameters] = result.value(); |
| 298 | if (command_name != name) { |
| 299 | server_.getLogger().warning("Unexpected command name '{}' in usage '{}', do you mean '{}'?", command_name, |
| 300 | usage, name); |
| 301 | } |
| 302 | |
| 303 | bool success = true; |
| 304 | std::vector<CommandParameterData> param_data; |
| 305 | for (const auto ¶meter : parameters) { |
| 306 | auto data = CommandParameterData({0}, &CommandRegistry::ParseRuleFor<MinecraftCommandAdapter>::instance, |
| 307 | parameter.name.c_str(), CommandParameterDataType::Basic, nullptr, nullptr, |
| 308 | 0, parameter.optional, -1); |
| 309 | |
| 310 | if (parameter.is_enum) { |
| 311 | const auto &enum_name = parameter.type; |
| 312 | |
| 313 | // Add suffix if the enum already exists |
| 314 | std::string enum_name_final = enum_name; |
| 315 | int i = 0; |
nothing calls this directly
no test coverage detected