Skip to content

Commit

Permalink
feat: add python bindings for tps, mspt and tick usage
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Aug 6, 2024
1 parent e6a58a2 commit 2652124
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
8 changes: 4 additions & 4 deletions include/endstone/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,14 @@ class Server {
[[nodiscard]] virtual std::shared_ptr<Scoreboard> getNewScoreboard() = 0;

/**
* @brief Returns the current milliseconds per tick (MSPT).
* @brief Gets the current milliseconds per tick (MSPT).
*
* @return The average current of milliseconds per tick.
*/
virtual float getCurrentMillisecondsPerTick() = 0;

/**
* @brief Returns the average milliseconds per tick (MSPT).
* @brief Gets the average milliseconds per tick (MSPT).
*
* @return The average number of milliseconds per tick.
*/
Expand All @@ -234,14 +234,14 @@ class Server {
virtual float getAverageTicksPerSecond() = 0;

/**
* @brief Returns the current tick usage of the server.
* @brief Gets the current tick usage of the server.
*
* @return The current tick usage in percentage.
*/
virtual float getCurrentTickUsage() = 0;

/**
* @brief Returns the average tick usage of the server.
* @brief Gets the average tick usage of the server.
*
* @return The average tick usage in percentage.
*/
Expand Down
30 changes: 30 additions & 0 deletions python/src/endstone/_internal/endstone_python.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2013,11 +2013,41 @@ class Server:
Shutdowns the server, stopping everything.
"""
@property
def average_mspt(self) -> float:
"""
Gets the average milliseconds per tick (MSPT).
"""
@property
def average_tick_usage(self) -> float:
"""
Gets the average tick usage of the server.
"""
@property
def average_tps(self) -> float:
"""
Gets the average ticks per second (TPS).
"""
@property
def command_sender(self) -> ConsoleCommandSender:
"""
Gets a CommandSender for this server.
"""
@property
def current_mspt(self) -> float:
"""
Gets the current milliseconds per tick (MSPT).
"""
@property
def current_tick_usage(self) -> float:
"""
Gets the current tick usage of the server.
"""
@property
def current_tps(self) -> float:
"""
Gets the current ticks per second (TPS).
"""
@property
def level(self) -> Level:
"""
Gets the server level.
Expand Down
16 changes: 14 additions & 2 deletions src/endstone_python/endstone_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,26 @@ void init_server(py::class_<Server> &server)
py::return_value_policy::reference)
.def("get_new_scoreboard", &Server::getNewScoreboard, "Gets a new Scoreboard to be tracked by the server.",
py::return_value_policy::reference)
.def_property_readonly("current_mspt", &Server::getCurrentMillisecondsPerTick,
"Gets the current milliseconds per tick (MSPT).")
.def_property_readonly("average_mspt", &Server::getAverageMillisecondsPerTick,
"Gets the average milliseconds per tick (MSPT).")
.def_property_readonly("current_tps", &Server::getCurrentTicksPerSecond,
"Gets the current ticks per second (TPS).")
.def_property_readonly("average_tps", &Server::getAverageTicksPerSecond,
"Gets the average ticks per second (TPS).")
.def_property_readonly("current_tick_usage", &Server::getCurrentTickUsage,
"Gets the current tick usage of the server.")
.def_property_readonly("average_tick_usage", &Server::getAverageTickUsage,
"Gets the average tick usage of the server.")
.def_property_readonly("start_time", &Server::getStartTime, "Gets the start time of the server.");
}

void init_player(py::module_ &m, py::class_<Player, Mob> &player)
{
py::class_<Skin>(m, "Skin")
.def(py::init([](std::string skin_id, py::array_t<std::uint8_t> skin_data, std::optional<std::string> cape_id,
std::optional<py::array_t<std::uint8_t>> cape_data) {
.def(py::init([](std::string skin_id, const py::array_t<std::uint8_t> &skin_data,
std::optional<std::string> cape_id, std::optional<py::array_t<std::uint8_t>> cape_data) {
py::buffer_info info1 = skin_data.request();
if (info1.ndim != 3 || info1.shape[2] != 4) {
throw std::runtime_error("Incompatible shape. Expected (h, w, 4)");
Expand Down

0 comments on commit 2652124

Please sign in to comment.