From 26c6f00f666c317490ade9bb9d67783c0fda9800 Mon Sep 17 00:00:00 2001 From: Vincent Date: Thu, 8 Aug 2024 11:07:09 +0100 Subject: [PATCH] feat: add Player::sendToast --- .../network/packet/toast_request_packet.h | 25 +++++++++++++++++++ include/endstone/detail/player.h | 1 + include/endstone/player.h | 8 ++++++ .../endstone/_internal/endstone_python.pyi | 4 +++ src/endstone_core/player.cpp | 18 ++++++++++--- src/endstone_python/endstone_python.cpp | 2 ++ 6 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 include/bedrock/network/packet/toast_request_packet.h diff --git a/include/bedrock/network/packet/toast_request_packet.h b/include/bedrock/network/packet/toast_request_packet.h new file mode 100644 index 000000000..29f9041d7 --- /dev/null +++ b/include/bedrock/network/packet/toast_request_packet.h @@ -0,0 +1,25 @@ +// 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. + +#pragma once + +#include + +#include "bedrock/network/packet.h" + +class ToastRequestPacket : public Packet { +public: + std::string title; + std::string content; +}; diff --git a/include/endstone/detail/player.h b/include/endstone/detail/player.h index aed4244a5..4bc1c8154 100644 --- a/include/endstone/detail/player.h +++ b/include/endstone/detail/player.h @@ -79,6 +79,7 @@ class EndstonePlayer : public EndstoneMob, public Player { [[nodiscard]] const SocketAddress &getAddress() const override; void sendPopup(std::string message) const override; void sendTip(std::string message) const override; + void sendToast(std::string title, std::string content) const override; void kick(std::string message) const override; void giveExp(int amount) override; void giveExpLevels(int amount) override; diff --git a/include/endstone/player.h b/include/endstone/player.h index a82f65494..b0a9348da 100644 --- a/include/endstone/player.h +++ b/include/endstone/player.h @@ -88,6 +88,14 @@ class Player : public Mob { */ virtual void sendTip(std::string message) const = 0; + /** + * @brief Sends this player a toast notification. + * + * @param title The title of the toast notification. + * @param content The content of the toast notification. + */ + virtual void sendToast(std::string title, std::string content) const = 0; + /** * @brief Kicks player with custom kick message. * diff --git a/python/src/endstone/_internal/endstone_python.pyi b/python/src/endstone/_internal/endstone_python.pyi index 06d999d27..87e3f614b 100644 --- a/python/src/endstone/_internal/endstone_python.pyi +++ b/python/src/endstone/_internal/endstone_python.pyi @@ -1316,6 +1316,10 @@ class Player(Mob): """ Sends a title and a subtitle message to the player. If they are empty strings, the display will be updated as such. """ + def send_toast(self, title: str, content: str) -> None: + """ + Sends this player a toast notification. + """ def transfer(self, host: str, port: int = 19132) -> None: """ Transfers the player to another server. diff --git a/src/endstone_core/player.cpp b/src/endstone_core/player.cpp index ab9863409..4be141c85 100644 --- a/src/endstone_core/player.cpp +++ b/src/endstone_core/player.cpp @@ -25,6 +25,7 @@ #include "bedrock/network/packet/modal_form_request_packet.h" #include "bedrock/network/packet/set_title_packet.h" #include "bedrock/network/packet/text_packet.h" +#include "bedrock/network/packet/toast_request_packet.h" #include "bedrock/network/packet/transfer_packet.h" #include "bedrock/network/packet/update_abilities_packet.h" #include "bedrock/network/server_network_handler.h" @@ -263,7 +264,7 @@ void EndstonePlayer::sendPopup(std::string message) const auto packet = MinecraftPackets::createPacket(MinecraftPacketIds::Text); auto pk = std::static_pointer_cast(packet); pk->type = TextPacketType::Popup; - pk->message = message; + pk->message = std::move(message); getHandle().sendNetworkPacket(*packet); } @@ -272,7 +273,16 @@ void EndstonePlayer::sendTip(std::string message) const auto packet = MinecraftPackets::createPacket(MinecraftPacketIds::Text); auto pk = std::static_pointer_cast(packet); pk->type = TextPacketType::Tip; - pk->message = message; + pk->message = std::move(message); + getHandle().sendNetworkPacket(*packet); +} + +void EndstonePlayer::sendToast(std::string title, std::string content) const +{ + auto packet = MinecraftPackets::createPacket(MinecraftPacketIds::ToastRequest); + auto pk = std::static_pointer_cast(packet); + pk->title = std::move(title); + pk->content = std::move(content); getHandle().sendNetworkPacket(*packet); } @@ -406,7 +416,7 @@ void EndstonePlayer::sendTitle(std::string title, std::string subtitle, int fade auto packet = MinecraftPackets::createPacket(MinecraftPacketIds::SetTitle); auto pk = std::static_pointer_cast(packet); pk->type = SetTitlePacket::TitleType::Title; - pk->title_text = title; + pk->title_text = std::move(title); pk->fade_in_time = fade_in; pk->stay_time = stay; pk->fade_out_time = fade_out; @@ -416,7 +426,7 @@ void EndstonePlayer::sendTitle(std::string title, std::string subtitle, int fade auto packet = MinecraftPackets::createPacket(MinecraftPacketIds::SetTitle); auto pk = std::static_pointer_cast(packet); pk->type = SetTitlePacket::TitleType::Subtitle; - pk->title_text = subtitle; + pk->title_text = std::move(subtitle); pk->fade_in_time = fade_in; pk->stay_time = stay; pk->fade_out_time = fade_out; diff --git a/src/endstone_python/endstone_python.cpp b/src/endstone_python/endstone_python.cpp index 74bb07865..a0c427c3d 100644 --- a/src/endstone_python/endstone_python.cpp +++ b/src/endstone_python/endstone_python.cpp @@ -312,6 +312,8 @@ void init_player(py::module_ &m, py::class_ &player) .def_property_readonly("address", &Player::getAddress, "Gets the socket address of this player") .def("send_popup", &Player::sendPopup, py::arg("message"), "Sends this player a popup message") .def("send_tip", &Player::sendTip, py::arg("message"), "Sends this player a tip message") + .def("send_toast", &Player::sendToast, py::arg("title"), py::arg("content"), + "Sends this player a toast notification.") .def("kick", &Player::kick, py::arg("message"), "Kicks player with custom kick message.") .def("give_exp", &Player::giveExp, py::arg("amount"), "Gives the player the amount of experience specified.") .def("give_exp_levels", &Player::giveExpLevels, py::arg("amount"),