Skip to content

Commit

Permalink
docs: add docstring for plugin.register_command
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Mar 7, 2024
1 parent 733f09a commit aae8bde
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
3 changes: 3 additions & 0 deletions python/src/endstone/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def __init__(self):
self._plugin_commands: list[PluginCommand] = [] # keep them alive

def register_command(self, command_type: typing.Type[Command]) -> PluginCommand:
"""
Registers a new PluginCommand.
"""
command = PluginCommand(command_type(), self)
self._plugin_commands.append(command)
return self.server.register_plugin_command(command)
Expand Down
8 changes: 6 additions & 2 deletions src/endstone_python/endstone_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ namespace endstone::detail {
void init_command(py::module_ &);
void init_logger(py::module_ &);
void init_plugin(py::module_ &);
void init_server(py::module_ &);
void init_server(py::module_ &, py::class_<Server> &server);
void init_util(py::module_ &);

PYBIND11_MODULE(endstone_python, m) // NOLINT(*-use-anonymous-namespace)
{

// Forward declaration, see:
// https://pybind11.readthedocs.io/en/stable/advanced/misc.html#avoiding-c-types-in-docstrings
auto server = py::class_<Server>(m, "Server");
init_command(m);
init_logger(m);
init_plugin(m);
init_server(m);
init_server(m, server);
init_util(m);
}

Expand Down
8 changes: 6 additions & 2 deletions src/endstone_python/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ class PyPluginCommand : public PluginCommand {

void init_plugin(py::module &m)
{
auto plugin_command =
py::class_<PluginCommand, Command, PyPluginCommand, std::shared_ptr<PluginCommand>>(m, "PluginCommand");
auto plugin_loader = py::class_<PluginLoader, PyPluginLoader>(m, "PluginLoader");

py::class_<PluginDescription>(m, "PluginDescription")
.def(py::init(&createPluginDescription), py::arg("name"), py::arg("version"),
py::arg("description") = py::none(), py::arg("authors") = py::none(), py::arg("prefix") = py::none())
Expand Down Expand Up @@ -158,13 +162,13 @@ 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, PyPluginCommand, std::shared_ptr<PluginCommand>>(m, "PluginCommand")
plugin_command //
.def(py::init<const Command &, Plugin &>(), py::arg("command"), py::arg("owner"))
.def("_get_executor", &PluginCommand::getExecutor, py::return_value_policy::reference)
.def("_set_executor", &PluginCommand::setExecutor, py::arg("executor"))
.def_property_readonly("plugin", &PluginCommand::getPlugin, "Gets the owner of this PluginCommand");

py::class_<PluginLoader, PyPluginLoader>(m, "PluginLoader")
plugin_loader //
.def(py::init<Server &>(), py::arg("server"))
.def("load_plugins", &PluginLoader::loadPlugins, py::arg("directory"),
py::return_value_policy::reference_internal, "Loads the plugin contained within the specified directory")
Expand Down
4 changes: 2 additions & 2 deletions src/endstone_python/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ namespace py = pybind11;

namespace endstone::detail {

void init_server(py::module &m)
void init_server(py::module &m, py::class_<Server> &server)
{
py::class_<Server>(m, "Server")
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,
Expand Down

0 comments on commit aae8bde

Please sign in to comment.