// Removes 1 from each item stack with replacement support // Example: if replacements contains the pair ("bucket:bucket_water", "bucket:bucket_empty"), // a water bucket will not be removed but replaced by an empty bucket. static void craftDecrementOrReplaceInput(CraftInput &input, const CraftReplacements &replacements, IGameDef *gamedef) { if(replacements.pairs.empty()) { craftDecrementInput(input, gamedef); return; } // Make a copy of the replacements pair list std::vector<std::pair<std::string, std::string> > pairs = replacements.pairs; for(std::vector<ItemStack>::iterator i = input.items.begin(); i != input.items.end(); i++) { if(i->count == 1) { // Find an appropriate replacement bool found_replacement = false; for(std::vector<std::pair<std::string, std::string> >::iterator j = pairs.begin(); j != pairs.end(); j++) { ItemStack from_item; from_item.deSerialize(j->first, gamedef->idef()); if(i->name == from_item.name) { i->deSerialize(j->second, gamedef->idef()); found_replacement = true; pairs.erase(j); break; } } // No replacement was found, simply decrement count to zero if(!found_replacement) i->remove(1); } else if(i->count >= 2) { // Ignore replacements for items with count >= 2 i->remove(1); } } }
// Removes 1 from each item stack with replacement support // Example: if replacements contains the pair ("bucket:bucket_water", "bucket:bucket_empty"), // a water bucket will not be removed but replaced by an empty bucket. static void craftDecrementOrReplaceInput(CraftInput &input, std::vector<ItemStack> &output_replacements, const CraftReplacements &replacements, IGameDef *gamedef) { if (replacements.pairs.empty()) { craftDecrementInput(input, gamedef); return; } // Make a copy of the replacements pair list std::vector<std::pair<std::string, std::string> > pairs = replacements.pairs; for (auto &item : input.items) { // Find an appropriate replacement bool found_replacement = false; for (auto j = pairs.begin(); j != pairs.end(); ++j) { if (inputItemMatchesRecipe(item.name, j->first, gamedef->idef())) { if (item.count == 1) { item.deSerialize(j->second, gamedef->idef()); found_replacement = true; pairs.erase(j); break; } ItemStack rep; rep.deSerialize(j->second, gamedef->idef()); item.remove(1); found_replacement = true; output_replacements.push_back(rep); break; } } // No replacement was found, simply decrement count by one if (!found_replacement && item.count > 0) item.remove(1); } }
void CraftDefinitionToolRepair::decrementInput(CraftInput &input, IGameDef *gamedef) const { craftDecrementInput(input, gamedef); }
void CraftDefinitionToolRepair::decrementInput(CraftInput &input, std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const { craftDecrementInput(input, gamedef); }