| 51 | } |
| 52 | |
| 53 | bool EndstoneCommandMap::dispatch(CommandSender &sender, std::string command_line) const |
| 54 | { |
| 55 | if (!command_line.empty() && command_line[0] == '/') { |
| 56 | // remove the leading slash if present |
| 57 | command_line = command_line.substr(1); |
| 58 | } |
| 59 | |
| 60 | // split the command line by space |
| 61 | std::vector<std::string> args; |
| 62 | split(args, command_line, boost::is_any_of(" "), boost::token_compress_on); |
| 63 | if (args.empty()) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | // get the command by name |
| 68 | auto name = args[0]; |
| 69 | std::ranges::transform(name, name.begin(), [](unsigned char c) { return std::tolower(c); }); |
| 70 | const std::shared_ptr<Command> command = getCommand(name); |
| 71 | if (!command) { |
| 72 | sender.sendErrorMessage(Translatable("commands.generic.unknown", {args[0]})); |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | if (!custom_commands_.contains(name)) { |
| 77 | // This is a vanilla command |
| 78 | try { |
| 79 | return command->execute(sender, std::vector(args.begin() + 1, args.end())); |
| 80 | } |
| 81 | catch (const std::exception &e) { |
| 82 | server_.getLogger().error("Unhandled exception executing '{}': {}", command_line, e.what()); |
| 83 | return false; |
| 84 | } |
| 85 | } |
| 86 | else { |
| 87 | // This is a custom command |
| 88 | const auto command_origin = MinecraftCommandWrapper::getCommandOrigin(sender); |
| 89 | if (!command_origin) { |
| 90 | sender.sendErrorMessage("Unsupported sender type!"); |
| 91 | return false; |
| 92 | } |
| 93 | const auto *compiled = getHandle().compileCommand( // |
| 94 | command_line, *command_origin, CurrentCmdVersion::Latest, |
| 95 | [&sender](auto const &err) { sender.sendErrorMessage(err); }); |
| 96 | if (!compiled) { |
| 97 | return false; |
| 98 | } |
| 99 | try { |
| 100 | return command->execute(sender, static_cast<const MinecraftCommandAdapter *>(compiled)->args_); |
| 101 | } |
| 102 | catch (const std::exception &e) { |
| 103 | server_.getLogger().error("Unhandled exception executing '{}': {}", command_line, e.what()); |
| 104 | return false; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void EndstoneCommandMap::clearCommands() |
| 110 | { |
no test coverage detected