protected bool GetItemsToDecrement(Creature creature, Stage stage, ManualData manualData, List<ProductionMaterialData> requiredMaterials, List<ProductionMaterial> materials, out List<ProductionMaterial> toDecrement)
{
toDecrement = new List<ProductionMaterial>();
var inUse = new HashSet<long>();
foreach (var reqMat in requiredMaterials)
{
// Check all selected items for tag matches
foreach (var material in materials)
{
// Satisfy requirement with item, up to the max amount
// needed or available
if (material.Item.HasTag(reqMat.Tag))
{
// Cancel if one item matches multiple materials.
// It's unknown how this would be handled, can it even
// happen? Can one item maybe only be used as one material?
if (inUse.Contains(material.Item.EntityId))
{
Send.ServerMessage(creature, Localization.Get("Unable to handle request, please report, with this information: ({0}/{1}:{2})."), material.Item.Info.Id, manualData.Category, manualData.Id);
Log.Warning("CreationSkill.GetItemsToDecrement: Item '{0}' matches multiple materials for manual '{1}:{2}'.", material.Item.Info.Id, manualData.Category, manualData.Id);
return false;
}
var amount = Math.Min(reqMat.Amount, material.Item.Amount);
reqMat.Amount -= amount;
toDecrement.Add(new ProductionMaterial(material.Item, amount));
inUse.Add(material.Item.EntityId);
}
// Break once we got what we need
if (reqMat.Amount == 0)
break;
}
}
if (requiredMaterials.Any(a => a.Amount != 0))
{
// Unofficial, the client should normally prevent this.
Send.ServerMessage(creature, Localization.Get("Insufficient materials."));
return false;
}
return true;
}