Decompiled source of ReturnHome v1.0.0

plugins\ReturnHome.dll

Decompiled 2 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("ReturnHome")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ReturnHome")]
[assembly: AssemblyTitle("ReturnHome")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace ReturnHome;

[BepInPlugin("com.jimmy.valheim.returnhome", "Return Home", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
public sealed class ReturnHomePlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Humanoid), "UseItem")]
	private static class UseRecallStonePatch
	{
		private static bool Prefix(Humanoid __instance, ItemData item)
		{
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: 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_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0118: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)item?.m_dropPrefab == (Object)null || ((Object)item.m_dropPrefab).name != "ReturnHomeStone")
			{
				return true;
			}
			Player val = (Player)(object)((__instance is Player) ? __instance : null);
			if ((Object)(object)val == (Object)null || (Object)(object)val != (Object)(object)Player.m_localPlayer)
			{
				return false;
			}
			Game instance = Game.instance;
			PlayerProfile val2 = ((instance != null) ? instance.GetPlayerProfile() : null);
			if (val2 == null || !val2.HaveCustomSpawnPoint())
			{
				ShowMessage(val, "Claim a bed at your main base first.");
				return false;
			}
			double totalSeconds = (DateTime.UtcNow - LastUseUtc).TotalSeconds;
			float num = Mathf.Max(0f, CooldownSeconds.Value);
			if (totalSeconds < (double)num)
			{
				int num2 = Mathf.CeilToInt(num - (float)totalSeconds);
				ShowMessage(val, $"The stone is recharging: {num2}s");
				return false;
			}
			if (BlockRestrictedItems.Value && HasRestrictedItems(((Humanoid)val).GetInventory()))
			{
				ShowMessage(val, "The stone cannot carry metal or other restricted cargo.");
				return false;
			}
			Vector3 val3 = val2.GetCustomSpawnPoint() + Vector3.up * 0.5f;
			if (!((Character)val).TeleportTo(val3, ((Component)val).transform.rotation, true))
			{
				ShowMessage(val, "The recall failed. Try again in a clear area.");
				return false;
			}
			LastUseUtc = DateTime.UtcNow;
			ShowMessage(val, "The stone calls you home...");
			return false;
		}

		private static bool HasRestrictedItems(Inventory inventory)
		{
			if (inventory == null)
			{
				return false;
			}
			foreach (ItemData allItem in inventory.GetAllItems())
			{
				if (allItem?.m_shared != null && !allItem.m_shared.m_teleportable)
				{
					return true;
				}
			}
			return false;
		}
	}

	public const string PluginGuid = "com.jimmy.valheim.returnhome";

	public const string PluginName = "Return Home";

	public const string PluginVersion = "1.0.0";

	internal const string ItemPrefabName = "ReturnHomeStone";

	internal const string ItemDisplayName = "Return Home Stone";

	internal static ConfigEntry<float> CooldownSeconds;

	internal static ConfigEntry<bool> BlockRestrictedItems;

	internal static ConfigEntry<int> WoodCost;

	internal static ConfigEntry<int> LeatherScrapsCost;

	internal static ConfigEntry<int> DeerHideCost;

	internal static DateTime LastUseUtc = DateTime.MinValue;

	private Harmony harmony;

	private float nextCountdownUpdate;

	private bool countdownWasActive;

	private void Awake()
	{
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: Expected O, but got Unknown
		CooldownSeconds = ((BaseUnityPlugin)this).Config.Bind<float>("Recall", "CooldownSeconds", 300f, "Seconds before the stone may be used again. Set to 0 for no cooldown.");
		BlockRestrictedItems = ((BaseUnityPlugin)this).Config.Bind<bool>("Recall", "RespectPortalRestrictions", true, "When true, the stone cannot be used while carrying ore or other non-teleportable items.");
		WoodCost = ((BaseUnityPlugin)this).Config.Bind<int>("Recipe", "Wood", 10, "Wood required to craft the stone.");
		LeatherScrapsCost = ((BaseUnityPlugin)this).Config.Bind<int>("Recipe", "LeatherScraps", 5, "Leather scraps required to craft the stone.");
		DeerHideCost = ((BaseUnityPlugin)this).Config.Bind<int>("Recipe", "DeerHide", 2, "Deer hide required to craft the stone.");
		PrefabManager.OnVanillaPrefabsAvailable += RegisterRecallStone;
		harmony = new Harmony("com.jimmy.valheim.returnhome");
		harmony.PatchAll();
	}

	private void OnDestroy()
	{
		PrefabManager.OnVanillaPrefabsAvailable -= RegisterRecallStone;
		Harmony obj = harmony;
		if (obj != null)
		{
			obj.UnpatchSelf();
		}
	}

	private void Update()
	{
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null || LastUseUtc == DateTime.MinValue)
		{
			return;
		}
		int num = Mathf.CeilToInt(Mathf.Max(0f, CooldownSeconds.Value) - (float)(DateTime.UtcNow - LastUseUtc).TotalSeconds);
		if (num > 0)
		{
			countdownWasActive = true;
			if (Time.unscaledTime >= nextCountdownUpdate)
			{
				int num2 = num / 60;
				int num3 = num % 60;
				((Character)localPlayer).Message((MessageType)1, $"Return Home: {num2}:{num3:00} remaining", 0, (Sprite)null, false);
				nextCountdownUpdate = Time.unscaledTime + 1f;
			}
		}
		else if (countdownWasActive)
		{
			((Character)localPlayer).Message((MessageType)1, "Return Home: Ready", 0, (Sprite)null, false);
			countdownWasActive = false;
			LastUseUtc = DateTime.MinValue;
		}
	}

	private void RegisterRecallStone()
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: 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_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Expected O, but got Unknown
		//IL_0091: Unknown result type (might be due to invalid IL or missing references)
		//IL_0097: Expected O, but got Unknown
		ItemConfig val = new ItemConfig
		{
			Name = "Return Home Stone",
			Description = "A rune-bound stone that recalls you to your claimed bed. Right-click to use.",
			CraftingStation = "piece_workbench",
			MinStationLevel = 2,
			Amount = 1,
			Weight = 1f,
			StackSize = 1
		};
		AddRequirement(val, "Wood", WoodCost.Value);
		AddRequirement(val, "LeatherScraps", LeatherScrapsCost.Value);
		AddRequirement(val, "DeerHide", DeerHideCost.Value);
		CustomItem val2 = new CustomItem("ReturnHomeStone", "Wishbone", val);
		ItemManager.Instance.AddItem(val2);
		PrefabManager.OnVanillaPrefabsAvailable -= RegisterRecallStone;
	}

	private static void AddRequirement(ItemConfig config, string item, int amount)
	{
		//IL_0009: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Expected O, but got Unknown
		if (amount > 0)
		{
			config.AddRequirement(new RequirementConfig(item, amount, 0, true));
		}
	}

	internal static void ShowMessage(Player player, string message)
	{
		((Character)player).Message((MessageType)2, message, 0, (Sprite)null, false);
	}
}