Decompiled source of Custom Starter Kit v1.1.2

tony4twentys-Custom Starter Kit.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
using UnityEngine.SceneManagement;
using Zorro.Core.Serizalization;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("Custom Starter Kit")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Custom Starter Kit")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("d95d122d-502e-48c2-8fe8-8c01135f1069")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Custom_Starter_Kit;

[BepInPlugin("tony4twentys.Custom_Starter_Kit", "Custom Starter Kit", "1.1.1")]
public class Plugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Campfire), "Light_Rpc")]
	private static class CampfireLightPatch
	{
		private static void Postfix()
		{
			if (!PhotonNetwork.IsMasterClient || (Object)(object)Instance == (Object)null || !Instance._repeatOnCampfire.Value)
			{
				return;
			}
			List<Character> allCharacters = Character.AllCharacters;
			int count = allCharacters.Count;
			for (int i = 0; i < count; i++)
			{
				Character val = allCharacters[i];
				if (StarterKitManager.IsAlive(val) && !val.isBot && (Object)(object)val.refs?.view != (Object)null)
				{
					StarterKitManager.GiveStarterKitAtCampfire(val);
				}
			}
		}
	}

	private Harmony _harmony;

	public static Plugin Instance;

	private Coroutine _scanCoroutine;

	private ConfigEntry<int>[] _itemReferenceEntries;

	public ConfigEntry<int> _pocketSlot1ItemId;

	public ConfigEntry<int> _pocketSlot2ItemId;

	public ConfigEntry<int> _pocketSlot3ItemId;

	public ConfigEntry<bool> _giveBackpack;

	public ConfigEntry<int> _backpackSlot1ItemId;

	public ConfigEntry<int> _backpackSlot2ItemId;

	public ConfigEntry<int> _backpackSlot3ItemId;

	public ConfigEntry<int> _backpackSlot4ItemId;

	public ConfigEntry<bool> _repeatOnCampfire;

	private static readonly Dictionary<ushort, string> ItemNames = new Dictionary<ushort, string>
	{
		{ 0, "Airplane Food" },
		{ 1, "Anti-Rope Spool" },
		{ 2, "Antidote" },
		{ 3, "Green Crispberry" },
		{ 4, "Red Crispberry" },
		{ 5, "Yellow Crispberry" },
		{ 6, "Backpack" },
		{ 7, "Bandages" },
		{ 8, "Beehive" },
		{ 9, "Blue Berrynana" },
		{ 10, "Brown Berrynana" },
		{ 11, "Pink Berrynana" },
		{ 12, "Yellow Berrynana" },
		{ 13, "Bing Bong" },
		{ 14, "Binoculars" },
		{ 15, "Bugle" },
		{ 16, "Bugle of Friendship" },
		{ 17, "Chain Launcher" },
		{ 18, "Piton" },
		{ 19, "Black Clusterberry" },
		{ 20, "Red Clusterberry" },
		{ 21, "Yellow Clusterberry" },
		{ 22, "Green Clusterberry" },
		{ 23, "Compass" },
		{ 24, "Cure-All" },
		{ 25, "Cursed Skull" },
		{ 26, "Egg" },
		{ 27, "Energy Drink" },
		{ 28, "Stick" },
		{ 29, "First Aid Kit" },
		{ 30, "Seagull Flag" },
		{ 31, "Pink Berrynana Peel" },
		{ 32, "Flare" },
		{ 33, "Granola Bar" },
		{ 34, "Guidebook" },
		{ 35, "Heat Pack" },
		{ 36, "Coconut" },
		{ 37, "Balloon Bunch" },
		{ 38, "Honeycomb" },
		{ 39, "Cactus Ball" },
		{ 40, "Purple Kingberry" },
		{ 41, "Yellow Kingberry" },
		{ 42, "Lantern" },
		{ 43, "Faerie Lantern" },
		{ 44, "Lollipop" },
		{ 45, "Magic Bean" },
		{ 46, "Marshmallow" },
		{ 47, "Ancient Idol" },
		{ 48, "Basketball" },
		{ 49, "Scroll" },
		{ 50, "Torn Page 1" },
		{ 51, "Glowshroom" },
		{ 52, "Torn Page 3" },
		{ 53, "Torn Page 4" },
		{ 54, "Torn Page 8" },
		{ 55, "Coconut Half" },
		{ 56, "Green Kingberry" },
		{ 57, "Big Egg" },
		{ 58, "Pandoras Lunchbox" },
		{ 59, "Passport" },
		{ 60, "Scorchberry" },
		{ 61, "Pirates Compass" },
		{ 62, "Portable Stove" },
		{ 63, "Rope Cannon" },
		{ 64, "Anti-Rope Cannon" },
		{ 65, "Rope Spool" },
		{ 66, "Scout Cookies" },
		{ 67, "Scout Effigy" },
		{ 68, "Shelf Fungus" },
		{ 69, "Conch" },
		{ 70, "Blowgun" },
		{ 71, "Sports Drink" },
		{ 72, "Stone" },
		{ 73, "Trail Mix" },
		{ 74, "Warp Compass" },
		{ 75, "Orange Winterberry" },
		{ 76, "Yellow Winterberry" },
		{ 77, "Scoutmasters Bugle" },
		{ 78, "Megaphone" },
		{ 79, "Bounce Fungus" },
		{ 80, "Torn Page Blank" },
		{ 81, "Medicinal Root" },
		{ 82, "Torn Page 2" },
		{ 83, "Chubby Mushroom" },
		{ 84, "Poisonous Clustershroom" },
		{ 85, "Torn Page 5" },
		{ 86, "Torn Page 6" },
		{ 87, "Torn Page 7" },
		{ 88, "Clustershroom" },
		{ 89, "Poisonous Bugleshroom" },
		{ 90, "Remedy Fungus" },
		{ 91, "Blue Berrynana Peel" },
		{ 92, "Brown Berrynana Peel" },
		{ 93, "Bugleshroom" },
		{ 94, "Yellow Berrynana Peel" },
		{ 95, "Tick" },
		{ 96, "Torn Page 9" },
		{ 97, "Poisonous Buttonshroom" },
		{ 98, "Parasol" },
		{ 99, "Frisbee" },
		{ 100, "Rescue Hook" },
		{ 101, "Aloe Vera" },
		{ 102, "Buttonshroom" },
		{ 103, "Gold Prickleberry" },
		{ 104, "Sunscreen" },
		{ 105, "Balloon" },
		{ 106, "Dynamite" },
		{ 107, "Scout Cannon" },
		{ 108, "Red Prickleberry" },
		{ 109, "Torch" },
		{ 110, "Napberry" },
		{ 111, "Scorpion" },
		{ 112, "Strange Gem" },
		{ 113, "Cheat Compass" },
		{ 114, "Roasted Bird" },
		{ 115, "Bishop Black" },
		{ 116, "Bishop White" },
		{ 117, "King Black" },
		{ 118, "King White" },
		{ 119, "Knight Black" },
		{ 120, "Knight White" },
		{ 121, "Pawn Black" },
		{ 122, "Pawn White" },
		{ 123, "Queen Black" },
		{ 124, "Queen White" },
		{ 125, "Rook Black" },
		{ 126, "Rook White" },
		{ 127, "Turtle Flag" },
		{ 128, "BingBong Prop" },
		{ 129, "Binoculars Prop" },
		{ 130, "Bugle Prop" },
		{ 131, "Lollipop Prop" }
	};

	private void Awake()
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: Expected O, but got Unknown
		Debug.Log((object)"[Custom Starter Kit] Plugin Awake() called");
		try
		{
			_harmony = new Harmony("tony4twentys.Custom_Starter_Kit");
			_harmony.PatchAll();
			Debug.Log((object)"[Custom Starter Kit] Harmony patches applied");
			SceneManager.sceneLoaded += OnSceneLoaded;
			Debug.Log((object)"[Custom Starter Kit] Scene loaded event subscribed");
			Instance = this;
			CreateItemReferenceEntries();
			Debug.Log((object)"[Custom Starter Kit] Item reference entries created");
			_pocketSlot1ItemId = ((BaseUnityPlugin)this).Config.Bind<int>("Section 1 - Pockets", "PocketSlot1ItemID", 0, "Item ID for pocket slot 1 (0 = empty). See Section 0 for item IDs. Changes take effect for newly spawned players.");
			_pocketSlot2ItemId = ((BaseUnityPlugin)this).Config.Bind<int>("Section 1 - Pockets", "PocketSlot2ItemID", 0, "Item ID for pocket slot 2 (0 = empty). See Section 0 for item IDs. Changes take effect for newly spawned players.");
			_pocketSlot3ItemId = ((BaseUnityPlugin)this).Config.Bind<int>("Section 1 - Pockets", "PocketSlot3ItemID", 0, "Item ID for pocket slot 3 (0 = empty). See Section 0 for item IDs. Changes take effect for newly spawned players.");
			_giveBackpack = ((BaseUnityPlugin)this).Config.Bind<bool>("Section 2 - Backpack", "GiveBackpack", true, "Give and equip a backpack to all players. If false, backpack item slots are ignored. Changes take effect for newly spawned players.");
			_backpackSlot1ItemId = ((BaseUnityPlugin)this).Config.Bind<int>("Section 2 - Backpack", "BackpackSlot1ItemID", 0, "Item ID for backpack slot 1 (0 = empty, only used if GiveBackpack is true). See Section 0 for item IDs.");
			_backpackSlot2ItemId = ((BaseUnityPlugin)this).Config.Bind<int>("Section 2 - Backpack", "BackpackSlot2ItemID", 0, "Item ID for backpack slot 2 (0 = empty, only used if GiveBackpack is true). See Section 0 for item IDs.");
			_backpackSlot3ItemId = ((BaseUnityPlugin)this).Config.Bind<int>("Section 2 - Backpack", "BackpackSlot3ItemID", 0, "Item ID for backpack slot 3 (0 = empty, only used if GiveBackpack is true). See Section 0 for item IDs.");
			_backpackSlot4ItemId = ((BaseUnityPlugin)this).Config.Bind<int>("Section 2 - Backpack", "BackpackSlot4ItemID", 0, "Item ID for backpack slot 4 (0 = empty, only used if GiveBackpack is true). See Section 0 for item IDs.");
			_repeatOnCampfire = ((BaseUnityPlugin)this).Config.Bind<bool>("Section 3 - Campfire Re-Kit", "RepeatOnCampfire", false, "Give starter kit items again to all players each time a campfire is lit. If player already has backpack, backpack is skipped. Full inventory spawns items above head.");
			Debug.Log((object)("[Custom Starter Kit] Plugin loaded successfully! Config: Pocket1=" + _pocketSlot1ItemId.Value + ", GiveBackpack=" + _giveBackpack.Value));
		}
		catch (Exception ex)
		{
			Debug.LogError((object)("[Custom Starter Kit] Error in Awake(): " + ex.Message + "\n" + ex.StackTrace));
		}
	}

	private void CreateItemReferenceEntries()
	{
		_itemReferenceEntries = new ConfigEntry<int>[ItemNames.Count];
		int num = 0;
		foreach (KeyValuePair<ushort, string> item in ItemNames.OrderBy((KeyValuePair<ushort, string> x) => x.Key))
		{
			string value = item.Value;
			int itemId = item.Key;
			_itemReferenceEntries[num] = ((BaseUnityPlugin)this).Config.Bind<int>("Section 0 - Item Reference", value, itemId, $"READ-ONLY: {value} has ID {itemId}. This value auto-corrects if changed.");
			_itemReferenceEntries[num].SettingChanged += delegate(object sender, EventArgs e)
			{
				ConfigEntry<int> val = (ConfigEntry<int>)sender;
				if (val.Value != itemId)
				{
					val.Value = itemId;
				}
			};
			num++;
		}
	}

	private bool IsFoodOrConsumable(ushort id)
	{
		return id == 0 || id == 27 || id == 33 || id == 44 || id == 46 || id == 71 || id == 73 || (id >= 3 && id <= 5) || (id >= 9 && id <= 12) || (id >= 19 && id <= 22) || (id >= 40 && id <= 41) || id == 56 || id == 60 || (id >= 75 && id <= 76) || id == 36 || id == 55 || id == 26 || id == 57 || (id >= 83 && id <= 93) || (id >= 101 && id <= 103) || id == 108 || id == 110 || id == 114;
	}

	private bool IsToolOrEquipment(ushort id)
	{
		return id == 1 || (id >= 14 && id <= 18) || id == 23 || id == 42 || id == 43 || id == 61 || (id >= 63 && id <= 65) || id == 68 || id == 74 || id == 79 || id == 98 || id == 100 || (id >= 105 && id <= 107) || id == 109;
	}

	private bool IsMedicalItem(ushort id)
	{
		return id == 2 || id == 7 || id == 24 || id == 29 || id == 35 || id == 81 || id == 90 || id == 104;
	}

	private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
	{
		Debug.Log((object)("[Custom Starter Kit] Scene loaded: " + (((Scene)(ref scene)).name ?? "null")));
		if (_scanCoroutine != null)
		{
			((MonoBehaviour)this).StopCoroutine(_scanCoroutine);
			_scanCoroutine = null;
			Debug.Log((object)"[Custom Starter Kit] Stopped previous scan coroutine");
		}
		StarterKitManager.ResetTracking();
		if (((Scene)(ref scene)).name != null && ((Scene)(ref scene)).name.Contains("Level_"))
		{
			Debug.Log((object)("[Custom Starter Kit] Starting player scan coroutine for scene: " + ((Scene)(ref scene)).name));
			_scanCoroutine = ((MonoBehaviour)this).StartCoroutine(StarterKitManager.PeriodicPlayerScan());
		}
		else
		{
			Debug.Log((object)"[Custom Starter Kit] Skipping scan - not a gameplay scene");
		}
	}

	private void OnDestroy()
	{
		try
		{
			SceneManager.sceneLoaded -= OnSceneLoaded;
		}
		catch
		{
		}
		try
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
		catch
		{
		}
		if (_scanCoroutine != null)
		{
			((MonoBehaviour)this).StopCoroutine(_scanCoroutine);
		}
	}
}
internal static class StarterKitManager
{
	[CompilerGenerated]
	private sealed class <PeriodicPlayerScan>d__3 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		private List<Character> <characters>5__1;

		private int <count>5__2;

		private int <i>5__3;

		private Character <c>5__4;

		private bool <isAlive>5__5;

		private bool <isBot>5__6;

		private bool <hasView>5__7;

		private int <viewId>5__8;

		private Exception <ex>5__9;

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

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

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

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<characters>5__1 = null;
			<c>5__4 = null;
			<ex>5__9 = null;
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			//IL_03af: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b9: Expected O, but got Unknown
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Expected O, but got Unknown
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				Debug.Log((object)"[Custom Starter Kit] PeriodicPlayerScan coroutine started");
				<>2__current = (object)new WaitForSeconds(2f);
				<>1__state = 1;
				return true;
			case 1:
				<>1__state = -1;
				Debug.Log((object)"[Custom Starter Kit] Initial wait complete, starting scan loop");
				break;
			case 2:
				<>1__state = -1;
				break;
			}
			try
			{
				if (PhotonNetwork.IsMasterClient)
				{
					<characters>5__1 = Character.AllCharacters;
					<count>5__2 = <characters>5__1?.Count ?? 0;
					Debug.Log((object)("[Custom Starter Kit] Scan: Found " + <count>5__2 + " characters, IsMasterClient=" + PhotonNetwork.IsMasterClient));
					<i>5__3 = 0;
					while (<i>5__3 < <count>5__2)
					{
						<c>5__4 = <characters>5__1[<i>5__3];
						if ((Object)(object)<c>5__4 == (Object)null)
						{
							Debug.Log((object)("[Custom Starter Kit] Scan: Character[" + <i>5__3 + "] is null"));
						}
						else
						{
							<isAlive>5__5 = IsAlive(<c>5__4);
							<isBot>5__6 = <c>5__4.isBot;
							<hasView>5__7 = (Object)(object)<c>5__4.refs?.view != (Object)null;
							Debug.Log((object)("[Custom Starter Kit] Scan: Character[" + <i>5__3 + "] - Alive=" + <isAlive>5__5 + ", Bot=" + <isBot>5__6 + ", HasView=" + <hasView>5__7));
							if ((<isAlive>5__5 && !<isBot>5__6) & <hasView>5__7)
							{
								<viewId>5__8 = <c>5__4.refs.view.ViewID;
								if (!_receivedKit.Contains(<viewId>5__8))
								{
									Debug.Log((object)("[Custom Starter Kit] Attempting to give starter kit to character[" + <i>5__3 + "] (ViewID=" + <viewId>5__8 + ")"));
									if (TryGiveStarterKitTo(<c>5__4))
									{
										_receivedKit.Add(<viewId>5__8);
										Debug.Log((object)("[Custom Starter Kit] Starter kit given successfully to character[" + <i>5__3 + "]"));
									}
									else
									{
										Debug.LogWarning((object)("[Custom Starter Kit] Failed to give starter kit to character[" + <i>5__3 + "]"));
									}
								}
								else
								{
									Debug.Log((object)("[Custom Starter Kit] Character[" + <i>5__3 + "] already received kit (ViewID=" + <viewId>5__8 + ")"));
								}
							}
							<c>5__4 = null;
						}
						<i>5__3++;
					}
					<characters>5__1 = null;
				}
				else
				{
					Debug.Log((object)"[Custom Starter Kit] Scan: Not master client, skipping");
				}
			}
			catch (Exception ex)
			{
				<ex>5__9 = ex;
				Debug.LogError((object)("[Custom Starter Kit] Error in PeriodicPlayerScan: " + <ex>5__9.Message + "\n" + <ex>5__9.StackTrace));
			}
			<>2__current = (object)new WaitForSeconds(5f);
			<>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 ScanIntervalSeconds = 5f;

	private static readonly HashSet<int> _receivedKit = new HashSet<int>();

	public static void ResetTracking()
	{
		_receivedKit.Clear();
	}

	[IteratorStateMachine(typeof(<PeriodicPlayerScan>d__3))]
	public static IEnumerator PeriodicPlayerScan()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <PeriodicPlayerScan>d__3(0);
	}

	public static bool IsAlive(Character c)
	{
		return (Object)(object)c != (Object)null && (Object)(object)c.data != (Object)null && !c.data.dead && c.data.fullyConscious;
	}

	public static void GiveStarterKitWithFallback(Character c, bool giveBackpack)
	{
		try
		{
			Player player = c.player;
			if (!((Object)(object)player == (Object)null))
			{
				if (giveBackpack && Plugin.Instance._giveBackpack.Value && player.backpackSlot != null && !player.backpackSlot.hasBackpack)
				{
					EquipBackpackOnCharacter(c);
				}
				AddPocketItemsWithFallback(c);
				if (player.backpackSlot != null && player.backpackSlot.hasBackpack)
				{
					AddBackpackItemsWithFallback(c);
				}
			}
		}
		catch
		{
		}
	}

	public static void GiveStarterKitAtCampfire(Character c)
	{
		try
		{
			Player val = ((c != null) ? c.player : null);
			if ((Object)(object)val == (Object)null || !PhotonNetwork.IsMasterClient)
			{
				return;
			}
			List<ushort> list = new List<ushort>();
			ushort[] array = new ushort[3]
			{
				(ushort)Plugin.Instance._pocketSlot1ItemId.Value,
				(ushort)Plugin.Instance._pocketSlot2ItemId.Value,
				(ushort)Plugin.Instance._pocketSlot3ItemId.Value
			};
			ushort[] array2 = array;
			foreach (ushort num in array2)
			{
				if (num != 0)
				{
					list.Add(num);
				}
			}
			if (Plugin.Instance._giveBackpack.Value)
			{
				ushort[] array3 = new ushort[4]
				{
					(ushort)Plugin.Instance._backpackSlot1ItemId.Value,
					(ushort)Plugin.Instance._backpackSlot2ItemId.Value,
					(ushort)Plugin.Instance._backpackSlot3ItemId.Value,
					(ushort)Plugin.Instance._backpackSlot4ItemId.Value
				};
				ushort[] array4 = array3;
				foreach (ushort num2 in array4)
				{
					if (num2 != 0)
					{
						list.Add(num2);
					}
				}
			}
			if (list.Count == 0)
			{
				return;
			}
			int num3 = 0;
			for (int k = 0; k < 3 && k < val.itemSlots.Length; k++)
			{
				if (val.itemSlots[k].IsEmpty())
				{
					num3++;
				}
			}
			int num4 = 0;
			if (val.backpackSlot != null && val.backpackSlot.hasBackpack && ((ItemSlot)val.backpackSlot).data != null)
			{
				BackpackData val2 = default(BackpackData);
				if (((ItemSlot)val.backpackSlot).data.TryGetDataEntry<BackpackData>((DataEntryKey)7, ref val2) && val2 != null && val2.itemSlots != null)
				{
					for (int l = 0; l < val2.itemSlots.Length; l++)
					{
						if (val2.itemSlots[l].IsEmpty())
						{
							num4++;
						}
					}
				}
			}
			else if (Plugin.Instance._giveBackpack.Value)
			{
				num4 = 4;
			}
			int num5 = num3 + num4;
			if (num5 >= list.Count)
			{
				if (Plugin.Instance._giveBackpack.Value && (val.backpackSlot == null || !val.backpackSlot.hasBackpack))
				{
					EquipBackpackOnCharacter(c);
				}
				AddPocketItems(c);
				if (Plugin.Instance._giveBackpack.Value && val.backpackSlot != null && val.backpackSlot.hasBackpack)
				{
					AddBackpackItems(c);
				}
			}
			else
			{
				SpawnItemsInCircle(c, list);
			}
		}
		catch
		{
		}
	}

	private static bool TryGiveStarterKitTo(Character c)
	{
		try
		{
			Debug.Log((object)"[Custom Starter Kit] TryGiveStarterKitTo called");
			Player val = ((c != null) ? c.player : null);
			if ((Object)(object)val == (Object)null)
			{
				Debug.LogWarning((object)"[Custom Starter Kit] TryGiveStarterKitTo: Character.player is null");
				return false;
			}
			Debug.Log((object)("[Custom Starter Kit] TryGiveStarterKitTo: Player found, GiveBackpack=" + Plugin.Instance._giveBackpack.Value));
			if (Plugin.Instance._giveBackpack.Value)
			{
				Debug.Log((object)"[Custom Starter Kit] Attempting to equip backpack");
				if (EquipBackpackOnCharacter(c))
				{
					Debug.Log((object)"[Custom Starter Kit] Backpack equipped successfully");
				}
				else
				{
					Debug.LogWarning((object)"[Custom Starter Kit] Failed to equip backpack");
				}
			}
			Debug.Log((object)"[Custom Starter Kit] Adding pocket items");
			AddPocketItems(c);
			if (Plugin.Instance._giveBackpack.Value)
			{
				Debug.Log((object)"[Custom Starter Kit] Adding backpack items");
				AddBackpackItems(c);
			}
			Debug.Log((object)"[Custom Starter Kit] TryGiveStarterKitTo completed successfully");
			return true;
		}
		catch (Exception ex)
		{
			Debug.LogError((object)("[Custom Starter Kit] Error in TryGiveStarterKitTo: " + ex.Message + "\n" + ex.StackTrace));
			return false;
		}
	}

	private static bool EquipBackpackOnCharacter(Character c)
	{
		//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ad: Expected O, but got Unknown
		//IL_015c: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			Debug.Log((object)"[Custom Starter Kit] EquipBackpackOnCharacter called");
			Player val = ((c != null) ? c.player : null);
			if ((Object)(object)val == (Object)null)
			{
				Debug.LogWarning((object)"[Custom Starter Kit] EquipBackpackOnCharacter: Character.player is null");
				return false;
			}
			if (val.backpackSlot == null)
			{
				Debug.LogWarning((object)"[Custom Starter Kit] EquipBackpackOnCharacter: Player.backpackSlot is null");
				return false;
			}
			if (val.backpackSlot.hasBackpack)
			{
				Debug.Log((object)"[Custom Starter Kit] EquipBackpackOnCharacter: Player already has backpack");
				return false;
			}
			if (((ItemSlot)val.backpackSlot).data == null)
			{
				Debug.Log((object)"[Custom Starter Kit] EquipBackpackOnCharacter: Creating new ItemInstanceData for backpack");
				ItemInstanceData val2 = new ItemInstanceData(Guid.NewGuid());
				ItemInstanceDataHandler.AddInstanceData(val2);
				((ItemSlot)val.backpackSlot).data = val2;
			}
			BackpackData val3 = default(BackpackData);
			if (!((ItemSlot)val.backpackSlot).data.TryGetDataEntry<BackpackData>((DataEntryKey)7, ref val3) || val3 == null)
			{
				Debug.Log((object)"[Custom Starter Kit] EquipBackpackOnCharacter: Registering new BackpackData entry");
				((ItemSlot)val.backpackSlot).data.RegisterNewEntry<BackpackData>((DataEntryKey)7);
				((ItemSlot)val.backpackSlot).data.TryGetDataEntry<BackpackData>((DataEntryKey)7, ref val3);
			}
			val.backpackSlot.hasBackpack = true;
			Debug.Log((object)"[Custom Starter Kit] EquipBackpackOnCharacter: Backpack flag set to true");
			InventorySyncData val4 = default(InventorySyncData);
			((InventorySyncData)(ref val4))..ctor(val.itemSlots, val.backpackSlot, val.tempFullSlot);
			((MonoBehaviourPun)val).photonView.RPC("SyncInventoryRPC", (RpcTarget)1, new object[2]
			{
				IBinarySerializable.ToManagedArray<InventorySyncData>(val4),
				false
			});
			Debug.Log((object)"[Custom Starter Kit] EquipBackpackOnCharacter: Inventory synced");
			return true;
		}
		catch (Exception ex)
		{
			Debug.LogError((object)("[Custom Starter Kit] Error in EquipBackpackOnCharacter: " + ex.Message + "\n" + ex.StackTrace));
			return false;
		}
	}

	private static void AddPocketItems(Character c)
	{
		Player val = ((c != null) ? c.player : null);
		if ((Object)(object)val == (Object)null)
		{
			Debug.LogWarning((object)"[Custom Starter Kit] AddPocketItems: Character.player is null");
			return;
		}
		ushort[] array = new ushort[3]
		{
			(ushort)Plugin.Instance._pocketSlot1ItemId.Value,
			(ushort)Plugin.Instance._pocketSlot2ItemId.Value,
			(ushort)Plugin.Instance._pocketSlot3ItemId.Value
		};
		Debug.Log((object)("[Custom Starter Kit] AddPocketItems: Items=[" + array[0] + ", " + array[1] + ", " + array[2] + "]"));
		for (int i = 0; i < 3 && i < array.Length; i++)
		{
			ushort num = array[i];
			if (num == 0)
			{
				Debug.Log((object)("[Custom Starter Kit] AddPocketItems: Skipping slot " + i + " (itemId=0)"));
				continue;
			}
			bool flag = TryAddItemToPocket(val, num, i);
			Debug.Log((object)("[Custom Starter Kit] AddPocketItems: Slot " + i + " result=" + flag));
		}
	}

	private static void AddPocketItemsWithFallback(Character c)
	{
		Player val = ((c != null) ? c.player : null);
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		ushort[] array = new ushort[3]
		{
			(ushort)Plugin.Instance._pocketSlot1ItemId.Value,
			(ushort)Plugin.Instance._pocketSlot2ItemId.Value,
			(ushort)Plugin.Instance._pocketSlot3ItemId.Value
		};
		for (int i = 0; i < 3 && i < array.Length; i++)
		{
			ushort num = array[i];
			if (num != 0 && !TryAddItemToPocket(val, num, i) && !TryAddItemToBackpackAny(val, num))
			{
				SpawnItemAboveHead(c, num);
			}
		}
	}

	private static bool TryAddItemToPocket(Player p, ushort itemId, int slotIndex)
	{
		//IL_0161: Unknown result type (might be due to invalid IL or missing references)
		//IL_0167: Expected O, but got Unknown
		//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			Debug.Log((object)("[Custom Starter Kit] TryAddItemToPocket: itemId=" + itemId + ", slotIndex=" + slotIndex));
			if (slotIndex < 0 || slotIndex >= 3)
			{
				Debug.LogWarning((object)("[Custom Starter Kit] TryAddItemToPocket: Invalid slotIndex=" + slotIndex));
				return false;
			}
			if (p.itemSlots == null)
			{
				Debug.LogWarning((object)"[Custom Starter Kit] TryAddItemToPocket: p.itemSlots is null");
				return false;
			}
			if (slotIndex >= p.itemSlots.Length)
			{
				Debug.LogWarning((object)("[Custom Starter Kit] TryAddItemToPocket: slotIndex " + slotIndex + " >= itemSlots.Length " + p.itemSlots.Length));
				return false;
			}
			if (!p.itemSlots[slotIndex].IsEmpty())
			{
				Debug.Log((object)("[Custom Starter Kit] TryAddItemToPocket: Slot " + slotIndex + " is not empty"));
				return false;
			}
			Item val = default(Item);
			if (!ItemDatabase.TryGetItem(itemId, ref val))
			{
				Debug.LogWarning((object)("[Custom Starter Kit] TryAddItemToPocket: ItemDatabase.TryGetItem failed for itemId=" + itemId));
				return false;
			}
			object obj;
			if (val == null)
			{
				obj = null;
			}
			else
			{
				GameObject gameObject = ((Component)val).gameObject;
				obj = ((gameObject != null) ? ((Object)gameObject).name : null);
			}
			if (obj == null)
			{
				obj = "null";
			}
			Debug.Log((object)("[Custom Starter Kit] TryAddItemToPocket: Item prefab found: " + (string?)obj));
			ItemInstanceData val2 = new ItemInstanceData(Guid.NewGuid());
			ItemInstanceDataHandler.AddInstanceData(val2);
			p.itemSlots[slotIndex].SetItem(val, val2);
			Debug.Log((object)("[Custom Starter Kit] TryAddItemToPocket: Item set in slot " + slotIndex));
			try
			{
				InventorySyncData val3 = default(InventorySyncData);
				((InventorySyncData)(ref val3))..ctor(p.itemSlots, p.backpackSlot, p.tempFullSlot);
				((MonoBehaviourPun)p).photonView.RPC("SyncInventoryRPC", (RpcTarget)1, new object[2]
				{
					IBinarySerializable.ToManagedArray<InventorySyncData>(val3),
					false
				});
				Debug.Log((object)"[Custom Starter Kit] TryAddItemToPocket: Inventory synced");
			}
			catch (Exception ex)
			{
				Debug.LogWarning((object)("[Custom Starter Kit] TryAddItemToPocket: Inventory sync failed: " + ex.Message));
			}
			return true;
		}
		catch (Exception ex2)
		{
			Debug.LogError((object)("[Custom Starter Kit] Error in TryAddItemToPocket: " + ex2.Message + "\n" + ex2.StackTrace));
			return false;
		}
	}

	private static void AddBackpackItems(Character c)
	{
		//IL_021c: Unknown result type (might be due to invalid IL or missing references)
		Player val = ((c != null) ? c.player : null);
		if ((Object)(object)val == (Object)null)
		{
			Debug.LogWarning((object)"[Custom Starter Kit] AddBackpackItems: Character.player is null");
			return;
		}
		if (val.backpackSlot == null)
		{
			Debug.LogWarning((object)"[Custom Starter Kit] AddBackpackItems: Player.backpackSlot is null");
			return;
		}
		if (!val.backpackSlot.hasBackpack)
		{
			Debug.Log((object)"[Custom Starter Kit] AddBackpackItems: Player doesn't have backpack");
			return;
		}
		BackpackData val2 = default(BackpackData);
		if (!((ItemSlot)val.backpackSlot).data.TryGetDataEntry<BackpackData>((DataEntryKey)7, ref val2) || val2 == null)
		{
			Debug.LogWarning((object)"[Custom Starter Kit] AddBackpackItems: Could not get BackpackData");
			return;
		}
		ushort[] array = new ushort[4]
		{
			(ushort)Plugin.Instance._backpackSlot1ItemId.Value,
			(ushort)Plugin.Instance._backpackSlot2ItemId.Value,
			(ushort)Plugin.Instance._backpackSlot3ItemId.Value,
			(ushort)Plugin.Instance._backpackSlot4ItemId.Value
		};
		Debug.Log((object)("[Custom Starter Kit] AddBackpackItems: Items=[" + array[0] + ", " + array[1] + ", " + array[2] + ", " + array[3] + "]"));
		byte b = 0;
		while (b < 4 && b < array.Length)
		{
			ushort num = array[b];
			if (num == 0)
			{
				Debug.Log((object)("[Custom Starter Kit] AddBackpackItems: Skipping slot " + b + " (itemId=0)"));
			}
			else
			{
				bool flag = TryAddItemToBackpack(val2, num, b);
				Debug.Log((object)("[Custom Starter Kit] AddBackpackItems: Slot " + b + " result=" + flag));
			}
			b++;
		}
		try
		{
			InventorySyncData val3 = default(InventorySyncData);
			((InventorySyncData)(ref val3))..ctor(val.itemSlots, val.backpackSlot, val.tempFullSlot);
			((MonoBehaviourPun)val).photonView.RPC("SyncInventoryRPC", (RpcTarget)1, new object[2]
			{
				IBinarySerializable.ToManagedArray<InventorySyncData>(val3),
				false
			});
			Debug.Log((object)"[Custom Starter Kit] AddBackpackItems: Inventory synced");
		}
		catch (Exception ex)
		{
			Debug.LogWarning((object)("[Custom Starter Kit] AddBackpackItems: Inventory sync failed: " + ex.Message));
		}
	}

	private static void AddBackpackItemsWithFallback(Character c)
	{
		//IL_014a: Unknown result type (might be due to invalid IL or missing references)
		Player val = ((c != null) ? c.player : null);
		BackpackData val2 = default(BackpackData);
		if ((Object)(object)val == (Object)null || val.backpackSlot == null || !val.backpackSlot.hasBackpack || !((ItemSlot)val.backpackSlot).data.TryGetDataEntry<BackpackData>((DataEntryKey)7, ref val2) || val2 == null)
		{
			return;
		}
		ushort[] array = new ushort[4]
		{
			(ushort)Plugin.Instance._backpackSlot1ItemId.Value,
			(ushort)Plugin.Instance._backpackSlot2ItemId.Value,
			(ushort)Plugin.Instance._backpackSlot3ItemId.Value,
			(ushort)Plugin.Instance._backpackSlot4ItemId.Value
		};
		byte b = 0;
		while (b < 4 && b < array.Length)
		{
			ushort num = array[b];
			if (num != 0 && !TryAddItemToBackpack(val2, num, b) && !TryAddItemToBackpackAny(val, num))
			{
				SpawnItemAboveHead(c, num);
			}
			b++;
		}
		try
		{
			InventorySyncData val3 = default(InventorySyncData);
			((InventorySyncData)(ref val3))..ctor(val.itemSlots, val.backpackSlot, val.tempFullSlot);
			((MonoBehaviourPun)val).photonView.RPC("SyncInventoryRPC", (RpcTarget)1, new object[2]
			{
				IBinarySerializable.ToManagedArray<InventorySyncData>(val3),
				false
			});
		}
		catch
		{
		}
	}

	private static bool TryAddItemToBackpack(BackpackData bpData, ushort itemId, byte slotIndex)
	{
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0151: Expected O, but got Unknown
		try
		{
			Debug.Log((object)("[Custom Starter Kit] TryAddItemToBackpack: itemId=" + itemId + ", slotIndex=" + slotIndex));
			if (bpData == null)
			{
				Debug.LogWarning((object)"[Custom Starter Kit] TryAddItemToBackpack: bpData is null");
				return false;
			}
			if (bpData.itemSlots == null)
			{
				Debug.LogWarning((object)"[Custom Starter Kit] TryAddItemToBackpack: bpData.itemSlots is null");
				return false;
			}
			if (slotIndex >= bpData.itemSlots.Length)
			{
				Debug.LogWarning((object)("[Custom Starter Kit] TryAddItemToBackpack: slotIndex " + slotIndex + " >= itemSlots.Length " + bpData.itemSlots.Length));
				return false;
			}
			if (!bpData.itemSlots[slotIndex].IsEmpty())
			{
				Debug.Log((object)("[Custom Starter Kit] TryAddItemToBackpack: Slot " + slotIndex + " is not empty"));
				return false;
			}
			Item val = default(Item);
			if (!ItemDatabase.TryGetItem(itemId, ref val))
			{
				Debug.LogWarning((object)("[Custom Starter Kit] TryAddItemToBackpack: ItemDatabase.TryGetItem failed for itemId=" + itemId));
				return false;
			}
			object obj;
			if (val == null)
			{
				obj = null;
			}
			else
			{
				GameObject gameObject = ((Component)val).gameObject;
				obj = ((gameObject != null) ? ((Object)gameObject).name : null);
			}
			if (obj == null)
			{
				obj = "null";
			}
			Debug.Log((object)("[Custom Starter Kit] TryAddItemToBackpack: Item prefab found: " + (string?)obj));
			ItemInstanceData val2 = new ItemInstanceData(Guid.NewGuid());
			ItemInstanceDataHandler.AddInstanceData(val2);
			bpData.AddItem(val, val2, slotIndex);
			Debug.Log((object)("[Custom Starter Kit] TryAddItemToBackpack: Item added to backpack slot " + slotIndex));
			return true;
		}
		catch (Exception ex)
		{
			Debug.LogError((object)("[Custom Starter Kit] Error in TryAddItemToBackpack: " + ex.Message + "\n" + ex.StackTrace));
			return false;
		}
	}

	private static bool TryAddItemToBackpackAny(Player p, ushort itemId)
	{
		//IL_008d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Expected O, but got Unknown
		//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if (p.backpackSlot == null || !p.backpackSlot.hasBackpack || ((ItemSlot)p.backpackSlot).data == null)
			{
				return false;
			}
			BackpackData val = default(BackpackData);
			if (!((ItemSlot)p.backpackSlot).data.TryGetDataEntry<BackpackData>((DataEntryKey)7, ref val) || val == null)
			{
				return false;
			}
			Item val2 = default(Item);
			InventorySyncData val4 = default(InventorySyncData);
			for (byte b = 0; b < val.itemSlots.Length; b++)
			{
				if (val.itemSlots[b].IsEmpty() && ItemDatabase.TryGetItem(itemId, ref val2))
				{
					ItemInstanceData val3 = new ItemInstanceData(Guid.NewGuid());
					ItemInstanceDataHandler.AddInstanceData(val3);
					val.AddItem(val2, val3, b);
					((InventorySyncData)(ref val4))..ctor(p.itemSlots, p.backpackSlot, p.tempFullSlot);
					((MonoBehaviourPun)p).photonView.RPC("SyncInventoryRPC", (RpcTarget)1, new object[2]
					{
						IBinarySerializable.ToManagedArray<InventorySyncData>(val4),
						false
					});
					return true;
				}
			}
			return false;
		}
		catch
		{
			return false;
		}
	}

	private static void SpawnItemAboveHead(Character c, ushort itemId)
	{
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: 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)
		try
		{
			Item val = default(Item);
			if (!((Object)(object)c == (Object)null) && ItemDatabase.TryGetItem(itemId, ref val))
			{
				Vector3 val2 = c.Center + Vector3.up * 1.5f;
				PhotonNetwork.InstantiateItemRoom(((Object)((Component)val).gameObject).name, val2, Quaternion.identity);
			}
		}
		catch
		{
		}
	}

	private static void SpawnItemsInCircle(Character c, List<ushort> itemIds)
	{
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: 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_00ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b9: 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_00d8: 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)
		try
		{
			if ((Object)(object)c == (Object)null || itemIds == null || itemIds.Count == 0 || !PhotonNetwork.IsMasterClient)
			{
				return;
			}
			Vector3 center = c.Center;
			int count = itemIds.Count;
			float num = 360f / (float)count;
			Item val = default(Item);
			for (int i = 0; i < count; i++)
			{
				ushort num2 = itemIds[i];
				if (num2 != 0 && ItemDatabase.TryGetItem(num2, ref val))
				{
					float num3 = num * (float)i * ((float)Math.PI / 180f);
					Vector3 val2 = new Vector3(Mathf.Cos(num3), 0f, Mathf.Sin(num3)) * 0.4f;
					Vector3 val3 = center + Vector3.up * 0.5f + val2;
					PhotonNetwork.InstantiateItemRoom(((Object)((Component)val).gameObject).name, val3, Quaternion.identity);
				}
			}
		}
		catch
		{
		}
	}

	private static string SafeName(Character c)
	{
		object obj;
		if (c == null)
		{
			obj = null;
		}
		else
		{
			CharacterRefs refs = c.refs;
			if (refs == null)
			{
				obj = null;
			}
			else
			{
				PhotonView view = refs.view;
				if (view == null)
				{
					obj = null;
				}
				else
				{
					Player owner = view.Owner;
					obj = ((owner != null) ? owner.NickName : null);
				}
			}
		}
		if (obj == null)
		{
			obj = ((c != null) ? ((Object)c).name : null) ?? "Unknown";
		}
		return (string)obj;
	}
}
internal static class StarterKitHelper
{
	public static string SafeName(Character c)
	{
		object obj;
		if (c == null)
		{
			obj = null;
		}
		else
		{
			CharacterRefs refs = c.refs;
			if (refs == null)
			{
				obj = null;
			}
			else
			{
				PhotonView view = refs.view;
				if (view == null)
				{
					obj = null;
				}
				else
				{
					Player owner = view.Owner;
					obj = ((owner != null) ? owner.NickName : null);
				}
			}
		}
		if (obj == null)
		{
			obj = ((c != null) ? ((Object)c).name : null) ?? "Unknown";
		}
		return (string)obj;
	}
}