diff --git a/include/endstone/detail/scoreboard/score.h b/include/endstone/detail/scoreboard/score.h index 330ad8d38..b54e5cc92 100644 --- a/include/endstone/detail/scoreboard/score.h +++ b/include/endstone/detail/scoreboard/score.h @@ -28,15 +28,15 @@ class EndstoneScore : public Score { public: EndstoneScore(std::unique_ptr objective, ScoreEntry entry); [[nodiscard]] ScoreEntry getEntry() const override; - [[nodiscard]] int getValue() const override; - void setValue(int score) override; - [[nodiscard]] bool isScoreSet() const override; + [[nodiscard]] Result getValue() const override; + Result setValue(int score) override; + [[nodiscard]] Result isScoreSet() const override; [[nodiscard]] Objective &getObjective() const override; [[nodiscard]] Scoreboard &getScoreboard() const override; private: - [[nodiscard]] const ScoreboardId &getScoreboardId() const; - [[nodiscard]] const ScoreboardId &getOrCreateScoreboardId() ; + [[nodiscard]] Result getScoreboardId() const; + [[nodiscard]] Result getOrCreateScoreboardId(); std::unique_ptr objective_; ScoreEntry entry_; diff --git a/include/endstone/scoreboard/score.h b/include/endstone/scoreboard/score.h index d533148bd..5aec6a302 100644 --- a/include/endstone/scoreboard/score.h +++ b/include/endstone/scoreboard/score.h @@ -15,6 +15,7 @@ #pragma once #include "endstone/scoreboard/score_entry.h" +#include "endstone/util/result.h" namespace endstone { @@ -40,14 +41,14 @@ class Score { * * @return the current score */ - [[nodiscard]] virtual int getValue() const = 0; + [[nodiscard]] virtual Result getValue() const = 0; /** * @brief Sets the current score. * * @param score New score */ - virtual void setValue(int score) = 0; + virtual Result setValue(int score) = 0; /** * @brief Shows if this score has been set at any point in time. @@ -55,7 +56,7 @@ class Score { * @return if this score has been set before */ - [[nodiscard]] virtual bool isScoreSet() const = 0; + [[nodiscard]] virtual Result isScoreSet() const = 0; /** * @brief Gets the Objective being tracked by this Score. diff --git a/src/endstone_core/scoreboard/score.cpp b/src/endstone_core/scoreboard/score.cpp index e4a19f0c9..319649caa 100644 --- a/src/endstone_core/scoreboard/score.cpp +++ b/src/endstone_core/scoreboard/score.cpp @@ -20,6 +20,7 @@ #include "endstone/detail/scoreboard/objective.h" #include "endstone/detail/scoreboard/scoreboard.h" #include "endstone/detail/server.h" +#include "endstone/detail/util/error.h" namespace endstone::detail { @@ -33,47 +34,46 @@ ScoreEntry EndstoneScore::getEntry() const return entry_; } -int EndstoneScore::getValue() const +Result EndstoneScore::getValue() const { - if (objective_->checkState()) { - const auto &id = getScoreboardId(); - if (id.isValid() && objective_->objective_.hasScore(id)) { - return objective_->objective_.getPlayerScore(id).value; - } - } - return 0; + return objective_->checkState().and_then([&](const auto *obj) -> Result { + return getScoreboardId().and_then([&](const auto *id) -> Result { + if (id->isValid() && obj->objective_.hasScore(*id)) { + return obj->objective_.getPlayerScore(*id).value; + } + return 0; + }); + }); } -void EndstoneScore::setValue(int score) +Result EndstoneScore::setValue(int score) { - if (objective_->checkState()) { - const auto &id = getOrCreateScoreboardId(); - if (!id.isValid()) { - throw std::runtime_error("Invalid scoreboard id"); - } - - auto &server = entt::locator::value(); - if (!objective_->isModifiable()) { - server.getLogger().error("Cannot modify read-only score"); - return; - } - - bool success = false; - objective_->scoreboard_.board_.modifyPlayerScore(success, id, objective_->objective_, score, - PlayerScoreSetFunction::Set); - if (!success) { - server.getLogger().error("Cannot modify score"); - } - } + return objective_->checkState().and_then([&](const auto *obj) -> Result { + return getOrCreateScoreboardId().and_then([&](const auto *id) -> Result { + return obj->isModifiable().and_then([&](bool modifiable) -> Result { + if (!modifiable) { + return nonstd::make_unexpected(make_error("Cannot modify read-only score.")); + } + + bool success = false; + obj->scoreboard_.board_.modifyPlayerScore(success, *id, obj->objective_, score, + PlayerScoreSetFunction::Set); + + if (!success) { + return nonstd::make_unexpected(make_error("Unable to modify score.")); + } + return {}; + }); + }); + }); } -bool EndstoneScore::isScoreSet() const +Result EndstoneScore::isScoreSet() const { - if (objective_->checkState()) { - const auto &id = getScoreboardId(); - return id.isValid() && objective_->objective_.hasScore(id); - } - return false; + return objective_->checkState().and_then([&](const auto *obj) -> Result { + return getScoreboardId().and_then( + [&](const auto *id) -> Result { return id->isValid() && obj->objective_.hasScore(*id); }); + }); } Objective &EndstoneScore::getObjective() const @@ -86,20 +86,17 @@ Scoreboard &EndstoneScore::getScoreboard() const return objective_->getScoreboard(); } -const ScoreboardId &EndstoneScore::getScoreboardId() const +Result EndstoneScore::getScoreboardId() const { - if (objective_->checkState()) { - return objective_->scoreboard_.getScoreboardId(entry_); - } - return ScoreboardId::INVALID; + return objective_->checkState().and_then( + [this](const auto *obj) -> Result { return &obj->scoreboard_.getScoreboardId(entry_); }); } -const ScoreboardId &EndstoneScore::getOrCreateScoreboardId() +Result EndstoneScore::getOrCreateScoreboardId() { - if (objective_->checkState()) { - return objective_->scoreboard_.getOrCreateScoreboardId(entry_); - } - return ScoreboardId::INVALID; + return objective_->checkState().and_then([this](const auto *obj) -> Result { + return &obj->scoreboard_.getOrCreateScoreboardId(entry_); + }); } } // namespace endstone::detail