Skip to content

Commit

Permalink
feat(scoreboard): add Scoreboard::addObjective
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jul 22, 2024
1 parent 625d375 commit a1b20e0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
8 changes: 5 additions & 3 deletions include/bedrock/world/scores/objective.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,25 @@

class Objective : public Bedrock::EnableNonOwnerReferences {
public:
Objective(const std::string &name, const ObjectiveCriteria &criteria);

[[nodiscard]] const std::unordered_map<ScoreboardId, int> &getScores() const;
[[nodiscard]] const std::string &getName() const;
[[nodiscard]] const std::string &getDisplayName() const;
[[nodiscard]] const ObjectiveCriteria *getCriteria() const;
[[nodiscard]] const ObjectiveCriteria &getCriteria() const;
[[nodiscard]] ObjectiveRenderType getRenderType() const;
void setDisplayName(const std::string &display_name);

[[nodiscard]] bool hasScore(const ScoreboardId &id) const;
[[nodiscard]] int getPlayerScore(const ScoreboardId &id) const;

bool setPlayerScore(const ScoreboardId &id, int value); // Endstone
bool setPlayerScore(const ScoreboardId &id, int value); // Endstone

private:
bool _modifyPlayerScore(int &result, const ScoreboardId &id, int value, PlayerScoreSetFunction action); // NOLINT

std::unordered_map<ScoreboardId, int> scores_; // +24
std::string name_; // +88
std::string display_name_; // +120
const ObjectiveCriteria *criteria_; // +152
const ObjectiveCriteria &criteria_; // +152
};
20 changes: 11 additions & 9 deletions include/bedrock/world/scores/scoreboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Scoreboard {
virtual void writeToLevelStorage() = 0;
[[nodiscard]] virtual bool isClientSide() const = 0;

Objective *Scoreboard::addObjective(const std::string &, const std::string &, const ObjectiveCriteria &criteria);
bool removeObjective(Objective *);
[[nodiscard]] Objective *getObjective(const std::string &name) const;
[[nodiscard]] const DisplayObjective *getDisplayObjective(const std::string &name) const;
Expand All @@ -60,16 +61,17 @@ class Scoreboard {
[[nodiscard]] const ScoreboardId &getScoreboardId(const std::string &fake) const;
[[nodiscard]] bool hasIdentityFor(const ScoreboardId &id) const;
[[nodiscard]] ScoreboardIdentityRef *getScoreboardIdentityRef(const ScoreboardId &id);
[[nodiscard]] ObjectiveCriteria *getCriteria(const std::string &name) const;

private:
CommandSoftEnumRegistry registry_; // +8
std::unordered_map<std::string, DisplayObjective> display_objectives_; // +16
IdentityDictionary identity_dictionary_; // +80
std::unordered_map<ScoreboardId, ScoreboardIdentityRef> identity_refs_; // +336 (+216)
bool should_update_ui_; // +400 (+256)
std::unordered_map<std::string, std::unique_ptr<Objective>> objectives_; // +408 (+264)
std::unordered_map<std::uint64_t, Bedrock::NonOwnerPointer<Objective>> objectives_lookup_; // +472 (+304)
std::unordered_map<std::string, std::unique_ptr<ObjectiveCriteria>> criteria_; // +536 (+344)
ScoreboardEventCoordinator scoreboard_event_coordinator_; // +600 (+384)
CommandSoftEnumRegistry registry_; // +8
std::unordered_map<std::string, DisplayObjective> display_objectives_; // +16
IdentityDictionary identity_dictionary_; // +80
std::unordered_map<ScoreboardId, ScoreboardIdentityRef> identity_refs_; // +336 (+216)
bool should_update_ui_; // +400 (+256)
std::unordered_map<std::string, std::unique_ptr<Objective>> objectives_; // +408 (+264)
std::unordered_map<std::size_t, Bedrock::NonOwnerPointer<Objective>> objectives_lookup_; // +472 (+304)
std::unordered_map<std::string, std::unique_ptr<ObjectiveCriteria>> criteria_; // +536 (+344)
ScoreboardEventCoordinator scoreboard_event_coordinator_; // +600 (+384)
// PlayerScoreboardEventListener player_listener_; // +712 (+504)
};
2 changes: 1 addition & 1 deletion src/endstone_core/scoreboard/objective.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
namespace endstone::detail {

EndstoneObjective::EndstoneObjective(EndstoneScoreboard &scoreboard, ::Objective &objective)
: name_(objective.getName()), scoreboard_(scoreboard), objective_(objective), criteria_(*objective.getCriteria())
: name_(objective.getName()), scoreboard_(scoreboard), objective_(objective), criteria_(objective.getCriteria())
{
}

Expand Down
11 changes: 8 additions & 3 deletions src/endstone_runtime/bedrock/world/scores/objective.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

#include <stdexcept>

Objective::Objective(const std::string &name, const ObjectiveCriteria &criteria)
: name_(name), criteria_(criteria), display_name_(name)
{
}

const std::unordered_map<ScoreboardId, int> &Objective::getScores() const
{
return scores_;
Expand All @@ -31,14 +36,14 @@ const std::string &Objective::getDisplayName() const
return display_name_;
}

const ObjectiveCriteria *Objective::getCriteria() const
const ObjectiveCriteria &Objective::getCriteria() const
{
return criteria_;
}

ObjectiveRenderType Objective::getRenderType() const
{
return criteria_->getRenderType();
return criteria_.getRenderType();
}

void Objective::setDisplayName(const std::string &display_name)
Expand Down Expand Up @@ -68,7 +73,7 @@ bool Objective::setPlayerScore(const ScoreboardId &id, int value)

bool Objective::_modifyPlayerScore(int &result, const ScoreboardId &id, int value, PlayerScoreSetFunction action)
{
if (criteria_->isReadOnly()) {
if (criteria_.isReadOnly()) {
result = 0;
return false;
}
Expand Down
25 changes: 25 additions & 0 deletions src/endstone_runtime/bedrock/world/scores/scoreboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@

#include "bedrock/world/actor/player/player.h"

Objective *Scoreboard::addObjective(const std::string &name, const std::string &display_name,
const ObjectiveCriteria &criteria)
{
if (getObjective(name)) {
return nullptr; // already exist
}

auto objective = std::make_unique<Objective>(name, criteria);
objectives_[name] = std::move(objective);
auto &ref = *objectives_[name];
objectives_lookup_.emplace(HashedString::computeHash(name), ref);
ref.setDisplayName(display_name);
onObjectiveAdded(ref);
return &ref;
}

bool Scoreboard::removeObjective(Objective *objective)
{
if (!objective) {
Expand Down Expand Up @@ -102,3 +118,12 @@ ScoreboardIdentityRef *Scoreboard::getScoreboardIdentityRef(const ScoreboardId &
}
return nullptr;
}

ObjectiveCriteria *Scoreboard::getCriteria(const std::string &name) const
{
auto it = criteria_.find(name);
if (it != criteria_.end()) {
return it->second.get();
}
return nullptr;
}

0 comments on commit a1b20e0

Please sign in to comment.