-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplugin.cpp
More file actions
86 lines (73 loc) · 2.74 KB
/
Copy pathplugin.cpp
File metadata and controls
86 lines (73 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <endstone/endstone.hpp>
#include <endstone_papi/papi.h>
namespace {
/**
* @brief A minimal provider expansion: answers {player:name}.
*
* PAPI's core supplies no placeholder values of its own; every value comes from an
* expansion like this one, registered by a plugin.
*/
class NameExpansion final : public papi::PlaceholderExpansion {
public:
[[nodiscard]] std::string getIdentifier() const override { return "player"; }
[[nodiscard]] std::string getAuthor() const override { return "Endstone"; }
[[nodiscard]] std::string getVersion() const override { return "1.0.0"; }
[[nodiscard]] std::optional<std::string> onRequest(const endstone::OfflinePlayer *player,
const std::string_view params) override
{
if (params != "name") {
// Unresolved: PAPI leaves the original placeholder text in place.
return std::nullopt;
}
// The player may be null, and is borrowed for this call only.
return player ? player->getName() : std::string("nobody");
}
};
} // namespace
class JoinExample : public endstone::Plugin {
public:
void onEnable() override
{
api_ =
getServer().getServiceManager().load<papi::PlaceholderAPI>(std::string(papi::PlaceholderAPI::ServiceName));
if (!api_ || !api_->isActive()) {
getLogger().warning("PlaceholderAPI is unavailable; disabling example.");
getServer().getPluginManager().disablePlugin(*this);
return;
}
if (!api_->registerExpansion(*this, std::make_shared<NameExpansion>())) {
getLogger().warning("Could not register the example expansion.");
return;
}
registerEvent(&JoinExample::onPlayerJoin, *this, endstone::EventPriority::Highest);
}
void onDisable() override
{
// PAPI removes owned expansions on disable anyway; doing it here is explicit.
if (api_) {
api_->unregisterExpansions(*this);
api_.reset();
}
}
void onPlayerJoin(endstone::PlayerJoinEvent &event)
{
if (!api_) {
return;
}
event.setJoinMessage(api_->setPlaceholders(&event.getPlayer(), "{player:name} joined the server!"));
}
private:
std::shared_ptr<papi::PlaceholderAPI> api_;
};
ENDSTONE_PLUGIN(/*name=*/"papi_example", /*version=*/"1.0.0", /*main_class=*/JoinExample)
{
prefix = "PlaceholderAPI Example";
description = "An example plugin that provides and consumes placeholders";
website = "https://github.com/EndstoneMC/papi";
authors = {"Vincent <magicdroidx@gmail.com>"};
soft_depend = {"papi"};
}