private void DoRepair(BasePlayer player, BaseCombatEntity entity, PlayerRepairStats stats, bool noCost) { if (!entity.repair.enabled || entity.health == entity.MaxHealth()) { return; } if (Interface.CallHook("OnStructureRepair", this, player) != null) { return; } if (entity.SecondsSinceAttacked <= 30f) { entity.OnRepairFailed(null, string.Empty); stats.RecentlyDamaged++; return; } float missingHealth = entity.MaxHealth() - entity.health; float healthPercentage = missingHealth / entity.MaxHealth(); if (missingHealth <= 0f || healthPercentage <= 0f) { entity.OnRepairFailed(null, string.Empty); return; } if (!noCost) { List <ItemAmount> itemAmounts = entity.RepairCost(healthPercentage); if (itemAmounts.Sum(x => x.amount) <= 0f) { entity.health += missingHealth; entity.SendNetworkUpdate(); entity.OnRepairFinished(); return; } if (_pluginConfig.RepairCostMultiplier != 1f) { foreach (ItemAmount amount in itemAmounts) { amount.amount *= _pluginConfig.RepairCostMultiplier; } } if (itemAmounts.Any(ia => player.inventory.GetAmount(ia.itemid) < (int)ia.amount)) { entity.OnRepairFailed(null, string.Empty); foreach (ItemAmount amount in itemAmounts) { stats.MissingAmounts[amount.itemid] += (int)amount.amount; } stats.TotalCantAfford++; return; } foreach (ItemAmount amount in itemAmounts) { player.inventory.Take(null, amount.itemid, (int)amount.amount); stats.AmountTaken[amount.itemid] += (int)amount.amount; } } entity.health += missingHealth; entity.SendNetworkUpdate(); if (entity.health < entity.MaxHealth()) { entity.OnRepair(); } else { entity.OnRepairFinished(); } stats.TotalSuccess++; }
// BaseCombatEntity public bool DoRepair(BaseCombatEntity entity, BasePlayer player) { if (!entity.repair.enabled) { return(false); } if (Interface.CallHook("OnStructureRepair", new object[] { entity, player }) != null) { return(false); } if (entity.SecondsSinceAttacked <= lastAttackLimit) { entity.OnRepairFailed(); return(false); } float num = entity.MaxHealth() - entity.Health(); float num2 = num / entity.MaxHealth(); if (num <= 0f || num2 <= 0f) { entity.OnRepairFailed(); return(false); } var list = entity.RepairCost(num2); if (list == null || list.Count == 0) { return(false); } foreach (var ia in list) { ia.amount *= repairMulti; } float num3 = list.Sum(x => x.amount); if (num3 > 0f) { float num4 = list.Min(x => Mathf.Clamp01((float)player.inventory.GetAmount(x.itemid) / x.amount)); num4 = Mathf.Min(num4, 50f / num); if (num4 <= 0f) { entity.OnRepairFailed(); return(false); } int num5 = 0; foreach (var current in list) { int amount = Mathf.CeilToInt(num4 * current.amount); num5 += player.inventory.Take(null, current.itemid, amount); } float num7 = (float)num5 / num3; entity.health += num * num7; entity.SendNetworkUpdate(BasePlayer.NetworkQueue.Update); } else { entity.health += num; entity.SendNetworkUpdate(BasePlayer.NetworkQueue.Update); } if (entity.health >= entity.MaxHealth()) { entity.OnRepairFinished(); } else { entity.OnRepair(); } return(true); }