Decompiled source of EndosCoinPurse v1.0.5

EndosCoinPurse.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ValheimMod")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("FSOS-X R10 EXP")]
[assembly: AssemblyProduct("ValheimMod")]
[assembly: AssemblyCopyright("Copyright © FSOS-X R10 EXP 2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("ad339bed-d2c1-4e38-afdc-8494abc8ceeb")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace ValheimMuntBuidelHUD;

public class BuidelClickComponent : MonoBehaviour, IPointerClickHandler, IEventSystemHandler, IPointerEnterHandler, IPointerExitHandler
{
	public void OnPointerEnter(PointerEventData eventData)
	{
	}

	public void OnPointerExit(PointerEventData eventData)
	{
	}

	public void OnPointerClick(PointerEventData eventData)
	{
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
		Player localPlayer = Player.m_localPlayer;
		InventoryGui instance = InventoryGui.instance;
		if ((Object)(object)localPlayer == (Object)null || (Object)(object)instance == (Object)null)
		{
			return;
		}
		if ((int)eventData.button == 0)
		{
			FieldInfo field = typeof(InventoryGui).GetField("m_dragItem", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			FieldInfo field2 = typeof(InventoryGui).GetField("m_dragInventory", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			MethodInfo method = typeof(InventoryGui).GetMethod("SetupDragItem", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (field == null)
			{
				field = typeof(InventoryGui).GetField("dragItem", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			}
			if (field2 == null)
			{
				field2 = typeof(InventoryGui).GetField("dragInventory", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			}
			if (method == null)
			{
				method = typeof(InventoryGui).GetMethod("SetDragItem", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			}
			ItemData val = ((!(field != null)) ? ((ItemData)null) : ((ItemData)field.GetValue(instance)));
			Inventory val2 = ((!(field2 != null)) ? ((Inventory)null) : ((Inventory)field2.GetValue(instance)));
			if (val != null)
			{
				string name = val.m_shared.m_name;
				int num = 0;
				switch (name)
				{
				case "$item_coins":
					num = 1;
					break;
				case "$item_ruby":
					num = 20;
					break;
				case "$item_amber":
					num = 5;
					break;
				case "$item_amberpearl":
					num = 10;
					break;
				case "$item_silvernecklace":
					num = 30;
					break;
				}
				if (num > 0)
				{
					int num2 = val.m_stack * num;
					ItemData item = EndosCoinPurseMod.VirtueleBuidelInv.GetItem("$item_coins", -1, false);
					if (item == null)
					{
						GameObject itemPrefab = ObjectDB.instance.GetItemPrefab("Coins");
						if ((Object)(object)itemPrefab != (Object)null)
						{
							EndosCoinPurseMod.VirtueleBuidelInv.AddItem(itemPrefab, num2);
							if (val2 != null)
							{
								val2.RemoveItem(val);
							}
							ClearDragItem(method, instance);
						}
					}
					else
					{
						item.m_stack += num2;
						if (val2 != null)
						{
							val2.RemoveItem(val);
						}
						ClearDragItem(method, instance);
					}
				}
			}
			else if (val == null)
			{
				ItemData item2 = EndosCoinPurseMod.VirtueleBuidelInv.GetItem("$item_coins", -1, false);
				if (item2 != null && item2.m_stack > 0)
				{
					GameObject itemPrefab2 = ObjectDB.instance.GetItemPrefab("Coins");
					if ((Object)(object)itemPrefab2 != (Object)null)
					{
						int maxStackSize = itemPrefab2.GetComponent<ItemDrop>().m_itemData.m_shared.m_maxStackSize;
						int num3 = Mathf.Min(item2.m_stack, maxStackSize);
						InventoryPickupPatch.BlokkeerAutomatischeBuidel = true;
						ItemData val3 = ((Humanoid)localPlayer).PickupPrefab(itemPrefab2, num3, true);
						InventoryPickupPatch.BlokkeerAutomatischeBuidel = false;
						if (val3 != null)
						{
							item2.m_stack -= num3;
							if (item2.m_stack <= 0)
							{
								EndosCoinPurseMod.VirtueleBuidelInv.RemoveItem(item2);
							}
							if (method != null)
							{
								try
								{
									method.Invoke(instance, new object[3]
									{
										val3,
										((Humanoid)localPlayer).GetInventory(),
										val3.m_stack
									});
								}
								catch
								{
									method.Invoke(instance, new object[2]
									{
										val3,
										((Humanoid)localPlayer).GetInventory()
									});
								}
							}
						}
					}
				}
			}
		}
		EndosCoinPurseMod.UpdateBuidelCount();
		instance.m_playerGrid.UpdateInventory(((Humanoid)localPlayer).GetInventory(), localPlayer, (ItemData)null);
	}

	private void ClearDragItem(MethodInfo setupDragMethod, InventoryGui gui)
	{
		if (setupDragMethod != null)
		{
			try
			{
				setupDragMethod.Invoke(gui, new object[3] { null, null, 0 });
			}
			catch
			{
				setupDragMethod.Invoke(gui, null);
			}
		}
	}
}
[BepInPlugin("EndosCoinPurse", "EndosCoinPurseMod", "1.0.5")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class EndosCoinPurseMod : BaseUnityPlugin
{
	public const string ModGUID = "EndosCoinPurse";

	public const string ModName = "EndosCoinPurseMod";

	public const string ModVersion = "1.0.5";

	private Harmony harmony;

	public static GameObject BuidelPanel;

	public static Text BuidelText;

	public static Inventory VirtueleBuidelInv;

	public static ConfigEntry<float> ConfigPosX;

	public static ConfigEntry<float> ConfigPosY;

	public static void CreateBuidelSlotUI(InventoryGui gui)
	{
		if ((Object)(object)gui != (Object)null && ((Component)gui).gameObject.activeInHierarchy)
		{
			EndosCoinPurseMod endosCoinPurseMod = Object.FindAnyObjectByType<EndosCoinPurseMod>();
			if ((Object)(object)endosCoinPurseMod != (Object)null)
			{
				((MonoBehaviour)endosCoinPurseMod).StartCoroutine(DelayedCreateUI(gui));
			}
		}
	}

	private static IEnumerator DelayedCreateUI(InventoryGui gui)
	{
		yield return (object)new WaitForSecondsRealtime(0.15f);
		if ((Object)(object)gui == (Object)null || (Object)(object)BuidelPanel != (Object)null)
		{
			yield break;
		}
		GameObject val = GameObject.Find("ArmorSlots") ?? GameObject.Find("EquipmentAndQuickSlots") ?? GameObject.Find("EquipmentPanel") ?? GameObject.Find("EquipmentGui");
		if ((Object)(object)val == (Object)null && (Object)(object)((Component)gui).transform.parent != (Object)null)
		{
			Transform[] componentsInChildren = ((Component)((Component)gui).transform.parent).GetComponentsInChildren<Transform>(true);
			foreach (Transform val2 in componentsInChildren)
			{
				if ((((Object)((Component)val2).gameObject).name.Contains("Equipment") || ((Object)((Component)val2).gameObject).name.Contains("ArmorSlots")) && ((Component)val2).gameObject.activeInHierarchy)
				{
					val = ((Component)val2).gameObject;
					break;
				}
			}
		}
		Transform val3 = ((!((Object)(object)val != (Object)null)) ? ((Component)gui.m_playerGrid).transform.parent : val.transform);
		if ((Object)(object)val3 == (Object)null)
		{
			yield break;
		}
		BuidelPanel = new GameObject("BuidelSlotHUD", new Type[4]
		{
			typeof(RectTransform),
			typeof(Image),
			typeof(CanvasGroup),
			typeof(BuidelClickComponent)
		});
		BuidelPanel.transform.SetParent(val3, false);
		Image component = BuidelPanel.GetComponent<Image>();
		((Graphic)component).raycastTarget = true;
		Sprite val4 = null;
		Image[] array = Resources.FindObjectsOfTypeAll<Image>();
		foreach (Image val5 in array)
		{
			if ((Object)(object)val5 != (Object)null && (Object)(object)val5.sprite != (Object)null && (((Object)val5.sprite).name == "ac_bkg" || ((Object)val5.sprite).name == "stat_bkg" || ((Object)val5.sprite).name == "bkg"))
			{
				val4 = val5.sprite;
				((Graphic)component).material = ((Graphic)val5).material;
				((Graphic)component).color = ((Graphic)val5).color;
				break;
			}
		}
		if ((Object)(object)val4 == (Object)null)
		{
			GameObject val6 = GameObject.Find("ac") ?? GameObject.Find("ac_bkg") ?? GameObject.Find("Armor");
			if ((Object)(object)val6 != (Object)null)
			{
				Image val7 = val6.GetComponent<Image>() ?? val6.GetComponentInChildren<Image>(true);
				if ((Object)(object)val7 != (Object)null && (Object)(object)val7.sprite != (Object)null)
				{
					val4 = val7.sprite;
					((Graphic)component).material = ((Graphic)val7).material;
					((Graphic)component).color = ((Graphic)val7).color;
				}
			}
		}
		if ((Object)(object)val4 != (Object)null)
		{
			component.sprite = val4;
			component.type = (Type)0;
		}
		else
		{
			((Graphic)component).color = new Color(0.2f, 0.15f, 0.12f, 0.95f);
		}
		RectTransform component2 = BuidelPanel.GetComponent<RectTransform>();
		component2.anchorMin = new Vector2(0f, 1f);
		component2.anchorMax = new Vector2(0f, 1f);
		component2.pivot = new Vector2(0.5f, 0.5f);
		component2.sizeDelta = new Vector2(54f, 60f);
		UpdateBuidelPosition();
		GameObject val8 = new GameObject("SlotBinnenzijde", new Type[2]
		{
			typeof(RectTransform),
			typeof(Image)
		});
		val8.transform.SetParent(BuidelPanel.transform, false);
		Image component3 = val8.GetComponent<Image>();
		((Graphic)component3).raycastTarget = false;
		((Graphic)component3).color = new Color(0f, 0f, 0f, 0f);
		RectTransform component4 = val8.GetComponent<RectTransform>();
		component4.anchorMin = new Vector2(0f, 0f);
		component4.anchorMax = new Vector2(1f, 1f);
		component4.offsetMin = new Vector2(0f, 0f);
		component4.offsetMax = new Vector2(0f, 0f);
		GameObject val9 = new GameObject("BuidelIcon", new Type[2]
		{
			typeof(RectTransform),
			typeof(Image)
		});
		val9.transform.SetParent(val8.transform, false);
		Image component5 = val9.GetComponent<Image>();
		((Graphic)component5).raycastTarget = false;
		if ((Object)(object)ObjectDB.instance != (Object)null)
		{
			GameObject itemPrefab = ObjectDB.instance.GetItemPrefab("Coins");
			if ((Object)(object)itemPrefab != (Object)null)
			{
				ItemDrop component6 = itemPrefab.GetComponent<ItemDrop>();
				if ((Object)(object)component6 != (Object)null && component6.m_itemData.m_shared.m_icons != null && component6.m_itemData.m_shared.m_icons.Length != 0)
				{
					component5.sprite = component6.m_itemData.m_shared.m_icons[0];
					((Graphic)component5).color = Color.white;
					((Graphic)component5).material = null;
				}
			}
		}
		RectTransform component7 = val9.GetComponent<RectTransform>();
		component7.anchorMin = new Vector2(0.5f, 1f);
		component7.anchorMax = new Vector2(0.5f, 1f);
		component7.pivot = new Vector2(0.5f, 0.5f);
		component7.sizeDelta = new Vector2(44f, 44f);
		component7.anchoredPosition = new Vector2(0f, -10f);
		GameObject val10 = new GameObject("BuidelText", new Type[2]
		{
			typeof(RectTransform),
			typeof(Text)
		});
		val10.transform.SetParent(val8.transform, false);
		BuidelText = val10.GetComponent<Text>();
		((Graphic)BuidelText).raycastTarget = false;
		Font[] array2 = Resources.FindObjectsOfTypeAll<Font>();
		foreach (Font val11 in array2)
		{
			if (((Object)val11).name.Contains("Averia"))
			{
				BuidelText.font = val11;
				break;
			}
		}
		if ((Object)(object)BuidelText.font == (Object)null)
		{
			? val12 = BuidelText;
			Object builtinResource = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");
			((Text)val12).font = (Font)(object)((builtinResource is Font) ? builtinResource : null);
		}
		BuidelText.fontSize = 14;
		((Graphic)BuidelText).color = new Color(1f, 0.82f, 0f, 1f);
		BuidelText.alignment = (TextAnchor)4;
		Shadow obj = val10.AddComponent<Shadow>();
		obj.effectColor = Color.black;
		obj.effectDistance = new Vector2(1.5f, -1.5f);
		RectTransform component8 = val10.GetComponent<RectTransform>();
		component8.anchorMin = new Vector2(0f, 0f);
		component8.anchorMax = new Vector2(1f, 1f);
		component8.pivot = new Vector2(0.5f, 0.5f);
		component8.offsetMin = new Vector2(0f, 0f);
		component8.offsetMax = new Vector2(0f, 0f);
		UpdateBuidelCount();
	}

	private void Awake()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Expected O, but got Unknown
		//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c7: Expected O, but got Unknown
		harmony = new Harmony("EndosCoinPurse");
		harmony.PatchAll();
		ConfigPosX = ((BaseUnityPlugin)this).Config.Bind<float>("Position", "Bundle X Position", 230f, "The horizontal position relative to the equipment panel.");
		ConfigPosY = ((BaseUnityPlugin)this).Config.Bind<float>("Position", "Bundle Y Position", -30f, "The vertical position relative to the equipment panel.");
		ConfigPosX.SettingChanged += delegate
		{
			UpdateBuidelPosition();
		};
		ConfigPosY.SettingChanged += delegate
		{
			UpdateBuidelPosition();
		};
		VirtueleBuidelInv = new Inventory("VirtueleMuntbuidel", (Sprite)null, 1, 1);
		Inventory virtueleBuidelInv = VirtueleBuidelInv;
		virtueleBuidelInv.m_onChanged = (Action)Delegate.Combine(virtueleBuidelInv.m_onChanged, new Action(UpdateBuidelCount));
		Logger.LogInfo((object)"EndosCoinPurse successfully loaded with Advanced Auto-Pickup!");
	}

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

	public static void UpdateBuidelPosition()
	{
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)BuidelPanel != (Object)null)
		{
			RectTransform component = BuidelPanel.GetComponent<RectTransform>();
			if ((Object)(object)component != (Object)null)
			{
				component.anchoredPosition = new Vector2(ConfigPosX.Value, ConfigPosY.Value);
			}
		}
	}

	public static void UpdateBuidelCount()
	{
		if (VirtueleBuidelInv != null && !((Object)(object)BuidelText == (Object)null))
		{
			ItemData item = VirtueleBuidelInv.GetItem("$item_coins", -1, false);
			if (item != null && item.m_stack > 0)
			{
				BuidelText.text = item.m_stack.ToString();
			}
			else
			{
				BuidelText.text = "0";
			}
		}
	}

	public static void EmptyVirtueleBuidel()
	{
		if (VirtueleBuidelInv == null)
		{
			return;
		}
		foreach (ItemData item in new List<ItemData>(VirtueleBuidelInv.GetAllItems()))
		{
			VirtueleBuidelInv.RemoveItem(item);
		}
	}
}
[HarmonyPatch(typeof(InventoryGui), "Show")]
public static class InventoryGuiShowPatch
{
	private static void Postfix(InventoryGui __instance)
	{
		if ((Object)(object)EndosCoinPurseMod.BuidelPanel == (Object)null)
		{
			EndosCoinPurseMod.CreateBuidelSlotUI(__instance);
		}
		if ((Object)(object)EndosCoinPurseMod.BuidelPanel != (Object)null)
		{
			EndosCoinPurseMod.BuidelPanel.SetActive(true);
			EndosCoinPurseMod.UpdateBuidelPosition();
			EndosCoinPurseMod.UpdateBuidelCount();
		}
	}
}
[HarmonyPatch(typeof(InventoryGui), "Hide")]
public static class InventoryGuiHidePatch
{
	private static void Postfix()
	{
		if ((Object)(object)EndosCoinPurseMod.BuidelPanel != (Object)null)
		{
			EndosCoinPurseMod.BuidelPanel.SetActive(false);
		}
	}
}
[HarmonyPatch(typeof(Player), "Save")]
public static class PlayerSavePatch
{
	public static void Postfix(Player __instance)
	{
		if (!((Object)(object)__instance != (Object)(object)Player.m_localPlayer))
		{
			int num = 0;
			ItemData item = EndosCoinPurseMod.VirtueleBuidelInv.GetItem("$item_coins", -1, false);
			if (item != null)
			{
				num = item.m_stack;
			}
			__instance.m_customData["buidel_items_hud_count"] = num.ToString();
		}
	}
}
[HarmonyPatch(typeof(Player), "Load")]
public static class PlayerLoadPatch
{
	public static void Postfix(Player __instance)
	{
		if ((Object)(object)__instance != (Object)(object)Player.m_localPlayer)
		{
			return;
		}
		EndosCoinPurseMod.EmptyVirtueleBuidel();
		if (__instance.m_customData.TryGetValue("buidel_items_hud_count", out var value) && int.TryParse(value, out var result) && result > 0)
		{
			if ((Object)(object)ObjectDB.instance != (Object)null)
			{
				GameObject itemPrefab = ObjectDB.instance.GetItemPrefab("Coins");
				if ((Object)(object)itemPrefab != (Object)null)
				{
					EndosCoinPurseMod.VirtueleBuidelInv.AddItem(itemPrefab, result);
					ItemData item = EndosCoinPurseMod.VirtueleBuidelInv.GetItem("$item_coins", -1, false);
					if (item != null)
					{
						item.m_stack = result;
					}
				}
			}
			else
			{
				Logger.LogWarning((object)"ObjectDB not ready during load. Queueing wallet item restoration.");
				((MonoBehaviour)__instance).StartCoroutine(HerstelBuidelWanneerKlaar(result));
			}
		}
		EndosCoinPurseMod.UpdateBuidelCount();
	}

	private static IEnumerator HerstelBuidelWanneerKlaar(int coinCount)
	{
		while ((Object)(object)ObjectDB.instance == (Object)null || (Object)(object)ObjectDB.instance.GetItemPrefab("Coins") == (Object)null)
		{
			yield return (object)new WaitForSecondsRealtime(0.1f);
		}
		GameObject itemPrefab = ObjectDB.instance.GetItemPrefab("Coins");
		if ((Object)(object)itemPrefab != (Object)null && EndosCoinPurseMod.VirtueleBuidelInv != null)
		{
			EndosCoinPurseMod.VirtueleBuidelInv.AddItem(itemPrefab, coinCount);
			ItemData item = EndosCoinPurseMod.VirtueleBuidelInv.GetItem("$item_coins", -1, false);
			if (item != null)
			{
				item.m_stack = coinCount;
			}
			EndosCoinPurseMod.UpdateBuidelCount();
		}
	}
}
[HarmonyPatch(typeof(Inventory), "CountItems")]
public static class InventoryCountItemsPatch
{
	public static void Postfix(Inventory __instance, string name, ref int __result)
	{
		if (name == "$item_coins" && (Object)(object)Player.m_localPlayer != (Object)null && __instance == ((Humanoid)Player.m_localPlayer).GetInventory() && EndosCoinPurseMod.VirtueleBuidelInv != null)
		{
			ItemData item = EndosCoinPurseMod.VirtueleBuidelInv.GetItem("$item_coins", -1, false);
			if (item != null && item.m_stack > 0)
			{
				__result += item.m_stack;
			}
		}
	}
}
[HarmonyPatch(typeof(StoreGui), "OnBuyItem")]
public static class StoreGuiOnBuyItemPatch
{
	public static bool IsHandelBezig;

	public static void Prefix(StoreGui __instance)
	{
		IsHandelBezig = true;
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null || EndosCoinPurseMod.VirtueleBuidelInv == null)
		{
			IsHandelBezig = false;
			return;
		}
		FieldInfo fieldInfo = typeof(StoreGui).GetField("m_selectedItem", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) ?? typeof(StoreGui).GetField("selectedItem", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		if (fieldInfo == null)
		{
			IsHandelBezig = false;
			return;
		}
		object value = fieldInfo.GetValue(__instance);
		if (value == null)
		{
			IsHandelBezig = false;
			return;
		}
		FieldInfo fieldInfo2 = value.GetType().GetField("m_price", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) ?? value.GetType().GetField("price", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		if (fieldInfo2 == null)
		{
			IsHandelBezig = false;
			return;
		}
		int num = (int)fieldInfo2.GetValue(value);
		if (num <= 0)
		{
			IsHandelBezig = false;
			return;
		}
		int num2 = 0;
		foreach (ItemData allItem in ((Humanoid)localPlayer).GetInventory().GetAllItems())
		{
			if (allItem.m_shared.m_name == "$item_coins")
			{
				num2 += allItem.m_stack;
			}
		}
		if (num2 < num)
		{
			int num3 = num - num2;
			ItemData item = EndosCoinPurseMod.VirtueleBuidelInv.GetItem("$item_coins", -1, false);
			if (item != null && item.m_stack >= num3)
			{
				item.m_stack -= num3;
				GameObject itemPrefab = ObjectDB.instance.GetItemPrefab("Coins");
				if ((Object)(object)itemPrefab != (Object)null)
				{
					((Humanoid)localPlayer).GetInventory().AddItem(itemPrefab, num3);
				}
				if (item.m_stack <= 0)
				{
					EndosCoinPurseMod.VirtueleBuidelInv.RemoveItem(item);
				}
				EndosCoinPurseMod.UpdateBuidelCount();
			}
		}
		IsHandelBezig = false;
	}

	public static void Postfix()
	{
		IsHandelBezig = false;
	}
}
[HarmonyPatch(typeof(Inventory))]
public static class InventoryPickupPatch
{
	public static bool BlokkeerAutomatischeBuidel;

	[HarmonyPatch("CanAddItem", new Type[]
	{
		typeof(GameObject),
		typeof(int)
	})]
	[HarmonyPrefix]
	public static bool PreCanAddItem(Inventory __instance, GameObject prefab, ref bool __result)
	{
		if (BlokkeerAutomatischeBuidel || StoreGuiOnBuyItemPatch.IsHandelBezig)
		{
			return true;
		}
		if ((Object)(object)Player.m_localPlayer != (Object)null && __instance == ((Humanoid)Player.m_localPlayer).GetInventory() && (Object)(object)prefab != (Object)null)
		{
			ItemDrop component = prefab.GetComponent<ItemDrop>();
			if ((Object)(object)component != (Object)null && component.m_itemData.m_shared.m_name == "$item_coins")
			{
				__result = true;
				return false;
			}
		}
		return true;
	}

	[HarmonyPatch("CanAddItem", new Type[]
	{
		typeof(ItemData),
		typeof(int)
	})]
	[HarmonyPrefix]
	public static bool PreCanAddItemData(Inventory __instance, ItemData item, ref bool __result)
	{
		if (BlokkeerAutomatischeBuidel || StoreGuiOnBuyItemPatch.IsHandelBezig)
		{
			return true;
		}
		if ((Object)(object)Player.m_localPlayer != (Object)null && __instance == ((Humanoid)Player.m_localPlayer).GetInventory() && item != null && item.m_shared.m_name == "$item_coins")
		{
			__result = true;
			return false;
		}
		return true;
	}

	[HarmonyPatch("AddItem", new Type[]
	{
		typeof(GameObject),
		typeof(int)
	})]
	[HarmonyPrefix]
	public static bool PreAddItemPrefab(Inventory __instance, GameObject prefab, int amount, ref bool __result)
	{
		if (BlokkeerAutomatischeBuidel || StoreGuiOnBuyItemPatch.IsHandelBezig)
		{
			return true;
		}
		if ((Object)(object)Player.m_localPlayer != (Object)null && __instance == ((Humanoid)Player.m_localPlayer).GetInventory() && (Object)(object)prefab != (Object)null)
		{
			ItemDrop component = prefab.GetComponent<ItemDrop>();
			if ((Object)(object)component != (Object)null && component.m_itemData.m_shared.m_name == "$item_coins")
			{
				VoegMuntenToeAanBuidel(amount);
				__result = true;
				return false;
			}
		}
		return true;
	}

	[HarmonyPatch("AddItem", new Type[] { typeof(ItemData) })]
	[HarmonyPrefix]
	public static bool PreAddItemData(Inventory __instance, ItemData item, ref bool __result)
	{
		if (BlokkeerAutomatischeBuidel || StoreGuiOnBuyItemPatch.IsHandelBezig)
		{
			return true;
		}
		if ((Object)(object)Player.m_localPlayer != (Object)null && __instance == ((Humanoid)Player.m_localPlayer).GetInventory() && item != null && item.m_shared.m_name == "$item_coins")
		{
			VoegMuntenToeAanBuidel(item.m_stack);
			__result = true;
			return false;
		}
		return true;
	}

	public static void VoegMuntenToeAanBuidel(int aantal)
	{
		if (EndosCoinPurseMod.VirtueleBuidelInv == null || aantal <= 0)
		{
			return;
		}
		ItemData item = EndosCoinPurseMod.VirtueleBuidelInv.GetItem("$item_coins", -1, false);
		if (item == null)
		{
			GameObject itemPrefab = ObjectDB.instance.GetItemPrefab("Coins");
			if ((Object)(object)itemPrefab != (Object)null)
			{
				EndosCoinPurseMod.VirtueleBuidelInv.AddItem(itemPrefab, aantal);
			}
		}
		else
		{
			item.m_stack += aantal;
		}
		EndosCoinPurseMod.UpdateBuidelCount();
		if ((Object)(object)Player.m_localPlayer != (Object)null)
		{
			int num = EndosCoinPurseMod.VirtueleBuidelInv.GetItem("$item_coins", -1, false)?.m_stack ?? 0;
			Player.m_localPlayer.m_customData["buidel_items_hud_count"] = num.ToString();
		}
	}
}
[HarmonyPatch(typeof(Inventory))]
public static class InventoryRemovePatch
{
	[HarmonyPatch("RemoveItem", new Type[]
	{
		typeof(string),
		typeof(int),
		typeof(int),
		typeof(bool)
	})]
	[HarmonyPrefix]
	public static bool PreRemoveItemByName(Inventory __instance, string name, int amount)
	{
		if (InventoryPickupPatch.BlokkeerAutomatischeBuidel || StoreGuiOnBuyItemPatch.IsHandelBezig)
		{
			return true;
		}
		if ((Object)(object)Player.m_localPlayer != (Object)null && __instance == ((Humanoid)Player.m_localPlayer).GetInventory() && name == "$item_coins")
		{
			int num = 0;
			foreach (ItemData allItem in __instance.GetAllItems())
			{
				if (allItem.m_shared.m_name == "$item_coins")
				{
					num += allItem.m_stack;
				}
			}
			if (num >= amount)
			{
				return true;
			}
			int num2 = amount - num;
			ItemData item = EndosCoinPurseMod.VirtueleBuidelInv.GetItem("$item_coins", -1, false);
			if (item != null && item.m_stack >= num2)
			{
				if (num > 0)
				{
					InventoryPickupPatch.BlokkeerAutomatischeBuidel = true;
					__instance.RemoveItem("$item_coins", num, -1, true);
					InventoryPickupPatch.BlokkeerAutomatischeBuidel = false;
				}
				item.m_stack -= num2;
				if (item.m_stack <= 0)
				{
					EndosCoinPurseMod.VirtueleBuidelInv.RemoveItem(item);
				}
				SyncCoinPurseData();
				return false;
			}
		}
		return true;
	}

	[HarmonyPatch("RemoveItem", new Type[] { typeof(ItemData) })]
	[HarmonyPrefix]
	public static bool PreRemoveItemByData(Inventory __instance, ItemData item)
	{
		if (InventoryPickupPatch.BlokkeerAutomatischeBuidel || StoreGuiOnBuyItemPatch.IsHandelBezig)
		{
			return true;
		}
		if ((Object)(object)Player.m_localPlayer != (Object)null && __instance == ((Humanoid)Player.m_localPlayer).GetInventory() && item != null && item.m_shared.m_name == "$item_coins")
		{
			int stack = item.m_stack;
			int num = 0;
			foreach (ItemData allItem in __instance.GetAllItems())
			{
				if (allItem.m_shared.m_name == "$item_coins")
				{
					num += allItem.m_stack;
				}
			}
			if (num >= stack)
			{
				return true;
			}
			int num2 = stack - num;
			ItemData item2 = EndosCoinPurseMod.VirtueleBuidelInv.GetItem("$item_coins", -1, false);
			if (item2 != null && item2.m_stack >= num2)
			{
				if (num > 0)
				{
					InventoryPickupPatch.BlokkeerAutomatischeBuidel = true;
					__instance.RemoveItem(item);
					InventoryPickupPatch.BlokkeerAutomatischeBuidel = false;
				}
				item2.m_stack -= num2;
				if (item2.m_stack <= 0)
				{
					EndosCoinPurseMod.VirtueleBuidelInv.RemoveItem(item2);
				}
				SyncCoinPurseData();
				return false;
			}
		}
		return true;
	}

	private static void SyncCoinPurseData()
	{
		EndosCoinPurseMod.UpdateBuidelCount();
		if ((Object)(object)Player.m_localPlayer != (Object)null)
		{
			int num = EndosCoinPurseMod.VirtueleBuidelInv.GetItem("$item_coins", -1, false)?.m_stack ?? 0;
			Player.m_localPlayer.m_customData["buidel_items_hud_count"] = num.ToString();
		}
	}
}