diff --git a/Exiled.CustomModules/API/Features/CustomItems/Items/Candies/BaseCandy.cs b/Exiled.CustomModules/API/Features/CustomItems/Items/Candies/BaseCandy.cs index d06da0e36a..ebb2e92eea 100644 --- a/Exiled.CustomModules/API/Features/CustomItems/Items/Candies/BaseCandy.cs +++ b/Exiled.CustomModules/API/Features/CustomItems/Items/Candies/BaseCandy.cs @@ -27,6 +27,7 @@ internal class BaseCandy : ICandy internal BaseCandy(CandySettings settings, Action applyEffectsDelegate) { this.settings = settings; + ApplyEffects = applyEffectsDelegate; } /// diff --git a/Exiled.CustomModules/API/Features/CustomItems/Items/Candies/CandyBehaviour.cs b/Exiled.CustomModules/API/Features/CustomItems/Items/Candies/CandyBehaviour.cs index a4e5ab7fd1..7afdd55d9d 100644 --- a/Exiled.CustomModules/API/Features/CustomItems/Items/Candies/CandyBehaviour.cs +++ b/Exiled.CustomModules/API/Features/CustomItems/Items/Candies/CandyBehaviour.cs @@ -84,7 +84,6 @@ protected override void SubscribeEvents() base.SubscribeEvents(); Exiled.Events.Handlers.Scp330.EatingScp330 += OnEatingScp330Internal; - Exiled.Events.Handlers.Scp330.InteractingScp330 += OnInteractingScp330Internal; } /// @@ -93,7 +92,6 @@ protected override void UnsubscribeEvents() base.UnsubscribeEvents(); Exiled.Events.Handlers.Scp330.EatingScp330 -= OnEatingScp330Internal; - Exiled.Events.Handlers.Scp330.InteractingScp330 -= OnInteractingScp330Internal; } /// @@ -120,14 +118,6 @@ protected virtual void OnEatingCandy(EatingScp330EventArgs ev) { } - /// - /// Fired when player interacts with SCP-330 and gets a custom candy. - /// - /// The event instance. - protected virtual void OnInteracting(InteractingScp330EventArgs ev) - { - } - /// private protected void OnEatingScp330Internal(EatingScp330EventArgs ev) { @@ -139,21 +129,5 @@ private protected void OnEatingScp330Internal(EatingScp330EventArgs ev) OnEatingCandy(ev); } - - /// - private protected void OnInteractingScp330Internal(InteractingScp330EventArgs ev) - { - if (ev.Candy != CandySettings.CandyType || Random.value * 100 >= CandySettings.Weight || - !ev.Player.TryGetItems(x => x.Type == ItemType.SCP330, out IEnumerable items) || - !items.Single().Is(out Scp330 scp330)) - return; - - TrackedCandies.Add(scp330.SelectedCandyId); - StaticActor.Get().AddOrTrack(scp330); - scp330.AddComponent(); - ev.Player.ShowTextDisplay(CandySettings.ReceivedCustomCandyMessage); - - OnInteracting(ev); - } } } \ No newline at end of file diff --git a/Exiled.CustomModules/API/Features/CustomItems/Items/Candies/CandyTracker.cs b/Exiled.CustomModules/API/Features/CustomItems/Items/Candies/CandyTracker.cs new file mode 100644 index 0000000000..cc7f2fc394 --- /dev/null +++ b/Exiled.CustomModules/API/Features/CustomItems/Items/Candies/CandyTracker.cs @@ -0,0 +1,86 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) Exiled Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- + +namespace Exiled.CustomModules.API.Features.CustomItems.Items.Candies +{ + using System.Collections.Generic; + using System.Linq; + + using Exiled.API.Features.Items; + using Exiled.Events.EventArgs.Player; + using Exiled.Events.EventArgs.Scp330; + using UnityEngine; + + /// + /// A custom tracker for candies. + /// + public class CandyTracker : ItemTracker + { + /// + internal void OnInteractingScp330(InteractingScp330EventArgs ev) + { + if (!ev.Player.TryGetItems(x => x.Type == ItemType.SCP330, out IEnumerable items) || + !items.Single().Is(out Scp330 scp330)) + return; + + if (!scp330.TryGetComponent(out CandyBehaviour behaviour)) + { + behaviour = scp330.AddComponent(); + + if (ev.Candy != behaviour.CandySettings.CandyType || Random.value * 100 <= behaviour.CandySettings.Weight) + { + scp330.RemoveComponent(); + return; + } + + AddOrTrack(scp330); + behaviour.TrackedCandies.Add(0); + return; + } + + if (ev.Candy != behaviour.CandySettings.CandyType || Random.value * 100 <= behaviour.CandySettings.Weight) + return; + + behaviour.TrackedCandies.Add(scp330.Candies.Count); + } + + /// + internal void OnInternalItemAdded(ItemAddedEventArgs ev) + { + if (IsTracked(ev.Pickup) && ev.Item.Is(out Scp330 scp330)) + { + if (scp330.TryGetComponent(out CandyBehaviour behaviour)) + { + behaviour.TrackedCandies.Add(scp330.Candies.Count - 1); + } + else + { + behaviour = scp330.AddComponent(); + behaviour.TrackedCandies.Add(0); + } + } + } + + /// + protected override void SubscribeEvents() + { + base.SubscribeEvents(); + + Exiled.Events.Handlers.Scp330.InteractingScp330 += OnInteractingScp330; + Exiled.Events.Handlers.Player.ItemAdded += OnInternalItemAdded; + } + + /// + protected override void UnsubscribeEvents() + { + base.UnsubscribeEvents(); + + Exiled.Events.Handlers.Scp330.InteractingScp330 += OnInteractingScp330; + Exiled.Events.Handlers.Player.ItemAdded -= OnInternalItemAdded; + } + } +} \ No newline at end of file diff --git a/Exiled.CustomModules/API/Features/CustomItems/Items/Candies/Patches/DroppingCandy.cs b/Exiled.CustomModules/API/Features/CustomItems/Items/Candies/Patches/DroppingCandy.cs index 07e79e68ee..d0165359ab 100644 --- a/Exiled.CustomModules/API/Features/CustomItems/Items/Candies/Patches/DroppingCandy.cs +++ b/Exiled.CustomModules/API/Features/CustomItems/Items/Candies/Patches/DroppingCandy.cs @@ -10,10 +10,13 @@ namespace Exiled.CustomModules.API.Features.CustomItems.Items.Candies.Patches using System.Collections.Generic; using System.Reflection.Emit; + using Exiled.API.Features.Core; using Exiled.API.Features.Core.Generic.Pools; using Exiled.API.Features.Items; + using Exiled.API.Features.Pickups; using HarmonyLib; using InventorySystem.Items; + using InventorySystem.Items.Pickups; using InventorySystem.Items.Usables.Scp330; using static HarmonyLib.AccessTools; @@ -38,6 +41,8 @@ private static IEnumerable Transpiler(IEnumerable Transpiler(IEnumerable().AddOrTrack(pickup); } } } \ No newline at end of file