diff --git a/CustomItemsAPI/EventHandlers/DamageHandler.cs b/CustomItemsAPI/EventHandlers/DamageHandler.cs index ea7967e..2d9d04c 100644 --- a/CustomItemsAPI/EventHandlers/DamageHandler.cs +++ b/CustomItemsAPI/EventHandlers/DamageHandler.cs @@ -1,7 +1,10 @@ using CustomItemsAPI.Events; +using CustomItemsAPI.Extensions; using CustomItemsAPI.Items; +using InventorySystem.Items.Armor; using LabApi.Events.Arguments.PlayerEvents; using LabApi.Events.CustomHandlers; +using LabApi.Features.Wrappers; using PlayerStatsSystem; namespace CustomItemsAPI.EventHandlers; @@ -22,12 +25,25 @@ public override void OnPlayerHurting(PlayerHurtingEventArgs ev) return; if (firearmDamageHandler.Firearm == null) return; - if (!CustomItems.TryGetCustomItem(firearmDamageHandler.Firearm.ItemSerial, out CustomFirearmBase cur_item)) + + if (CustomItems.TryGetCustomItem(firearmDamageHandler.Firearm.ItemSerial, out CustomFirearmBase cur_item)) + { + TypeWrapper isAllowedHelper = new(ev.IsAllowed); + CustomFirearmEvents.OnHurting(cur_item, ev.Player, ev.Attacker, firearmDamageHandler, isAllowedHelper); + cur_item.OnHurting(ev.Player, ev.Attacker, firearmDamageHandler, isAllowedHelper); + ev.IsAllowed = isAllowedHelper.Value; + } + + if (!ev.Player.Inventory.TryGetBodyArmor(out BodyArmor equipedArmor)) return; - TypeWrapper isAllowedHelper = new(ev.IsAllowed); - CustomFirearmEvents.OnHurting(cur_item, ev.Player, ev.Attacker, firearmDamageHandler, isAllowedHelper); - cur_item.OnHurting(ev.Player, ev.Attacker, firearmDamageHandler, isAllowedHelper); - ev.IsAllowed = isAllowedHelper.Value; + + if (CustomItems.TryGetCustomItem(item: Item.Get(equipedArmor), out CustomArmorBase cur_armor)) + { + TypeWrapper isAllowedHelper = new(ev.IsAllowed); + CustomItemEvents.OnTakingDamage(cur_armor, ev.Player, ev.Attacker, ev.DamageHandler, isAllowedHelper); + cur_armor.OnTakingDamage(ev.Player, ev.Attacker, firearmDamageHandler, isAllowedHelper); + ev.IsAllowed = isAllowedHelper.Value; + } } public override void OnPlayerHurt(PlayerHurtEventArgs ev) diff --git a/CustomItemsAPI/Events/CustomItemEvents.cs b/CustomItemsAPI/Events/CustomItemEvents.cs index f6956d4..96cdae1 100644 --- a/CustomItemsAPI/Events/CustomItemEvents.cs +++ b/CustomItemsAPI/Events/CustomItemEvents.cs @@ -2,6 +2,7 @@ using InventorySystem.Items; using InventorySystem.Items.Pickups; using LabApi.Features.Wrappers; +using PlayerStatsSystem; using Scp914; using UnityEngine; @@ -31,43 +32,46 @@ public static class CustomItemEvents public static event Action, TypeWrapper>? ProcessingItem; public static event Action, TypeWrapper, TypeWrapper>? ProcessingPickup; public static event Action? Removed; + public static event Action>? TakingDamage; - public static void OnRegistered(CustomItemBase customItem) + public static void OnRegistered(CustomItemBase customItem) => Registered?.Invoke(customItem); - public static void OnUnregistered(CustomItemBase customItem) + public static void OnUnregistered(CustomItemBase customItem) => Unregistered?.Invoke(customItem); - public static void OnNewCreated(CustomItemBase customItem) + public static void OnNewCreated(CustomItemBase customItem) => NewCreated?.Invoke(customItem); - public static void OnParsePickup(CustomItemBase customItem, Pickup pickup) + public static void OnParsePickup(CustomItemBase customItem, Pickup pickup) => ParsePickup?.Invoke(customItem, pickup); - public static void OnParseItem(CustomItemBase customItem, Item item) + public static void OnParseItem(CustomItemBase customItem, Item item) => ParseItem?.Invoke(customItem, item); - public static void OnDistribute(CustomItemBase customItem) + public static void OnDistribute(CustomItemBase customItem) => Distribute?.Invoke(customItem); - public static void OnChanged(CustomItemBase customItem, Player player, Item? oldItem, Item? newItem, bool changedToThisItem) + public static void OnChanged(CustomItemBase customItem, Player player, Item? oldItem, Item? newItem, bool changedToThisItem) => Changed?.Invoke(customItem, player, oldItem, newItem, changedToThisItem); public static void OnChanging(CustomItemBase customItem, Player player, Item? oldItem, Item? newItem, bool changedToThisItem, TypeWrapper isAllowed) => Changing?.Invoke(customItem, player, oldItem, newItem, changedToThisItem, isAllowed); - public static void OnDropped(CustomItemBase customItem, Player player, Pickup pickup) + public static void OnDropped(CustomItemBase customItem, Player player, Pickup pickup) => Dropped?.Invoke(customItem, player, pickup); public static void OnDropping(CustomItemBase customItem, Player player, Item item, TypeWrapper isThrow, TypeWrapper isAllowed) => Dropping?.Invoke(customItem, player, item, isThrow, isAllowed); - public static void OnSearched(CustomItemBase customItem, Player player, Pickup pickup) + public static void OnSearched(CustomItemBase customItem, Player player, Pickup pickup) => Searched?.Invoke(customItem, player, pickup); - public static void OnSearching(CustomItemBase customItem, Player player, Pickup pickup, TypeWrapper isAllowed) + public static void OnSearching(CustomItemBase customItem, Player player, Pickup pickup, TypeWrapper isAllowed) => Searching?.Invoke(customItem, player, pickup, isAllowed); - public static void OnPicked(CustomItemBase customItem, Player player, Item item) + public static void OnPicked(CustomItemBase customItem, Player player, Item item) => Picked?.Invoke(customItem, player, item); - public static void OnPicking(CustomItemBase customItem, Player player, Pickup pickup, TypeWrapper isAllowed) + public static void OnPicking(CustomItemBase customItem, Player player, Pickup pickup, TypeWrapper isAllowed) => Picking?.Invoke(customItem, player, pickup, isAllowed); - public static void OnThrew(CustomItemBase customItem, Player player, Pickup pickup, Rigidbody rigidbody) + public static void OnThrew(CustomItemBase customItem, Player player, Pickup pickup, Rigidbody rigidbody) => Threw?.Invoke(customItem, player, pickup, rigidbody); - public static void OnThrowing(CustomItemBase customItem, Player player, Pickup pickup, Rigidbody rigidbody, TypeWrapper isAllowed) + public static void OnThrowing(CustomItemBase customItem, Player player, Pickup pickup, Rigidbody rigidbody, TypeWrapper isAllowed) => Throwing?.Invoke(customItem, player, pickup, rigidbody, isAllowed); - public static void OnProcessingItem(CustomItemBase customItem, Player player, Item item, TypeWrapper knobSetting, TypeWrapper isAllowed) + public static void OnProcessingItem(CustomItemBase customItem, Player player, Item item, TypeWrapper knobSetting, TypeWrapper isAllowed) => ProcessingItem?.Invoke(customItem, player, item, knobSetting, isAllowed); - public static void OnProcessingPickup(CustomItemBase customItem, Pickup pickup, TypeWrapper knobSetting, TypeWrapper newPosition, TypeWrapper isAllowed) - => ProcessingPickup?.Invoke(customItem, pickup, knobSetting, newPosition, isAllowed); - public static void OnRemoved(CustomItemBase customItem, Player player, ItemBase? itemBase, ItemPickupBase? itemPickupBase) + public static void OnProcessingPickup(CustomItemBase customItem, Pickup pickup, TypeWrapper knobSetting, TypeWrapper newPosition, TypeWrapper isAllowed) + => ProcessingPickup?.Invoke(customItem, pickup, knobSetting, newPosition, isAllowed); + public static void OnRemoved(CustomItemBase customItem, Player player, ItemBase? itemBase, ItemPickupBase? itemPickupBase) => Removed?.Invoke(customItem, player, itemBase, itemPickupBase); + public static void OnTakingDamage(CustomItemBase customItem, Player player, Player attacker, DamageHandlerBase damageHandler, TypeWrapper isAllowed) + => TakingDamage?.Invoke(customItem, player, attacker, damageHandler, isAllowed); } diff --git a/CustomItemsAPI/Items/CustomArmorBase.cs b/CustomItemsAPI/Items/CustomArmorBase.cs index ba23b7e..eebc4e3 100644 --- a/CustomItemsAPI/Items/CustomArmorBase.cs +++ b/CustomItemsAPI/Items/CustomArmorBase.cs @@ -1,7 +1,11 @@ -using CustomItemsAPI.Extensions; +using CustomItemsAPI.EventHandlers; +using CustomItemsAPI.Extensions; using InventorySystem.Items.Armor; +using LabApi.Events.Arguments.PlayerEvents; using LabApi.Features.Wrappers; +using PlayerStatsSystem; using System.ComponentModel; +using UnityEngine; namespace CustomItemsAPI.Items; @@ -68,4 +72,28 @@ public override void Parse(Item item) if (CategoryLimits != null) body.Base.CategoryLimits = [.. CategoryLimits]; } + + /// + /// Triggers when user is damaged. + /// + /// Reciever of the damage. + /// Damage dealer. + /// Damage Handler. + /// /// Whether or not the action is allowed to perform. + public virtual void OnTakingDamage(Player reciever, Player attacker, FirearmDamageHandler damageHandler, TypeWrapper isAllowed) + { + CL.Debug($"OnTakingDamage {reciever.DisplayName} from {attacker.DisplayName} dealing {damageHandler.Damage} damage", Main.Instance.Config.Debug); + } + + /// + /// This who picked up the current Custom Item (Armour). + /// + /// The Player who called this function. + /// The armour has been equiped. + public override void OnSearched(Player player, Pickup pickup) + { + CL.Debug($"OnSearched (Armor) {player.PlayerId} {pickup.Serial}", Main.Instance.Config.Debug); + if (OverrideShowPickedUpHint) + HintShow?.Invoke(player, string.Format(OverridePickedUpHint, DisplayName, Description)); + } }