Decompiled source of HandsNotFull v1.1.0

OneHandNeeded.dll

Decompiled 3 weeks ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("OneHandNeeded")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("OneHandNeeded")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("bae26c84-1626-4982-b1ae-21a30ffed679")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace OneHandNeeded;

[BepInPlugin("Swaggies.OneHandNeeded", "OneHandNeeded", "1.1.0")]
public class Plugin : BaseUnityPlugin
{
	private const string _guid = "Swaggies.OneHandNeeded";

	private const string _name = "OneHandNeeded";

	private const string _ver = "1.1.0";

	private readonly Harmony harmony = new Harmony("Swaggies.OneHandNeeded");

	private static Plugin Instance;

	private static ManualLogSource loggimino;

	private static ConfigEntry<int> MaxTwoHandeds;

	private void Awake()
	{
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Expected O, but got Unknown
		if ((Object)(object)Instance == (Object)null)
		{
			Instance = this;
		}
		loggimino = Logger.CreateLogSource("Swaggies.OneHandNeeded");
		harmony.PatchAll(typeof(Plugin));
		loggimino.LogInfo((object)"OneHandNeeded up and running.");
		MaxTwoHandeds = ((BaseUnityPlugin)this).Config.Bind<int>("General", "Max Two-Handed Items", 0, new ConfigDescription("Max number of two-handed items a player can hold before \"Hands Full\" is triggered. Set to 0 for no limit.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 10), Array.Empty<object>()));
	}

	[HarmonyPatch(typeof(PlayerControllerB), "BeginGrabObject")]
	[HarmonyPostfix]
	private static void SetPlayerTwoHandedCheck1(ref PlayerControllerB __instance)
	{
		CancelTwoHanded(__instance);
	}

	[HarmonyPatch(typeof(PlayerControllerB), "SwitchToItemSlot")]
	[HarmonyPostfix]
	private static void SetPlayerTwoHandedCheck2(ref PlayerControllerB __instance)
	{
		CancelTwoHanded(__instance);
	}

	[HarmonyPatch(typeof(PlayerControllerB), "GrabObjectClientRpc")]
	[HarmonyPostfix]
	private static void SetPlayerTwoHandedCheck3(ref PlayerControllerB __instance)
	{
		CancelTwoHanded(__instance);
	}

	private static void CancelTwoHanded(PlayerControllerB player)
	{
		if (MaxTwoHandeds.Value == 0)
		{
			player.twoHanded = false;
			if (((NetworkBehaviour)player).IsOwner)
			{
				((Behaviour)HUDManager.Instance.holdingTwoHandedItem).enabled = false;
			}
		}
		else
		{
			EnemyAI val = default(EnemyAI);
			if (!player.twoHanded || ((Object)(object)player.currentlyHeldObjectServer != (Object)null && ((Component)player.currentlyHeldObjectServer).TryGetComponent<EnemyAI>(ref val) && !val.isEnemyDead))
			{
				return;
			}
			int num = 0;
			GrabbableObject[] itemSlots = player.ItemSlots;
			foreach (GrabbableObject val2 in itemSlots)
			{
				if (!((Object)(object)val2 == (Object)null) && val2.itemProperties.twoHanded)
				{
					num++;
					if (num >= MaxTwoHandeds.Value)
					{
						return;
					}
				}
			}
			player.twoHanded = false;
			if (((NetworkBehaviour)player).IsOwner)
			{
				((Behaviour)HUDManager.Instance.holdingTwoHandedItem).enabled = false;
			}
		}
	}
}