@@ -2906,69 +2906,40 @@ Local<Value> PlayerClass::clearItem(const Arguments& args) {
2906
2906
if (!player) {
2907
2907
return {};
2908
2908
}
2909
- unsigned int clearCount = 1 ;
2909
+ int clearCount = 1 ;
2910
2910
if (args.size () > 1 ) {
2911
2911
CHECK_ARG_TYPE (args[1 ], ValueKind::kNumber );
2912
2912
clearCount = args[1 ].asNumber ().toInt32 ();
2913
2913
}
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
+ }
2959
2930
}
2960
- ActorEquipment::getArmorContainer (player->getEntityContext ()).removeItem (slot, clearCount);
2961
2931
}
2962
- }
2932
+ };
2933
+ clearFunction (player->getInventory ());
2934
+ clearFunction (ActorEquipment::getHandContainer (player->getEntityContext ()));
2935
+ clearFunction (ActorEquipment::getArmorContainer (player->getEntityContext ()));
2963
2936
player->refreshInventory ();
2964
2937
return Number::newNumber (result);
2965
2938
}
2966
2939
CATCH (" Fail in clearItem!" );
2967
2940
}
2968
2941
2969
2942
Local<Value> PlayerClass::isSprinting (const Arguments& args) {
2970
- CHECK_ARGS_COUNT (args, 0 );
2971
-
2972
2943
try {
2973
2944
Player* player = get ();
2974
2945
if (!player) return Local<Value>();
0 commit comments