From bca990465df5aa92194679f0229c6824eba1fe3a Mon Sep 17 00:00:00 2001 From: Vincent Date: Sun, 29 Sep 2024 23:35:13 +0100 Subject: [PATCH] feat(scoreboard): Add Objective::isDisplayed method --- .../endstone/detail/scoreboard/objective.h | 1 + include/endstone/scoreboard/objective.h | 7 ++++++ .../endstone/_internal/endstone_python.pyi | 9 +++++-- src/endstone_core/scoreboard/objective.cpp | 14 +++++++++++ src/endstone_python/scoreboard.cpp | 24 +++++++++++++++---- 5 files changed, 49 insertions(+), 6 deletions(-) diff --git a/include/endstone/detail/scoreboard/objective.h b/include/endstone/detail/scoreboard/objective.h index 7198067ee..30613e829 100644 --- a/include/endstone/detail/scoreboard/objective.h +++ b/include/endstone/detail/scoreboard/objective.h @@ -38,6 +38,7 @@ class EndstoneObjective : public Objective { [[nodiscard]] Result isModifiable() const override; [[nodiscard]] Scoreboard &getScoreboard() const override; [[nodiscard]] Result unregister() const override; + [[nodiscard]] Result isDisplayed() const override; [[nodiscard]] Result getDisplaySlot() const override; [[nodiscard]] Result getSortOrder() const override; Result setDisplay(std::optional slot) override; diff --git a/include/endstone/scoreboard/objective.h b/include/endstone/scoreboard/objective.h index c36098f7c..6f88147cc 100644 --- a/include/endstone/scoreboard/objective.h +++ b/include/endstone/scoreboard/objective.h @@ -78,6 +78,13 @@ class Objective { */ [[nodiscard]] virtual Result unregister() const = 0; + /** + * @brief Gets if the objective is currently displayed in a slot. + * + * @return true if the objective is displayed + */ + [[nodiscard]] virtual Result isDisplayed() const = 0; + /** * @brief Gets the display slot this objective is displayed at. * diff --git a/python/src/endstone/_internal/endstone_python.pyi b/python/src/endstone/_internal/endstone_python.pyi index 2740231a1..e9bacb046 100644 --- a/python/src/endstone/_internal/endstone_python.pyi +++ b/python/src/endstone/_internal/endstone_python.pyi @@ -1382,11 +1382,16 @@ class Objective: def display_name(self, arg1: str) -> None: ... @property - def display_slot(self) -> DisplaySlot: + def display_slot(self) -> DisplaySlot | None: """ Gets the display slot this objective is displayed at """ @property + def is_displayed(self) -> bool: + """ + Gets if the objective is currently displayed in a slot. + """ + @property def is_modifiable(self) -> bool: """ Gets if the objective's scores can be modified directly by a plugin @@ -1410,7 +1415,7 @@ class Objective: Gets the scoreboard to which this objective is attached """ @property - def sort_order(self) -> ObjectiveSortOrder: + def sort_order(self) -> ObjectiveSortOrder | None: """ Gets and sets the sort order for this objective """ diff --git a/src/endstone_core/scoreboard/objective.cpp b/src/endstone_core/scoreboard/objective.cpp index 14daef12d..1dc867f51 100644 --- a/src/endstone_core/scoreboard/objective.cpp +++ b/src/endstone_core/scoreboard/objective.cpp @@ -84,6 +84,20 @@ Result EndstoneObjective::unregister() const .or_else([](const auto &err) -> Result { return nonstd::make_unexpected(err); }); } +Result EndstoneObjective::isDisplayed() const +{ + bool displayed = false; + return forEachDisplayObjective([&](auto /*slot*/, const auto &display) -> bool { + if (&display.getObjective() == &objective_) { + displayed = true; + return false; + } + return true; + }) + .and_then([&displayed]() -> Result { return displayed; }) + .or_else([](const auto &err) -> Result { return nonstd::make_unexpected(err); }); +} + Result EndstoneObjective::getDisplaySlot() const { std::optional result; diff --git a/src/endstone_python/scoreboard.cpp b/src/endstone_python/scoreboard.cpp index b6ca502d3..ef3c70a23 100644 --- a/src/endstone_python/scoreboard.cpp +++ b/src/endstone_python/scoreboard.cpp @@ -81,10 +81,26 @@ void init_scoreboard(py::module_ &m) "Gets the scoreboard to which this objective is attached", py::return_value_policy::reference) .def("unregister", &Objective::unregister, "Unregisters this objective from the associated Scoreboard.") - .def_property_readonly("display_slot", &Objective::getDisplaySlot, - "Gets the display slot this objective is displayed at") - .def_property_readonly("sort_order", &Objective::getSortOrder, - "Gets and sets the sort order for this objective") + .def_property_readonly("is_displayed", &Objective::isDisplayed, + "Gets if the objective is currently displayed in a slot.") + .def_property_readonly( + "display_slot", + [](const Objective &self) -> std::variant, py::none> { + if (self.isDisplayed().value_or(false)) { + return self.getDisplaySlot(); + } + return py::none(); + }, + "Gets the display slot this objective is displayed at") + .def_property_readonly( + "sort_order", + [](const Objective &self) -> std::variant, py::none> { + if (self.isDisplayed().value_or(false)) { + return self.getSortOrder(); + } + return py::none(); + }, + "Gets and sets the sort order for this objective") .def( "set_display", [](Objective &self, std::optional slot, std::optional order) {