public static PartResource AddOrCreateResource(this Part part, PartResourceDefinition info, float maxAmount, float amount)
{
if (amount > maxAmount)
{
part.LogWarning($"Cannot add resource '{info.name}' with amount > maxAmount, will use maxAmount (amount = {amount}, maxAmount = {maxAmount})");
amount = maxAmount;
}
PartResource resource = part.Resources[info.name];
if (resource == null)
{
if (amount < 0f)
amount = maxAmount;
resource = part.AddResource(info, maxAmount, amount);
}
else
{
resource.maxAmount = maxAmount;
if (amount >= 0f)
resource.amount = amount;
}
return resource;
}