Skip to content

Commit

Permalink
feat: showcase a finer permission control
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Mar 14, 2024
1 parent dbebdbc commit 9dc7780
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
31 changes: 20 additions & 11 deletions include/example_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <n: int>"}, // 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 <n: int>"}, // usages
{"fib"}, // aliases
{"cpp_example.command.fibonacci", "cpp_example.command.fibonacci.large_n"}) // permissions
->setExecutor(std::make_unique<FibonacciCommandExecutor>());
}

Expand All @@ -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<std::string> &args) override
{
// You can also handle commands here instead of setting an executor in onEnable if you prefer
Expand Down
45 changes: 27 additions & 18 deletions include/fibonacci_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> &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;
}
};

0 comments on commit 9dc7780

Please sign in to comment.