Skip to content

Commit 63d816b

Browse files
committed
fix: fix Player::clearItem #216
1 parent 06a1c1c commit 63d816b

File tree

8 files changed

+60
-79
lines changed

8 files changed

+60
-79
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.9.3] - 2025-01-30
11+
12+
### Fixed
13+
14+
- Fixed Player::clearItem [#216]
15+
1016
## [0.9.2] - 2025-01-29
1117

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

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

748-
[Unreleased]: https://github.com/LiteLDev/LegacyScriptEngine/compare/v0.9.2...HEAD
754+
[#216]: https://github.com/LiteLDev/LegacyScriptEngine/issues/216
755+
756+
[Unreleased]: https://github.com/LiteLDev/LegacyScriptEngine/compare/v0.9.3...HEAD
757+
758+
[0.9.3]: https://github.com/LiteLDev/LegacyScriptEngine/compare/v0.9.2...v0.9.3
749759

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

src/legacy/api/DataAPI.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,8 @@ Local<Value> MoneyClass::set(const Arguments& args) {
532532
CHECK_ARG_TYPE(args[1], ValueKind::kNumber);
533533

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

572573
try {
573-
return Boolean::newBoolean(EconomySystem::addMoney(args[0].asString().toString(), args[1].asNumber().toInt64())
574+
return Boolean::newBoolean(
575+
EconomySystem::addMoney(args[0].asString().toString(), args[1].asNumber().toInt64())
574576
);
575577
} catch (const std::invalid_argument& e) {
576578
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().error("Bad argument in MoneyAdd!");
@@ -614,12 +616,14 @@ Local<Value> MoneyClass::trans(const Arguments& args) {
614616
try {
615617
string note = "";
616618
if (args.size() >= 4 && args[3].getKind() == ValueKind::kString) note = args[3].asString().toString();
617-
return Boolean::newBoolean(EconomySystem::transMoney(
618-
args[0].asString().toString(),
619-
args[1].asString().toString(),
620-
args[2].asNumber().toInt64(),
621-
note
622-
));
619+
return Boolean::newBoolean(
620+
EconomySystem::transMoney(
621+
args[0].asString().toString(),
622+
args[1].asString().toString(),
623+
args[2].asNumber().toInt64(),
624+
note
625+
)
626+
);
623627
} catch (const std::invalid_argument& e) {
624628
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().error("Bad argument in MoneyTrans!");
625629
ll::error_utils::printException(e, lse::LegacyScriptEngine::getInstance().getSelf().getLogger());
@@ -759,8 +763,6 @@ Local<Value> DataClass::xuid2uuid(const Arguments& args) {
759763
}
760764

761765
Local<Value> DataClass::getAllPlayerInfo(const Arguments& args) {
762-
CHECK_ARGS_COUNT(args, 0);
763-
764766
try {
765767
auto arr = Array::newArray();
766768
auto level = ll::service::getLevel();

src/legacy/api/NetworkAPI.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,16 @@ void WSClientClass::initListeners() {
154154
}
155155
});
156156

157-
ws->OnBinaryReceived([nowList{&listeners[int(WSClientEvents::onBinaryReceived)]
158-
}](WebSocketClient&, vector<uint8_t> data) {
159-
if (!nowList->empty())
160-
for (auto& listener : *nowList) {
161-
if (!EngineManager::isValid(listener.engine)) return;
162-
EngineScope enter(listener.engine);
163-
NewTimeout(listener.func.get(), {ByteBuffer::newByteBuffer(data.data(), data.size())}, 1);
164-
}
165-
});
157+
ws->OnBinaryReceived(
158+
[nowList{&listeners[int(WSClientEvents::onBinaryReceived)]}](WebSocketClient&, vector<uint8_t> data) {
159+
if (!nowList->empty())
160+
for (auto& listener : *nowList) {
161+
if (!EngineManager::isValid(listener.engine)) return;
162+
EngineScope enter(listener.engine);
163+
NewTimeout(listener.func.get(), {ByteBuffer::newByteBuffer(data.data(), data.size())}, 1);
164+
}
165+
}
166+
);
166167

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

780782
Local<Value> HttpServerClass::stop(const Arguments& args) {
781-
CHECK_ARGS_COUNT(args, 0);
782-
783783
try {
784784
RecordOperation(getEngineOwnData()->pluginName, "StopHttpServer", "");
785785
svr->stop();
@@ -789,8 +789,6 @@ Local<Value> HttpServerClass::stop(const Arguments& args) {
789789
}
790790

791791
Local<Value> HttpServerClass::isRunning(const Arguments& args) {
792-
CHECK_ARGS_COUNT(args, 0);
793-
794792
try {
795793
return Boolean::newBoolean(svr->is_running());
796794
}

src/legacy/api/PlayerAPI.cpp

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2906,69 +2906,40 @@ Local<Value> PlayerClass::clearItem(const Arguments& args) {
29062906
if (!player) {
29072907
return {};
29082908
}
2909-
unsigned int clearCount = 1;
2909+
int clearCount = 1;
29102910
if (args.size() > 1) {
29112911
CHECK_ARG_TYPE(args[1], ValueKind::kNumber);
29122912
clearCount = args[1].asNumber().toInt32();
29132913
}
2914-
int result = 0;
2915-
auto& inventorySlots = player->getInventory().getSlots();
2916-
for (unsigned short slot = 0; slot < inventorySlots.size(); ++slot) {
2917-
if (clearCount <= 0) {
2918-
break;
2919-
}
2920-
if (inventorySlots[slot]->getTypeName() == args[0].asString().toString()) {
2921-
if (inventorySlots[slot]->mCount < clearCount) {
2922-
result += inventorySlots[slot]->mCount;
2923-
clearCount -= inventorySlots[slot]->mCount;
2924-
} else {
2925-
result += clearCount;
2926-
clearCount = 0;
2927-
}
2928-
player->getInventory().removeItem(slot, clearCount);
2929-
}
2930-
}
2931-
auto& handSlots = ActorEquipment::getHandContainer(player->getEntityContext()).getSlots();
2932-
for (unsigned short slot = 0; slot < handSlots.size(); ++slot) {
2933-
if (clearCount <= 0) {
2934-
break;
2935-
}
2936-
if (handSlots[slot]->getTypeName() == args[0].asString().toString()) {
2937-
if (handSlots[slot]->mCount < clearCount) {
2938-
result += handSlots[slot]->mCount;
2939-
clearCount -= handSlots[slot]->mCount;
2940-
} else {
2941-
result += clearCount;
2942-
clearCount = 0;
2943-
}
2944-
ActorEquipment::getHandContainer(player->getEntityContext()).removeItem(slot, clearCount);
2945-
}
2946-
}
2947-
auto& armorSlots = ActorEquipment::getArmorContainer(player->getEntityContext()).getSlots();
2948-
for (unsigned short slot = 0; slot < armorSlots.size(); ++slot) {
2949-
if (clearCount <= 0) {
2950-
break;
2951-
}
2952-
if (armorSlots[slot]->getTypeName() == args[0].asString().toString()) {
2953-
if (armorSlots[slot]->mCount < clearCount) {
2954-
result += armorSlots[slot]->mCount;
2955-
clearCount -= armorSlots[slot]->mCount;
2956-
} else {
2957-
result += clearCount;
2958-
clearCount = 0;
2914+
int result = 0;
2915+
std::string typeName = args[0].asString().toString();
2916+
auto clearFunction = [&result, &typeName, &clearCount](Container& container) {
2917+
auto slots = container.getSlots();
2918+
for (size_t slot = 0; slot < slots.size() && clearCount > 0; ++slot) {
2919+
if (slots[slot]->getTypeName() == typeName) {
2920+
auto count = slots[slot]->mCount;
2921+
if (count <= clearCount) {
2922+
result += count;
2923+
clearCount -= count;
2924+
container.setItem(slot, ItemStack::EMPTY_ITEM());
2925+
} else {
2926+
result += clearCount;
2927+
container.removeItem(slot, clearCount);
2928+
clearCount = 0;
2929+
}
29592930
}
2960-
ActorEquipment::getArmorContainer(player->getEntityContext()).removeItem(slot, clearCount);
29612931
}
2962-
}
2932+
};
2933+
clearFunction(player->getInventory());
2934+
clearFunction(ActorEquipment::getHandContainer(player->getEntityContext()));
2935+
clearFunction(ActorEquipment::getArmorContainer(player->getEntityContext()));
29632936
player->refreshInventory();
29642937
return Number::newNumber(result);
29652938
}
29662939
CATCH("Fail in clearItem!");
29672940
}
29682941

29692942
Local<Value> PlayerClass::isSprinting(const Arguments& args) {
2970-
CHECK_ARGS_COUNT(args, 0);
2971-
29722943
try {
29732944
Player* player = get();
29742945
if (!player) return Local<Value>();

tooth.lua.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"format_version": 2,
33
"tooth": "gitea.litebds.com/LiteLDev/legacy-script-engine-lua",
4-
"version": "0.9.2",
4+
"version": "0.9.3",
55
"info": {
66
"name": "LegacyScriptEngine with Lua backend",
77
"description": "A plugin engine for running LLSE plugins on LeviLamina",

tooth.nodejs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"format_version": 2,
33
"tooth": "gitea.litebds.com/LiteLDev/legacy-script-engine-nodejs",
4-
"version": "0.9.2",
4+
"version": "0.9.3",
55
"info": {
66
"name": "LegacyScriptEngine with NodeJs backend",
77
"description": "A plugin engine for running LLSE plugins on LeviLamina",

tooth.python.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"format_version": 2,
33
"tooth": "gitea.litebds.com/LiteLDev/legacy-script-engine-python",
4-
"version": "0.9.2",
4+
"version": "0.9.3",
55
"info": {
66
"name": "LegacyScriptEngine with Python backend",
77
"description": "A plugin engine for running LLSE plugins on LeviLamina",

tooth.quickjs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"format_version": 2,
33
"tooth": "gitea.litebds.com/LiteLDev/legacy-script-engine-quickjs",
4-
"version": "0.9.2",
4+
"version": "0.9.3",
55
"info": {
66
"name": "LegacyScriptEngine with QuickJs backend",
77
"description": "A plugin engine for running LLSE plugins on LeviLamina",

0 commit comments

Comments
 (0)