diff --git a/include/example_listener.h b/include/example_listener.h new file mode 100644 index 0000000..69b7f5c --- /dev/null +++ b/include/example_listener.h @@ -0,0 +1,19 @@ +// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved. + +#pragma once + +#include "endstone/event/server/server_load_event.h" +#include "endstone/plugin/plugin.h" + +class ExampleListener { +public: + explicit ExampleListener(endstone::Plugin &plugin) : plugin_(plugin) {} + + void onServerLoad(endstone::ServerLoadEvent &event) + { + plugin_.getLogger().info("ServerLoadEvent is passed to ExampleListener::onServerLoad"); + } + +private: + endstone::Plugin &plugin_; +}; diff --git a/include/example_plugin.h b/include/example_plugin.h index c76713f..5f293cb 100644 --- a/include/example_plugin.h +++ b/include/example_plugin.h @@ -1,10 +1,16 @@ // Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved. #include "endstone/command/plugin_command.h" +#include "endstone/event/server/server_command_event.h" +#include "endstone/event/server/server_load_event.h" #include "endstone/plugin/plugin.h" #include "endstone/util/color_format.h" +#include "example_listener.h" #include "fibonacci_command.h" +#include +#include + class ExamplePlugin : public endstone::Plugin { public: void onLoad() override @@ -15,9 +21,16 @@ class ExamplePlugin : public endstone::Plugin { void onEnable() override { getLogger().info("onEnable is called"); + if (auto *command = getCommand("fibonacci")) { command->setExecutor(std::make_unique()); } + + registerEventHandler(&ExamplePlugin::onServerLoad, *this); + + listener_ = std::make_unique(*this); + registerEventHandler(&ExampleListener::onServerLoad, *listener_, + endstone::EventPriority::HIGH); } void onDisable() override @@ -42,4 +55,12 @@ class ExamplePlugin : public endstone::Plugin { sender.sendErrorMessage("Unknown command: /{}", command.getName()); return false; } + + void onServerLoad(endstone::ServerLoadEvent &event) + { + getLogger().info("ServerLoadEvent is passed to ExamplePlugin::onServerLoad"); + } + +private: + std::unique_ptr listener_; };