Decompiled source of GrabSlotFix v1.0.0

GrabSlotFix.dll

Decompiled a day ago
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Unity.Netcode;
using UnityEngine;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[BepInPlugin("flamsey.grabslotfix", "Grab Slot Fix", "1.0.0")]
public class GrabSlotFix : BaseUnityPlugin
{
	internal static ManualLogSource Log;

	private static readonly FieldInfo GrabInvalidated = AccessTools.Field(typeof(PlayerControllerB), "grabInvalidated");

	private void Awake()
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		new Harmony("flamsey.grabslotfix").PatchAll(typeof(GrabSlotFix));
		Log.LogInfo((object)"Grab Slot Fix loaded.");
	}

	[HarmonyPatch(typeof(PlayerControllerB), "SwitchToItemSlot")]
	[HarmonyPrefix]
	private static void FixSlot(PlayerControllerB __instance, ref int slot, GrabbableObject fillSlotWithItem)
	{
		GrabbableObject[] itemSlots = __instance.ItemSlots;
		if (itemSlots == null || slot == 50 || (slot >= 0 && slot < itemSlots.Length) || (Object)(object)fillSlotWithItem == (Object)null)
		{
			return;
		}
		for (int i = 0; i < itemSlots.Length; i++)
		{
			if ((Object)(object)itemSlots[i] == (Object)(object)fillSlotWithItem)
			{
				Log.LogWarning((object)("Item '" + ((Object)fillSlotWithItem).name + "' was already in slot " + i + " for " + __instance.playerUsername + "; using that slot instead of " + slot + "."));
				slot = i;
				break;
			}
		}
	}

	[HarmonyPatch(typeof(PlayerControllerB), "GrabObjectClientRpc")]
	[HarmonyFinalizer]
	private static Exception ReleaseStuckGrab(PlayerControllerB __instance, Exception __exception)
	{
		if (__exception == null)
		{
			return null;
		}
		Log.LogWarning((object)("A grab failed for " + __instance.playerUsername + " (" + __exception.GetType().Name + ": " + __exception.Message + ")."));
		try
		{
			if (((NetworkBehaviour)__instance).IsOwner && __instance.isGrabbingObjectAnimation)
			{
				GrabInvalidated.SetValue(__instance, true);
				Log.LogWarning((object)"Cancelled the grab so the player isn't left stuck.");
			}
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Couldn't cancel the failed grab: " + ex));
		}
		return null;
	}
}