Skip to content

Commit

Permalink
Fix Jailbird::WearState (#12)
Browse files Browse the repository at this point in the history
* Jailbird

* Fix

* Exception

* Fix NW moment
  • Loading branch information
louis1706 authored Aug 2, 2024
1 parent 5653733 commit 5a519a4
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 7 deletions.
30 changes: 28 additions & 2 deletions EXILED/Exiled.API/Features/Items/Jailbird.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

namespace Exiled.API.Features.Items
{
using System;

using Exiled.API.Features.Pickups;
using Exiled.API.Interfaces;
using InventorySystem.Items.Autosync;
using InventorySystem.Items.Jailbird;
using Mirror;
using UnityEngine;

using JailbirdPickup = Pickups.JailbirdPickup;

Expand Down Expand Up @@ -114,12 +117,35 @@ public JailbirdWearState WearState
get => Base._deterioration.WearState;
set
{
if (JailbirdDeteriorationTracker.ReceivedStates.ContainsKey(Serial))
JailbirdDeteriorationTracker.ReceivedStates[Serial] = value;
TotalDamageDealt = GetDamage(value);
TotalCharges = GetCharge(value);
Base._deterioration.RecheckUsage();
}
}

/// <summary>
/// Calculates the damage corresponding to a given <see cref="JailbirdWearState"/>.
/// </summary>
/// <param name="wearState">The wear state to calculate damage for.</param>
/// <returns>The amount of damage associated with the specified wear state.</returns>
public float GetDamage(JailbirdWearState wearState)
{
foreach (Keyframe keyframe in Base._deterioration._damageToWearState.keys)
{
if (Base._deterioration.FloatToState(keyframe.value) == wearState)
return keyframe.time;
}

throw new Exception("Wear state not found in damage to wear state mapping.");
}

/// <summary>
/// Gets the charge needed to reach a specific <see cref="JailbirdWearState"/>.
/// </summary>
/// <param name="wearState">The desired wear state to calculate the charge for.</param>
/// <returns>The charge value required to achieve the specified wear state.</returns>
public int GetCharge(JailbirdWearState wearState) => (int)wearState;

/// <summary>
/// Breaks the Jailbird.
/// </summary>
Expand Down
11 changes: 8 additions & 3 deletions EXILED/Exiled.Events/EventArgs/Item/ChargingJailbirdEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ChargingJailbirdEventArgs : IPlayerEvent, IItemEvent, IDeniableEven
public ChargingJailbirdEventArgs(ReferenceHub player, InventorySystem.Items.ItemBase swingItem, bool isAllowed = true)
{
Player = Player.Get(player);
Item = Item.Get(swingItem);
Jailbird = (Jailbird)Item.Get(swingItem);
#pragma warning disable CS0618
IsAllowed = isAllowed;
#pragma warning restore CS0618
Expand All @@ -39,9 +39,14 @@ public ChargingJailbirdEventArgs(ReferenceHub player, InventorySystem.Items.Item
public Player Player { get; }

/// <summary>
/// Gets the <see cref="API.Features.Items.Item"/> that is being charged. This will always be a <see cref="Jailbird"/>.
/// Gets the <see cref="API.Features.Items.Jailbird"/> that is being charged.
/// </summary>
public Item Item { get; }
public Jailbird Jailbird { get; }

/// <summary>
/// Gets the <see cref="API.Features.Items.Item"/> that is being charged.
/// </summary>
public Item Item => Jailbird;

/// <summary>
/// Gets or sets a value indicating whether or not the Jailbird can be charged.
Expand Down
9 changes: 7 additions & 2 deletions EXILED/Exiled.Events/EventArgs/Item/SwingingEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class SwingingEventArgs : IPlayerEvent, IItemEvent, IDeniableEvent
public SwingingEventArgs(ReferenceHub player, InventorySystem.Items.ItemBase swingItem, bool isAllowed = true)
{
Player = Player.Get(player);
Item = Item.Get(swingItem);
Jailbird = (Jailbird)Item.Get(swingItem);
IsAllowed = isAllowed;
}

Expand All @@ -34,10 +34,15 @@ public SwingingEventArgs(ReferenceHub player, InventorySystem.Items.ItemBase swi
/// </summary>
public Player Player { get; }

/// <summary>
/// Gets the <see cref="API.Features.Items.Jailbird"/> that is being swung.
/// </summary>
public Jailbird Jailbird { get; }

/// <summary>
/// Gets the <see cref="API.Features.Items.Item"/> that is being swung.
/// </summary>
public Item Item { get; }
public Item Item => Jailbird;

/// <summary>
/// Gets or sets a value indicating whether or not the item can be swung.
Expand Down
61 changes: 61 additions & 0 deletions EXILED/Exiled.Events/Patches/Fixes/Jailbird914CoarseFix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// -----------------------------------------------------------------------
// <copyright file="Jailbird914CoarseFix.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.Patches.Events.Player
{
#pragma warning disable IDE0060

using System.Collections.Generic;
using System.Reflection.Emit;

using API.Features;
using API.Features.Pools;

using HarmonyLib;
using InventorySystem.Items.Jailbird;
using Mirror;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="JailbirdDeteriorationTracker.Setup(JailbirdItem, JailbirdHitreg)" />.
/// Bug reported to NW (https://trello.com/c/kyr3hV9B).
/// </summary>
[HarmonyPatch(typeof(JailbirdDeteriorationTracker), nameof(JailbirdDeteriorationTracker.Setup))]
internal static class Jailbird914CoarseFix
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

int offset = -1;
int index = newInstructions.FindIndex(i => i.opcode == OpCodes.Blt_S) + offset;

List<Label> labels = newInstructions[index].ExtractLabels();

int offsetToRemove = 2;
int indexToRemove = newInstructions.FindIndex(i => i.opcode == OpCodes.Blt_S) + offsetToRemove;
int countToRemove = newInstructions.FindLastIndex(i => i.opcode == OpCodes.Blt_S) - indexToRemove + offsetToRemove;

newInstructions.RemoveRange(indexToRemove, countToRemove);

newInstructions.InsertRange(
index,
new CodeInstruction[]
{
// JailbirdDeteriorationTracker.Scp914CoarseCharges = JailbirdWearState.AlmostBroken
new CodeInstruction(OpCodes.Ldc_I4_4).WithLabels(labels),
new(OpCodes.Call, PropertySetter(typeof(JailbirdDeteriorationTracker), nameof(JailbirdDeteriorationTracker.Scp914CoarseCharges))),
});

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

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}
}
}

0 comments on commit 5a519a4

Please sign in to comment.