diff --git a/include/example_plugin.h b/include/example_plugin.h index 260a573..ea03952 100644 --- a/include/example_plugin.h +++ b/include/example_plugin.h @@ -1,13 +1,12 @@ // Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved. +#include "endstone/command/plugin_command.h" #include "endstone/plugin/plugin.h" #include "endstone/util/color_format.h" #include "fibonacci_command.h" class ExamplePlugin : public endstone::Plugin { public: - ExamplePlugin() = default; - void onLoad() override { getLogger().info("onLoad is called"); @@ -16,25 +15,9 @@ class ExamplePlugin : public endstone::Plugin { void onEnable() override { getLogger().info("onEnable is called"); - - // Register permissions - auto *root = registerPermission("cpp_example.command", - "Allow users to use all commands provided by this example plugin"); - registerPermission(root, "cpp_example.command.debug", "Allow users to use the debug command", - endstone::PermissionDefault::Operator); - auto *fib = registerPermission(root, "cpp_example.command.fibonacci", - "Allow users to use the fibonacci command", endstone::PermissionDefault::True); - registerPermission(fib, "cpp_example.command.fibonacci.large_n", - "Allow users to use the fibonacci command with n >= 1000", - endstone::PermissionDefault::Operator); - - // Register commands - registerCommand("debug", "Print debug information of this plugin.", {"/debug"}, {}, - {"cpp_example.command.debug"}); - registerCommand("fibonacci", "A simple command that writes the Fibonacci series up to n.", - {"/fibonacci "}, {"fib"}, - {"cpp_example.command.fibonacci", "cpp_example.command.fibonacci.large_n"}) - ->setExecutor(std::make_unique()); + if (auto *command = getCommand("fibonacci")) { + command->setExecutor(std::make_unique()); + } } void onDisable() override @@ -42,11 +25,6 @@ class ExamplePlugin : public endstone::Plugin { getLogger().info("onDisable is called"); } - [[nodiscard]] const endstone::PluginDescription &getDescription() const override - { - return description_; - } - bool onCommand(endstone::CommandSender &sender, const endstone::Command &command, const std::vector &args) override { @@ -64,12 +42,4 @@ class ExamplePlugin : public endstone::Plugin { sender.sendErrorMessage("Unknown command: /{}", command.getName()); return false; } - -private: - endstone::PluginDescription description_{ - "CppExamplePlugin", // name - "0.2.0", // version - "C++ example plugin for Endstone servers", // description - {"Endstone Developers "} // authors - }; }; diff --git a/src/example_plugin.cpp b/src/example_plugin.cpp index 03fa0ca..f107a3f 100644 --- a/src/example_plugin.cpp +++ b/src/example_plugin.cpp @@ -2,5 +2,38 @@ #include "example_plugin.h" -// The ENDSTONE_PLUGIN macro defines the entry point for the plugin. -ENDSTONE_PLUGIN(ExamplePlugin) +// The ENDSTONE_PLUGIN macro defines the metadata for the plugin. +ENDSTONE_PLUGIN(/*name=*/"CppExamplePlugin", /*version=*/"0.3.0", /*main_class=*/ExamplePlugin) +{ + plugin.description = "C++ example plugin for Endstone servers"; + plugin.authors = {"Endstone Developers "}; + + plugin.command("debug") + .description("Print debug information.") + .usages("/debug") + .permissions("cpp_example.command.debug"); + + plugin.command("fibonacci") + .description("A simple command that writes the Fibonacci series up to n.") + .usages("/fibonacci ") + .aliases("fib") + .permissions("cpp_example.command.fibonacci", "cpp_example.command.fibonacci.large_n"); + + plugin.permission("cpp_example.command") + .description("Allow users to use all commands provided by this example plugin") + .children("cpp_example.command.debug", true) + .children("cpp_example.command.fibonacci", true); + + plugin.permission("cpp_example.command.debug") + .description("Allow users to use the debug command") + .default_(endstone::PermissionDefault::Operator); + + plugin.permission("cpp_example.command.fibonacci") + .description("Allow users to use the fibonacci command") + .default_(endstone::PermissionDefault::True) + .children("cpp_example.command.fibonacci.large_n", true); + + plugin.permission("cpp_example.command.fibonacci.large_n") + .description("Allow users to use the fibonacci command with n >= 1000") + .default_(endstone::PermissionDefault::Operator); +}