Decompiled source of GatherTweaks v1.1.0

plugins/GatherTweaks/GatherTweaks.dll

Decompiled 14 hours ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace GatherTweaks;

public enum SlowdownMode
{
	Off,
	Picking,
	AllActions
}
[BepInPlugin("felih.GatherTweaks", "Gather Tweaks", "1.1.0")]
public class Plugin : BaseUnityPlugin
{
	public const string GUID = "felih.GatherTweaks";

	public const string NAME = "Gather Tweaks";

	public const string VERSION = "1.1.0";

	internal static ManualLogSource Log;

	internal static ConfigEntry<SlowdownMode> NoSlowdown;

	internal static ConfigEntry<bool> DirectToInventory;

	internal static ConfigEntry<bool> TreeFallsWhereYouLook;

	internal static ConfigEntry<float> TreeFallPush;

	internal static ConfigEntry<bool> DetailedLog;

	private void Awake()
	{
		//IL_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Expected O, but got Unknown
		//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		NoSlowdown = ((BaseUnityPlugin)this).Config.Bind<SlowdownMode>("1 - Movement", "NoSlowdown", SlowdownMode.Picking, "Removes the walk-speed slowdown of short actions. Picking = only when picking berries, mushrooms, stones and the like. AllActions = also when equipping and other short actions. Off = vanilla.");
		DirectToInventory = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Picking", "DirectToInventory", true, "What you pick goes straight into your inventory. If it doesn't fit, it drops on the ground as usual.");
		TreeFallsWhereYouLook = ((BaseUnityPlugin)this).Config.Bind<bool>("3 - Trees", "FallWhereYouLook", true, "A tree you cut down falls in the direction your camera is facing.");
		TreeFallPush = ((BaseUnityPlugin)this).Config.Bind<float>("3 - Trees", "FallPush", 3f, new ConfigDescription("How hard the falling tree is pushed (vanilla is 1). Higher makes the direction more reliable.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 6f), new object[0]));
		DetailedLog = ((BaseUnityPlugin)this).Config.Bind<bool>("9 - Debug", "DetailedLog", false, "Writes details to the BepInEx log.");
		new Harmony("felih.GatherTweaks").PatchAll(typeof(Plugin).Assembly);
		Log.LogInfo((object)"Gather Tweaks 1.1.0 loaded");
	}

	private void Update()
	{
		Picking.Update();
	}

	internal static void Debug(string msg)
	{
		if (DetailedLog.Value)
		{
			Log.LogInfo((object)msg);
		}
	}
}
[HarmonyPatch]
internal static class Picking
{
	private const float Window = 2.5f;

	private const float Radius = 3f;

	internal static float LastPickTime = -100f;

	internal static Vector3 _lastPickPos;

	private static readonly List<ItemDrop> Pending = new List<ItemDrop>();

	private static readonly MethodInfo RequestOwn = AccessTools.Method(typeof(ItemDrop), "RequestOwn", (Type[])null, (Type[])null);

	private static bool _myPick;

	[HarmonyPatch(typeof(Pickable), "Interact")]
	[HarmonyPostfix]
	private static void Interacted(Pickable __instance, Humanoid character, bool __result)
	{
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		if (__result && !((Object)(object)character == (Object)null) && !((Object)(object)character != (Object)(object)Player.m_localPlayer))
		{
			LastPickTime = Time.time;
			_lastPickPos = ((Component)__instance).transform.position;
			Plugin.Debug("[pick] " + ((Object)__instance).name);
		}
	}

	[HarmonyPatch(typeof(Pickable), "RPC_Pick")]
	[HarmonyPrefix]
	private static void PickStart(long sender)
	{
		_myPick = Plugin.DirectToInventory.Value && (Object)(object)Player.m_localPlayer != (Object)null && sender == ZNet.GetUID();
	}

	[HarmonyPatch(typeof(Pickable), "RPC_Pick")]
	[HarmonyFinalizer]
	private static void PickEnd()
	{
		_myPick = false;
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(Pickable), "Drop")]
	private static bool Drop(GameObject prefab, int stack)
	{
		if (!_myPick || (Object)(object)prefab == (Object)null || stack <= 0)
		{
			return true;
		}
		Player localPlayer = Player.m_localPlayer;
		Inventory val = (((Object)(object)localPlayer != (Object)null) ? ((Humanoid)localPlayer).GetInventory() : null);
		if (val == null || !val.CanAddItem(prefab, stack))
		{
			Plugin.Debug("[pick] inventory full, dropping " + ((Object)prefab).name + " x" + stack);
			return true;
		}
		val.AddItem(prefab, stack);
		ItemDrop component = prefab.GetComponent<ItemDrop>();
		if ((Object)(object)component != (Object)null)
		{
			((Character)localPlayer).Message((MessageType)1, "$msg_added " + component.m_itemData.m_shared.m_name, stack, component.m_itemData.GetIcon(), false);
		}
		Plugin.Debug("[pick] to inventory (direct): " + ((Object)prefab).name + " x" + stack);
		return false;
	}

	[HarmonyPatch(typeof(ItemDrop), "Awake")]
	[HarmonyPostfix]
	private static void Spawned(ItemDrop __instance)
	{
		if (!Extractors.Capture(__instance) && Plugin.DirectToInventory.Value && !(Time.time - LastPickTime > 2.5f))
		{
			Pending.Add(__instance);
			Plugin.Debug("[pick] drop seen: " + ((Object)__instance).name);
		}
	}

	internal static void Update()
	{
		//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
		if (Pending.Count == 0)
		{
			return;
		}
		Player localPlayer = Player.m_localPlayer;
		bool flag = Time.time - LastPickTime > 2.5f;
		for (int num = Pending.Count - 1; num >= 0; num--)
		{
			ItemDrop val = Pending[num];
			if ((Object)(object)val == (Object)null || (Object)(object)localPlayer == (Object)null)
			{
				Pending.RemoveAt(num);
			}
			else if (flag)
			{
				ZNetView component = ((Component)val).GetComponent<ZNetView>();
				Plugin.Debug("[pick] gave up on " + ((Object)val).name + " (owner " + ((Object)(object)component != (Object)null && component.IsValid() && component.IsOwner()) + ")");
				Pending.RemoveAt(num);
			}
			else if (Vector3.Distance(((Component)val).transform.position, _lastPickPos) > 3f)
			{
				Plugin.Debug("[pick] ignored drop too far: " + ((Object)val).name);
				Pending.RemoveAt(num);
			}
			else
			{
				ZNetView component2 = ((Component)val).GetComponent<ZNetView>();
				if ((Object)(object)component2 != (Object)null && component2.IsValid() && !component2.IsOwner())
				{
					RequestOwn.Invoke(val, null);
				}
				else
				{
					string text = ((val.m_itemData != null) ? val.m_itemData.m_shared.m_name : ((Object)val).name);
					if (((Humanoid)localPlayer).Pickup(((Component)val).gameObject, false, false))
					{
						Plugin.Debug("[pick] to inventory: " + text);
						Pending.RemoveAt(num);
					}
				}
			}
		}
	}
}
[HarmonyPatch]
internal static class Extractors
{
	private static bool _capturing;

	private static readonly List<ItemDrop> Caught = new List<ItemDrop>();

	internal static bool Capture(ItemDrop d)
	{
		if (!_capturing)
		{
			return false;
		}
		Caught.Add(d);
		return true;
	}

	private static void Start(long caller, Component source)
	{
		Caught.Clear();
		_capturing = Plugin.DirectToInventory.Value && (Object)(object)Player.m_localPlayer != (Object)null && caller == ZNet.GetUID();
	}

	private static void Finish(string what)
	{
		if (!_capturing)
		{
			return;
		}
		_capturing = false;
		Player localPlayer = Player.m_localPlayer;
		foreach (ItemDrop item in Caught)
		{
			if (!((Object)(object)item == (Object)null) && !((Object)(object)localPlayer == (Object)null))
			{
				string text = item.m_itemData.m_shared.m_name + " x" + item.m_itemData.m_stack;
				if (((Humanoid)localPlayer).Pickup(((Component)item).gameObject, false, false))
				{
					Plugin.Debug("[" + what + "] to inventory (direct): " + text);
				}
				else
				{
					Plugin.Debug("[" + what + "] didn't fit, left on the ground: " + text);
				}
			}
		}
		Caught.Clear();
	}

	private static void Interacted(Component c, Humanoid user)
	{
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)user == (Object)null) && !((Object)(object)user != (Object)(object)Player.m_localPlayer) && Plugin.DirectToInventory.Value)
		{
			Picking.LastPickTime = Time.time;
			Picking._lastPickPos = c.transform.position;
		}
	}

	[HarmonyPatch(typeof(Beehive), "Interact")]
	[HarmonyPostfix]
	private static void HiveUsed(Beehive __instance, Humanoid character)
	{
		Interacted((Component)(object)__instance, character);
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(Beehive), "RPC_Extract")]
	private static void HiveStart(Beehive __instance, long caller)
	{
		Start(caller, (Component)(object)__instance);
	}

	[HarmonyFinalizer]
	[HarmonyPatch(typeof(Beehive), "RPC_Extract")]
	private static void HiveEnd()
	{
		Finish("beehive");
	}

	[HarmonyPatch(typeof(SapCollector), "Interact")]
	[HarmonyPostfix]
	private static void SapUsed(SapCollector __instance, Humanoid character)
	{
		Interacted((Component)(object)__instance, character);
	}

	[HarmonyPatch(typeof(SapCollector), "RPC_Extract")]
	[HarmonyPrefix]
	private static void SapStart(SapCollector __instance, long caller)
	{
		Start(caller, (Component)(object)__instance);
	}

	[HarmonyFinalizer]
	[HarmonyPatch(typeof(SapCollector), "RPC_Extract")]
	private static void SapEnd()
	{
		Finish("sap");
	}
}
[HarmonyPatch]
internal static class Ownership
{
	private static void Take(ZNetView nview, Humanoid user, string what)
	{
		if (Plugin.DirectToInventory.Value && !((Object)(object)user == (Object)null) && !((Object)(object)user != (Object)(object)Player.m_localPlayer) && !((Object)(object)nview == (Object)null) && nview.IsValid() && !nview.IsOwner())
		{
			nview.ClaimOwnership();
			Plugin.Debug("[" + what + "] took it over from another player's game, so it goes straight to you");
		}
	}

	[HarmonyPatch(typeof(Pickable), "Interact")]
	[HarmonyPriority(600)]
	[HarmonyPrefix]
	private static void Pick(Humanoid character, ZNetView ___m_nview)
	{
		Take(___m_nview, character, "pick");
	}

	[HarmonyPriority(600)]
	[HarmonyPrefix]
	[HarmonyPatch(typeof(Beehive), "Interact")]
	private static void Hive(Humanoid character, ZNetView ___m_nview)
	{
		Take(___m_nview, character, "beehive");
	}

	[HarmonyPriority(600)]
	[HarmonyPatch(typeof(SapCollector), "Interact")]
	[HarmonyPrefix]
	private static void Sap(Humanoid character, ZNetView ___m_nview)
	{
		Take(___m_nview, character, "sap");
	}
}
[HarmonyPatch]
internal static class Slowdown
{
	[HarmonyPatch(typeof(Player), "InMinorActionSlowdown")]
	[HarmonyPrefix]
	private static bool NoSlow(Player __instance, ref bool __result)
	{
		SlowdownMode value = Plugin.NoSlowdown.Value;
		if (value == SlowdownMode.Off || (Object)(object)__instance != (Object)(object)Player.m_localPlayer)
		{
			return true;
		}
		if (value == SlowdownMode.Picking && Time.time - Picking.LastPickTime > 1.5f)
		{
			return true;
		}
		__result = false;
		return false;
	}
}
[HarmonyPatch]
internal static class Trees
{
	[HarmonyPatch(typeof(TreeBase), "Damage")]
	[HarmonyPrefix]
	private static void Aim(HitData hit)
	{
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		//IL_007f: Unknown result type (might be due to invalid IL or missing references)
		if (Plugin.TreeFallsWhereYouLook.Value && hit != null && !((Object)(object)Player.m_localPlayer == (Object)null) && !((Object)(object)GameCamera.instance == (Object)null) && !((Object)(object)hit.GetAttacker() != (Object)(object)Player.m_localPlayer))
		{
			Vector3 forward = ((Component)GameCamera.instance).transform.forward;
			forward.y = 0f;
			if (!(((Vector3)(ref forward)).sqrMagnitude < 0.01f))
			{
				hit.m_dir = ((Vector3)(ref forward)).normalized * Plugin.TreeFallPush.Value;
			}
		}
	}
}