Skip to content

Commit

Permalink
feat: add Player::sendToast
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Aug 8, 2024
1 parent eac008b commit 26c6f00
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 4 deletions.
25 changes: 25 additions & 0 deletions include/bedrock/network/packet/toast_request_packet.h
Original file line number Diff line number Diff line change
@@ -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 <string>

#include "bedrock/network/packet.h"

class ToastRequestPacket : public Packet {
public:
std::string title;
std::string content;
};
1 change: 1 addition & 0 deletions include/endstone/detail/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions include/endstone/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
4 changes: 4 additions & 0 deletions python/src/endstone/_internal/endstone_python.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 14 additions & 4 deletions src/endstone_core/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -263,7 +264,7 @@ void EndstonePlayer::sendPopup(std::string message) const
auto packet = MinecraftPackets::createPacket(MinecraftPacketIds::Text);
auto pk = std::static_pointer_cast<TextPacket>(packet);
pk->type = TextPacketType::Popup;
pk->message = message;
pk->message = std::move(message);
getHandle().sendNetworkPacket(*packet);
}

Expand All @@ -272,7 +273,16 @@ void EndstonePlayer::sendTip(std::string message) const
auto packet = MinecraftPackets::createPacket(MinecraftPacketIds::Text);
auto pk = std::static_pointer_cast<TextPacket>(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<ToastRequestPacket>(packet);
pk->title = std::move(title);
pk->content = std::move(content);
getHandle().sendNetworkPacket(*packet);
}

Expand Down Expand Up @@ -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<SetTitlePacket>(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;
Expand All @@ -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<SetTitlePacket>(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;
Expand Down
2 changes: 2 additions & 0 deletions src/endstone_python/endstone_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ void init_player(py::module_ &m, py::class_<Player, Mob> &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"),
Expand Down

0 comments on commit 26c6f00

Please sign in to comment.