From e8a56a14f1850da54581f385cea3e1dc992f08f3 Mon Sep 17 00:00:00 2001 From: ShrBox Date: Sun, 23 Jun 2024 18:58:56 +0800 Subject: [PATCH] fix: fix reduceExperience #145 --- src/legacy/api/PlayerAPI.cpp | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/legacy/api/PlayerAPI.cpp b/src/legacy/api/PlayerAPI.cpp index c93d28a..73a6fc9 100644 --- a/src/legacy/api/PlayerAPI.cpp +++ b/src/legacy/api/PlayerAPI.cpp @@ -1828,8 +1828,31 @@ Local PlayerClass::reduceExperience(const Arguments& args) { return Local(); } - player->addExperience(-args[0].toInt()); - return Boolean::newBoolean(true); + float exp = args[0].asNumber().toFloat(); + auto attr = player->getMutableAttribute(Player::EXPERIENCE); + if (!attr) { + return Boolean::newBoolean(false); + } + int neededExp = player->getXpNeededForNextLevel(); + int currentExp = static_cast(attr->getCurrentValue() * neededExp); + if (exp <= currentExp) { + attr->setCurrentValue(static_cast(currentExp - exp) / neededExp); + return Boolean::newBoolean(true); + } + attr->setCurrentValue(0); + size_t needExp = exp - currentExp; + int level = player->getPlayerLevel(); + while (level > 0) { + player->addLevels(-1); + int levelXp = player->getXpNeededForNextLevel(); + if (needExp < levelXp) { + attr->setCurrentValue(static_cast(levelXp - needExp) / player->getXpNeededForNextLevel()); + return Boolean::newBoolean(true); + } + needExp -= levelXp; + level = player->getPlayerLevel(); + } + return Boolean::newBoolean(false); } CATCH("Fail in reduceExperience!"); }