Skip to content

Commit

Permalink
feat: add python binding for class CommandSender
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Mar 6, 2024
1 parent b19f531 commit cb6af2f
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 25 deletions.
1 change: 1 addition & 0 deletions include/endstone/command/command_sender.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Server;
class CommandSender {
public:
CommandSender() = default;
virtual ~CommandSender() = default;

/**
* Sends this sender a message
Expand Down
2 changes: 1 addition & 1 deletion include/endstone/plugin/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Plugin : public CommandExecutor {
}

/**
* Returns a value indicating whether or not this plugin is currently
* Returns a value indicating whether this plugin is currently
* enabled
*
* @return true if this plugin is enabled, otherwise false
Expand Down
43 changes: 29 additions & 14 deletions 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', 'Logger', 'Plugin', 'PluginDescription', 'PluginLoader', 'PluginManager', 'Server']
__all__ = ['ColorFormat', 'CommandSender', 'Logger', 'Plugin', 'PluginDescription', 'PluginLoader', 'PluginManager', 'Server']
class ColorFormat:
AQUA: typing.ClassVar[str] = '§b'
BLACK: typing.ClassVar[str] = '§0'
Expand Down Expand Up @@ -33,21 +33,36 @@ class ColorFormat:
RESET: typing.ClassVar[str] = '§r'
WHITE: typing.ClassVar[str] = '§f'
YELLOW: typing.ClassVar[str] = '§e'
class CommandSender:
def send_message(self, message: str) -> None:
"""
Sends this sender a message
"""
@property
def name(self) -> str:
"""
Gets the name of this command sender
"""
@property
def server(self) -> Server:
"""
Returns the server instance that this command is running on
"""
class Logger:
class Level:
"""
Members:
TRACE
DEBUG
INFO
WARNING
ERROR
CRITICAL
"""
CRITICAL: typing.ClassVar[Logger.Level] # value = <Level.CRITICAL: 5>
Expand Down Expand Up @@ -89,19 +104,19 @@ class Logger:
INFO: typing.ClassVar[Logger.Level] # value = <Level.INFO: 2>
TRACE: typing.ClassVar[Logger.Level] # value = <Level.TRACE: 0>
WARNING: typing.ClassVar[Logger.Level] # value = <Level.WARNING: 3>
def critical(self, msg: str) -> None:
def critical(self, message: str) -> None:
"""
Log a message at the CRITICAL level.
"""
def debug(self, msg: str) -> None:
def debug(self, message: str) -> None:
"""
Log a message at the DEBUG level.
"""
def error(self, msg: str) -> None:
def error(self, message: str) -> None:
"""
Log a message at the ERROR level.
"""
def info(self, msg: str) -> None:
def info(self, message: str) -> None:
"""
Log a message at the INFO level.
"""
Expand All @@ -113,11 +128,11 @@ class Logger:
"""
Set the logging level for this Logger instance.
"""
def trace(self, msg: str) -> None:
def trace(self, message: str) -> None:
"""
Log a message at the TRACE level.
"""
def warning(self, msg: str) -> None:
def warning(self, message: str) -> None:
"""
Log a message at the WARNING level.
"""
Expand Down Expand Up @@ -146,7 +161,7 @@ class Plugin:
@property
def enabled(self) -> bool:
"""
Returns a value indicating whether or not this plugin is currently enabled
Returns a value indicating whether this plugin is currently enabled
"""
@property
def logger(self) -> Logger:
Expand Down
42 changes: 42 additions & 0 deletions src/endstone_python/command.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "endstone/command/command.h"

#include <pybind11/pybind11.h>

#include "endstone/command/command_sender.h"
#include "endstone/detail/python.h"
#include "endstone/logger.h"
#include "endstone/server.h"

namespace py = pybind11;

namespace endstone::detail {

void init_command(py::module &m)
{
py_class<Server>(m, "Server");

py_class<CommandSender>(m, "CommandSender")
.def(
"send_message",
[](const CommandSender &sender, const std::string &message) { sender.sendMessage(message); },
py::arg("message"), "Sends this sender a message")
.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");
}

} // namespace endstone::detail
2 changes: 2 additions & 0 deletions src/endstone_python/endstone_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ namespace py = pybind11;

namespace endstone::detail {

void init_command(py::module_ &);
void init_logger(py::module_ &);
void init_server(py::module_ &);
void init_plugin(py::module_ &);
void init_util(py::module_ &);

PYBIND11_MODULE(endstone_python, m) // NOLINT(*-use-anonymous-namespace)
{
init_command(m);
init_logger(m);
init_server(m);
init_plugin(m);
Expand Down
18 changes: 9 additions & 9 deletions src/endstone_python/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ void init_logger(py::module &m)
.def("is_enabled_for", &Logger::isEnabledFor, py::arg("level"),
"Check if the Logger instance is enabled for the given log Level.")
.def(
"trace", [](const Logger &logger, const std::string &message) { logger.trace(message); }, py::arg("msg"),
"Log a message at the TRACE level.")
"trace", [](const Logger &logger, const std::string &message) { logger.trace(message); },
py::arg("message"), "Log a message at the TRACE level.")
.def(
"debug", [](const Logger &logger, const std::string &message) { logger.debug(message); }, py::arg("msg"),
"Log a message at the DEBUG level.")
"debug", [](const Logger &logger, const std::string &message) { logger.debug(message); },
py::arg("message"), "Log a message at the DEBUG level.")
.def(
"info", [](const Logger &logger, const std::string &message) { logger.info(message); }, py::arg("msg"),
"info", [](const Logger &logger, const std::string &message) { logger.info(message); }, py::arg("message"),
"Log a message at the INFO level.")
.def(
"warning", [](const Logger &logger, const std::string &message) { logger.warning(message); },
py::arg("msg"), "Log a message at the WARNING level.")
py::arg("message"), "Log a message at the WARNING level.")
.def(
"error", [](const Logger &logger, const std::string &message) { logger.error(message); }, py::arg("msg"),
"Log a message at the ERROR level.")
"error", [](const Logger &logger, const std::string &message) { logger.error(message); },
py::arg("message"), "Log a message at the ERROR level.")
.def(
"critical", [](const Logger &logger, const std::string &message) { logger.critical(message); },
py::arg("msg"), "Log a message at the CRITICAL level.")
py::arg("message"), "Log a message at the CRITICAL level.")
.def_property_readonly("name", &Logger::getName, "Get the name of this Logger instance.");
}

Expand Down
2 changes: 1 addition & 1 deletion src/endstone_python/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void init_plugin(py::module &m)
.def_property_readonly("server", &Plugin::getServer, py::return_value_policy::reference,
"Returns the Server instance currently running this plugin")
.def_property_readonly("enabled", &Plugin::isEnabled,
"Returns a value indicating whether or not this plugin is currently enabled")
"Returns a value indicating whether this plugin is currently enabled")
.def_property_readonly("name", &Plugin::getName, "Returns the name of the plugin.");

py_class<PluginLoader, PyPluginLoader>(m, "PluginLoader")
Expand Down

0 comments on commit cb6af2f

Please sign in to comment.