Decompiled source of ToolLockWhenEnemyNearby v1.1.0

plugins/ToolLockWhenEnemyNearby.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("ToolLockWhenEnemyNearby")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ToolLockWhenEnemyNearby")]
[assembly: AssemblyTitle("ToolLockWhenEnemyNearby")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
[BepInPlugin("ToolLockWhenEnemyNearby", "ToolLockWhenEnemyNearby", "1.1.0")]
public class ToolLockWhenEnemyNearby : BaseUnityPlugin
{
	public const string PluginGuid = "ToolLockWhenEnemyNearby";

	public const string PluginName = "ToolLockWhenEnemyNearby";

	public const string PluginVersion = "1.1.0";

	private const string GeneralSection = "0 - General";

	private const string HammerSection = "1 - Hammer";

	private const string HoeSection = "2 - Hoe";

	private const string CultivatorSection = "3 - Cultivator";

	internal static ConfigEntry<float> DetectionDistance = null;

	internal static ConfigEntry<bool> HammerEnabled = null;

	internal static ConfigEntry<string> HammerBlockedMessage = null;

	internal static ConfigEntry<bool> HoeEnabled = null;

	internal static ConfigEntry<string> HoeBlockedMessage = null;

	internal static ConfigEntry<bool> CultivatorEnabled = null;

	internal static ConfigEntry<string> CultivatorBlockedMessage = null;

	private static readonly List<Character> NearbyCharacters = new List<Character>();

	private void Awake()
	{
		//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
		DetectionDistance = ((BaseUnityPlugin)this).Config.Bind<float>("0 - General", "DetectionDistance", 20f, "Distance in metres at which nearby enemies will prevent tool use. Default: 20 metres.");
		HammerEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - Hammer", "Enabled", true, "If enabled, the hammer cannot be used while an enemy is nearby.");
		HammerBlockedMessage = ((BaseUnityPlugin)this).Config.Bind<string>("1 - Hammer", "BlockedMessage", "Hammer cannot be used while enemy is nearby.", "Message shown when hammer use is blocked. Default: Hammer cannot be used while enemy is nearby.");
		HoeEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Hoe", "Enabled", true, "If enabled, the hoe cannot be used while an enemy is nearby.");
		HoeBlockedMessage = ((BaseUnityPlugin)this).Config.Bind<string>("2 - Hoe", "BlockedMessage", "Hoe cannot be used while enemy is nearby.", "Message shown when hoe use is blocked. Default: Hoe cannot be used while enemy is nearby.");
		CultivatorEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("3 - Cultivator", "Enabled", true, "If enabled, the cultivator cannot be used while an enemy is nearby.");
		CultivatorBlockedMessage = ((BaseUnityPlugin)this).Config.Bind<string>("3 - Cultivator", "BlockedMessage", "Cultivator cannot be used while enemy is nearby.", "Message shown when cultivator use is blocked. Default: Cultivator cannot be used while enemy is nearby.");
		new Harmony("ToolLockWhenEnemyNearby").PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"ToolLockWhenEnemyNearby loaded (1.1.0)");
	}

	internal static bool IsToolRestricted(string itemName)
	{
		return itemName == "$item_hammer" || itemName == "$item_hoe" || itemName == "$item_cultivator";
	}

	internal static bool IsRestrictionEnabled(string itemName)
	{
		if (1 == 0)
		{
		}
		bool result = itemName switch
		{
			"$item_hammer" => HammerEnabled.Value, 
			"$item_hoe" => HoeEnabled.Value, 
			"$item_cultivator" => CultivatorEnabled.Value, 
			_ => false, 
		};
		if (1 == 0)
		{
		}
		return result;
	}

	internal static string GetBlockedMessage(string itemName)
	{
		if (1 == 0)
		{
		}
		string result = itemName switch
		{
			"$item_hammer" => string.IsNullOrWhiteSpace(HammerBlockedMessage.Value) ? "Hammer cannot be used while enemy is nearby." : HammerBlockedMessage.Value, 
			"$item_hoe" => string.IsNullOrWhiteSpace(HoeBlockedMessage.Value) ? "Hoe cannot be used while enemy is nearby." : HoeBlockedMessage.Value, 
			"$item_cultivator" => string.IsNullOrWhiteSpace(CultivatorBlockedMessage.Value) ? "Cultivator cannot be used while enemy is nearby." : CultivatorBlockedMessage.Value, 
			_ => "This tool cannot be used while enemy is nearby.", 
		};
		if (1 == 0)
		{
		}
		return result;
	}

	internal static bool IsEnemyNearby(Player player)
	{
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)player == (Object)null)
		{
			return false;
		}
		if (DetectionDistance.Value <= 0f)
		{
			return false;
		}
		NearbyCharacters.Clear();
		Character.GetCharactersInRange(((Component)player).transform.position, DetectionDistance.Value, NearbyCharacters);
		foreach (Character nearbyCharacter in NearbyCharacters)
		{
			if ((Object)(object)nearbyCharacter == (Object)null || (Object)(object)nearbyCharacter == (Object)(object)player || nearbyCharacter.IsPlayer() || nearbyCharacter.IsDead() || nearbyCharacter.IsTamed() || !BaseAI.IsEnemy((Character)(object)player, nearbyCharacter))
			{
				continue;
			}
			return true;
		}
		return false;
	}
}
[HarmonyPatch(typeof(Player), "UpdatePlacement")]
public static class Patch_Player_UpdatePlacement_ToolLockWhenEnemyNearby
{
	private static readonly FieldRef<Humanoid, ItemData> RightItemRef = AccessTools.FieldRefAccess<Humanoid, ItemData>("m_rightItem");

	private static float _nextMsgTime;

	private static bool Prefix(Player __instance)
	{
		if (!((Character)__instance).InPlaceMode())
		{
			return true;
		}
		ItemData val = RightItemRef.Invoke((Humanoid)(object)__instance);
		if (val?.m_shared == null)
		{
			return true;
		}
		string name = val.m_shared.m_name;
		if (!ToolLockWhenEnemyNearby.IsToolRestricted(name))
		{
			return true;
		}
		if (!ToolLockWhenEnemyNearby.IsRestrictionEnabled(name))
		{
			return true;
		}
		if (!ToolLockWhenEnemyNearby.IsEnemyNearby(__instance))
		{
			return true;
		}
		if (Time.time >= _nextMsgTime)
		{
			_nextMsgTime = Time.time + 1f;
			((Character)__instance).Message((MessageType)2, ToolLockWhenEnemyNearby.GetBlockedMessage(name), 0, (Sprite)null, false);
		}
		return false;
	}
}