Skip to content

Commit

Permalink
feat(scoreboard): implement EndstoneScoreboard::addObjective
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jul 22, 2024
1 parent a1b20e0 commit 48b79e9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
1 change: 0 additions & 1 deletion include/endstone/detail/scoreboard/objective.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class EndstoneObjective : public Objective {
friend class EndstoneScore;

void forEachDisplayObjective(const std::function<bool(DisplaySlot, const DisplayObjective &)> &callback) const;
static std::string getDisplaySlotName(DisplaySlot slot);

std::string name_;
EndstoneScoreboard &scoreboard_;
Expand Down
30 changes: 16 additions & 14 deletions src/endstone_core/scoreboard/objective.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@

namespace endstone::detail {

namespace {
std::string getDisplaySlotName(DisplaySlot slot)
{
switch (slot) {
case DisplaySlot::BelowName:
return "belowname";
case DisplaySlot::PlayerList:
return "list";
case DisplaySlot::SideBar:
return "sidebar";
default:
throw std::runtime_error("Unknown DisplaySlot!");
}
}
} // namespace

EndstoneObjective::EndstoneObjective(EndstoneScoreboard &scoreboard, ::Objective &objective)
: name_(objective.getName()), scoreboard_(scoreboard), objective_(objective), criteria_(objective.getCriteria())
{
Expand Down Expand Up @@ -188,20 +204,6 @@ void EndstoneObjective::forEachDisplayObjective(
}
}

std::string EndstoneObjective::getDisplaySlotName(DisplaySlot slot)
{
switch (slot) {
case DisplaySlot::BelowName:
return "belowname";
case DisplaySlot::PlayerList:
return "list";
case DisplaySlot::SideBar:
return "sidebar";
default:
throw std::runtime_error("Unknown DisplaySlot!");
}
}

std::unique_ptr<EndstoneObjective> EndstoneObjective::copy() const
{
return std::make_unique<EndstoneObjective>(scoreboard_, objective_);
Expand Down
28 changes: 27 additions & 1 deletion src/endstone_core/scoreboard/scoreboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,22 @@
#include "bedrock/world/scores/objective_criteria.h"
#include "bedrock/world/scores/scoreboard.h"
#include "endstone/detail/scoreboard/objective.h"
#include "endstone/detail/server.h"

namespace endstone::detail {

namespace {
std::string getCriteriaName(Criteria::Type type)
{
switch (type) {
case Criteria::Type::Dummy:
return "dummy";
default:
throw std::runtime_error("Unknown Criteria::Type!");
}
}
} // namespace

EndstoneScoreboard::EndstoneScoreboard(::Scoreboard &board) : board_(board) {}

std::unique_ptr<Objective> EndstoneScoreboard::addObjective(std::string name, Criteria::Type criteria)
Expand All @@ -38,7 +51,20 @@ std::unique_ptr<Objective> EndstoneScoreboard::addObjective(std::string name, Cr
std::unique_ptr<Objective> EndstoneScoreboard::addObjective(std::string name, Criteria::Type criteria,
std::string display_name, RenderType render_type)
{
throw std::runtime_error("Not implemented.");
auto &server = entt::locator<EndstoneServer>::value();
auto *cr = board_.getCriteria(getCriteriaName(criteria));
if (!cr) {
server.getLogger().error("Unknown criteria: {}.", getCriteriaName(criteria));
return nullptr;
}

auto *objective = board_.addObjective(name, display_name, *cr);
if (!objective) {
server.getLogger().error("Objective {} already exists.", name);
return nullptr;
}

return std::make_unique<EndstoneObjective>(*this, *objective);
}

std::unique_ptr<Objective> EndstoneScoreboard::getObjective(std::string name) const
Expand Down

0 comments on commit 48b79e9

Please sign in to comment.