From 9dc7780df5d1adc22bb9ae215a705d7336dcd2f0 Mon Sep 17 00:00:00 2001 From: Vincent Date: Thu, 14 Mar 2024 17:23:10 +0000 Subject: [PATCH] feat: showcase a finer permission control --- include/example_plugin.h | 31 ++++++++++++++++--------- include/fibonacci_command.h | 45 ++++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/include/example_plugin.h b/include/example_plugin.h index 19f0b9a..8acefd2 100644 --- a/include/example_plugin.h +++ b/include/example_plugin.h @@ -16,16 +16,25 @@ class ExamplePlugin : public endstone::Plugin { { getLogger().info("onEnable is called"); - auto *root = registerPermission("cpp_example.command", - "Allow users to use all commands provided by this example plugin"); - registerPermission(root, "cpp_example.command.fibonacci", "Allow users to use the fibonacci command", - endstone::PermissionDefault::Operator); - - registerCommand("fibonacci", // name - "A simple command that writes the Fibonacci series up to n.", // description - {"/fibonacci "}, // usages - {"fib"}, // aliases - {"cpp_example.command.fibonacci"}) // permissions + auto *root = registerPermission( // root permission + "cpp_example.command", // name + "Allow users to use all commands provided by this example plugin"); // description + + auto *fibonacci = registerPermission(root, // parent + "cpp_example.command.fibonacci", // name + "Allow users to use the fibonacci command", // description + endstone::PermissionDefault::True); // default + + registerPermission(fibonacci, // parent + "cpp_example.command.fibonacci.large_n", // name + "Allow users to use the fibonacci command with n >= 1000", // description + endstone::PermissionDefault::Operator); // default + + registerCommand("fibonacci", // name + "A simple command that writes the Fibonacci series up to n.", // description + {"/fibonacci "}, // usages + {"fib"}, // aliases + {"cpp_example.command.fibonacci", "cpp_example.command.fibonacci.large_n"}) // permissions ->setExecutor(std::make_unique()); } @@ -39,7 +48,7 @@ class ExamplePlugin : public endstone::Plugin { return description_; } - bool onCommand(const endstone::CommandSender &sender, const endstone::Command &command, + bool onCommand(endstone::CommandSender &sender, const endstone::Command &command, const std::vector &args) override { // You can also handle commands here instead of setting an executor in onEnable if you prefer diff --git a/include/fibonacci_command.h b/include/fibonacci_command.h index 94c88f3..478fcbd 100644 --- a/include/fibonacci_command.h +++ b/include/fibonacci_command.h @@ -9,29 +9,38 @@ class FibonacciCommandExecutor : public endstone::CommandExecutor { public: - bool onCommand(const endstone::CommandSender &sender, const endstone::Command &command, + bool onCommand(endstone::CommandSender &sender, const endstone::Command &command, const std::vector &args) override { - int n = std::stoi(args[0]); - if (n > 0) { - int a = 0, b = 1; - std::string result; - while (a < n) { - if (!result.empty()) { - result += ", "; - } - - result += std::to_string(a); - int temp = b; - b = a + b; - a = temp; - } - sender.sendMessage("Fibonacci series up to {}: {}", n, result); + if (!command.testPermission(sender)) { return true; } - else { + + int n = std::stoi(args[0]); + + if (n <= 0) { sender.sendErrorMessage("'n' must be an integer greater than 0."); - return false; + return true; + } + + if (n >= 1000 && !sender.hasPermission("cpp_example.command.fibonacci.large_n")) { + sender.sendErrorMessage("Only operator can use this command with n >= 1000."); + return true; + } + + int a = 0, b = 1; + std::string result; + while (a < n) { + if (!result.empty()) { + result += ", "; + } + + result += std::to_string(a); + int temp = b; + b = a + b; + a = temp; } + sender.sendMessage("Fibonacci series up to {}: {}", n, result); + return true; } };