@@ -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
29692942Local<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>();
0 commit comments