Skip to content

Commit

Permalink
fix (#2433)
Browse files Browse the repository at this point in the history
  • Loading branch information
VALERA771 authored Feb 4, 2024
1 parent 1dac1ef commit ee9109b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal class BaseCandy : ICandy
internal BaseCandy(CandySettings settings, Action<Pawn> applyEffectsDelegate)
{
this.settings = settings;
ApplyEffects = applyEffectsDelegate;
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ protected override void SubscribeEvents()
base.SubscribeEvents();

Exiled.Events.Handlers.Scp330.EatingScp330 += OnEatingScp330Internal;
Exiled.Events.Handlers.Scp330.InteractingScp330 += OnInteractingScp330Internal;
}

/// <inheritdoc/>
Expand All @@ -93,7 +92,6 @@ protected override void UnsubscribeEvents()
base.UnsubscribeEvents();

Exiled.Events.Handlers.Scp330.EatingScp330 -= OnEatingScp330Internal;
Exiled.Events.Handlers.Scp330.InteractingScp330 -= OnInteractingScp330Internal;
}

/// <inheritdoc/>
Expand All @@ -120,14 +118,6 @@ protected virtual void OnEatingCandy(EatingScp330EventArgs ev)
{
}

/// <summary>
/// Fired when player interacts with SCP-330 and gets a custom candy.
/// </summary>
/// <param name="ev">The <see cref="EatingScp330EventArgs"/> event instance.</param>
protected virtual void OnInteracting(InteractingScp330EventArgs ev)
{
}

/// <inheritdoc cref="OnEatingCandy"/>
private protected void OnEatingScp330Internal(EatingScp330EventArgs ev)
{
Expand All @@ -139,21 +129,5 @@ private protected void OnEatingScp330Internal(EatingScp330EventArgs ev)

OnEatingCandy(ev);
}

/// <inheritdoc cref="OnInteracting"/>
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<Item> items) ||
!items.Single().Is(out Scp330 scp330))
return;

TrackedCandies.Add(scp330.SelectedCandyId);
StaticActor.Get<ItemTracker>().AddOrTrack(scp330);
scp330.AddComponent<CandyBehaviour>();
ev.Player.ShowTextDisplay(CandySettings.ReceivedCustomCandyMessage);

OnInteracting(ev);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// -----------------------------------------------------------------------
// <copyright file="CandyTracker.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

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;

/// <summary>
/// A custom tracker for candies.
/// </summary>
public class CandyTracker : ItemTracker
{
/// <inheritdoc cref="Exiled.Events.Handlers.Scp330.OnInteractingScp330"/>
internal void OnInteractingScp330(InteractingScp330EventArgs ev)
{
if (!ev.Player.TryGetItems(x => x.Type == ItemType.SCP330, out IEnumerable<Item> items) ||
!items.Single().Is(out Scp330 scp330))
return;

if (!scp330.TryGetComponent(out CandyBehaviour behaviour))
{
behaviour = scp330.AddComponent<CandyBehaviour>();

if (ev.Candy != behaviour.CandySettings.CandyType || Random.value * 100 <= behaviour.CandySettings.Weight)
{
scp330.RemoveComponent<CandyBehaviour>();
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);
}

/// <inheritdoc cref="Exiled.Events.Handlers.Player.OnItemAdded"/>
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<CandyBehaviour>();
behaviour.TrackedCandies.Add(0);
}
}
}

/// <inheritdoc/>
protected override void SubscribeEvents()
{
base.SubscribeEvents();

Exiled.Events.Handlers.Scp330.InteractingScp330 += OnInteractingScp330;
Exiled.Events.Handlers.Player.ItemAdded += OnInternalItemAdded;
}

/// <inheritdoc/>
protected override void UnsubscribeEvents()
{
base.UnsubscribeEvents();

Exiled.Events.Handlers.Scp330.InteractingScp330 += OnInteractingScp330;
Exiled.Events.Handlers.Player.ItemAdded -= OnInternalItemAdded;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,20 +41,24 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new CodeInstruction(OpCodes.Ldloc_1).MoveLabelsFrom(newInstructions[index]),
new(OpCodes.Call, Method(typeof(Item), nameof(Item.Get), new[] { typeof(ItemBase) })),
new(OpCodes.Ldarg_1),
new(OpCodes.Ldloc_3),
new(OpCodes.Call, Method(typeof(Pickup), nameof(Pickup.Get), new[] { typeof(ItemPickupBase) })),
new(OpCodes.Call, Method(typeof(DroppingCandy), nameof(DropCandy))),
});

for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];
}

private static void DropCandy(Item item, SelectScp330Message msg)
private static void DropCandy(Item item, SelectScp330Message msg, Pickup pickup)
{
if (!item.Is(out Scp330 _) || !CustomItem.TryGet(item, out CustomItem customItem)
|| customItem.BehaviourComponent != typeof(CandyBehaviour) || !item.TryGetComponent(out CandyBehaviour candyBehaviour))
return;

candyBehaviour.TrackedCandies.Remove(msg.CandyID);

StaticActor.Get<ItemTracker>().AddOrTrack(pickup);
}
}
}

0 comments on commit ee9109b

Please sign in to comment.