Skip to content

Commit 5f23bc0

Browse files
committed
fix: fix Player::clearItem
1 parent b87504b commit 5f23bc0

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

src/legacy/api/PlayerAPI.cpp

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2788,31 +2788,41 @@ Local<Value> PlayerClass::clearItem(const Arguments& args) {
27882788
CHECK_ARG_TYPE(args[1], ValueKind::kNumber);
27892789
clearCount = args[1].asNumber().toInt32();
27902790
}
2791-
int result = 0;
2792-
for (std::reference_wrapper<ItemStack> item : player->getInventory().getSlotCopies()) {
2793-
if (item.get().getTypeName() == args[0].asString().toString()) {
2794-
if (item.get().mCount < clearCount) {
2795-
result += item.get().mCount;
2791+
int result = 0;
2792+
auto& inventorySlots = player->getInventory().getSlots();
2793+
for (unsigned short slot = 0; slot < inventorySlots.size(); ++slot) {
2794+
if (inventorySlots[slot]->getTypeName() == args[0].asString().toString()) {
2795+
if (inventorySlots[slot]->mCount < clearCount) {
2796+
result += inventorySlots[slot]->mCount;
2797+
} else {
2798+
result += clearCount;
27962799
}
2797-
item.get().remove(clearCount);
2800+
player->getInventory().removeItem(slot, clearCount);
27982801
}
27992802
}
2800-
for (std::reference_wrapper<ItemStack> item : player->getHandContainer().getSlotCopies()) {
2801-
if (item.get().getTypeName() == args[0].asString().toString()) {
2802-
if (item.get().mCount < clearCount) {
2803-
result += item.get().mCount;
2803+
auto& handSlots = player->getHandContainer().getSlots();
2804+
for (unsigned short slot = 0; slot < handSlots.size(); ++slot) {
2805+
if (handSlots[slot]->getTypeName() == args[0].asString().toString()) {
2806+
if (handSlots[slot]->mCount < clearCount) {
2807+
result += handSlots[slot]->mCount;
2808+
} else {
2809+
result += clearCount;
28042810
}
2805-
item.get().remove(clearCount);
2811+
player->getInventory().removeItem(slot, clearCount);
28062812
}
28072813
}
2808-
for (std::reference_wrapper<ItemStack> item : player->getArmorContainer().getSlotCopies()) {
2809-
if (item.get().getTypeName() == args[0].asString().toString()) {
2810-
if (item.get().mCount < clearCount) {
2811-
result += item.get().mCount;
2814+
auto& armorSlots = player->getArmorContainer().getSlots();
2815+
for (unsigned short slot = 0; slot < armorSlots.size(); ++slot) {
2816+
if (armorSlots[slot]->getTypeName() == args[0].asString().toString()) {
2817+
if (armorSlots[slot]->mCount < clearCount) {
2818+
result += armorSlots[slot]->mCount;
2819+
} else {
2820+
result += clearCount;
28122821
}
2813-
item.get().remove(clearCount);
2822+
player->getInventory().removeItem(slot, clearCount);
28142823
}
28152824
}
2825+
player->refreshInventory();
28162826
return Number::newNumber(result);
28172827
}
28182828
CATCH("Fail in clearItem!");

0 commit comments

Comments
 (0)