Decompiled source of Wormhole Potion v0.1.0

RegisideWormholePotion.dll

Decompiled 11 hours ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using HarmonyLib;
using Jotunn;
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.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("RegisideWormholePotion")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RegisideWormholePotion")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")]
[assembly: AssemblyFileVersion("0.0.1")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.1.0")]
namespace RegisideWormholePotion;

[BepInPlugin("com.jotunn.ValheimTeleportPlugin", "Teleport to Player (Jotunn)", "1.0.2")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
public class RegisideWormholePotion : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Minimap), "OnMapLeftClick")]
	private static class Minimap_OnMapLeftClick_Patch
	{
		private static bool Prefix(Minimap __instance)
		{
			//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_004d: 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_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)Player.m_localPlayer != (Object)null && !((Character)Player.m_localPlayer).m_seman.HaveStatusEffect("WormholeEffect".GetHashCode()))
			{
				return true;
			}
			Vector3 position = __instance.ScreenToWorldPoint(Input.mousePosition);
			Player val = FindClosestPlayer(position);
			if ((Object)(object)val == (Object)null)
			{
				Logger.LogInfo((object)"No player found near the clicked position.");
				MessageHud.instance.ShowMessage((MessageType)2, "No player found nearby.", 0, (Sprite)null);
				return true;
			}
			((Character)localPlayer).TeleportTo(((Component)val).transform.position, ((Component)localPlayer).transform.rotation, true);
			Logger.LogInfo((object)("Teleported to " + val.GetPlayerName()));
			Inventory inventory = ((Humanoid)localPlayer).m_inventory;
			string text = "$item_wormholePotion";
			ItemData item = inventory.GetItem(text, -1, false);
			if (item != null)
			{
				inventory.RemoveItem(item, 1);
				Logger.LogInfo((object)("Removed one " + text + " from inventory."));
			}
			else
			{
				Logger.LogInfo((object)("Item " + text + " not found in inventory."));
			}
			if ((Object)(object)Minimap.instance != (Object)null)
			{
				Minimap.instance.SetMapMode((MapMode)1);
			}
			return false;
		}

		private static Player FindClosestPlayer(Vector3 position)
		{
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			float num = 60f;
			Player val = null;
			foreach (Player allPlayer in Player.GetAllPlayers())
			{
				Logger.LogInfo((object)allPlayer.GetPlayerName());
				if (!((Object)(object)allPlayer == (Object)(object)Player.m_localPlayer))
				{
					float num2 = Vector3.Distance(position, ((Component)allPlayer).transform.position);
					Logger.LogInfo((object)(allPlayer.GetPlayerName() + " Distance: " + num2));
					if (num2 < num)
					{
						num = num2;
						val = allPlayer;
					}
				}
			}
			if ((Object)(object)val != (Object)null)
			{
				Logger.LogInfo((object)("Closest Player is: " + val.GetPlayerName()));
			}
			return val;
		}
	}

	[HarmonyPatch(typeof(Player), "ConsumeItem")]
	public static class Player_ConsumeItem_Patch
	{
		public static bool Prefix(Player __instance, Inventory inventory, ItemData item, bool checkWorldLevel = false)
		{
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			if (item == null || item.m_shared.m_name != "$item_wormholePotion")
			{
				return true;
			}
			if (!inventoryCanPortal(inventory))
			{
				MessageHud.instance.ShowMessage((MessageType)2, "The weight of the world binds you. Try dropping your ores.", 0, (Sprite)null);
				return false;
			}
			if ((Object)(object)((Character)__instance).m_zanim != (Object)null)
			{
				Logger.LogWarning((object)"Playing 'eat' animation.");
				((Character)__instance).m_zanim.SetTrigger("eat");
			}
			else
			{
				Logger.LogWarning((object)"Player animation controller (m_zanim) is null!");
			}
			if (((Humanoid)__instance).m_consumeItemEffects != null)
			{
				((Humanoid)__instance).m_consumeItemEffects.Create(((Component)__instance).transform.position, Quaternion.identity, (Transform)null, 1f, -1);
			}
			else
			{
				Logger.LogWarning((object)"Consume item effects not found!");
			}
			StatusEffect statusEffect = ObjectDB.instance.GetStatusEffect("WormholeEffect".GetHashCode());
			StatusEffect val = ((Character)__instance).m_seman.AddStatusEffect(statusEffect, false, 0, 0f);
			if ((Object)(object)val != (Object)null)
			{
				Logger.LogInfo((object)"Wormhole effect applied.");
			}
			else
			{
				Logger.LogWarning((object)"Failed to apply wormhole effect.");
			}
			Logger.LogInfo((object)"began wormhole potion");
			if ((Object)(object)InventoryGui.instance != (Object)null && InventoryGui.IsVisible())
			{
				InventoryGui.instance.Hide();
			}
			if ((Object)(object)Minimap.instance != (Object)null)
			{
				Minimap.instance.SetMapMode((MapMode)2);
			}
			return false;
		}
	}

	[HarmonyPatch(typeof(Minimap), "SetMapMode")]
	public static class Minimap_SetMapMode_Patch
	{
		public static void Postfix(MapMode mode)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Invalid comparison between Unknown and I4
			if ((int)mode != 2)
			{
				Player localPlayer = Player.m_localPlayer;
				if ((Object)(object)localPlayer == (Object)null)
				{
					Logger.LogWarning((object)"Local player not found.");
				}
				else if (((Character)Player.m_localPlayer).m_seman.HaveStatusEffect("WormholeEffect".GetHashCode()))
				{
					((Character)localPlayer).m_seman.RemoveStatusEffect("WormholeEffect".GetHashCode(), false);
					Logger.LogInfo((object)"Wormhole effect removed by map close.");
				}
			}
		}
	}

	public const string PluginGUID = "com.jotunn.RegisideWormholePotion";

	public const string PluginName = "RegisideWormholePotion";

	public const string PluginVersion = "0.0.1";

	private CustomStatusEffect WormholeEffect;

	private CustomLocalization Localization;

	private readonly Harmony harmony = new Harmony("com.jotunn.ValheimTeleportPlugin");

	private void Awake()
	{
		AddStatusEffects();
		PrefabManager.OnVanillaPrefabsAvailable += AddClonedItems;
		AddLocalizations();
		harmony.PatchAll();
		Logger.LogInfo((object)"RegisideWormholePotion is loaded");
	}

	private void OnDestroy()
	{
		harmony.UnpatchSelf();
	}

	private void AddLocalizations()
	{
		Localization = LocalizationManager.Instance.GetLocalization();
		CustomLocalization localization = Localization;
		string text = "English";
		localization.AddTranslation(ref text, new Dictionary<string, string>
		{
			{ "item_wormholePotion", "Potion of Frith’s Bond" },
			{ "item_wormholePotion_desc", "This elixir binds the threads of fate between companions. With a single sip, the distance melts away, and you are carried to the side of your shield-brothers and sisters, no matter how far the battle has taken them." },
			{ "wormhole_effect", "Frith’s Bond" },
			{ "wormhole_effect_desc", "Teleport to the location of a chosen ally. Cannot be used within realms barred by the gods." },
			{ "wormhole_effectstart", "Choose a friend to be reunited with." },
			{ "wormhole_effectstop", "Wormhole effect removed." }
		});
	}

	private static bool inventoryCanPortal(Inventory inventory)
	{
		foreach (ItemData allItem in inventory.GetAllItems())
		{
			if (!allItem.m_shared.m_teleportable)
			{
				return false;
			}
		}
		return true;
	}

	private void AddStatusEffects()
	{
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: 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_005a: Expected O, but got Unknown
		StatusEffect val = ScriptableObject.CreateInstance<StatusEffect>();
		((Object)val).name = "WormholeEffect";
		val.m_name = "$wormhole_effect";
		val.m_tooltip = "$wormhole_effect_desc";
		val.m_startMessageType = (MessageType)2;
		val.m_startMessage = "$wormhole_effectstart";
		val.m_stopMessageType = (MessageType)2;
		val.m_stopMessage = "$wormhole_effectstop";
		WormholeEffect = new CustomStatusEffect(val, false);
		ItemManager.Instance.AddStatusEffect(WormholeEffect);
	}

	private void AddClonedItems()
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Expected O, but got Unknown
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Expected O, but got Unknown
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Expected O, but got Unknown
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_006f: Expected O, but got Unknown
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0081: Expected O, but got Unknown
		ItemConfig val = new ItemConfig
		{
			Name = "$item_wormholePotion",
			Description = "$item_wormholePotion_desc",
			CraftingStation = CraftingStations.Cauldron,
			MinStationLevel = 2
		};
		val.AddRequirement(new RequirementConfig("MushroomYellow", 10, 0, false));
		val.AddRequirement(new RequirementConfig("Raspberry", 3, 0, false));
		val.AddRequirement(new RequirementConfig("Fish2", 1, 0, false));
		CustomItem val2 = new CustomItem("WormholePotion", "MeadPoisonResist", val);
		GameObject itemPrefab = val2.ItemPrefab;
		if ((Object)(object)itemPrefab != (Object)null)
		{
			ItemDrop component = itemPrefab.GetComponent<ItemDrop>();
			if ((Object)(object)component != (Object)null)
			{
				component.m_itemData.m_shared.m_food = 0f;
				component.m_itemData.m_shared.m_foodStamina = 0f;
				component.m_itemData.m_shared.m_foodBurnTime = 0f;
				component.m_itemData.m_shared.m_consumeStatusEffect = WormholeEffect.StatusEffect;
			}
		}
		ItemManager.Instance.AddItem(val2);
		PrefabManager.OnVanillaPrefabsAvailable -= AddClonedItems;
	}

	private static void SetPlayerVisibility(Player player, bool isVisible)
	{
		Renderer[] componentsInChildren = ((Component)player).GetComponentsInChildren<Renderer>();
		foreach (Renderer val in componentsInChildren)
		{
			val.enabled = isVisible;
		}
	}
}