Skip to content

Commit

Permalink
feat: add Player::getScoreboard and Player::setScoreboard
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Aug 2, 2024
1 parent 486686a commit 0d5fa68
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 9 deletions.
2 changes: 2 additions & 0 deletions include/endstone/detail/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class EndstonePlayer : public EndstoneMob, public Player {
void setFlySpeed(float value) const override;
[[nodiscard]] float getWalkSpeed() const override;
void setWalkSpeed(float value) const override;
[[nodiscard]] Scoreboard &getScoreboard() const override;
void setScoreboard(Scoreboard &scoreboard) override;
void sendTitle(std::string title, std::string subtitle) const override;
void sendTitle(std::string title, std::string subtitle, int fade_in, int stay, int fade_out) const override;
void resetTitle() const override;
Expand Down
6 changes: 3 additions & 3 deletions include/endstone/detail/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ class EndstoneServer : public Server {
[[nodiscard]] Scoreboard *getScoreboard() const override;
void setScoreboard(std::unique_ptr<EndstoneScoreboard> scoreboard);
[[nodiscard]] std::shared_ptr<Scoreboard> getNewScoreboard() override;
[[nodiscard]] EndstoneScoreboard &getPlayerBoard(EndstonePlayer &player) const;
void setPlayerBoard(EndstonePlayer &player, const std::shared_ptr<EndstoneScoreboard> &scoreboard);
[[nodiscard]] EndstoneScoreboard &getPlayerBoard(const EndstonePlayer &player) const;
void setPlayerBoard(EndstonePlayer &player, Scoreboard &scoreboard);
void removePlayerBoard(EndstonePlayer &player);
[[nodiscard]] ::ServerNetworkHandler &getServerNetworkHandler() const;

Expand All @@ -93,7 +93,7 @@ class EndstoneServer : public Server {
std::unordered_map<UUID, Player *> players_;
std::shared_ptr<EndstoneScoreboard> scoreboard_;
std::vector<std::weak_ptr<EndstoneScoreboard>> scoreboards_;
std::unordered_map<EndstonePlayer *, std::shared_ptr<EndstoneScoreboard>> player_boards_;
std::unordered_map<const EndstonePlayer *, std::shared_ptr<EndstoneScoreboard>> player_boards_;
};

} // namespace endstone::detail
15 changes: 15 additions & 0 deletions include/endstone/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "endstone/skin.h"
#include "endstone/util/socket_address.h"
#include "endstone/util/uuid.h"
#include "scoreboard/scoreboard.h"

namespace endstone {

Expand Down Expand Up @@ -205,6 +206,20 @@ class Player : public Mob {
*/
virtual void setWalkSpeed(float value) const = 0;

/**
*@brief Gets the Scoreboard displayed to this player
*
* @return The current scoreboard seen by this player
*/
[[nodiscard]] virtual Scoreboard &getScoreboard() const = 0;

/**
* @breif Sets the player's visible Scoreboard.
*
* @param scoreboard New Scoreboard for the player
*/
void virtual setScoreboard(Scoreboard &scoreboard) = 0;

/**
* @brief Sends a title and a subtitle message to the player. If they are empty strings, the display will be
* updated as such. The titles will be displayed with the default timings.
Expand Down
8 changes: 8 additions & 0 deletions python/src/endstone/_internal/endstone_python.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,14 @@ class Player(Mob):
Gets the player's average ping in milliseconds.
"""
@property
def scoreboard(self) -> Scoreboard:
"""
Gets or sets the player's visible Scoreboard.
"""
@scoreboard.setter
def scoreboard(self, arg1: Scoreboard) -> None:
...
@property
def skin(self) -> Skin:
"""
Get the player's skin.
Expand Down
10 changes: 10 additions & 0 deletions src/endstone_core/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,16 @@ void EndstonePlayer::setWalkSpeed(float value) const
updateAbilities();
}

Scoreboard &EndstonePlayer::getScoreboard() const
{
return server_.getPlayerBoard(*this);
}

void EndstonePlayer::setScoreboard(Scoreboard &scoreboard)
{
server_.setPlayerBoard(*this, scoreboard);
}

void EndstonePlayer::sendTitle(std::string title, std::string subtitle) const
{
sendTitle(std::move(title), std::move(subtitle), 10, 70, 20);
Expand Down
10 changes: 5 additions & 5 deletions src/endstone_core/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ std::shared_ptr<Scoreboard> EndstoneServer::getNewScoreboard()
return result;
}

EndstoneScoreboard &EndstoneServer::getPlayerBoard(EndstonePlayer &player) const
EndstoneScoreboard &EndstoneServer::getPlayerBoard(const EndstonePlayer &player) const
{
auto it = player_boards_.find(&player);
if (it == player_boards_.end()) {
Expand All @@ -277,20 +277,20 @@ EndstoneScoreboard &EndstoneServer::getPlayerBoard(EndstonePlayer &player) const
return *it->second;
}

void EndstoneServer::setPlayerBoard(EndstonePlayer &player, const std::shared_ptr<EndstoneScoreboard>& scoreboard)
void EndstoneServer::setPlayerBoard(EndstonePlayer &player, Scoreboard &scoreboard)
{
auto &old_board = getPlayerBoard(player).getHandle();
auto &new_board = scoreboard->getHandle();
auto &new_board = static_cast<EndstoneScoreboard &>(scoreboard).getHandle();

if (&old_board == &new_board) {
return;
}

if (scoreboard == scoreboard_) {
if (&scoreboard == scoreboard_.get()) {
player_boards_.erase(&player);
}
else {
player_boards_[&player] = scoreboard;
player_boards_[&player] = std::static_pointer_cast<EndstoneScoreboard>(scoreboard.shared_from_this());
}
// TODO(check): should we send any packet to client?
}
Expand Down
4 changes: 3 additions & 1 deletion src/endstone_python/endstone_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ PYBIND11_MODULE(endstone_python, m) // NOLINT(*-use-anonymous-namespace)
init_inventory(m);
init_util(m);
init_level(m);
init_scoreboard(m);
init_actor(m, actor, mob);
init_player(m, player);
init_scoreboard(m);
init_command(m, command_sender);
init_plugin(m);
init_scheduler(m);
Expand Down Expand Up @@ -312,6 +312,8 @@ void init_player(py::module_ &m, py::class_<Player, Mob> &player)
"Gets or sets the current allowed speed that a client can fly.")
.def_property("walk_speed", &Player::getWalkSpeed, &Player::setWalkSpeed,
"Gets or sets the current allowed speed that a client can walk.")
.def_property("scoreboard", &Player::getScoreboard, &Player::setScoreboard,
"Gets or sets the player's visible Scoreboard.", py::return_value_policy::reference)
.def("send_title", py::overload_cast<std::string, std::string, int, int, int>(&Player::sendTitle, py::const_),
"Sends a title and a subtitle message to the player. If they are empty strings, the display will be "
"updated as such.",
Expand Down

0 comments on commit 0d5fa68

Please sign in to comment.