#include #include #include #include #include #include #include #include #include #include #include LIAPI Objective* Scoreboard::newObjective(const std::string& objname, const std::string& displayName) { std::string criteria = "dummy"; return Global->addObjective(objname, displayName, *Global->getCriteria(criteria)); } /* LIAPI bool Scoreboard::setDisplayObjective(const std::string& objname, const std::string& slot, int sort) { if (checkSlotName(slot)) { auto obj = Global->getObjective(objname); if (!obj) return false; ((ServerScoreboard*)Global)->setDisplayObjective(slot,*obj,(ObjectiveSortOrder)sort); return true; } return false; } LIAPI Objective* Scoreboard::clearDisplayObjective(const std::string& slot) { if (checkSlotName(slot)) return Global->clearDisplayObjective(slot); return nullptr; } LIAPI Objective* Scoreboard::getDisplayObjective(const std::string& slot) { if (checkSlotName(slot)) { auto disp = Global->getDisplayObjective(slot); if (disp) return const_cast(&disp->getObjective()); } return nullptr; }*/ LIAPI struct ScoreboardId& Scoreboard::getOrCreateScoreboardId(std::string const& id) { auto& identity = const_cast(Global->getScoreboardId(id)); if (!scoreboardIdIsValid(&identity)) { identity = const_cast(Global->createScoreboardId(id)); } return identity; }; LIAPI std::optional Scoreboard::addScore(const std::string& objname, const std::string& id, int score) { auto obj = Global->getObjective(objname); if (!obj) return std::nullopt; auto& identity = getOrCreateScoreboardId(id); int a1 = 0; auto ref = Global->getScoreboardIdentityRef(identity); bool res = ref->modifyScoreInObjective(a1, *obj, score, PlayerScoreSetFunction::Add); if (res) { Global->onScoreChanged(identity, *obj); return a1; } return std::nullopt; } LIAPI std::optional Scoreboard::setScore(const std::string& objname, const std::string& id, int score) { auto obj = Global->getObjective(objname); if (!obj) return std::nullopt; auto& identity = getOrCreateScoreboardId(id); int a1 = 0; auto ref = Global->getScoreboardIdentityRef(identity); bool res = ref->modifyScoreInObjective(a1, *obj, score, PlayerScoreSetFunction::Set); if (res) { Global->onScoreChanged(identity, *obj); return a1; } return std::nullopt; } LIAPI std::optional Scoreboard::reduceScore(const std::string& objname, const std::string& id, int score) { auto obj = Global->getObjective(objname); if (!obj) return std::nullopt; auto& identity = getOrCreateScoreboardId(id); int a1 = 0; auto ref = Global->getScoreboardIdentityRef(identity); bool res = ref->modifyScoreInObjective(a1, *obj, score, PlayerScoreSetFunction::Remove); if (res) { Global->onScoreChanged(identity, *obj); return a1; } return std::nullopt; } LIAPI bool Scoreboard::removeFromObjective(const std::string& objname, const std::string& id) { auto& identity = const_cast(Global->getScoreboardId(id)); if (!scoreboardIdIsValid(&identity)) return true; auto obj = Global->getObjective(objname); if (!obj) return true; return Global->getScoreboardIdentityRef(identity)->removeFromObjective(*Global, *obj); } LIAPI bool Scoreboard::removeFromObjective(const std::string& objname, Player* player) { auto& identity = const_cast(Global->getScoreboardId(*player)); if (!scoreboardIdIsValid(&identity)) return true; Objective* obj = Global->getObjective(objname); if (!obj) return true; vector info; ScorePacketInfo i((ScoreboardId*)&identity, objname, Global->getScoreboardIdentityRef(identity)->getIdentityType(), obj->getPlayerScore(identity).getCount(), obj->getName()); info.emplace_back(i); for (auto sp : Level::getAllPlayers()) { sp->sendSetScorePacket(1, info); } auto out = Global->getScoreboardIdentityRef(identity)->removeFromObjective(*Global, *obj); return out; } LIAPI int Scoreboard::getScore(const std::string& objname, const std::string& id) { auto& identity = const_cast( Global->getOrCreateScoreboardId(id) // If not exists, create a new one ); if (!scoreboardIdIsValid(&identity)) { throw std::runtime_error("Bad ScoreboardId"); } auto obj = Global->getObjective(objname); if (!obj) { throw std::invalid_argument("Objective " + objname + " not found"); } auto scores = Global->getIdScores(identity); #ifdef DEBUG struct voids { void** filler[100]; }; auto& vs = *(voids*&)scores; // -> sizeof(ScoreInfo) == 16 #endif // DEBUG for (auto& it : scores) { if (it.getObjective() == obj) { return it.getCount(); } } throw std::runtime_error("Cannot get score"); } LIAPI bool Scoreboard::getScore(const std::string& objname, const std::string& id, int& score) { try { score = getScore(objname, id); return true; } catch (...) {} return false; } // For compatibility LIAPI int Scoreboard::getScore(Player* player, const std::string& objname) { return getScore(objname, player); } LIAPI int Scoreboard::getScore(const std::string& objname, Player* player) { Objective* obj = Global->getObjective(objname); if (!obj) { throw std::invalid_argument("Objective " + objname + " not found"); } auto& identity = const_cast(Global->getScoreboardId(*player)); if (!scoreboardIdIsValid(&identity)) { throw std::runtime_error("Bad ScoreboardId"); } auto score = obj->getPlayerScore(identity); return score.getCount(); } LIAPI bool Scoreboard::getScore(const std::string& objname, Player* player, int& score) { try { score = getScore(objname, player); return true; } catch (...) {} return false; } // For compatibility LIAPI bool Scoreboard::setScore(Player* player, const std::string& objname, int value) { return setScore(objname, player, value); } LIAPI bool Scoreboard::setScore(const std::string& objname, Player* player, int value) { Objective* obj = Global->getObjective(objname); if (!obj) { return false; } auto& identity = const_cast(Global->getScoreboardId(*player)); if (!scoreboardIdIsValid(&identity)) { Global->createScoreboardId(*player); } bool a2 = true; Global->modifyPlayerScore(a2, identity, *obj, value, (PlayerScoreSetFunction)0); // Set return true; } // For compatibility LIAPI bool Scoreboard::addScore(Player* player, const std::string& objname, int value) { return addScore(objname, player, value); } LIAPI bool Scoreboard::addScore(const std::string& objname, Player* player, int value) { Objective* obj = Global->getObjective(objname); if (!obj) { return false; } auto& identity = const_cast(Global->getScoreboardId(*player)); if (!scoreboardIdIsValid(&identity)) { Global->createScoreboardId(*player); } bool a2 = true; Global->modifyPlayerScore(a2, identity, *obj, value, (PlayerScoreSetFunction)1); // Add return true; } // For compatibility LIAPI bool Scoreboard::reduceScore(Player* player, const std::string& objname, int value) { return reduceScore(objname, player, value); } LIAPI bool Scoreboard::reduceScore(const std::string& objname, Player* player, int value) { Objective* obj = Global->getObjective(objname); if (!obj) { return false; } bool a1 = true; bool& pa = a1; auto& identity = const_cast(Global->getScoreboardId(*player)); if (!scoreboardIdIsValid(&identity)) { Global->createScoreboardId(*player); } bool a2 = true; Global->modifyPlayerScore(a2, identity, *obj, value, (PlayerScoreSetFunction)2); // Reduce return true; } // For compatibility LIAPI bool Scoreboard::deleteScore(Player* player, const std::string& objname) { return deleteScore(objname, player); } LIAPI bool Scoreboard::deleteScore(const std::string& objname, Player* player) { return removeFromObjective(objname, player); } LIAPI bool Scoreboard::scoreboardIdIsValid(ScoreboardId* id) { return id->isValid(); }