Skip to content

Commit

Permalink
feat: add python binding for class CommandExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Mar 6, 2024
1 parent cb6af2f commit 65b9a22
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/endstone/command/command_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CommandExecutor {
virtual ~CommandExecutor() = default;

/**
* Executes the given command, returning its forceOutput.
* Executes the given command, returning its success.
*
* @param sender Source of the command
* @param command Command which was executed
Expand Down
9 changes: 8 additions & 1 deletion python/src/endstone/_internal/endstone_python.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations
import typing
__all__ = ['ColorFormat', 'CommandSender', 'Logger', 'Plugin', 'PluginDescription', 'PluginLoader', 'PluginManager', 'Server']
__all__ = ['ColorFormat', 'CommandExecutor', 'CommandSender', 'Logger', 'Plugin', 'PluginDescription', 'PluginLoader', 'PluginManager', 'Server']
class ColorFormat:
AQUA: typing.ClassVar[str] = '§b'
BLACK: typing.ClassVar[str] = '§0'
Expand Down Expand Up @@ -33,6 +33,13 @@ class ColorFormat:
RESET: typing.ClassVar[str] = '§r'
WHITE: typing.ClassVar[str] = '§f'
YELLOW: typing.ClassVar[str] = '§e'
class CommandExecutor:
def __init__(self) -> None:
...
def on_command(self, sender: CommandSender, command: ..., args: list[str]) -> bool:
"""
Executes the given command, returning its success.
"""
class CommandSender:
def send_message(self, message: str) -> None:
"""
Expand Down
3 changes: 3 additions & 0 deletions python/src/endstone/command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from endstone._internal.endstone_python import CommandSender, CommandExecutor

__all__ = ["CommandSender", "CommandExecutor"]
18 changes: 18 additions & 0 deletions src/endstone_python/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#include "endstone/command/command.h"

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include "endstone/command/command_executor.h"
#include "endstone/command/command_sender.h"
#include "endstone/detail/python.h"
#include "endstone/logger.h"
Expand All @@ -25,6 +27,17 @@ namespace py = pybind11;

namespace endstone::detail {

class PyCommandExecutor : public CommandExecutor {
public:
using CommandExecutor::CommandExecutor;

bool onCommand(const endstone::CommandSender &sender, const endstone::Command &command,
const std::vector<std::string> &args) override
{
PYBIND11_OVERRIDE(bool, endstone::CommandExecutor, onCommand, sender, command, args);
}
};

void init_command(py::module &m)
{
py_class<Server>(m, "Server");
Expand All @@ -37,6 +50,11 @@ void init_command(py::module &m)
.def_property_readonly("server", &CommandSender::getServer, py::return_value_policy::reference,
"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<CommandExecutor, PyCommandExecutor>(m, "CommandExecutor")
.def(py::init<>())
.def("on_command", &endstone::CommandExecutor::onCommand, py::arg("sender"), py::arg("command"),
py::arg("args"), "Executes the given command, returning its success.");
}

} // namespace endstone::detail

0 comments on commit 65b9a22

Please sign in to comment.