Decompiled source of BetterTames v0.0.2

plugins/BetterTames.dll

Decompiled 3 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BetterTames.Patches;
using BetterTames.Utils;
using HarmonyLib;
using JetBrains.Annotations;
using Microsoft.CodeAnalysis;
using ServerSync;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Koro.BetterTames")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("TeamKoro")]
[assembly: AssemblyProduct("Koro.BetterTames")]
[assembly: AssemblyCopyright("TeamKoro")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("c7a9c4aa-0b32-47f7-a590-a68e94a2511e")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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;
		}
	}
}
namespace BetterTames
{
	public enum DebugFeature
	{
		None,
		MakeCommandable,
		TeleportFollow,
		PetProtection,
		Initialization,
		TamingAndFeeder
	}
	[BepInPlugin("Koro.bettertames", "BetterTames", "0.0.2")]
	public class BetterTamesPlugin : BaseUnityPlugin
	{
		public readonly struct Peer
		{
			private readonly ZNetPeer _peer;

			public long m_uid => _peer?.m_uid ?? 0;

			public Vector3 m_refPos => (Vector3)(((??)_peer?.m_refPos) ?? default(Vector3));

			public ZDOID m_characterID => (ZDOID)(((??)_peer?.m_characterID) ?? default(ZDOID));

			public bool IsConnected
			{
				get
				{
					ZNetPeer peer = _peer;
					bool? obj;
					if (peer == null)
					{
						obj = null;
					}
					else
					{
						ISocket socket = peer.m_socket;
						obj = ((socket != null) ? new bool?(socket.IsConnected()) : null);
					}
					bool? flag = obj;
					return flag.GetValueOrDefault();
				}
			}

			public bool IsDefault => _peer == null;

			public Peer(ZNetPeer peer)
			{
				_peer = peer;
			}

			public string GetHostName()
			{
				ZNetPeer peer = _peer;
				object obj;
				if (peer == null)
				{
					obj = null;
				}
				else
				{
					ISocket socket = peer.m_socket;
					obj = ((socket != null) ? socket.GetHostName() : null);
				}
				if (obj == null)
				{
					obj = "";
				}
				return (string)obj;
			}

			public override bool Equals(object obj)
			{
				if (_peer != null)
				{
					return ((object)_peer).Equals(obj);
				}
				return obj == null;
			}

			public override int GetHashCode()
			{
				return ((object)_peer)?.GetHashCode() ?? 0;
			}
		}

		public const string PluginId = "Koro.bettertames";

		public const string PluginName = "BetterTames";

		public const string PluginVersion = "0.0.2";

		public static ServerSync.ConfigSync _configSync;

		private Harmony _harmony;

		public const string RPC_REQUEST_PET_PROTECTION = "BT_RequestPetProtection";

		public const string RPC_PET_PROTECTION_SYNC = "BT_PetProtectionSync";

		public const string RPC_TELEPORT_SYNC = "BT_TeleportSync";

		private static bool _corePatchesAppliedSession;

		private Coroutine _autofeederCoroutine;

		public static ConfigSync ConfigInstance { get; private set; }

		public static BetterTamesPlugin Instance { get; private set; }

		public static void LogIfDebug(string message, DebugFeature feature = DebugFeature.Initialization)
		{
			if (ConfigInstance != null)
			{
				bool flag = false;
				if (feature switch
				{
					DebugFeature.MakeCommandable => ConfigInstance.Tames.DebugMakeCommandable.Value, 
					DebugFeature.TeleportFollow => ConfigInstance.Tames.DebugTeleportFollow.Value, 
					DebugFeature.PetProtection => ConfigInstance.Tames.DebugPetProtection.Value, 
					DebugFeature.Initialization => false, 
					_ => false, 
				})
				{
					((BaseUnityPlugin)Instance).Logger.LogWarning((object)$"[{feature}] {message}");
				}
			}
		}

		private void Awake()
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Expected O, but got Unknown
			Instance = this;
			_configSync = new ServerSync.ConfigSync("Koro.bettertames")
			{
				DisplayName = "BetterTames",
				CurrentVersion = "0.0.2",
				MinimumRequiredVersion = "0.0.2",
				ModRequired = true
			};
			ConfigInstance = new ConfigSync(this);
			LogIfDebug("AWAKE: ConfigInstance und _configSync initialisiert.");
			_harmony = new Harmony("Koro.bettertames");
			try
			{
				LogIfDebug("Applying Initialization and Pet Protection patches...");
				_harmony.PatchAll(typeof(PetProtectionPatch));
				_harmony.PatchAll(typeof(InitializationPatches));
				LogIfDebug("AWAKE: InitializationPatches and Pet Protection applied.");
			}
			catch (Exception arg)
			{
				LogIfDebug($"AWAKE: CRITICAL ERROR applying initial patches: {arg}");
			}
		}

		public static void OnZNetReady()
		{
			LogIfDebug("OnZNetReady: ZNet is ready.");
			try
			{
				if (ZRoutedRpc.instance == null)
				{
					LogIfDebug("ZRoutedRpc.instance is still null in OnZNetReady!");
					LogIfDebug("ZRoutedRpc.instance is null in OnZNetReady. Cannot register RPCs.");
				}
				else
				{
					TryRegisterRpc<string, ZPackage>("BT_PetProtectionSync", Instance.RPC_PetProtectionSync_Client_Router);
					TryRegisterRpc<string, ZPackage>("BT_TeleportSync", Instance.RPC_TeleportSync_Client_Router);
					LogIfDebug("Spiel-spezifische RPCs registration attempted in OnZNetReady.");
				}
			}
			catch (Exception ex)
			{
				LogIfDebug("Exception during game-specific RPC registration: " + ex.Message + "\n" + ex.StackTrace);
			}
		}

		private static void TryRegisterRpc<T1, T2>(string name, Action<long, T1, T2> action)
		{
			try
			{
				ZRoutedRpc.instance.Register<T1, T2>(name, action);
				LogIfDebug("Spiel-RPC '" + name + "' registriert.");
			}
			catch (Exception ex)
			{
				LogIfDebug("Exception during game-specific RPC registration for '" + name + "': " + ex.Message + "\n" + ex.StackTrace);
			}
		}

		private static void TryRegisterRpc<T>(string name, Action<long, T> action)
		{
			try
			{
				ZRoutedRpc.instance.Register<T>(name, action);
				LogIfDebug("Spiel-RPC '" + name + "' registriert.");
			}
			catch (Exception ex)
			{
				LogIfDebug("Exception during game-specific RPC registration for '" + name + "': " + ex.Message + "\n" + ex.StackTrace);
			}
		}

		private void RPC_PetProtectionSync_Client_Router(long sender, string zdoID_str, ZPackage pkg)
		{
			RPC_PetProtectionSync_Client(sender, zdoID_str, pkg);
		}

		private void RPC_TeleportSync_Client_Router(long sender, string zdoID_str, ZPackage pkg)
		{
			RPC_TeleportSync_Client(sender, zdoID_str, pkg);
		}

		private void RPC_RequestPetProtection_Server_Router(long senderPeerId, string zdoID_str_fromClient)
		{
			RPC_RequestPetProtection_Server(senderPeerId, zdoID_str_fromClient);
		}

		public static void OnLocalPlayerReady()
		{
			LogIfDebug("OnLocalPlayerReady: Local player is set.");
			if (!_corePatchesAppliedSession)
			{
				ApplyCorePatches();
				_corePatchesAppliedSession = true;
			}
		}

		public static void ApplyCorePatches()
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			if (Instance._harmony == null)
			{
				Instance._harmony = new Harmony("Koro.bettertames");
			}
			try
			{
				LogIfDebug("Applying all BetterTames patches from assembly...");
				Instance._harmony.PatchAll(typeof(MakeCommandablePatch));
				Instance._harmony.PatchAll(typeof(TeleportFollowPatch));
				Instance._harmony.PatchAll(typeof(AnimationHelper));
				Instance._harmony.PatchAll(typeof(Billboard));
				Instance._harmony.PatchAll(typeof(MonsterAI_StunBehaviorPatches));
				Instance._harmony.PatchAll(typeof(TameAndFeed));
				LogIfDebug("All BetterTames patches applied successfully.");
			}
			catch (Exception arg)
			{
				LogIfDebug($"Exception during core patching: {arg}");
			}
		}

		private void RPC_RequestPetProtection_Server(long senderPeerId, string zdoID_str_fromClient)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (!ZNet.instance.IsServer())
				{
					return;
				}
				ZDOID val = ParseZDOID(zdoID_str_fromClient);
				if (((ZDOID)(ref val)).IsNone())
				{
					return;
				}
				ZDO zDO = ZDOMan.instance.GetZDO(val);
				if (zDO == null || !zDO.IsValid())
				{
					return;
				}
				if (zDO.GetOwner() != ZNet.GetUID())
				{
					LogIfDebug($"SERVER: ZDO {val} not owned by server. Current owner: {zDO.GetOwner()}. Taking ownership.", DebugFeature.PetProtection);
					zDO.SetOwner(ZNet.GetUID());
					if (zDO.GetOwner() != ZNet.GetUID())
					{
						LogIfDebug($"SERVER: Failed to take ownership for ZDO {val}. Cannot proceed with PetProtection logic for this ZDO from sender {senderPeerId}.");
						return;
					}
				}
				ZNetView val2 = ZNetScene.instance.FindInstance(zDO);
				if (!((Object)(object)val2 == (Object)null))
				{
					Character component = ((Component)val2).gameObject.GetComponent<Character>();
					if (!((Object)(object)component == (Object)null))
					{
						PetProtectionPatch.ApplyPetProtectionLogic(component, val2, zDO);
					}
				}
			}
			catch (Exception ex)
			{
				LogIfDebug("Exception in RPC_RequestPetProtection_Server: " + ex.Message + "\n" + ex.StackTrace);
			}
		}

		private void RPC_TeleportSync_Client(long sender, string zdoID_str, ZPackage pkg)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: 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)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: 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_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if ((Object)(object)ZNet.instance == (Object)null || ZNet.instance.IsServer())
				{
					return;
				}
				ZDOID val = ParseZDOID(zdoID_str);
				if (((ZDOID)(ref val)).IsNone())
				{
					return;
				}
				ZDO zDO = ZDOMan.instance.GetZDO(val);
				if (zDO == null || !zDO.IsValid())
				{
					LogIfDebug($"TeleportSync_Client: No valid ZDO for '{val}'", DebugFeature.TeleportFollow);
					return;
				}
				Vector3 position = pkg.ReadVector3();
				Quaternion rotation = pkg.ReadQuaternion();
				ZNetView val2 = ZNetScene.instance.FindInstance(zDO);
				if (!((Object)(object)val2 != (Object)null))
				{
					return;
				}
				GameObject gameObject = ((Component)val2).gameObject;
				Character component = gameObject.GetComponent<Character>();
				if ((Object)(object)component != (Object)null && !component.IsTeleporting())
				{
					gameObject.transform.position = position;
					gameObject.transform.rotation = rotation;
					Rigidbody component2 = gameObject.GetComponent<Rigidbody>();
					if ((Object)(object)component2 != (Object)null)
					{
						component2.WakeUp();
					}
				}
			}
			catch (Exception ex)
			{
				LogIfDebug("Exception in RPC_TeleportSync_Client: " + ex.Message + "\n" + ex.StackTrace, DebugFeature.TeleportFollow);
			}
		}

		private void RPC_PetProtectionSync_Client(long sender, string zdoID_str, ZPackage pkg)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if ((Object)(object)ZNet.instance == (Object)null || ZNet.instance.IsServer())
				{
					return;
				}
				ZDOID val = ParseZDOID(zdoID_str);
				if (((ZDOID)(ref val)).IsNone())
				{
					LogIfDebug("PetProtectionSync_Client: Parsed ZDOID is None for '" + zdoID_str + "'.", DebugFeature.PetProtection);
					return;
				}
				ZDO zDO = ZDOMan.instance.GetZDO(val);
				if (zDO == null || !zDO.IsValid())
				{
					LogIfDebug($"PetProtectionSync_Client: No valid ZDO for '{val}'", DebugFeature.PetProtection);
					return;
				}
				pkg.ReadBool();
				pkg.ReadSingle();
				bool flag = pkg.ReadBool();
				pkg.ReadSingle();
				float speed = pkg.ReadSingle();
				float walkSpeed = pkg.ReadSingle();
				float runSpeed = pkg.ReadSingle();
				float swimSpeed = pkg.ReadSingle();
				string text = pkg.ReadString();
				ZNetView val2 = ZNetScene.instance.FindInstance(zDO);
				if (!((Object)(object)val2 != (Object)null))
				{
					return;
				}
				Character component = ((Component)val2).gameObject.GetComponent<Character>();
				if (!((Object)(object)component != (Object)null))
				{
					return;
				}
				ZSyncAnimation component2 = ((Component)component).GetComponent<ZSyncAnimation>();
				if ((Object)(object)component2 != (Object)null)
				{
					Animator componentInChildren = ((Component)component2).GetComponentInChildren<Animator>();
					component2.SetSpeed(1f);
					if (flag)
					{
						if (!string.IsNullOrEmpty(text) && (Object)(object)componentInChildren != (Object)null)
						{
							component2.SetTrigger(text);
						}
						else if ((Object)(object)componentInChildren != (Object)null && !PetProtectionPatch.IsAnimatorPlayingBool(componentInChildren, "sleeping", expectedValue: true))
						{
							component2.SetBool("sleeping", true);
						}
					}
					else if ((Object)(object)componentInChildren != (Object)null && PetProtectionPatch.IsAnimatorPlayingBool(componentInChildren, "sleeping", expectedValue: true))
					{
						component2.SetBool("sleeping", false);
					}
				}
				component.m_speed = speed;
				component.m_walkSpeed = walkSpeed;
				component.m_runSpeed = runSpeed;
				component.m_swimSpeed = swimSpeed;
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Exception in RPC_PetProtectionSync_Client: " + ex.Message + "\n" + ex.StackTrace));
			}
		}

		private static ZDOID ParseZDOID(string zdoID_str)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: 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)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			if (string.IsNullOrEmpty(zdoID_str))
			{
				return ZDOID.None;
			}
			string[] array = zdoID_str.Split(new char[1] { ':' });
			if (array.Length != 2)
			{
				return ZDOID.None;
			}
			if (!long.TryParse(array[0], out var result))
			{
				return ZDOID.None;
			}
			if (!uint.TryParse(array[1], out var result2))
			{
				return ZDOID.None;
			}
			return new ZDOID(result, result2);
		}

		private void OnDestroy()
		{
			LogIfDebug("BetterTamesPlugin.OnDestroy() called.");
			if (_autofeederCoroutine != null)
			{
				((MonoBehaviour)this).StopCoroutine(_autofeederCoroutine);
				_autofeederCoroutine = null;
				LogIfDebug("Autofeeder coroutine stopped.", DebugFeature.TamingAndFeeder);
			}
			if (_harmony != null)
			{
				_harmony.UnpatchSelf();
				LogIfDebug("Harmony patches unapplied.");
			}
			ConfigInstance = null;
			_configSync = null;
			Instance = null;
			LogIfDebug("BetterTamesPlugin.OnDestroy() completed.");
		}

		public static void ExecutePetTeleportAsOwner(Character petCharacter, Vector3 playerDestinationPos, Quaternion playerDestinationRot)
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_0389: Unknown result type (might be due to invalid IL or missing references)
			//IL_038a: Unknown result type (might be due to invalid IL or missing references)
			//IL_038f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0392: Unknown result type (might be due to invalid IL or missing references)
			//IL_039a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0350: Unknown result type (might be due to invalid IL or missing references)
			//IL_0351: Unknown result type (might be due to invalid IL or missing references)
			//IL_0359: Unknown result type (might be due to invalid IL or missing references)
			//IL_035e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0363: Unknown result type (might be due to invalid IL or missing references)
			//IL_036a: Unknown result type (might be due to invalid IL or missing references)
			//IL_036f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0374: Unknown result type (might be due to invalid IL or missing references)
			//IL_0378: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Unknown result type (might be due to invalid IL or missing references)
			//IL_0108: Unknown result type (might be due to invalid IL or missing references)
			//IL_010d: Unknown result type (might be due to invalid IL or missing references)
			//IL_010f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_014c: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ef: Expected O, but got Unknown
			//IL_03f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_03fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_045c: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_03de: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Unknown result type (might be due to invalid IL or missing references)
			//IL_0189: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_02df: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_030a: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_020b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0226: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)petCharacter == (Object)null)
			{
				return;
			}
			ZNetView component = ((Component)petCharacter).GetComponent<ZNetView>();
			if ((Object)(object)component == (Object)null || !component.IsValid() || !component.IsOwner())
			{
				return;
			}
			ZDO zDO = component.GetZDO();
			if (zDO == null)
			{
				return;
			}
			float petRadius = GetPetRadius(petCharacter);
			Vector3 val = playerDestinationRot * Vector3.forward;
			Vector3 val2 = playerDestinationRot * Vector3.right;
			float num = Mathf.Max(2.5f, petRadius + 1.5f);
			float num2 = num + 3f;
			float num3 = Mathf.Max(2f, petRadius * 2.5f);
			float num4 = Random.Range(num, num2);
			float num5 = Random.Range((0f - num3) / 2f, num3 / 2f);
			Vector3 val3 = -val * num4;
			Vector3 val4 = val2 * num5;
			Vector3 val5 = val3 + val4;
			Vector3 val6 = Vector3.zero;
			bool flag = false;
			int num6 = 0;
			Vector3 val10 = default(Vector3);
			float num7 = default(float);
			float num8 = default(float);
			RaycastHit val11 = default(RaycastHit);
			while (!flag && num6 < 5)
			{
				num6++;
				Vector2 val7 = Random.insideUnitCircle * 0.75f;
				Vector3 val8 = val5 + new Vector3(val7.x, 0f, val7.y);
				Vector3 val9 = playerDestinationPos + val8;
				((Vector3)(ref val10))..ctor(val9.x, playerDestinationPos.y + 1f, val9.z);
				if ((Object)(object)ZoneSystem.instance != (Object)null && ZoneSystem.instance.FindFloor(val10, ref num7))
				{
					((Vector3)(ref val6))..ctor(val10.x, num7 + 0.1f, val10.z);
					flag = true;
					LogIfDebug($"Attempt {num6}: Height found for {petCharacter.m_name} using ZoneSystem.FindFloor: {val6.y}", DebugFeature.TeleportFollow);
				}
				if (!flag)
				{
					LogIfDebug($"Attempt {num6}: ZoneSystem.FindFloor failed for {petCharacter.m_name}. Trying GetSolidHeight.", DebugFeature.TeleportFollow);
					if ((Object)(object)ZoneSystem.instance != (Object)null && ZoneSystem.instance.GetSolidHeight(new Vector3(val10.x, playerDestinationPos.y, val10.z), ref num8, 10))
					{
						((Vector3)(ref val6))..ctor(val10.x, num8 + 1f, val10.z);
						flag = true;
						LogIfDebug($"Attempt {num6}: Height found using GetSolidHeight: {val6.y}", DebugFeature.TeleportFollow);
					}
				}
				if (!flag)
				{
					LogIfDebug($"Attempt {num6}: GetSolidHeight failed. Using Raycast fallback.", DebugFeature.TeleportFollow);
					int mask = LayerMask.GetMask(new string[7] { "Default", "static_solid", "Default_small", "piece", "terrain", "blocker", "vehicle" });
					if (Physics.Raycast(new Vector3(val10.x, playerDestinationPos.y + 20f, val10.z), Vector3.down, ref val11, 40f, mask))
					{
						((Vector3)(ref val6))..ctor(val10.x, ((RaycastHit)(ref val11)).point.y + 0.1f, val10.z);
						flag = true;
						LogIfDebug($"Attempt {num6}: Height found using Raycast: {val6.y}", DebugFeature.TeleportFollow);
					}
				}
				if (flag)
				{
					break;
				}
			}
			if (!flag)
			{
				LogIfDebug("All attempts failed for " + petCharacter.m_name + ". Using player Y as last resort and slightly offset.", DebugFeature.TeleportFollow);
				val6 = playerDestinationPos - val * (petRadius + 1f) + val2 * 0.5f;
				val6.y = playerDestinationPos.y + 0.1f;
			}
			Quaternion val12 = Quaternion.LookRotation(val);
			zDO.SetPosition(val6);
			zDO.SetRotation(val12);
			MonsterAI component2 = ((Component)petCharacter).GetComponent<MonsterAI>();
			if ((Object)(object)component2 != (Object)null)
			{
				((BaseAI)component2).StopMoving();
			}
			if ((Object)(object)((Component)petCharacter).GetComponent<Rigidbody>() != (Object)null)
			{
				((Component)petCharacter).GetComponent<Rigidbody>().velocity = Vector3.zero;
				((Component)petCharacter).GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
			}
			ZPackage val13 = new ZPackage();
			val13.Write(val6);
			val13.Write(val12);
			string text = $"{((ZDOID)(ref zDO.m_uid)).UserID}:{((ZDOID)(ref zDO.m_uid)).ID}";
			ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "BT_TeleportSync", new object[2] { text, val13 });
			LogIfDebug($"Teleported {petCharacter.m_name} to {val6}. RPC Sent. Attempts: {num6}", DebugFeature.TeleportFollow);
		}

		private static float GetPetRadius(Character character)
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: 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)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			float num = 0.5f;
			CapsuleCollider component = ((Component)character).GetComponent<CapsuleCollider>();
			if ((Object)(object)component != (Object)null)
			{
				num = Mathf.Max(component.radius, component.height / 2f) * Mathf.Max(((Component)character).transform.localScale.x, ((Component)character).transform.localScale.z);
			}
			else
			{
				Collider component2 = ((Component)character).GetComponent<Collider>();
				if ((Object)(object)component2 != (Object)null)
				{
					Bounds bounds = component2.bounds;
					float x = ((Bounds)(ref bounds)).extents.x;
					bounds = component2.bounds;
					float y = ((Bounds)(ref bounds)).extents.y;
					bounds = component2.bounds;
					num = Mathf.Max(x, Mathf.Max(y, ((Bounds)(ref bounds)).extents.z));
				}
			}
			return Mathf.Max(0.5f, num);
		}
	}
	[HarmonyPatch]
	internal static class InitializationPatches
	{
		private static bool _zNetReadyCalled;

		private static bool _localPlayerReadyCalled;

		public static void ResetInitializationFlags()
		{
			BetterTamesPlugin.LogIfDebug("INITPATCH: Resetting initialization flags (_zNetReadyCalled, _localPlayerReadyCalled) to false.");
			_zNetReadyCalled = false;
			_localPlayerReadyCalled = false;
		}

		[HarmonyPatch(typeof(ZNet), "Awake")]
		[HarmonyPostfix]
		private static void ZNet_Awake_Postfix(ZNet __instance)
		{
			if (_zNetReadyCalled)
			{
				BetterTamesPlugin.LogIfDebug($"INITPATCH: ZNet.Awake_Postfix CALLED (Instance ID: {((Object)__instance).GetInstanceID()}), but _zNetReadyCalled is already true. Skipping OnZNetReady call.");
				return;
			}
			if ((Object)(object)ZNet.instance != (Object)(object)__instance || (Object)(object)ZNet.instance == (Object)null)
			{
				BetterTamesPlugin.LogIfDebug($"INITPATCH: ZNet.Awake_Postfix CALLED for an instance (ID: {((Object)__instance).GetInstanceID()}) that is not the current ZNet.instance OR ZNet.instance is null. Skipping OnZNetReady call for this instance.");
				return;
			}
			BetterTamesPlugin.LogIfDebug($"INITPATCH: ZNet.Awake_Postfix CALLED (Instance ID: {((Object)__instance).GetInstanceID()}). Attempting to call OnZNetReady.");
			try
			{
				BetterTamesPlugin.LogIfDebug("[BetterTames] ZnetAwake in aktion");
				BetterTamesPlugin.OnZNetReady();
			}
			catch (Exception arg)
			{
				BetterTamesPlugin.LogIfDebug($"[BetterTames] Error in OnZNetReady from ZNet_Awake_Postfix: {arg}");
			}
			_zNetReadyCalled = true;
		}

		[HarmonyPatch(typeof(Player), "SetLocalPlayer")]
		[HarmonyPostfix]
		private static void Player_SetLocalPlayer_Postfix()
		{
			if (_localPlayerReadyCalled)
			{
				return;
			}
			if ((Object)(object)Player.m_localPlayer == (Object)null)
			{
				BetterTamesPlugin.LogIfDebug("INITPATCH: Player.SetLocalPlayer_Postfix CALLED, but Player.m_localPlayer is NULL. Skipping OnLocalPlayerReady call.");
				return;
			}
			BetterTamesPlugin.LogIfDebug("INITPATCH: Player.SetLocalPlayer_Postfix CALLED for " + Player.m_localPlayer.GetPlayerName() + ". Attempting to call OnLocalPlayerReady.");
			try
			{
				BetterTamesPlugin.LogIfDebug("[BetterTames] OnLocalPlayerReady in aktion");
				BetterTamesPlugin.OnLocalPlayerReady();
			}
			catch (Exception arg)
			{
				BetterTamesPlugin.LogIfDebug($"[BetterTames] Error in OnLocalPlayerReady from Player_SetLocalPlayer_Postfix: {arg}");
			}
			_localPlayerReadyCalled = true;
		}
	}
	public class ConfigSync
	{
		public class TamesConfig
		{
			public ConfigEntry<bool> ServerConfigLocked { get; private set; }

			public ConfigEntry<bool> DebugMakeCommandable { get; private set; }

			public ConfigEntry<bool> TeleportFollowEnabled { get; private set; }

			public ConfigEntry<float> TeleportOnDistanceMaxRange { get; private set; }

			public ConfigEntry<bool> DebugTeleportFollow { get; private set; }

			public ConfigEntry<bool> PetProtectionEnabled { get; private set; }

			public ConfigEntry<float> PetProtectionStunDuration { get; private set; }

			public ConfigEntry<float> PetProtectionHealPercentage { get; private set; }

			public ConfigEntry<string> PetProtectionStunIconItemPrefab { get; private set; }

			public ConfigEntry<float> PetProtectionStunIconScale { get; private set; }

			public ConfigEntry<float> PetProtectionStunIconHeightFactor { get; private set; }

			public ConfigEntry<bool> DebugPetProtection { get; private set; }

			public ConfigEntry<bool> ShowTamingProgressEnabled { get; private set; }

			public TamesConfig(ConfigFile cfg)
			{
				//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b0: Expected O, but got Unknown
				//IL_0133: Unknown result type (might be due to invalid IL or missing references)
				//IL_013d: Expected O, but got Unknown
				//IL_0179: Unknown result type (might be due to invalid IL or missing references)
				//IL_0183: Expected O, but got Unknown
				//IL_01ec: Unknown result type (might be due to invalid IL or missing references)
				//IL_01f6: Expected O, but got Unknown
				//IL_0232: Unknown result type (might be due to invalid IL or missing references)
				//IL_023c: Expected O, but got Unknown
				ServerConfigLocked = cfg.Bind<bool>("1. General ServerSync", "Lock Configuration", true, "If true on the server, this configuration file will be locked and synced to clients. ONLY an Admin can change this on a Server.");
				BetterTamesPlugin._configSync.AddLockingConfigEntry<bool>(ServerConfigLocked);
				string text = "2.MakeCommandable";
				DebugMakeCommandable = cfg.Bind<bool>(text, "Debug Logging", false, "Enables debug logging for this feature.");
				string text2 = "3.TeleportFollow";
				TeleportFollowEnabled = cfg.Bind<bool>(text2, "Enable", true, "Enables pets to teleport to the player if they get too far or the player uses a portal/teleports.");
				BetterTamesPlugin._configSync.AddConfigEntry<bool>(TeleportFollowEnabled);
				TeleportOnDistanceMaxRange = cfg.Bind<float>(text2, "Max Distance For AutoTeleport", 64f, new ConfigDescription("Maximum distance a pet can be from its owner before it attempts to teleport (if not in combat). Min: 20, Max: 64.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(20f, 64f), Array.Empty<object>()));
				BetterTamesPlugin._configSync.AddConfigEntry<float>(TeleportOnDistanceMaxRange);
				DebugTeleportFollow = cfg.Bind<bool>(text2, "Debug Logging", false, "Enables debug logging for teleport features.");
				string text3 = "4.PetProtection";
				PetProtectionEnabled = cfg.Bind<bool>(text3, "Enable", true, "Prevents tamed creatures from dying by knocking them out instead. They recover after a set time.");
				BetterTamesPlugin._configSync.AddConfigEntry<bool>(PetProtectionEnabled);
				PetProtectionStunDuration = cfg.Bind<float>(text3, "Stun Duration", 10f, new ConfigDescription("How long the pet stays stunned (seconds). Min: 1, Max: 300.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 300f), Array.Empty<object>()));
				BetterTamesPlugin._configSync.AddConfigEntry<float>(PetProtectionStunDuration);
				PetProtectionHealPercentage = cfg.Bind<float>(text3, "Heal After Stun Pct", 25f, new ConfigDescription("Percentage of max HP the pet recovers after stun. (0 = 1HP). Min: 0, Max: 100.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
				BetterTamesPlugin._configSync.AddConfigEntry<float>(PetProtectionHealPercentage);
				PetProtectionStunIconItemPrefab = cfg.Bind<string>(text3, "Stun Icon Item Prefab", "SledgeStagbreaker", "Prefab name of an item whose icon is used for the stun visual. Leave empty for no icon.");
				BetterTamesPlugin._configSync.AddConfigEntry<string>(PetProtectionStunIconItemPrefab);
				PetProtectionStunIconScale = cfg.Bind<float>(text3, "Stun Icon Scale", 1f, new ConfigDescription("Scale of the stun icon.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 2f), Array.Empty<object>()));
				BetterTamesPlugin._configSync.AddConfigEntry<float>(PetProtectionStunIconScale);
				PetProtectionStunIconHeightFactor = cfg.Bind<float>(text3, "Stun Icon Height Factor", 2f, new ConfigDescription("Height factor for the stun icon, relative to character's visual top (e.g., 0.9 means 90% of height).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 3f), Array.Empty<object>()));
				BetterTamesPlugin._configSync.AddConfigEntry<float>(PetProtectionStunIconHeightFactor);
				DebugPetProtection = cfg.Bind<bool>(text3, "Debug Logging", false, "Enables debug logging for this feature.");
				string text4 = "5.Taming";
				ShowTamingProgressEnabled = cfg.Bind<bool>(text4, "Show Taming Progress", true, "Shows a taming progress percentage above creatures being tamed.");
				BetterTamesPlugin._configSync.AddConfigEntry<bool>(ShowTamingProgressEnabled);
			}
		}

		public TamesConfig Tames { get; private set; }

		public ConfigSync(BetterTamesPlugin pluginInstance)
		{
			Tames = new TamesConfig(((BaseUnityPlugin)pluginInstance).Config);
		}
	}
	public static class PetTeleporterLogic
	{
		public const float PET_PLACEMENT_BUFFER = 0.15f;

		public static string GetPrefabName(Character c)
		{
			return ((c != null) ? ((Object)c).name.Replace("(Clone)", "") : null) ?? "UnknownCharacter";
		}

		public static float GetPetHeight(Character character)
		{
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)character == (Object)null)
			{
				BetterTamesPlugin.LogIfDebug("GetPetHeight: Pet nicht gefunden. Fallback auf Höhe 1f.", DebugFeature.TeleportFollow);
				return 1f;
			}
			CapsuleCollider component = ((Component)character).GetComponent<CapsuleCollider>();
			if ((Object)(object)component != (Object)null)
			{
				float num = component.height * ((Component)character).transform.localScale.y;
				BetterTamesPlugin.LogIfDebug($"GetPetHeight für {GetPrefabName(character)}: CapsuleCollider gefunden. Höhe = {num:F2} (capsule.height={component.height:F2} * scale.y={((Component)character).transform.localScale.y:F2})", DebugFeature.TeleportFollow);
				return num;
			}
			Collider component2 = ((Component)character).GetComponent<Collider>();
			if ((Object)(object)component2 != (Object)null)
			{
				Bounds bounds = component2.bounds;
				float y = ((Bounds)(ref bounds)).size.y;
				BetterTamesPlugin.LogIfDebug($"GetPetHeight für {GetPrefabName(character)}: Fallback auf generischen Collider ({((object)component2).GetType().Name}) gefunden. Höhe (bounds.size.y) = {y:F2}", DebugFeature.TeleportFollow);
				return y;
			}
			BetterTamesPlugin.LogIfDebug("GetPetHeight für " + GetPrefabName(character) + ": Keinen Collider gefunden. Fallback auf Höhe 1f.", DebugFeature.TeleportFollow);
			return 1f;
		}

		public static void TeleportPetToActualPosition(Character petCharacter, Vector3 targetPosition, Quaternion targetRotation, Player teleportingPlayer)
		{
			//IL_0030: 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)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_0119: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Expected O, but got Unknown
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)petCharacter == (Object)null)
			{
				return;
			}
			ZNetView component = ((Component)petCharacter).GetComponent<ZNetView>();
			if ((Object)(object)component == (Object)null || !component.IsValid())
			{
				return;
			}
			string prefabName = GetPrefabName(petCharacter);
			BetterTamesPlugin.LogIfDebug($"TeleportPetToActualPosition: Setting {prefabName} to {targetPosition} with rotation {((Quaternion)(ref targetRotation)).eulerAngles}", DebugFeature.TeleportFollow);
			((Component)petCharacter).transform.position = targetPosition;
			((Component)petCharacter).transform.rotation = targetRotation;
			Rigidbody component2 = ((Component)petCharacter).GetComponent<Rigidbody>();
			if ((Object)(object)component2 != (Object)null)
			{
				if (!component2.isKinematic)
				{
					BetterTamesPlugin.LogIfDebug(prefabName + " ist nicht kinematisch, Velocity wird zurückgesetzt.", DebugFeature.TeleportFollow);
					component2.velocity = Vector3.zero;
					component2.angularVelocity = Vector3.zero;
				}
				else
				{
					BetterTamesPlugin.LogIfDebug(prefabName + " ist kinematisch, Velocity-Änderungen übersprungen.", DebugFeature.TeleportFollow);
				}
			}
			Tameable component3 = ((Component)petCharacter).GetComponent<Tameable>();
			if ((Object)(object)component3 != (Object)null)
			{
				component3.m_unsummonDistance = 0f;
			}
			MonsterAI component4 = ((Component)petCharacter).GetComponent<MonsterAI>();
			if ((Object)(object)component4 != (Object)null)
			{
				((BaseAI)component4).StopMoving();
				if ((Object)(object)teleportingPlayer != (Object)null)
				{
					component4.SetFollowTarget(((Component)teleportingPlayer).gameObject);
				}
			}
			ZDO zDO = component.GetZDO();
			zDO.SetPosition(targetPosition);
			zDO.SetRotation(targetRotation);
			ZPackage val = new ZPackage();
			val.Write(targetPosition);
			val.Write(targetRotation);
			string text = $"{((ZDOID)(ref zDO.m_uid)).UserID}:{((ZDOID)(ref zDO.m_uid)).ID}";
			ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "BT_TeleportSync", new object[2] { text, val });
			BetterTamesPlugin.LogIfDebug("TeleportPetToActualPosition: RPC_TELEPORT_SYNC sent for " + prefabName + ".", DebugFeature.TeleportFollow);
		}
	}
	[HarmonyPatch]
	internal static class TameAndFeed
	{
		public class TamingProgressUI : MonoBehaviour
		{
			private TextMeshPro _textMesh;

			public void Initialize(TextMeshPro tmproInstance)
			{
				_textMesh = tmproInstance;
				if ((Object)(object)((Component)this).gameObject != (Object)null)
				{
					((Component)this).gameObject.SetActive(false);
				}
			}

			public void SetText(string text)
			{
				if ((Object)(object)_textMesh != (Object)null)
				{
					((TMP_Text)_textMesh).text = text;
				}
			}

			public void Show()
			{
				if ((Object)(object)((Component)this).gameObject != (Object)null && !((Component)this).gameObject.activeSelf)
				{
					((Component)this).gameObject.SetActive(true);
				}
			}

			public bool IsVisible()
			{
				if ((Object)(object)((Component)this).gameObject != (Object)null)
				{
					return ((Component)this).gameObject.activeSelf;
				}
				return false;
			}

			public void HideAndDestroy()
			{
				if ((Object)(object)((Component)this).gameObject != (Object)null)
				{
					Object.Destroy((Object)(object)((Component)this).gameObject);
				}
			}
		}

		private const float TamingProgressUpdateIntervalValue = 2f;

		private const float TamingProgressFontSizeValue = 3f;

		private static readonly Dictionary<ZDOID, TamingProgressUI> activeTamingDisplays;

		private static readonly Dictionary<ZDOID, float> lastTamingDisplayUpdateTime;

		private static MethodInfo _getRemainingTimeMethodInfo;

		private static MethodInfo _resetFeedingTimerMethodInfo;

		private static bool IsShowTamingProgressEnabled => (BetterTamesPlugin.ConfigInstance?.Tames?.ShowTamingProgressEnabled?.Value).GetValueOrDefault();

		static TameAndFeed()
		{
			activeTamingDisplays = new Dictionary<ZDOID, TamingProgressUI>();
			lastTamingDisplayUpdateTime = new Dictionary<ZDOID, float>();
			try
			{
				_getRemainingTimeMethodInfo = typeof(Tameable).GetMethod("GetRemainingTime", BindingFlags.Instance | BindingFlags.NonPublic);
				if (_getRemainingTimeMethodInfo == null)
				{
					Debug.LogError((object)"[BetterTames] STATIC_INIT FEHLER: Methode 'GetRemainingTime' nicht gefunden!");
				}
				_resetFeedingTimerMethodInfo = typeof(Tameable).GetMethod("ResetFeedingTimer", BindingFlags.Instance | BindingFlags.NonPublic);
				if (_resetFeedingTimerMethodInfo == null)
				{
					Debug.LogError((object)"[BetterTames] STATIC_INIT FEHLER: Methode 'ResetFeedingTimer' nicht gefunden!");
				}
			}
			catch (Exception arg)
			{
				Debug.LogError((object)$"[BetterTames] TameAndFeed Static Constructor Exception: {arg}");
			}
		}

		private static float GetTameableRemainingTime(Tameable tameableInstance)
		{
			if ((Object)(object)tameableInstance == (Object)null)
			{
				BetterTamesPlugin.LogIfDebug("GetTameableRemainingTime: tameableInstance ist null.", DebugFeature.TamingAndFeeder);
				return float.MaxValue;
			}
			if (_getRemainingTimeMethodInfo == null)
			{
				BetterTamesPlugin.LogIfDebug("GetTameableRemainingTime: _getRemainingTimeMethodInfo ist null.", DebugFeature.TamingAndFeeder);
				return float.MaxValue;
			}
			try
			{
				return (float)_getRemainingTimeMethodInfo.Invoke(tameableInstance, null);
			}
			catch (Exception ex)
			{
				BetterTamesPlugin.LogIfDebug("GetRemainingTime Reflection Fehler: " + ex.Message, DebugFeature.TamingAndFeeder);
				return float.MaxValue;
			}
		}

		private static void CallTameableResetFeedingTimer(Tameable tameableInstance)
		{
			if ((Object)(object)tameableInstance == (Object)null)
			{
				BetterTamesPlugin.LogIfDebug("CallTameableResetFeedingTimer: tameableInstance ist null.", DebugFeature.TamingAndFeeder);
				return;
			}
			if (_resetFeedingTimerMethodInfo == null)
			{
				BetterTamesPlugin.LogIfDebug("CallTameableResetFeedingTimer: _resetFeedingTimerMethodInfo ist null.", DebugFeature.TamingAndFeeder);
				return;
			}
			try
			{
				_resetFeedingTimerMethodInfo.Invoke(tameableInstance, null);
			}
			catch (Exception ex)
			{
				BetterTamesPlugin.LogIfDebug("ResetFeedingTimer Reflection Fehler: " + ex.Message, DebugFeature.TamingAndFeeder);
			}
		}

		[HarmonyPatch(typeof(Character), "CustomFixedUpdate")]
		[HarmonyPostfix]
		private static void UpdateTamingDisplay_Postfix(Character __instance, float dt)
		{
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			//IL_0161: Unknown result type (might be due to invalid IL or missing references)
			//IL_0127: Unknown result type (might be due to invalid IL or missing references)
			//IL_0177: Unknown result type (might be due to invalid IL or missing references)
			//IL_0183: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0222: Unknown result type (might be due to invalid IL or missing references)
			//IL_022f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0247: Unknown result type (might be due to invalid IL or missing references)
			//IL_0251: Unknown result type (might be due to invalid IL or missing references)
			//IL_027f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0284: Unknown result type (might be due to invalid IL or missing references)
			//IL_028e: Unknown result type (might be due to invalid IL or missing references)
			//IL_02aa: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance == (Object)null)
			{
				return;
			}
			if (BetterTamesPlugin.ConfigInstance == null || BetterTamesPlugin.ConfigInstance.Tames == null)
			{
				if (Time.frameCount % 300 == 0)
				{
					Debug.LogError((object)"[BetterTames] ConfigInstance oder ConfigInstance.Tames ist null in UpdateTamingDisplay_Postfix! Plugin-Initialisierung überprüfen.");
				}
				return;
			}
			bool isShowTamingProgressEnabled = IsShowTamingProgressEnabled;
			ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
			if ((Object)(object)component == (Object)null || !component.IsValid())
			{
				return;
			}
			ZDO zDO = component.GetZDO();
			if (zDO == null)
			{
				BetterTamesPlugin.LogIfDebug("UpdateTamingDisplay: ZDO ist null für Character " + __instance.m_name + ", obwohl NView gültig ist.", DebugFeature.TamingAndFeeder);
				return;
			}
			ZDOID uid = zDO.m_uid;
			if (!isShowTamingProgressEnabled || __instance.IsPlayer())
			{
				if (activeTamingDisplays.TryGetValue(uid, out var value))
				{
					value.HideAndDestroy();
					activeTamingDisplays.Remove(uid);
					lastTamingDisplayUpdateTime.Remove(uid);
				}
				return;
			}
			Tameable component2 = ((Component)__instance).GetComponent<Tameable>();
			if ((Object)(object)component2 == (Object)null || __instance.IsTamed())
			{
				if (activeTamingDisplays.TryGetValue(uid, out var value2))
				{
					value2.HideAndDestroy();
					activeTamingDisplays.Remove(uid);
					lastTamingDisplayUpdateTime.Remove(uid);
				}
			}
			else
			{
				if (lastTamingDisplayUpdateTime.TryGetValue(uid, out var value3) && Time.time - value3 < 2f && activeTamingDisplays.TryGetValue(uid, out var value4) && value4.IsVisible())
				{
					return;
				}
				float tameableRemainingTime = GetTameableRemainingTime(component2);
				float tamingTime = component2.m_tamingTime;
				if (tameableRemainingTime >= tamingTime || tamingTime <= 0f)
				{
					if (activeTamingDisplays.TryGetValue(uid, out var value5))
					{
						value5.HideAndDestroy();
						activeTamingDisplays.Remove(uid);
						lastTamingDisplayUpdateTime.Remove(uid);
					}
					return;
				}
				lastTamingDisplayUpdateTime[uid] = Time.time;
				int num = (int)((1f - Mathf.Clamp01(tameableRemainingTime / tamingTime)) * 100f);
				if (!activeTamingDisplays.TryGetValue(uid, out var value6) || (Object)(object)value6 == (Object)null)
				{
					GameObject val = new GameObject($"TamingProgress_{((ZDOID)(ref uid)).ID}");
					val.transform.SetParent(((Component)__instance).transform);
					CapsuleCollider component3 = ((Component)__instance).GetComponent<CapsuleCollider>();
					float num2 = (((Object)(object)component3 != (Object)null) ? component3.height : 1.5f) * ((Component)__instance).transform.localScale.y;
					val.transform.localPosition = new Vector3(0f, num2 + 0.7f, 0f);
					TextMeshPro val2 = val.AddComponent<TextMeshPro>();
					((TMP_Text)val2).fontSize = 3f;
					((TMP_Text)val2).alignment = (TextAlignmentOptions)514;
					((TMP_Text)val2).outlineWidth = 0.1f;
					((TMP_Text)val2).outlineColor = Color32.op_Implicit(Color.black);
					val.AddComponent<MonsterAI_StunBillboardPatch>();
					value6 = val.AddComponent<TamingProgressUI>();
					value6.Initialize(val2);
					activeTamingDisplays[uid] = value6;
				}
				string text = $"{num}%";
				value6.SetText(text);
				value6.Show();
			}
		}

		[HarmonyPatch(typeof(ZNetScene), "Destroy")]
		[HarmonyPrefix]
		private static void CleanupTamingDisplay_Prefix(GameObject go)
		{
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)go == (Object)null)
			{
				return;
			}
			Character component = go.GetComponent<Character>();
			if (!((Object)(object)component != (Object)null) || component.IsPlayer())
			{
				return;
			}
			ZNetView component2 = ((Component)component).GetComponent<ZNetView>();
			if (!((Object)(object)component2 != (Object)null) || !component2.IsValid())
			{
				return;
			}
			ZDO zDO = component2.GetZDO();
			if (zDO != null)
			{
				ZDOID uid = zDO.m_uid;
				if (activeTamingDisplays.TryGetValue(uid, out var value))
				{
					value.HideAndDestroy();
					activeTamingDisplays.Remove(uid);
					lastTamingDisplayUpdateTime.Remove(uid);
				}
			}
		}
	}
}
namespace BetterTames.Features
{
	[HarmonyPatch]
	public static class PortalPetNotifier
	{
		private static float lastMessageBroadcastTime;

		private const float MESSAGE_BROADCAST_COOLDOWN = 3f;

		private static int lastShownPetCount;

		private static string lastHoveredPortalUIDString;

		private static MethodInfo _teleportWorldHaveTargetMethodInfo;

		private static MethodInfo _teleportWorldTargetFoundMethodInfo;

		static PortalPetNotifier()
		{
			lastMessageBroadcastTime = -5f;
			lastShownPetCount = -1;
			lastHoveredPortalUIDString = "";
			try
			{
				_teleportWorldHaveTargetMethodInfo = typeof(TeleportWorld).GetMethod("HaveTarget", BindingFlags.Instance | BindingFlags.NonPublic);
				if (_teleportWorldHaveTargetMethodInfo == null)
				{
					BetterTamesPlugin.LogIfDebug("PortalNotifier Fehler: Private Methode 'HaveTarget' in TeleportWorld nicht gefunden!");
				}
				_teleportWorldTargetFoundMethodInfo = typeof(TeleportWorld).GetMethod("TargetFound", BindingFlags.Instance | BindingFlags.NonPublic);
				if (_teleportWorldTargetFoundMethodInfo == null)
				{
					BetterTamesPlugin.LogIfDebug("PortalNotifier Fehler: Private Methode 'TargetFound' in TeleportWorld nicht gefunden!");
				}
			}
			catch (Exception arg)
			{
				Debug.LogError((object)$"[BetterTames] PortalPetNotifier Static Constructor Exception: {arg}");
			}
		}

		private static bool CallTeleportWorldHaveTarget(TeleportWorld instance)
		{
			if ((Object)(object)instance == (Object)null || _teleportWorldHaveTargetMethodInfo == null)
			{
				return false;
			}
			try
			{
				return (bool)_teleportWorldHaveTargetMethodInfo.Invoke(instance, null);
			}
			catch (Exception ex)
			{
				BetterTamesPlugin.LogIfDebug("Fehler beim Aufruf von TeleportWorld.HaveTarget(): " + ex.Message, DebugFeature.TeleportFollow);
				return false;
			}
		}

		private static bool CallTeleportWorldTargetFound(TeleportWorld instance)
		{
			if ((Object)(object)instance == (Object)null || _teleportWorldTargetFoundMethodInfo == null)
			{
				return false;
			}
			try
			{
				return (bool)_teleportWorldTargetFoundMethodInfo.Invoke(instance, null);
			}
			catch (Exception ex)
			{
				BetterTamesPlugin.LogIfDebug("Fehler beim Aufruf von TeleportWorld.TargetFound(): " + ex.Message, DebugFeature.TeleportFollow);
				return false;
			}
		}

		[HarmonyPatch(typeof(TeleportWorld), "GetHoverText")]
		[HarmonyPostfix]
		private static void TeleportWorld_GetHoverText_Postfix(TeleportWorld __instance, ref string __result)
		{
			if (!((Object)(object)__instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null))
			{
				ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
				if (!((Object)(object)component == (Object)null) && component.IsValid() && component.GetZDO() != null)
				{
					bool flag = CallTeleportWorldHaveTarget(__instance);
					bool flag2 = CallTeleportWorldTargetFound(__instance);
					bool flag3 = (Object)(object)__instance.m_target_found != (Object)null && ((Component)__instance.m_target_found).gameObject.activeSelf;
					HandlePortalHoverLogic(component, __instance.GetText(), flag && flag2 && flag3);
				}
			}
		}

		[HarmonyPatch(typeof(Teleport), "GetHoverText")]
		[HarmonyPostfix]
		private static void DungeonTeleport_GetHoverText_Postfix(Teleport __instance, ref string __result)
		{
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance == (Object)null || (Object)(object)Player.m_localPlayer == (Object)null)
			{
				return;
			}
			ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
			if (!((Object)(object)component == (Object)null) && component.IsValid() && component.GetZDO() != null)
			{
				bool flag = true;
				if ((Object)(object)__instance.m_targetPoint == (Object)null)
				{
					flag = false;
				}
				else if (ZoneSystem.instance.GetGlobalKey((GlobalKeys)27) && ((Character)Player.m_localPlayer).InInterior() && Location.IsInsideActiveBossDungeon(((Component)Player.m_localPlayer).transform.position))
				{
					flag = false;
				}
				HandlePortalHoverLogic(component, __instance.m_hoverText, flag, flag);
			}
		}

		private static void HandlePortalHoverLogic(ZNetView portalNView, string portalIdentifierString, bool portalIsActiveAndUsable, bool isVisualEffectActiveForWorldPortal = true)
		{
			if (!((Object)(object)Player.m_localPlayer == (Object)null))
			{
				ConfigSync configInstance = BetterTamesPlugin.ConfigInstance;
				if (configInstance != null && (configInstance.Tames.TeleportFollowEnabled?.Value).GetValueOrDefault())
				{
					string text = ((object)(ZDOID)(ref portalNView.GetZDO().m_uid)).ToString();
					if (portalIsActiveAndUsable)
					{
						int num = CountPortablePetsForPlayer(Player.m_localPlayer);
						if (num > 0)
						{
							if (text != lastHoveredPortalUIDString || num != lastShownPetCount || Time.time > lastMessageBroadcastTime + 3f)
							{
								string text2 = string.Format("Teleport {0} {1} with you.", num, (num == 1) ? "Tame" : "Tames");
								MessageHud.instance.ShowMessage((MessageType)2, text2, 0, (Sprite)null, false);
								lastMessageBroadcastTime = Time.time;
								lastShownPetCount = num;
								lastHoveredPortalUIDString = text;
								BetterTamesPlugin.LogIfDebug("PortalNotifier (Hover): Broadcast '" + text2 + "'", DebugFeature.TeleportFollow);
							}
						}
						else if (text == lastHoveredPortalUIDString && lastShownPetCount > 0)
						{
							lastShownPetCount = 0;
							lastMessageBroadcastTime = Time.time;
							BetterTamesPlugin.LogIfDebug("PortalNotifier (Hover): Portal aktiv, aber keine Tiere mehr.", DebugFeature.TeleportFollow);
						}
						else if (text != lastHoveredPortalUIDString)
						{
							lastHoveredPortalUIDString = text;
							lastShownPetCount = 0;
						}
					}
					else if (text == lastHoveredPortalUIDString)
					{
						lastHoveredPortalUIDString = "";
						lastShownPetCount = -1;
						BetterTamesPlugin.LogIfDebug("PortalNotifier (Hover): Portal nicht mehr aktiv/nutzbar oder Hover beendet.", DebugFeature.TeleportFollow);
					}
					return;
				}
			}
			if (!string.IsNullOrEmpty(lastHoveredPortalUIDString))
			{
				lastHoveredPortalUIDString = "";
				lastShownPetCount = -1;
			}
		}

		private static int CountPortablePetsForPlayer(Player player)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			int num = 0;
			List<Character> list = new List<Character>();
			float num2 = BetterTamesPlugin.ConfigInstance?.Tames.TeleportOnDistanceMaxRange.Value ?? 60f;
			Character.GetCharactersInRange(((Component)player).transform.position, num2, list);
			foreach (Character item in list)
			{
				if (!((Object)(object)item == (Object)null) && !((Object)(object)item == (Object)(object)player) && item.IsTamed())
				{
					MonsterAI component = ((Component)item).GetComponent<MonsterAI>();
					if (!((Object)(object)component == (Object)null) && !((Object)(object)component.GetFollowTarget() != (Object)(object)((Component)player).gameObject))
					{
						num++;
					}
				}
			}
			return num;
		}
	}
}
namespace BetterTames.Patches
{
	[HarmonyPatch]
	public static class MonsterAI_StunBehaviorPatches
	{
		[HarmonyPatch(typeof(MonsterAI), "UpdateAI")]
		[HarmonyPrefix]
		public static bool PreventAIUpdateWhenStunned(MonsterAI __instance)
		{
			ConfigSync configInstance = BetterTamesPlugin.ConfigInstance;
			if (configInstance == null || !(configInstance.Tames?.PetProtectionEnabled?.Value).GetValueOrDefault())
			{
				return true;
			}
			ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
			if ((Object)(object)component == (Object)null || !component.IsValid())
			{
				return true;
			}
			ZDO zDO = component.GetZDO();
			Character component2 = ((Component)__instance).GetComponent<Character>();
			if (zDO != null && (Object)(object)component2 != (Object)null && component2.IsTamed() && zDO.GetBool("isRecoveringFromStun", false))
			{
				if (component2.m_speed != 0f)
				{
					component2.m_speed = 0f;
				}
				if (component2.m_walkSpeed != 0f)
				{
					component2.m_walkSpeed = 0f;
				}
				if (component2.m_runSpeed != 0f)
				{
					component2.m_runSpeed = 0f;
				}
				if (component2.m_swimSpeed != 0f)
				{
					component2.m_swimSpeed = 0f;
				}
				return false;
			}
			return true;
		}

		[HarmonyPatch(typeof(Humanoid), "StartAttack")]
		[HarmonyPrefix]
		public static bool PreventAttackWhenStunned_Humanoid(Humanoid __instance)
		{
			ConfigSync configInstance = BetterTamesPlugin.ConfigInstance;
			if (configInstance == null || !(configInstance.Tames?.PetProtectionEnabled?.Value).GetValueOrDefault() || !((Character)__instance).IsTamed())
			{
				return true;
			}
			ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
			if ((Object)(object)component != (Object)null && component.IsValid())
			{
				ZDO zDO = component.GetZDO();
				if (zDO != null && zDO.GetBool("isRecoveringFromStun", false))
				{
					BetterTamesPlugin.LogIfDebug("Attack attempt by " + ((Character)__instance).m_name + " (Humanoid) blocked due to PetProtection stun.", DebugFeature.PetProtection);
					return false;
				}
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(Tameable), "Interact")]
	public static class MakeCommandablePatch
	{
		[HarmonyPrefix]
		public static bool Prefix(Tameable __instance, Humanoid user, bool hold, bool alt, ref bool __result)
		{
			if (1 == 0 || hold || alt)
			{
				return true;
			}
			ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
			Character component2 = ((Component)__instance).GetComponent<Character>();
			if (!component.IsValid() || !component2.IsTamed())
			{
				return true;
			}
			BetterTamesPlugin.LogIfDebug("Interacting with tamed creature: " + __instance.GetHoverName(), DebugFeature.MakeCommandable);
			__instance.Command(user, true);
			MonsterAI component3 = ((Component)__instance).GetComponent<MonsterAI>();
			BetterTamesPlugin.LogIfDebug("Command issued to " + __instance.GetHoverName() + ": " + (((Object)(object)component3.GetFollowTarget() != (Object)null) ? "Follow" : "Stay"), DebugFeature.MakeCommandable);
			__result = true;
			return false;
		}
	}
	[HarmonyPatch(typeof(MonsterAI), "UpdateAI")]
	public static class TeleportFollowPatch
	{
		private static readonly int groundLayerMask = LayerMask.GetMask(new string[7] { "Default", "static_solid", "Default_small", "piece", "terrain", "blocker", "vehicle" });

		private static readonly Dictionary<ZDOID, float> nextTeleportCheckTime = new Dictionary<ZDOID, float>();

		private const float TELEPORT_CHECK_INTERVAL = 3f;

		[HarmonyPostfix]
		public static void Postfix(MonsterAI __instance, float dt)
		{
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_0155: Unknown result type (might be due to invalid IL or missing references)
			//IL_0185: Unknown result type (might be due to invalid IL or missing references)
			//IL_0187: Unknown result type (might be due to invalid IL or missing references)
			//IL_0189: Unknown result type (might be due to invalid IL or missing references)
			//IL_018e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_020e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0213: Unknown result type (might be due to invalid IL or missing references)
			//IL_0214: Unknown result type (might be due to invalid IL or missing references)
			//IL_0219: Unknown result type (might be due to invalid IL or missing references)
			//IL_021e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0220: Unknown result type (might be due to invalid IL or missing references)
			//IL_0225: Unknown result type (might be due to invalid IL or missing references)
			//IL_022a: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_0252: Unknown result type (might be due to invalid IL or missing references)
			//IL_0262: Unknown result type (might be due to invalid IL or missing references)
			//IL_0309: Unknown result type (might be due to invalid IL or missing references)
			//IL_030b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0312: Unknown result type (might be due to invalid IL or missing references)
			//IL_0317: Unknown result type (might be due to invalid IL or missing references)
			//IL_031b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0320: Unknown result type (might be due to invalid IL or missing references)
			//IL_0322: Unknown result type (might be due to invalid IL or missing references)
			//IL_0324: Unknown result type (might be due to invalid IL or missing references)
			//IL_0329: Unknown result type (might be due to invalid IL or missing references)
			//IL_032b: Unknown result type (might be due to invalid IL or missing references)
			//IL_032d: Unknown result type (might be due to invalid IL or missing references)
			//IL_032f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0334: Unknown result type (might be due to invalid IL or missing references)
			//IL_0336: Unknown result type (might be due to invalid IL or missing references)
			//IL_0338: Unknown result type (might be due to invalid IL or missing references)
			//IL_0342: Unknown result type (might be due to invalid IL or missing references)
			//IL_0347: Unknown result type (might be due to invalid IL or missing references)
			//IL_034c: Unknown result type (might be due to invalid IL or missing references)
			//IL_028a: Unknown result type (might be due to invalid IL or missing references)
			//IL_028f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0293: Unknown result type (might be due to invalid IL or missing references)
			//IL_029f: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0381: Unknown result type (might be due to invalid IL or missing references)
			//IL_0368: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_03aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_03af: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_03cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0411: Unknown result type (might be due to invalid IL or missing references)
			//IL_0418: Expected O, but got Unknown
			//IL_041a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0423: Unknown result type (might be due to invalid IL or missing references)
			ConfigSync configInstance = BetterTamesPlugin.ConfigInstance;
			if (configInstance == null || !(configInstance.Tames?.TeleportFollowEnabled?.Value).GetValueOrDefault() || (Object)(object)ZNet.instance == (Object)null)
			{
				return;
			}
			ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
			if ((Object)(object)component == (Object)null || !component.IsValid())
			{
				return;
			}
			ZDO zDO = component.GetZDO();
			if (zDO == null)
			{
				return;
			}
			Character component2 = ((Component)__instance).GetComponent<Character>();
			if ((Object)(object)component2 == (Object)null || !component2.IsTamed())
			{
				return;
			}
			GameObject followTarget = __instance.GetFollowTarget();
			if ((Object)(object)followTarget == (Object)null)
			{
				return;
			}
			Vector3 position = ((Component)component2).transform.position;
			Vector3 position2 = followTarget.transform.position;
			if (component.IsOwner())
			{
				float num = Vector3.Distance(position, position2);
				float num2 = 12f;
				Character targetCreature = ((BaseAI)__instance).GetTargetCreature();
				StaticTarget staticTarget = __instance.GetStaticTarget();
				bool flag = ((Object)(object)targetCreature != (Object)null && BaseAI.IsEnemy(component2, targetCreature)) || (Object)(object)staticTarget != (Object)null;
				if (num < num2 && !flag)
				{
					((BaseAI)__instance).StopMoving();
				}
			}
			if (!component.IsOwner())
			{
				return;
			}
			float time = Time.time;
			if (!nextTeleportCheckTime.TryGetValue(zDO.m_uid, out var value))
			{
				value = 0f;
			}
			if (!(time >= value))
			{
				return;
			}
			nextTeleportCheckTime[zDO.m_uid] = time + 3f;
			float num3 = Mathf.Max(BetterTamesPlugin.ConfigInstance.Tames.TeleportOnDistanceMaxRange.Value, 10f);
			Vector3 val = position - position2;
			float sqrMagnitude = ((Vector3)(ref val)).sqrMagnitude;
			float num4 = num3 * num3;
			if (!(sqrMagnitude > num4))
			{
				return;
			}
			BetterTamesPlugin.LogIfDebug($"DistanceSqr {sqrMagnitude:F1} > {num4:F1}. Attempting teleport for {component2.m_name}.", DebugFeature.TeleportFollow);
			Vector3 position3 = followTarget.transform.position;
			if (position3.y > 2000f)
			{
				BetterTamesPlugin.LogIfDebug($"Player Y position {position3.y:F1} is > 2000. Preventing pet teleport for {component2.m_name} to avoid dungeon issue.", DebugFeature.TeleportFollow);
				return;
			}
			Quaternion rotation = followTarget.transform.rotation;
			Vector3 val2 = rotation * Vector3.forward;
			Vector3 val3 = rotation * Vector3.right;
			float num5 = 1f;
			CapsuleCollider component3 = ((Component)component2).GetComponent<CapsuleCollider>();
			if ((Object)(object)component3 != (Object)null)
			{
				num5 = component3.radius * Mathf.Max(((Component)component2).transform.localScale.x, ((Component)component2).transform.localScale.z);
			}
			else
			{
				Collider component4 = ((Component)component2).GetComponent<Collider>();
				if ((Object)(object)component4 != (Object)null)
				{
					Bounds bounds = component4.bounds;
					float x = ((Bounds)(ref bounds)).extents.x;
					bounds = component4.bounds;
					num5 = Mathf.Max(x, ((Bounds)(ref bounds)).extents.z);
				}
			}
			float num6 = Mathf.Max(10f, num5 + 0.5f);
			float num7 = num6 + 5f;
			float num8 = Mathf.Max(5f, num5 * 1.5f);
			float num9 = Random.Range(num6, num7);
			float num10 = Random.Range((0f - num8) / 2f, num8 / 2f);
			Vector3 val4 = -val2 * num9;
			Vector3 val5 = val3 * num10;
			Vector3 val6 = val4 + val5;
			Vector3 val7 = position3 + val6;
			RaycastHit val8 = default(RaycastHit);
			if (Physics.Raycast(val7 + Vector3.up * 5f, Vector3.down, ref val8, 10f, groundLayerMask))
			{
				val7.y = ((RaycastHit)(ref val8)).point.y + 1f;
			}
			else
			{
				val7.y = position3.y;
				BetterTamesPlugin.LogIfDebug("No ground found via Raycast for auto-teleport of " + component2.m_name + ", using target Y position.", DebugFeature.TeleportFollow);
			}
			Quaternion val9 = Quaternion.LookRotation(val2);
			((Component)component2).transform.position = val7;
			((Component)component2).transform.rotation = val9;
			zDO.SetPosition(val7);
			zDO.SetRotation(val9);
			Rigidbody component5 = ((Component)component2).GetComponent<Rigidbody>();
			if ((Object)(object)component5 != (Object)null)
			{
				component5.WakeUp();
			}
			BetterTamesPlugin.LogIfDebug($"Teleported {component2.m_name} to {val7} (behind followTarget). Sending RPC.", DebugFeature.TeleportFollow);
			ZPackage val10 = new ZPackage();
			val10.Write(val7);
			val10.Write(val9);
			string text = $"{((ZDOID)(ref zDO.m_uid)).UserID}:{((ZDOID)(ref zDO.m_uid)).ID}";
			ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "BT_TeleportSync", new object[2] { text, val10 });
		}
	}
	[HarmonyPatch]
	public static class PetProtectionPatch
	{
		public const string StunKeyConstant = "isRecoveringFromStun";

		public const string TimeSinceStunKeyConstant = "timeSinceStun";

		public const string OriginalSpeedKeyConstant = "original_speed";

		public const string OriginalWalkSpeedKeyConstant = "original_walkSpeed";

		public const string OriginalRunSpeedKeyConstant = "original_runSpeed";

		public const string OriginalSwimSpeedKeyConstant = "original_swimSpeed";

		public const string DEFAULT_STUN_ICON_ITEM_PREFAB_NAME_CONFIG = "SledgeStagbreaker";

		private static Dictionary<Character, GameObject> stunIconObjects = new Dictionary<Character, GameObject>();

		private static Sprite stunSpriteInstance;

		private static string currentLoadedIconPrefabName = null;

		private static Material defaultSpriteMaterial;

		public static void InitializeStunIconAssets(string itemPrefabNameFromConfig)
		{
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Expected O, but got Unknown
			string text = (string.IsNullOrEmpty(itemPrefabNameFromConfig) ? "SledgeStagbreaker" : itemPrefabNameFromConfig);
			if ((Object)(object)stunSpriteInstance != (Object)null && (Object)(object)defaultSpriteMaterial != (Object)null && currentLoadedIconPrefabName == text)
			{
				return;
			}
			BetterTamesPlugin.LogIfDebug("Initializing/Reloading StunIconAssets with: " + text, DebugFeature.PetProtection);
			currentLoadedIconPrefabName = text;
			stunSpriteInstance = null;
			if ((Object)(object)ObjectDB.instance == (Object)null)
			{
				BetterTamesPlugin.LogIfDebug("[PetProtectionPatch] ObjectDB missing for StunIconSprite", DebugFeature.PetProtection);
				return;
			}
			GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(text);
			if ((Object)(object)itemPrefab != (Object)null)
			{
				ItemDrop component = itemPrefab.GetComponent<ItemDrop>();
				if ((Object)(object)component != (Object)null && component.m_itemData != null)
				{
					stunSpriteInstance = component.m_itemData.GetIcon();
					if ((Object)(object)stunSpriteInstance == (Object)null)
					{
						BetterTamesPlugin.LogIfDebug("[PetProtectionPatch] Sprite für '" + text + "' nicht geladen.", DebugFeature.PetProtection);
					}
					else
					{
						BetterTamesPlugin.LogIfDebug("[PetProtectionPatch] Stun-Icon-Sprite '" + text + "' geladen.", DebugFeature.PetProtection);
					}
				}
				else
				{
					BetterTamesPlugin.LogIfDebug("[PetProtectionPatch] ItemDrop/ItemData nicht auf '" + text + "'-Prefab gefunden.", DebugFeature.PetProtection);
				}
			}
			else
			{
				BetterTamesPlugin.LogIfDebug("[PetProtectionPatch] '" + text + "'-Prefab nicht in ObjectDB gefunden.", DebugFeature.PetProtection);
			}
			if ((Object)(object)defaultSpriteMaterial == (Object)null)
			{
				Shader val = Shader.Find("Sprites/Default");
				if ((Object)(object)val != (Object)null)
				{
					defaultSpriteMaterial = new Material(val);
					BetterTamesPlugin.LogIfDebug("[PetProtectionPatch] Standard-Sprite-Material erstellt.", DebugFeature.PetProtection);
				}
				else
				{
					BetterTamesPlugin.LogIfDebug("[PetProtectionPatch] 'Sprites/Default' Shader nicht gefunden.", DebugFeature.PetProtection);
				}
			}
		}

		private static bool ShouldApplyPetProtection(Character character)
		{
			if (!character.IsTamed())
			{
				return false;
			}
			ZNetView component = ((Component)character).GetComponent<ZNetView>();
			if ((Object)(object)component == (Object)null || !component.IsValid())
			{
				return false;
			}
			ZDO zDO = component.GetZDO();
			if (zDO == null)
			{
				return false;
			}
			return !((Object)ZNetScene.instance.GetPrefab(zDO.GetPrefab())).name.ToLower().Contains("summon");
		}

		[HarmonyPatch(typeof(Character), "ApplyDamage")]
		[HarmonyPrefix]
		public static bool ApplyDamagePrefix(Character __instance, HitData hit, bool showDamageText, bool triggerEffects, DamageModifier mod)
		{
			ConfigSync configInstance = BetterTamesPlugin.ConfigInstance;
			if (configInstance == null || !(configInstance.Tames?.PetProtectionEnabled?.Value).GetValueOrDefault() || (Object)(object)ZNet.instance == (Object)null || !ShouldApplyPetProtection(__instance))
			{
				return true;
			}
			Character attacker = hit.GetAttacker();
			if ((Object)(object)attacker != (Object)null)
			{
				Player val = (Player)(object)((attacker is Player) ? attacker : null);
				if (val != null)
				{
					ItemData currentWeapon = ((Humanoid)val).GetCurrentWeapon();
					if (currentWeapon != null && (Object)(object)currentWeapon.m_dropPrefab != (Object)null && ((Object)currentWeapon.m_dropPrefab).name == "KnifeButcher")
					{
						return true;
					}
				}
			}
			ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
			if ((Object)(object)component == (Object)null || !component.IsValid())
			{
				return true;
			}
			ZDO zDO = component.GetZDO();
			if (zDO == null)
			{
				return true;
			}
			if (zDO.GetBool("isRecoveringFromStun", false))
			{
				BetterTamesPlugin.LogIfDebug(__instance.m_name + " is already stunned. Damage ignored.", DebugFeature.PetProtection);
				return false;
			}
			if (zDO.GetFloat(ZDOVars.s_health, __instance.GetMaxHealth()) - hit.GetTotalDamage() > 0f)
			{
				return true;
			}
			if (component.IsOwner())
			{
				BetterTamesPlugin.LogIfDebug("OWNER (" + __instance.m_name + "): Applying PetProtection directly.", DebugFeature.PetProtection);
				ApplyPetProtectionLogic(__instance, component, zDO);
			}
			else
			{
				BetterTamesPlugin.LogIfDebug("CLIENT (" + __instance.m_name + "): Requesting PetProtection from server.", DebugFeature.PetProtection);
				string text = $"{((ZDOID)(ref zDO.m_uid)).UserID}:{((ZDOID)(ref zDO.m_uid)).ID}";
				ZRoutedRpc.instance.InvokeRoutedRPC(0L, "BT_RequestPetProtection", new object[1] { text });
			}
			return false;
		}

		public static void ApplyPetProtectionLogic(Character character, ZNetView nview, ZDO zdo)
		{
			//IL_0161: Unknown result type (might be due to invalid IL or missing references)
			//IL_0167: Expected O, but got Unknown
			if ((Object)(object)character == (Object)null || !character.IsTamed() || (Object)(object)nview == (Object)null || !nview.IsValid() || !nview.IsOwner() || zdo == null || zdo.GetBool("isRecoveringFromStun", false))
			{
				return;
			}
			BetterTamesPlugin.LogIfDebug("OWNER/SERVER: Executing ApplyPetProtectionLogic for " + character.m_name + ".", DebugFeature.PetProtection);
			ZSyncAnimation component = ((Component)character).GetComponent<ZSyncAnimation>();
			BaseAI component2 = ((Component)character).GetComponent<BaseAI>();
			if ((Object)(object)component2 != (Object)null)
			{
				component2.StopMoving();
				if (component2.IsCharging())
				{
					component2.ChargeStop();
				}
			}
			zdo.Set("isRecoveringFromStun", true);
			zdo.Set("timeSinceStun", 0f);
			zdo.Set(ZDOVars.s_health, 1f);
			zdo.Set(ZDOVars.s_sleeping, true);
			zdo.Set("original_speed", character.m_speed);
			zdo.Set("original_walkSpeed", character.m_walkSpeed);
			zdo.Set("original_runSpeed", character.m_runSpeed);
			zdo.Set("original_swimSpeed", character.m_swimSpeed);
			character.m_speed = 0f;
			character.m_walkSpeed = 0f;
			character.m_runSpeed = 0f;
			character.m_swimSpeed = 0f;
			string text = "stagger";
			if ((Object)(object)component != (Object)null)
			{
				component.SetSpeed(1f);
				if (!string.IsNullOrEmpty(text))
				{
					component.SetTrigger(text);
				}
				component.SetBool("sleeping", true);
			}
			ZPackage val = new ZPackage();
			val.Write(true);
			val.Write(0f);
			val.Write(true);
			val.Write(1f);
			val.Write(character.m_speed);
			val.Write(character.m_walkSpeed);
			val.Write(character.m_runSpeed);
			val.Write(character.m_swimSpeed);
			val.Write(text);
			string text2 = $"{((ZDOID)(ref zdo.m_uid)).UserID}:{((ZDOID)(ref zdo.m_uid)).ID}";
			ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "BT_PetProtectionSync", new object[2] { text2, val });
		}

		[HarmonyPatch(typeof(MonsterAI), "UpdateAI")]
		[HarmonyPostfix]
		public static void UpdateAIPostfix(MonsterAI __instance, float dt)
		{
			//IL_0282: Unknown result type (might be due to invalid IL or missing references)
			//IL_0289: Expected O, but got Unknown
			ConfigSync configInstance = BetterTamesPlugin.ConfigInstance;
			if (configInstance == null || !(configInstance.Tames?.PetProtectionEnabled?.Value).GetValueOrDefault() || (Object)(object)ZNet.instance == (Object)null)
			{
				return;
			}
			Character component = ((Component)__instance).GetComponent<Character>();
			if ((Object)(object)component == (Object)null || !component.IsTamed())
			{
				return;
			}
			ZNetView component2 = ((Component)component).GetComponent<ZNetView>();
			if ((Object)(object)component2 == (Object)null || !component2.IsValid())
			{
				return;
			}
			ZDO zDO = component2.GetZDO();
			if (zDO == null)
			{
				return;
			}
			bool @bool = zDO.GetBool("isRecoveringFromStun", false);
			HandleStunIcon(component, @bool);
			if (!component2.IsOwner())
			{
				return;
			}
			ZSyncAnimation component3 = ((Component)component).GetComponent<ZSyncAnimation>();
			Animator val = null;
			if ((Object)(object)component3 != (Object)null)
			{
				val = ((Component)component3).GetComponentInChildren<Animator>();
			}
			if (!@bool)
			{
				if (zDO.GetBool(ZDOVars.s_sleeping, false))
				{
					zDO.Set(ZDOVars.s_sleeping, false);
					if ((Object)(object)component3 != (Object)null && (Object)(object)val != (Object)null && IsAnimatorPlayingBool(val, "sleeping", expectedValue: true))
					{
						component3.SetBool("sleeping", false);
					}
					if ((Object)(object)component3 != (Object)null)
					{
						component3.SetSpeed(1f);
					}
				}
				return;
			}
			float num = zDO.GetFloat("timeSinceStun", 0f) + dt;
			zDO.Set("timeSinceStun", num);
			if (num >= BetterTamesPlugin.ConfigInstance.Tames.PetProtectionStunDuration.Value)
			{
				zDO.Set("isRecoveringFromStun", false);
				zDO.Set("timeSinceStun", 0f);
				zDO.Set(ZDOVars.s_sleeping, false);
				component.m_speed = zDO.GetFloat("original_speed", component.m_speed);
				component.m_walkSpeed = zDO.GetFloat("original_walkSpeed", component.m_walkSpeed);
				component.m_runSpeed = zDO.GetFloat("original_runSpeed", component.m_runSpeed);
				component.m_swimSpeed = zDO.GetFloat("original_swimSpeed", component.m_swimSpeed);
				float maxHealth = component.GetMaxHealth();
				float value = BetterTamesPlugin.ConfigInstance.Tames.PetProtectionHealPercentage.Value;
				float num2 = Mathf.Clamp(maxHealth * (value / 100f), 1f, maxHealth);
				zDO.Set(ZDOVars.s_health, num2);
				if ((Object)(object)component3 != (Object)null && (Object)(object)val != (Object)null && IsAnimatorPlayingBool(val, "sleeping", expectedValue: true))
				{
					component3.SetBool("sleeping", false);
				}
				if ((Object)(object)component3 != (Object)null)
				{
					component3.SetSpeed(1f);
				}
				ZPackage val2 = new ZPackage();
				val2.Write(false);
				val2.Write(0f);
				val2.Write(false);
				val2.Write(num2);
				val2.Write(component.m_speed);
				val2.Write(component.m_walkSpeed);
				val2.Write(component.m_runSpeed);
				val2.Write(component.m_swimSpeed);
				val2.Write(string.Empty);
				string text = $"{((ZDOID)(ref zDO.m_uid)).UserID}:{((ZDOID)(ref zDO.m_uid)).ID}";
				ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "BT_PetProtectionSync", new object[2] { text, val2 });
				return;
			}
			component.m_speed = 0f;
			component.m_walkSpeed = 0f;
			component.m_runSpeed = 0f;
			component.m_swimSpeed = 0f;
			if ((Object)(object)component3 != (Object)null && (Object)(object)val != (Object)null)
			{
				if (!IsAnimatorPlayingBool(val, "sleeping", expectedValue: true))
				{
					component3.SetBool("sleeping", true);
				}
				if (val.speed != 1f)
				{
					component3.SetSpeed(1f);
				}
			}
		}

		public static bool IsAnimatorPlayingBool(Animator animator, string boolName, bool expectedValue)
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Invalid comparison between Unknown and I4
			if ((Object)(object)animator == (Object)null)
			{
				return !expectedValue;
			}
			try
			{
				int num = Animator.StringToHash(boolName);
				bool flag = false;
				AnimatorControllerParameter[] parameters = animator.parameters;
				foreach (AnimatorControllerParameter val in parameters)
				{
					if (val.nameHash == num && (int)val.type == 4)
					{
						flag = true;
						break;
					}
				}
				if (flag)
				{
					return animator.GetBool(num) == expectedValue;
				}
			}
			catch (Exception ex)
			{
				BetterTamesPlugin.LogIfDebug("Error checking animator bool '" + boolName + "': " + ex.Message);
			}
			return !expectedValue;
		}

		private static void HandleStunIcon(Character character, bool isStunned)
		{
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0162: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0172: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ac: Expected O, but got Unknown
			//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_022c: Unknown result type (might be due to invalid IL or missing references)
			//IL_023c: Unknown result type (might be due to invalid IL or missing references)
			ConfigSync configInstance = BetterTamesPlugin.ConfigInstance;
			if (configInstance == null || !(configInstance.Tames?.PetProtectionEnabled?.Value).GetValueOrDefault())
			{
				if (stunIconObjects.TryGetValue(character, out var value) && (Object)(object)value != (Object)null)
				{
					Object.Destroy((Object)(object)value);
					stunIconObjects.Remove(character);
				}
				return;
			}
			if (!((Object)(object)stunSpriteInstance == (Object)null) && !((Object)(object)defaultSpriteMaterial == (Object)null))
			{
				Texture2D texture = stunSpriteInstance.texture;
				if (!(((texture != null) ? ((Object)texture).name : null) != BetterTamesPlugin.ConfigInstance.Tames.PetProtectionStunIconItemPrefab.Value + "_dummy_for_name_check") || !(((Object)stunSpriteInstance).name != BetterTamesPlugin.ConfigInstance.Tames.PetProtectionStunIconItemPrefab.Value))
				{
					goto IL_0129;
				}
			}
			InitializeStunIconAssets(BetterTamesPlugin.ConfigInstance.Tames.PetProtectionStunIconItemPrefab.Value);
			if ((Object)(object)stunSpriteInstance == (Object)null || (Object)(object)defaultSpriteMaterial == (Object)null)
			{
				return;
			}
			goto IL_0129;
			IL_0129:
			Vector3 centerPoint = character.GetCenterPoint();
			float height = character.GetHeight();
			float value2 = BetterTamesPlugin.ConfigInstance.Tames.PetProtectionStunIconHeightFactor.Value;
			float value3 = BetterTamesPlugin.ConfigInstance.Tames.PetProtectionStunIconScale.Value;
			float num = height * value2;
			Vector3 position = centerPoint + Vector3.up * num;
			GameObject value5;
			if (isStunned)
			{
				if (!stunIconObjects.TryGetValue(character, out var value4) || (Object)(object)value4 == (Object)null)
				{
					value4 = new GameObject(character.m_name + "_StunIcon");
					value4.transform.position = position;
					SpriteRenderer obj = value4.AddComponent<SpriteRenderer>();
					obj.sprite = stunSpriteInstance;
					((Renderer)obj).material = defaultSpriteMaterial;
					((Renderer)obj).sortingLayerName = "Default";
					((Renderer)obj).sortingOrder = 5000;
					value4.transform.localScale = new Vector3(value3, value3, value3);
					if ((Object)(object)value4.GetComponent<Billboard>() == (Object)null)
					{
						value4.AddComponent<Billboard>();
					}
					stunIconObjects[character] = value4;
				}
				else
				{
					value4.transform.position = position;
					value4.transform.localScale = new Vector3(value3, value3, value3);
					if (!value4.activeSelf)
					{
						value4.SetActive(true);
					}
				}
			}
			else if (stunIconObjects.TryGetValue(character, out value5) && (Object)(object)value5 != (Object)null)
			{
				Object.Destroy((Object)(object)value5);
				stunIconObjects.Remove(character);
			}
			if (Time.frameCount % 300 != 0)
			{
				return;
			}
			foreach (Character item in (from kvp in stunIconObjects
				where (Object)(object)kvp.Key == (Object)null || (Object)(object)kvp.Value == (Object)null
				select kvp.Key).ToList())
			{
				if (stunIconObjects.TryGetValue(item, out var value6) && (Object)(object)value6 != (Object)null)
				{
					Object.Destroy((Object)(object)value6);
				}
				stunIconObjects.Remove(item);
			}
		}
	}
	[HarmonyPatch(typeof(Player), "TeleportTo")]
	internal static class PlayerTeleportPatch
	{
		[CompilerGenerated]
		private sealed class <ManagePetsGroundPlacement>d__7 : IEnumerator<object>, IDisposable, IEnumerator
		{
			private int <>1__state;

			private object <>2__current;

			public List<Character> petsToAttemptTeleport;

			public Vector3 playerDestinationPos;

			public Quaternion playerDestinationRot;

			public Player teleportingPlayer;

			private float <minDistanceFromPlayer>5__2;

			private int <maxAttempts>5__3;

			private int <i>5__4;

			private Character <pet>5__5;

			private string <currentPetName>5__6;

			private Vector3 <finalTargetPositionForPet>5__7;

			private Rigidbody <rb>5__8;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <ManagePetsGroundPlacement>d__7(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<pet>5__5 = null;
				<currentPetName>5__6 = null;
				<rb>5__8 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_03fa: Unknown result type (might be due to invalid IL or missing references)
				//IL_0404: Expected O, but got Unknown
				//IL_031b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0320: Unknown result type (might be due to invalid IL or missing references)
				//IL_032d: Unknown result type (might be due to invalid IL or missing references)
				//IL_033f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0342: Unknown result type (might be due to invalid IL or missing references)
				//IL_03a5: Unknown result type (might be due to invalid IL or missing references)
				//IL_036e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0385: Unknown result type (might be due to invalid IL or missing references)
				//IL_0448: Unknown result type (might be due to invalid IL or missing references)
				//IL_044e: Unknown result type (might be due to invalid IL or missing references)
				//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
				//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
				//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
				//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
				//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
				//IL_013f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0144: Unknown result type (might be due to invalid IL or missing references)
				//IL_0149: Unknown result type (might be due to invalid IL or missing references)
				//IL_014e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0151: Unknown result type (might be due to invalid IL or missing references)
				//IL_0156: Unknown result type (might be due to invalid IL or missing references)
				//IL_015a: Unknown result type (might be due to invalid IL or missing references)
				//IL_015f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0164: Unknown result type (might be due to invalid IL or missing references)
				//IL_0172: Unknown result type (might be due to invalid IL or missing references)
				//IL_017c: Unknown result type (might be due to invalid IL or missing references)
				//IL_017e: Unknown result type (might be due to invalid IL or missing references)
				//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
				//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
				//IL_01cb: Unknown result type (might be due to invalid IL or missing references)
				//IL_019c: Unknown result type (might be due to invalid IL or missing references)
				//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
				//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
				//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
				//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
				//IL_01be: Unknown result type (might be due to invalid IL or missing references)
				//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
				//IL_020c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0279: Unknown result type (might be due to invalid IL or missing references)
				//IL_027f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0295: Unknown result type (might be due to invalid IL or missing references)
				//IL_029f: Expected O, but got Unknown
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					if (petsToAttemptTeleport.Count == 0)
					{
						return false;
					}
					<minDistanceFromPlayer>5__2 = 5f;
					<maxAttempts>5__3 = 10;
					<i>5__4 = 0;
					goto IL_03de;
				case 1:
					<>1__state = -1;
					if (!((Object)(object)<pet>5__5 == (Object)null) && !<pet>5__5.IsDead())
					{
						if ((Object)(object)<rb>5__8 != (Object)null)
						{
							BetterTamesPlugin.LogIfDebug($"{<currentPetName>5__6} Physik wird aktiviert, isKinematic: {<rb>5__8.isKinematic}", DebugFeature.TeleportFollow);
							<rb>5__8.isKinematic = false;
						}
						Vector3 position = ((Component)<pet>5__5).transform.position;
						BetterTamesPlugin.LogIfDebug($"{<currentPetName>5__6} Position nach Teleport: {position}", DebugFeature.TeleportFollow);
						float num3 = Vector3.Distance(position, <finalTargetPositionForPet>5__7);
						if (num3 > 2f)
						{
							BetterTamesPlugin.LogIfDebug($"{<currentPetName>5__6} nicht an Position {<finalTargetPositionForPet>5__7} (Abstand={num3:F2}m). Aktuelle Pos: {position}", DebugFeature.TeleportFollow);
						}
						else
						{
							BetterTamesPlugin.LogIfDebug($"{<currentPetName>5__6} erfolgreich teleportiert zu {position}", DebugFeature.TeleportFollow);
						}
						<pet>5__5 = null;
						<currentPetName>5__6 = null;
						<rb>5__8 = null;
					}
					goto IL_03cc;
				case 2:
					{
						<>1__state = -1;
						int num = 0;
						foreach (Character item in petsToAttemptTeleport)
						{
							if ((Object)(object)item != (Object)null && !item.IsDead())
							{
								float num2 = Vector3.Distance(((Component)item).transform.position, playerDestinationPos);
								BetterTamesPlugin.LogIfDebug($"{PetTeleporterLogic.GetPrefabName(item)} Abstand zum Spieler: {num2:F2}m", DebugFeature.TeleportFollow);
								if (num2 < 30f)
								{
									num++;
								}
							}
						}
						return false;
					}
					IL_03cc:
					<i>5__4++;
					goto IL_03de;
					IL_03de:
					if (<i>5__4 < petsToAttemptTeleport.Count)
					{
						<pet>5__5 = petsToAttemptTeleport[<i>5__4];
						if (!((Object)(object)<pet>5__5 == (Object)null) && !<pet>5__5.IsDead() && Object.op_Implicit((Object)(object)((Component)<pet>5__5).gameObject) && ((Component)<pet>5__5).gameObject.activeInHierarchy)
						{
							<currentPetName>5__6 = PetTeleporterLogic.GetPrefabName(<pet>5__5);
							float petFootprintRadius = GetPetFootprintRadius(<pet>5__5);
							Vector3 val = Vector3.zero;
							bool flag = false;
							Vector3 position2 = ((Component)<pet>5__5).transform.position;
							BetterTamesPlugin.LogIfDebug($"{<currentPetName>5__6} Position vor Teleport: {position2}", DebugFeature.TeleportFollow);
							for (int i = 0; i < <maxAttempts>5__3; i++)
							{
								float num4 = Random.Range(0, 360);
								float num5 = Random.Range(<minDistanceFromPlayer>5__2, <minDistanceFromPlayer>5__2 + 10f);
								Vector3 val2 = Quaternion.Euler(0f, num4, 0f) * Vector3.forward;
								Vector3 val3 = playerDestinationPos + val2 * num5;
								val3.y = 0f;
								if (IsPositionFree(val3, petFootprintRadius))
								{
									val = val3;
									flag = true;
									break;
								}
							}
							if (!flag)
							{
								val = playerDestinationPos + playerDestinationRot * Vector3.forward * (<minDistanceFromPlayer>5__2 + petFootprintRadius);
							}
							<finalTargetPositionForPet>5__7 = val;
							float y = default(float);
							if (Heightmap.GetHeight(val, ref y))
							{
								<finalTargetPositionForPet>5__7.y = y;
							}
							else
							{
								<finalTargetPositionForPet>5__7.y = playerDestinationPos.y + 5f;
							}
							BetterTamesPlugin.LogIfDebug($"{<currentPetName>5__6} Zielposition: {<finalTargetPositionForPet>5__7}", DebugFeature.TeleportFollow);
							<rb>5__8 = ((Component)<pet>5__5).GetComponent<Rigidbody>();
							if ((Object)(object)<rb>5__8 != (Object)null)
							{
								BetterTamesPlugin.LogIfDebug($"{<currentPetName>5__6} Physik deaktiviert, isKinematic: {<rb>5__8.isKinematic}", DebugFeature.TeleportFollow);
								<rb>5__8.isKinematic = true;
							}
							PetTeleporterLogic.TeleportPetToActualPosition(<pet>5__5, <finalTargetPositionForPet>5__7, playerDestinationRot, teleportingPlayer);
							<>2__current = (object)new WaitForSeconds(0.5f);
							<>1__state = 1;
							return true;
						}
						goto IL_03cc;
					}
					<>2__current = (object)new WaitForSeconds(0.2f);
					<>1__state = 2;
					return true;
				}
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		private const float SearchRadiusForPetsOriginalLocation = 60f;

		private const float VerticalToleranceForPets = 50f;

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

		private static void Prefix(Player __instance, Vector3 pos, Quaternion rot, bool distantTeleport)
		{
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			s_petsToTeleportGlobal.Clear();
			if ((Object)(object)__instance == (Object)null || !((