From c2476486f293bb9a4242234e821a9a96cce6a520 Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 6 Mar 2024 23:40:58 +0000 Subject: [PATCH] feat: add python binding for class `PluginCommand` --- .../endstone/_internal/endstone_python.pyi | 22 ++++++++++++++++++- src/endstone_python/command.cpp | 2 +- src/endstone_python/plugin.cpp | 12 +++++++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/python/src/endstone/_internal/endstone_python.pyi b/python/src/endstone/_internal/endstone_python.pyi index cb6c8c9d8..1568aa32e 100644 --- a/python/src/endstone/_internal/endstone_python.pyi +++ b/python/src/endstone/_internal/endstone_python.pyi @@ -1,6 +1,6 @@ from __future__ import annotations import typing -__all__ = ['ColorFormat', 'Command', 'CommandExecutor', 'CommandSender', 'Logger', 'Plugin', 'PluginDescription', 'PluginLoader', 'PluginManager', 'Server'] +__all__ = ['ColorFormat', 'Command', 'CommandExecutor', 'CommandSender', 'Logger', 'Plugin', 'PluginCommand', 'PluginDescription', 'PluginLoader', 'PluginManager', 'Server'] class ColorFormat: AQUA: typing.ClassVar[str] = '§b' BLACK: typing.ClassVar[str] = '§0' @@ -197,6 +197,10 @@ class Plugin(CommandExecutor): ... def _get_description(self) -> PluginDescription: ... + def get_command(self, name: str) -> PluginCommand: + """ + Gets the command with the given name, specific to this plugin. + """ def on_disable(self) -> None: """ Called when this plugin is disabled @@ -234,6 +238,22 @@ class Plugin(CommandExecutor): """ Returns the Server instance currently running this plugin """ +class PluginCommand(Command): + def __init__(self, command: Command, owner: Plugin) -> None: + ... + @property + def executor(self) -> CommandExecutor: + """ + The CommandExecutor to run when parsing this command + """ + @executor.setter + def executor(self, arg1: CommandExecutor) -> None: + ... + @property + def plugin(self) -> Plugin: + """ + Gets the owner of this PluginCommand + """ class PluginDescription: def __init__(self, name: str, version: str, description: str | None = None, authors: list[str] | None = None, prefix: str | None = None, *args, **kwargs) -> None: ... diff --git a/src/endstone_python/command.cpp b/src/endstone_python/command.cpp index c189cf09b..218db9474 100644 --- a/src/endstone_python/command.cpp +++ b/src/endstone_python/command.cpp @@ -34,7 +34,7 @@ class PyCommandExecutor : public CommandExecutor { bool onCommand(const endstone::CommandSender &sender, const endstone::Command &command, const std::vector &args) override { - PYBIND11_OVERRIDE(bool, endstone::CommandExecutor, onCommand, sender, command, args); + PYBIND11_OVERRIDE_NAME(bool, endstone::CommandExecutor, "on_command", onCommand, sender, command, args); } }; diff --git a/src/endstone_python/plugin.cpp b/src/endstone_python/plugin.cpp index 4e3e89ac3..878aa9cb5 100644 --- a/src/endstone_python/plugin.cpp +++ b/src/endstone_python/plugin.cpp @@ -122,6 +122,8 @@ void init_plugin(py::module &m) py_class(m, "PluginLoader"); + py_class(m, "PluginCommand"); + py_class(m, "Plugin") .def(py::init<>()) .def("on_load", &Plugin::onLoad, "Called after a plugin is loaded but before it has been enabled.") @@ -136,7 +138,15 @@ void init_plugin(py::module &m) "Returns the Server instance currently running this plugin") .def_property_readonly("enabled", &Plugin::isEnabled, "Returns a value indicating whether this plugin is currently enabled") - .def_property_readonly("name", &Plugin::getName, "Returns the name of the plugin."); + .def_property_readonly("name", &Plugin::getName, "Returns the name of the plugin.") + .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(m, "PluginCommand") + .def(py::init(), py::arg("command"), py::arg("owner")) + .def_property("executor", &PluginCommand::getExecutor, &PluginCommand::setExecutor, + "The CommandExecutor to run when parsing this command") + .def_property_readonly("plugin", &PluginCommand::getPlugin, "Gets the owner of this PluginCommand"); py_class(m, "PluginLoader") .def(py::init(), py::arg("server"))