object CanRepair(BaseCombatEntity entity, BasePlayer player, List <BaseCombatEntity> entities) { float num = entity.MaxHealth() - entity.health; float num2 = num / entity.MaxHealth(); var list = entity.RepairCost(num2); if (list != null && list.Count > 0) { foreach (var ia in list) { var items = player.inventory.FindItemIDs(ia.itemid); int sum = items.Sum(item => item.amount); if (sum * repairMulti < ia.amount * repairMulti) { return(new KeyValuePair <string, float>(ia.itemDef.displayName.english, ia.amount)); } } } var privs = entities.Where(ent => ent != null && ent.net != null && !ent.IsDestroyed && ent is BuildingPrivlidge).Cast <BuildingPrivlidge>().ToList(); if (privs.Count == 0) { return(true); } foreach (var priv in privs) { if (priv.Distance(entity) <= privDistance) { return(!priv.AnyAuthed() || priv.IsAuthed(player)); } } return(false); // player.CanBuild(new OBB(entity.transform, entity.bounds)); }
private object repairStructure(BaseCombatEntity entity, BasePlayer player) { if (entity.SecondsSinceAttacked <= (int)Config["nTimeSinceAttacked"]) { return(null); } if (!(bool)Config["bChargeForRepairs"]) { entity.health = entity.MaxHealth(); entity.OnRepair(); entity.SendNetworkUpdateImmediate(true); return(false); } float hp = entity.health; int i = 0; Dictionary <int, int> charge = new Dictionary <int, int>(); while (hp < entity.MaxHealth()) { if (i >= 30) { Puts("Breaking loop -- Something went wrong"); break; } i += 1; float single = 50f; float single1 = entity.MaxHealth() - hp; single1 = Mathf.Clamp(single1, 0f, single); float single2 = single1 / entity.MaxHealth(); if (single2 == 0f) { return(false); } List <ItemAmount> itemAmounts = entity.RepairCost(single2); if (itemAmounts == null) { return(false); } float single3 = itemAmounts.Sum <ItemAmount>((ItemAmount x) => x.amount); if (single3 <= 0f) { hp = entity.MaxHealth(); } else { float single4 = itemAmounts.Min <ItemAmount>((ItemAmount x) => Mathf.Clamp01((float)player.inventory.GetAmount(x.itemid) / x.amount)); if (single4 == 0f) { return(false); } int num = 0; foreach (ItemAmount itemAmount in itemAmounts) { int num1 = Mathf.CeilToInt(single4 * itemAmount.amount); int num2 = num1; num = num + num2; if (charge.ContainsKey(itemAmount.itemid)) { charge[itemAmount.itemid] = charge[itemAmount.itemid] + num2; } else { charge.Add(itemAmount.itemid, num2); } } float single5 = (float)num / (float)single3; hp = hp + single1 * single5; entity.OnRepair(); } } foreach (KeyValuePair <int, int> item in charge) { ItemDefinition defs; defs = ItemManager.FindItemDefinition(item.Key); if (player.inventory.GetAmount(defs.itemid) < item.Value) { return(null); } } foreach (KeyValuePair <int, int> item in charge) { player.inventory.Take(null, item.Key, item.Value); player.Command("note.inv", new object[] { item.Key, item.Value * -1 }); } entity.health = entity.MaxHealth(); entity.SendNetworkUpdateImmediate(true); return(false); }
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); }