Skip to content

Commit

Permalink
feat: replace Scoreboard's default packet sender with ScoreboardPacke…
Browse files Browse the repository at this point in the history
…tSender
  • Loading branch information
wu-vincent committed Aug 4, 2024
1 parent f61bd36 commit 39f41c8
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.
1 change: 0 additions & 1 deletion include/bedrock/network/packet_sender.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

class PacketSender : public Bedrock::EnableNonOwnerReferences {
public:
~PacketSender() override = 0;
virtual void send(Packet &) = 0;
virtual void sendToServer(Packet &) = 0;
virtual void sendToClient(UserEntityIdentifierComponent const *, Packet const &) = 0;
Expand Down
4 changes: 4 additions & 0 deletions include/endstone/detail/scoreboard/scoreboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <memory>

#include "bedrock/world/scores/scoreboard.h"
#include "endstone/detail/scoreboard/scoreboard_packet_sender.h"
#include "endstone/scoreboard/scoreboard.h"

namespace endstone::detail {
Expand All @@ -25,6 +26,8 @@ class EndstoneScoreboard : public Scoreboard {
public:
explicit EndstoneScoreboard(::Scoreboard &board);
explicit EndstoneScoreboard(std::unique_ptr<::Scoreboard> board);
void init();

std::unique_ptr<Objective> addObjective(std::string name, Criteria::Type criteria) override;
std::unique_ptr<Objective> addObjective(std::string name, Criteria::Type criteria,
std::string display_name) override;
Expand Down Expand Up @@ -52,6 +55,7 @@ class EndstoneScoreboard : public Scoreboard {

::Scoreboard &board_;
std::unique_ptr<::Scoreboard> holder_;
std::unique_ptr<ScoreboardPacketSender> packet_sender_;
};

} // namespace endstone::detail
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
#pragma once

#include "bedrock/network/packet_sender.h"
#include "endstone/detail/server.h"

namespace endstone::detail {

class EndstoneScoreboard;
class EndstoneServer;

class ScoreboardPacketSender : public PacketSender {
public:
ScoreboardPacketSender(EndstoneServer &server, EndstoneScoreboard &scoreboard, PacketSender &sender);
Expand Down
15 changes: 14 additions & 1 deletion src/endstone_core/scoreboard/scoreboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,31 @@
#include "bedrock/world/scores/objective_criteria.h"
#include "bedrock/world/scores/scoreboard.h"
#include "endstone/detail/actor/actor.h"
#include "endstone/detail/level/level.h"
#include "endstone/detail/player.h"
#include "endstone/detail/scoreboard/objective.h"
#include "endstone/detail/scoreboard/score.h"
#include "endstone/detail/server.h"

namespace endstone::detail {

EndstoneScoreboard::EndstoneScoreboard(::Scoreboard &board) : board_(board) {}
EndstoneScoreboard::EndstoneScoreboard(::Scoreboard &board) : board_(board)
{
init();
}

EndstoneScoreboard::EndstoneScoreboard(std::unique_ptr<::Scoreboard> board) : board_(*board)
{
holder_ = std::move(board);
init();
}

void EndstoneScoreboard::init()
{
auto &server = entt::locator<EndstoneServer>::value();
auto *level = static_cast<EndstoneLevel *>(server.getLevel());
packet_sender_ = std::make_unique<ScoreboardPacketSender>(server, *this, *level->getHandle().getPacketSender());
board_.setPacketSender(packet_sender_.get());
}

std::unique_ptr<Objective> EndstoneScoreboard::addObjective(std::string name, Criteria::Type criteria)
Expand Down
2 changes: 2 additions & 0 deletions src/endstone_core/scoreboard/scoreboard_packet_sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "endstone/detail/scoreboard/scoreboard_packet_sender.h"

#include "endstone/detail/server.h"

endstone::detail::ScoreboardPacketSender::ScoreboardPacketSender(EndstoneServer &server, EndstoneScoreboard &scoreboard,
PacketSender &sender)
: server_(server), scoreboard_(scoreboard), sender_(sender)
Expand Down
3 changes: 2 additions & 1 deletion src/endstone_runtime/bedrock/network/network_identifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ bool NetworkIdentifier::equalsTypeData(const NetworkIdentifier &other) const
sock.addr4.sin_addr.s_addr == other.sock.addr4.sin_addr.s_addr;
case Type::Address6:
return sock.addr6.sin6_port == other.sock.addr6.sin6_port &&
std::memcmp(sock.addr6.sin6_addr.s6_addr, other.sock.addr6.sin6_addr.s6_addr, sizeof(sock.addr6.sin6_addr.s6_addr);
std::memcmp(sock.addr6.sin6_addr.s6_addr, other.sock.addr6.sin6_addr.s6_addr,
sizeof(sock.addr6.sin6_addr.s6_addr)) == 0;
case Type::NetherNet:
return nether_net_id == other.nether_net_id;
case Type::Generic:
Expand Down

0 comments on commit 39f41c8

Please sign in to comment.