Skip to content

Commit

Permalink
fix: fix Player::clearItem #216
Browse files Browse the repository at this point in the history
  • Loading branch information
ShrBox committed Jan 30, 2025
1 parent 06a1c1c commit 63d816b
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 79 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.9.3] - 2025-01-30

### Fixed

- Fixed Player::clearItem [#216]

## [0.9.2] - 2025-01-29

### Fixed
Expand Down Expand Up @@ -745,7 +751,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[#214]: https://github.com/LiteLDev/LegacyScriptEngine/issues/214

[Unreleased]: https://github.com/LiteLDev/LegacyScriptEngine/compare/v0.9.2...HEAD
[#216]: https://github.com/LiteLDev/LegacyScriptEngine/issues/216

[Unreleased]: https://github.com/LiteLDev/LegacyScriptEngine/compare/v0.9.3...HEAD

[0.9.3]: https://github.com/LiteLDev/LegacyScriptEngine/compare/v0.9.2...v0.9.3

[0.9.2]: https://github.com/LiteLDev/LegacyScriptEngine/compare/v0.9.1...v0.9.2

Expand Down
22 changes: 12 additions & 10 deletions src/legacy/api/DataAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,8 @@ Local<Value> MoneyClass::set(const Arguments& args) {
CHECK_ARG_TYPE(args[1], ValueKind::kNumber);

try {
return Boolean::newBoolean(EconomySystem::setMoney(args[0].asString().toString(), args[1].asNumber().toInt64())
return Boolean::newBoolean(
EconomySystem::setMoney(args[0].asString().toString(), args[1].asNumber().toInt64())
);
} catch (const std::invalid_argument& e) {
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().error("Bad argument in MoneySet!");
Expand Down Expand Up @@ -570,7 +571,8 @@ Local<Value> MoneyClass::add(const Arguments& args) {
CHECK_ARG_TYPE(args[1], ValueKind::kNumber);

try {
return Boolean::newBoolean(EconomySystem::addMoney(args[0].asString().toString(), args[1].asNumber().toInt64())
return Boolean::newBoolean(
EconomySystem::addMoney(args[0].asString().toString(), args[1].asNumber().toInt64())
);
} catch (const std::invalid_argument& e) {
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().error("Bad argument in MoneyAdd!");
Expand Down Expand Up @@ -614,12 +616,14 @@ Local<Value> MoneyClass::trans(const Arguments& args) {
try {
string note = "";
if (args.size() >= 4 && args[3].getKind() == ValueKind::kString) note = args[3].asString().toString();
return Boolean::newBoolean(EconomySystem::transMoney(
args[0].asString().toString(),
args[1].asString().toString(),
args[2].asNumber().toInt64(),
note
));
return Boolean::newBoolean(
EconomySystem::transMoney(
args[0].asString().toString(),
args[1].asString().toString(),
args[2].asNumber().toInt64(),
note
)
);
} catch (const std::invalid_argument& e) {
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().error("Bad argument in MoneyTrans!");
ll::error_utils::printException(e, lse::LegacyScriptEngine::getInstance().getSelf().getLogger());
Expand Down Expand Up @@ -759,8 +763,6 @@ Local<Value> DataClass::xuid2uuid(const Arguments& args) {
}

Local<Value> DataClass::getAllPlayerInfo(const Arguments& args) {
CHECK_ARGS_COUNT(args, 0);

try {
auto arr = Array::newArray();
auto level = ll::service::getLevel();
Expand Down
26 changes: 12 additions & 14 deletions src/legacy/api/NetworkAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,16 @@ void WSClientClass::initListeners() {
}
});

ws->OnBinaryReceived([nowList{&listeners[int(WSClientEvents::onBinaryReceived)]
}](WebSocketClient&, vector<uint8_t> data) {
if (!nowList->empty())
for (auto& listener : *nowList) {
if (!EngineManager::isValid(listener.engine)) return;
EngineScope enter(listener.engine);
NewTimeout(listener.func.get(), {ByteBuffer::newByteBuffer(data.data(), data.size())}, 1);
}
});
ws->OnBinaryReceived(
[nowList{&listeners[int(WSClientEvents::onBinaryReceived)]}](WebSocketClient&, vector<uint8_t> data) {
if (!nowList->empty())
for (auto& listener : *nowList) {
if (!EngineManager::isValid(listener.engine)) return;
EngineScope enter(listener.engine);
NewTimeout(listener.func.get(), {ByteBuffer::newByteBuffer(data.data(), data.size())}, 1);
}
}
);

ws->OnError([nowList{&listeners[int(WSClientEvents::onError)]}](WebSocketClient&, string msg) {
if (!nowList->empty())
Expand Down Expand Up @@ -366,7 +367,8 @@ Local<Value> WSClientClass::connectAsync(const Arguments& args) {
if (callback.isEmpty()) return;
NewTimeout(callback.get(), {Boolean::newBoolean(result)}, 0);
} catch (...) {
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().error("WSClientClass::connectAsync Failed!"
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().error(
"WSClientClass::connectAsync Failed!"
);
ll::error_utils::printCurrentException(lse::LegacyScriptEngine::getInstance().getSelf().getLogger());
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().error("In Plugin: " + pluginName);
Expand Down Expand Up @@ -778,8 +780,6 @@ Local<Value> HttpServerClass::listen(const Arguments& args) {
}

Local<Value> HttpServerClass::stop(const Arguments& args) {
CHECK_ARGS_COUNT(args, 0);

try {
RecordOperation(getEngineOwnData()->pluginName, "StopHttpServer", "");
svr->stop();
Expand All @@ -789,8 +789,6 @@ Local<Value> HttpServerClass::stop(const Arguments& args) {
}

Local<Value> HttpServerClass::isRunning(const Arguments& args) {
CHECK_ARGS_COUNT(args, 0);

try {
return Boolean::newBoolean(svr->is_running());
}
Expand Down
71 changes: 21 additions & 50 deletions src/legacy/api/PlayerAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2906,69 +2906,40 @@ Local<Value> PlayerClass::clearItem(const Arguments& args) {
if (!player) {
return {};
}
unsigned int clearCount = 1;
int clearCount = 1;
if (args.size() > 1) {
CHECK_ARG_TYPE(args[1], ValueKind::kNumber);
clearCount = args[1].asNumber().toInt32();
}
int result = 0;
auto& inventorySlots = player->getInventory().getSlots();
for (unsigned short slot = 0; slot < inventorySlots.size(); ++slot) {
if (clearCount <= 0) {
break;
}
if (inventorySlots[slot]->getTypeName() == args[0].asString().toString()) {
if (inventorySlots[slot]->mCount < clearCount) {
result += inventorySlots[slot]->mCount;
clearCount -= inventorySlots[slot]->mCount;
} else {
result += clearCount;
clearCount = 0;
}
player->getInventory().removeItem(slot, clearCount);
}
}
auto& handSlots = ActorEquipment::getHandContainer(player->getEntityContext()).getSlots();
for (unsigned short slot = 0; slot < handSlots.size(); ++slot) {
if (clearCount <= 0) {
break;
}
if (handSlots[slot]->getTypeName() == args[0].asString().toString()) {
if (handSlots[slot]->mCount < clearCount) {
result += handSlots[slot]->mCount;
clearCount -= handSlots[slot]->mCount;
} else {
result += clearCount;
clearCount = 0;
}
ActorEquipment::getHandContainer(player->getEntityContext()).removeItem(slot, clearCount);
}
}
auto& armorSlots = ActorEquipment::getArmorContainer(player->getEntityContext()).getSlots();
for (unsigned short slot = 0; slot < armorSlots.size(); ++slot) {
if (clearCount <= 0) {
break;
}
if (armorSlots[slot]->getTypeName() == args[0].asString().toString()) {
if (armorSlots[slot]->mCount < clearCount) {
result += armorSlots[slot]->mCount;
clearCount -= armorSlots[slot]->mCount;
} else {
result += clearCount;
clearCount = 0;
int result = 0;
std::string typeName = args[0].asString().toString();
auto clearFunction = [&result, &typeName, &clearCount](Container& container) {
auto slots = container.getSlots();
for (size_t slot = 0; slot < slots.size() && clearCount > 0; ++slot) {
if (slots[slot]->getTypeName() == typeName) {
auto count = slots[slot]->mCount;
if (count <= clearCount) {
result += count;
clearCount -= count;
container.setItem(slot, ItemStack::EMPTY_ITEM());
} else {
result += clearCount;
container.removeItem(slot, clearCount);
clearCount = 0;
}
}
ActorEquipment::getArmorContainer(player->getEntityContext()).removeItem(slot, clearCount);
}
}
};
clearFunction(player->getInventory());
clearFunction(ActorEquipment::getHandContainer(player->getEntityContext()));
clearFunction(ActorEquipment::getArmorContainer(player->getEntityContext()));
player->refreshInventory();
return Number::newNumber(result);
}
CATCH("Fail in clearItem!");
}

Local<Value> PlayerClass::isSprinting(const Arguments& args) {
CHECK_ARGS_COUNT(args, 0);

try {
Player* player = get();
if (!player) return Local<Value>();
Expand Down
2 changes: 1 addition & 1 deletion tooth.lua.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"format_version": 2,
"tooth": "gitea.litebds.com/LiteLDev/legacy-script-engine-lua",
"version": "0.9.2",
"version": "0.9.3",
"info": {
"name": "LegacyScriptEngine with Lua backend",
"description": "A plugin engine for running LLSE plugins on LeviLamina",
Expand Down
2 changes: 1 addition & 1 deletion tooth.nodejs.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"format_version": 2,
"tooth": "gitea.litebds.com/LiteLDev/legacy-script-engine-nodejs",
"version": "0.9.2",
"version": "0.9.3",
"info": {
"name": "LegacyScriptEngine with NodeJs backend",
"description": "A plugin engine for running LLSE plugins on LeviLamina",
Expand Down
2 changes: 1 addition & 1 deletion tooth.python.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"format_version": 2,
"tooth": "gitea.litebds.com/LiteLDev/legacy-script-engine-python",
"version": "0.9.2",
"version": "0.9.3",
"info": {
"name": "LegacyScriptEngine with Python backend",
"description": "A plugin engine for running LLSE plugins on LeviLamina",
Expand Down
2 changes: 1 addition & 1 deletion tooth.quickjs.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"format_version": 2,
"tooth": "gitea.litebds.com/LiteLDev/legacy-script-engine-quickjs",
"version": "0.9.2",
"version": "0.9.3",
"info": {
"name": "LegacyScriptEngine with QuickJs backend",
"description": "A plugin engine for running LLSE plugins on LeviLamina",
Expand Down

0 comments on commit 63d816b

Please sign in to comment.