Skip to content

Commit

Permalink
refactor: plugin initialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Mar 20, 2024
1 parent 927eeb4 commit 24cf1f6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
38 changes: 4 additions & 34 deletions include/example_plugin.h
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -16,37 +15,16 @@ 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 <n: int>"}, {"fib"},
{"cpp_example.command.fibonacci", "cpp_example.command.fibonacci.large_n"})
->setExecutor(std::make_unique<FibonacciCommandExecutor>());
if (auto *command = getCommand("fibonacci")) {
command->setExecutor(std::make_unique<FibonacciCommandExecutor>());
}
}

void onDisable() override
{
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<std::string> &args) override
{
Expand All @@ -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 <[email protected]>"} // authors
};
};
37 changes: 35 additions & 2 deletions src/example_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"};

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 <n: int>")
.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);
}

0 comments on commit 24cf1f6

Please sign in to comment.