Skip to content

Commit

Permalink
feat: add bindings for plugin registration
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Mar 6, 2024
1 parent c247648 commit 35b0198
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion include/endstone/detail/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class EndstoneServer : public Server {
[[nodiscard]] MinecraftCommands &getMinecraftCommands();
[[nodiscard]] PluginManager &getPluginManager() const override;
[[nodiscard]] PluginCommand *getPluginCommand(std::string name) const override;
[[nodiscard]] PluginCommand *registerPluginCommand(std::unique_ptr<PluginCommand> command) const override;
[[nodiscard]] PluginCommand *registerPluginCommand(std::shared_ptr<PluginCommand> command) const override;

void loadPlugins();
void enablePlugins() const;
Expand Down
2 changes: 1 addition & 1 deletion include/endstone/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Server {
* @param command the command to be registered
* @return a pointer to the registered plugin command
*/
[[nodiscard]] virtual PluginCommand *registerPluginCommand(std::unique_ptr<PluginCommand> command) const = 0;
[[nodiscard]] virtual PluginCommand *registerPluginCommand(std::shared_ptr<PluginCommand> command) const = 0;

/**
* Gets the name of this server implementation.
Expand Down
10 changes: 9 additions & 1 deletion python/src/endstone/_internal/endstone_python.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,14 @@ class PluginManager:
Gets a list of all currently loaded plugins
"""
class Server:
def get_plugin_command(self, name: str) -> PluginCommand:
"""
Gets a PluginCommand with the given name or alias.
"""
def register_plugin_command(self, command: PluginCommand) -> PluginCommand:
"""
Registers a new PluginCommand.
"""
@property
def logger(self) -> Logger:
"""
Expand All @@ -370,5 +378,5 @@ class Server:
@property
def version(self) -> str:
"""
Gets the name of this server implementation.
Gets the version of this server implementation.
"""
4 changes: 2 additions & 2 deletions src/endstone_core/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ PluginCommand *EndstoneServer::getPluginCommand(std::string name) const
return dynamic_cast<PluginCommand *>(command);
}

PluginCommand *EndstoneServer::registerPluginCommand(std::unique_ptr<PluginCommand> command) const
PluginCommand *EndstoneServer::registerPluginCommand(std::shared_ptr<PluginCommand> command) const
{
auto name = command->getName();
if (command_map_->registerCommand(std::move(command))) {
if (command_map_->registerCommand(command)) {
return getPluginCommand(name);
}
return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/endstone_python/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void init_command(py::module &m)
"Returns the server instance that this command is running on")
.def_property_readonly("name", &CommandSender::getName, "Gets the name of this command sender");

py_class<Command>(m, "Command")
py_class<Command, std::shared_ptr<Command>>(m, "Command")
.def(py::init(&createCommand), py::arg("name"), py::arg("description") = py::none(),
py::arg("usages") = py::none(), py::arg("aliases") = py::none())
.def("execute", &Command::execute, py::arg("sender"), py::arg("args"),
Expand Down
4 changes: 1 addition & 3 deletions src/endstone_python/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ void init_plugin(py::module &m)

py_class<PluginLoader, PyPluginLoader>(m, "PluginLoader");

py_class<PluginCommand, Command>(m, "PluginCommand");

py_class<Plugin, CommandExecutor, PyPlugin>(m, "Plugin")
.def(py::init<>())
.def("on_load", &Plugin::onLoad, "Called after a plugin is loaded but before it has been enabled.")
Expand All @@ -142,7 +140,7 @@ void init_plugin(py::module &m)
.def("get_command", &Plugin::getCommand, py::return_value_policy::reference, py::arg("name"),
"Gets the command with the given name, specific to this plugin.");

py_class<PluginCommand, Command>(m, "PluginCommand")
py_class<PluginCommand, Command, std::shared_ptr<PluginCommand>>(m, "PluginCommand")
.def(py::init<const Command &, Plugin &>(), py::arg("command"), py::arg("owner"))
.def_property("executor", &PluginCommand::getExecutor, &PluginCommand::setExecutor,
"The CommandExecutor to run when parsing this command")
Expand Down
8 changes: 7 additions & 1 deletion src/endstone_python/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ void init_server(py::module &m)
{
py_class<PluginManager>(m, "PluginManager");

py_class<PluginCommand, Command, std::shared_ptr<PluginCommand>>(m, "PluginCommand");

py_class<Server>(m, "Server")
.def_property_readonly("logger", &Server::getLogger, py::return_value_policy::reference,
"Returns the primary logger associated with this server instance.")
.def_property_readonly("plugin_manager", &Server::getPluginManager, py::return_value_policy::reference,
"Gets the plugin manager for interfacing with plugins.")
.def("get_plugin_command", &Server::getPluginCommand, py::arg("name"), py::return_value_policy::reference,
"Gets a PluginCommand with the given name or alias.")
.def("register_plugin_command", &Server::registerPluginCommand, py::arg("command"),
"Registers a new PluginCommand.")
.def_property_readonly("name", &Server::getVersion, "Gets the name of this server implementation.")
.def_property_readonly("version", &Server::getVersion, "Gets the name of this server implementation.")
.def_property_readonly("version", &Server::getVersion, "Gets the version of this server implementation.")
.def_property_readonly("minecraft_version", &Server::getMinecraftVersion,
"Gets the Minecraft version that this server is running.");
}
Expand Down

0 comments on commit 35b0198

Please sign in to comment.