From 39f41c8ed773c5baf26368b72073fe3efe4f7102 Mon Sep 17 00:00:00 2001 From: Vincent Date: Sun, 4 Aug 2024 12:15:13 +0100 Subject: [PATCH] feat: replace Scoreboard's default packet sender with ScoreboardPacketSender --- include/bedrock/network/packet_sender.h | 1 - include/endstone/detail/scoreboard/scoreboard.h | 4 ++++ .../detail/scoreboard/scoreboard_packet_sender.h | 5 ++++- src/endstone_core/scoreboard/scoreboard.cpp | 15 ++++++++++++++- .../scoreboard/scoreboard_packet_sender.cpp | 2 ++ .../bedrock/network/network_identifier.cpp | 3 ++- 6 files changed, 26 insertions(+), 4 deletions(-) diff --git a/include/bedrock/network/packet_sender.h b/include/bedrock/network/packet_sender.h index f6a115a7e..2af848c93 100644 --- a/include/bedrock/network/packet_sender.h +++ b/include/bedrock/network/packet_sender.h @@ -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; diff --git a/include/endstone/detail/scoreboard/scoreboard.h b/include/endstone/detail/scoreboard/scoreboard.h index 785ef43d6..c8321151c 100644 --- a/include/endstone/detail/scoreboard/scoreboard.h +++ b/include/endstone/detail/scoreboard/scoreboard.h @@ -17,6 +17,7 @@ #include #include "bedrock/world/scores/scoreboard.h" +#include "endstone/detail/scoreboard/scoreboard_packet_sender.h" #include "endstone/scoreboard/scoreboard.h" namespace endstone::detail { @@ -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 addObjective(std::string name, Criteria::Type criteria) override; std::unique_ptr addObjective(std::string name, Criteria::Type criteria, std::string display_name) override; @@ -52,6 +55,7 @@ class EndstoneScoreboard : public Scoreboard { ::Scoreboard &board_; std::unique_ptr<::Scoreboard> holder_; + std::unique_ptr packet_sender_; }; } // namespace endstone::detail diff --git a/include/endstone/detail/scoreboard/scoreboard_packet_sender.h b/include/endstone/detail/scoreboard/scoreboard_packet_sender.h index 71045201a..781a93f11 100644 --- a/include/endstone/detail/scoreboard/scoreboard_packet_sender.h +++ b/include/endstone/detail/scoreboard/scoreboard_packet_sender.h @@ -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); diff --git a/src/endstone_core/scoreboard/scoreboard.cpp b/src/endstone_core/scoreboard/scoreboard.cpp index 396f82d72..99c5f7f46 100644 --- a/src/endstone_core/scoreboard/scoreboard.cpp +++ b/src/endstone_core/scoreboard/scoreboard.cpp @@ -21,6 +21,7 @@ #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" @@ -28,11 +29,23 @@ 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::value(); + auto *level = static_cast(server.getLevel()); + packet_sender_ = std::make_unique(server, *this, *level->getHandle().getPacketSender()); + board_.setPacketSender(packet_sender_.get()); } std::unique_ptr EndstoneScoreboard::addObjective(std::string name, Criteria::Type criteria) diff --git a/src/endstone_core/scoreboard/scoreboard_packet_sender.cpp b/src/endstone_core/scoreboard/scoreboard_packet_sender.cpp index cf9e78f20..c8a8f942f 100644 --- a/src/endstone_core/scoreboard/scoreboard_packet_sender.cpp +++ b/src/endstone_core/scoreboard/scoreboard_packet_sender.cpp @@ -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) diff --git a/src/endstone_runtime/bedrock/network/network_identifier.cpp b/src/endstone_runtime/bedrock/network/network_identifier.cpp index add6246df..952aa7b4f 100644 --- a/src/endstone_runtime/bedrock/network/network_identifier.cpp +++ b/src/endstone_runtime/bedrock/network/network_identifier.cpp @@ -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: