Skip to content

Commit

Permalink
feat: add ExampleListener to handle events
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Mar 22, 2024
1 parent 80eb9b3 commit 478193f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
19 changes: 19 additions & 0 deletions include/example_listener.h
Original file line number Diff line number Diff line change
@@ -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_;
};
21 changes: 21 additions & 0 deletions include/example_plugin.h
Original file line number Diff line number Diff line change
@@ -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 <memory>
#include <vector>

class ExamplePlugin : public endstone::Plugin {
public:
void onLoad() override
Expand All @@ -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<FibonacciCommandExecutor>());
}

registerEventHandler<endstone::ServerLoadEvent>(&ExamplePlugin::onServerLoad, *this);

listener_ = std::make_unique<ExampleListener>(*this);
registerEventHandler<endstone::ServerLoadEvent>(&ExampleListener::onServerLoad, *listener_,
endstone::EventPriority::HIGH);
}

void onDisable() override
Expand All @@ -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<ExampleListener> listener_;
};

0 comments on commit 478193f

Please sign in to comment.