Skip to content

Commit

Permalink
feat(scoreboard): Add Objective::isDisplayed method
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Sep 29, 2024
1 parent 9dc8d9e commit bca9904
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/endstone/detail/scoreboard/objective.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class EndstoneObjective : public Objective {
[[nodiscard]] Result<bool> isModifiable() const override;
[[nodiscard]] Scoreboard &getScoreboard() const override;
[[nodiscard]] Result<void> unregister() const override;
[[nodiscard]] Result<bool> isDisplayed() const override;
[[nodiscard]] Result<DisplaySlot> getDisplaySlot() const override;
[[nodiscard]] Result<ObjectiveSortOrder> getSortOrder() const override;
Result<void> setDisplay(std::optional<DisplaySlot> slot) override;
Expand Down
7 changes: 7 additions & 0 deletions include/endstone/scoreboard/objective.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ class Objective {
*/
[[nodiscard]] virtual Result<void> unregister() const = 0;

/**
* @brief Gets if the objective is currently displayed in a slot.
*
* @return true if the objective is displayed
*/
[[nodiscard]] virtual Result<bool> isDisplayed() const = 0;

/**
* @brief Gets the display slot this objective is displayed at.
*
Expand Down
9 changes: 7 additions & 2 deletions python/src/endstone/_internal/endstone_python.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
"""
Expand Down
14 changes: 14 additions & 0 deletions src/endstone_core/scoreboard/objective.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ Result<void> EndstoneObjective::unregister() const
.or_else([](const auto &err) -> Result<void> { return nonstd::make_unexpected(err); });
}

Result<bool> 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<bool> { return displayed; })
.or_else([](const auto &err) -> Result<bool> { return nonstd::make_unexpected(err); });
}

Result<DisplaySlot> EndstoneObjective::getDisplaySlot() const
{
std::optional<DisplaySlot> result;
Expand Down
24 changes: 20 additions & 4 deletions src/endstone_python/scoreboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result<DisplaySlot>, 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<Result<ObjectiveSortOrder>, 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<DisplaySlot> slot, std::optional<ObjectiveSortOrder> order) {
Expand Down

0 comments on commit bca9904

Please sign in to comment.