Decompiled source of KindredLogistics v1.2.0

KindredLogistics.dll

Decompiled 2 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using BepInEx.Unity.IL2CPP.Utils.Collections;
using HarmonyLib;
using Il2CppInterop.Runtime;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSystem;
using Il2CppSystem.Collections.Generic;
using KindredLogistics;
using KindredLogistics.Commands.Converters;
using KindredLogistics.Services;
using Microsoft.CodeAnalysis;
using ProjectM;
using ProjectM.CastleBuilding;
using ProjectM.CastleBuilding.Rebuilding;
using ProjectM.Network;
using ProjectM.Physics;
using ProjectM.Scripting;
using ProjectM.Shared;
using ProjectM.Shared.Systems;
using ProjectM.Terrain;
using Stunlock.Core;
using Stunlock.Localization;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Scenes;
using Unity.Transforms;
using UnityEngine;
using VampireCommandFramework;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("KindredLogistics")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Inventory management mod. Stashing, craftpulling, finding chests, etc")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyInformationalVersion("1.2.0+2.Branch.main.Sha.62bb8fcf39ee2ebe62410e48ec8f8fdc72e85b10.50caa704647c96bc6b960af07f169f2570f57e1d")]
[assembly: AssemblyProduct("KindredLogistics")]
[assembly: AssemblyTitle("KindredLogistics")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.2.0.0")]
[module: UnverifiableCode]
[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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 KindreddLogistics.Patches
{
	[HarmonyPatch]
	public class SortSingleInventorySystemPatch
	{
		private static List<(ulong, double)> lastSort = new List<(ulong, double)>();

		[HarmonyPatch(typeof(SortSingleInventorySystem), "OnUpdate")]
		[HarmonyPrefix]
		private static void Prefix(SortSingleInventorySystem __instance)
		{
			//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_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: 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_001c: 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_0029: 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_003b: 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_0041: 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_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_00b6: 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)
			EntityQuery eventQuery = __instance._EventQuery;
			NativeArray<Entity> val = ((EntityQuery)(ref eventQuery)).ToEntityArray(AllocatorHandle.op_Implicit((Allocator)2));
			try
			{
				Enumerator<Entity> enumerator = val.GetEnumerator();
				while (enumerator.MoveNext())
				{
					Entity current = enumerator.Current;
					if (((Entity)(ref current)).Equals(Entity.Null))
					{
						continue;
					}
					FromCharacter val2 = current.Read<FromCharacter>();
					ulong platformId = val2.User.Read<User>().PlatformId;
					if (!Core.PlayerSettings.IsSortStashEnabled(platformId))
					{
						continue;
					}
					double serverTime = Core.ServerTime;
					bool flag = false;
					for (int i = 0; i < lastSort.Count; i++)
					{
						if (lastSort[i].Item1 == platformId)
						{
							double item = lastSort[i].Item2;
							if (serverTime - item < 1.0)
							{
								flag = true;
								Core.Stash.StashCharacterInventory(val2.Character);
							}
							lastSort.RemoveAt(i);
							break;
						}
					}
					if (!flag)
					{
						lastSort.Add((platformId, serverTime));
					}
				}
			}
			catch (Exception ex)
			{
				Core.Log.LogError((object)ex);
			}
			finally
			{
				val.Dispose();
			}
		}
	}
}
namespace Logistics.Commands
{
	[CommandGroup("logistics", "l")]
	public static class LogisticsCommands
	{
		[Command("sortstash", "ss", ".l ss", "Toggles autostashing on double clicking sort button for player.", null, false)]
		public static void TogglePlayerAutoStash(ChatCommandContext ctx)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			ulong platformId = ctx.Event.User.PlatformId;
			bool flag = Core.PlayerSettings.ToggleSortStash(platformId);
			ctx.Reply("SortStash is " + (flag ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + ".");
		}

		[Command("craftpull", "cr", ".l cr", "Toggles right-clicking on recipes for missing ingredients.", null, false)]
		public static void TogglePlayerAutoPull(ChatCommandContext ctx)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			ulong platformId = ctx.Event.User.PlatformId;
			bool flag = Core.PlayerSettings.ToggleCraftPull(platformId);
			ctx.Reply("CraftPull is " + (flag ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + ".");
		}

		[Command("dontpulllast", "dpl", ".l dpl", "Toggles the ability to not pull the last item from a container for Logistics commands.", null, false)]
		public static void ToggleDontPullLast(ChatCommandContext ctx)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			ulong platformId = ctx.Event.User.PlatformId;
			bool flag = Core.PlayerSettings.ToggleDontPullLast(platformId);
			ctx.Reply("DontPullLast is " + (flag ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + ".");
		}

		[Command("autostashmissions", "asm", ".l asm", "Toggles autostashing for servant missions.", null, false)]
		public static void ToggleServantAutoStash(ChatCommandContext ctx)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			ulong platformId = ctx.Event.User.PlatformId;
			bool flag = Core.PlayerSettings.ToggleAutoStashMissions(platformId);
			ctx.Reply("AutoStash for missions is " + (flag ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + ".");
		}

		[Command("conveyor", "co", ".l co", "Toggles the ability of sender/receiver's to move items around.", null, false)]
		public static void ToggleConveyor(ChatCommandContext ctx)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			ulong platformId = ctx.Event.User.PlatformId;
			bool flag = Core.PlayerSettings.ToggleConveyor(platformId);
			ctx.Reply("Conveyor is " + (flag ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + ".");
		}

		[Command("salvage", "sal", ".l sal", "Toggles the ability to salvage items from a chest named 'salvage'.", null, false)]
		public static void ToggleSalvage(ChatCommandContext ctx)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			ulong platformId = ctx.Event.User.PlatformId;
			bool flag = Core.PlayerSettings.ToggleSalvage(platformId);
			ctx.Reply("Salvage is " + (flag ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + ".");
		}

		[Command("silentpull", "sp", null, "Toggles the ability to not send messages when pulling about where they came from.", null, false)]
		public static void ToggleSilentCraftPull(ChatCommandContext ctx)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			ulong platformId = ctx.Event.User.PlatformId;
			bool flag = Core.PlayerSettings.ToggleSilentPull(platformId);
			ctx.Reply("SilentPull is " + (flag ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + ".");
		}

		[Command("silentstash", "ssh", null, "Toggles the ability to not send messages when stashing items about where they go.", null, false)]
		public static void ToggleSilentStash(ChatCommandContext ctx)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			ulong platformId = ctx.Event.User.PlatformId;
			bool flag = Core.PlayerSettings.ToggleSilentStash(platformId);
			ctx.Reply("SilentStash is " + (flag ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + ".");
		}

		[Command("settings", "s", ".l s", "Displays current settings.", null, false)]
		public static void DisplaySettings(ChatCommandContext ctx)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			ulong platformId = ctx.Event.User.PlatformId;
			PlayerSettingsService.PlayerSettings settings = Core.PlayerSettings.GetSettings(platformId);
			PlayerSettingsService.PlayerSettings globalSettings = Core.PlayerSettings.GetGlobalSettings();
			ctx.Reply("KindredLogistics settings:\n" + $"SortStash{(globalSettings.SortStash ? "" : "(<color=red>Server Disabled</color>)")}: {(settings.SortStash ? "<color=green>enabled</color>" : "<color=red>disabled</color>")}\n" + "Pull (Global) : " + (globalSettings.Pull ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + "\n" + $"CraftPull{(globalSettings.CraftPull ? "" : "(<color=red>Server Disabled</color>)")}: {(settings.CraftPull ? "<color=green>enabled</color>" : "<color=red>disabled</color>")}\n" + "DontPullLast: " + (settings.DontPullLast ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + "\n" + $"AutoStashMissions{(globalSettings.AutoStashMissions ? "" : "(<color=red>Server Disabled</color>)")}: {(settings.AutoStashMissions ? "<color=green>enabled</color>" : "<color=red>disabled</color>")}\n" + $"Conveyor{(globalSettings.Conveyor ? "" : "(<color=red>Server Disabled</color>)")}: {(settings.Conveyor ? "<color=green>enabled</color>" : "<color=red>disabled</color>")}\n" + $"Salvage: {(globalSettings.Salvage ? "" : "(<color=red>Server Disabled</color>)")}: {(settings.Salvage ? "<color=green>enabled</color>" : "<color=red>disabled</color>")}\n" + "SilentPull: " + (settings.SilentPull ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + "\nSilentStash: " + (settings.SilentStash ? "<color=green>enabled</color>" : "<color=red>disabled</color>"));
		}
	}
	[CommandGroup("logisticsglobal", "lg")]
	public static class LogisticsGlobal
	{
		[Command("sortstash", "ss", ".lg ss", "Toggles autostashing on double clicking sort button for player.", null, true)]
		public static void TogglePlayerAutoStash(ChatCommandContext ctx)
		{
			bool flag = Core.PlayerSettings.ToggleSortStash(0uL);
			ctx.Reply("Global SortStash is " + (flag ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + ".");
		}

		[Command("pull", "p", ".lg p", "Toggles the ability to pull items from containers.", null, true)]
		public static void TogglePlayerPull(ChatCommandContext ctx)
		{
			bool flag = Core.PlayerSettings.TogglePull();
			ctx.Reply("Global Pull is " + (flag ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + ".");
		}

		[Command("craftpull", "cr", ".lg cr", "Toggles right-clicking on recipes for missing ingredients.", null, true)]
		public static void TogglePlayerAutoPull(ChatCommandContext ctx)
		{
			bool flag = Core.PlayerSettings.ToggleCraftPull(0uL);
			ctx.Reply("CraftPull is " + (flag ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + ".");
		}

		[Command("autostashmissions", "asm", ".lg asm", "Toggles autostashing for servant missions.", null, true)]
		public static void ToggleServantAutoStash(ChatCommandContext ctx)
		{
			bool flag = Core.PlayerSettings.ToggleAutoStashMissions(0uL);
			ctx.Reply("Global AutoStash for missions is " + (flag ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + ".");
		}

		[Command("conveyor", "co", ".lg co", "Toggles the ability of sender/receiver's to move items around.", null, true)]
		public static void ToggleConveyor(ChatCommandContext ctx)
		{
			bool flag = Core.PlayerSettings.ToggleConveyor(0uL);
			ctx.Reply("Global Conveyor is " + (flag ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + ".");
		}

		[Command("salvage", "sal", ".lg sal", "Toggles the ability to salvage items from a chest named 'salvage'.", null, true)]
		public static void ToggleSalvage(ChatCommandContext ctx)
		{
			bool flag = Core.PlayerSettings.ToggleSalvage(0uL);
			ctx.Reply("Global Salvage is " + (flag ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + ".");
		}

		[Command("settings", "s", ".lg s", "Displays current settings.", null, true)]
		public static void DisplaySettings(ChatCommandContext ctx)
		{
			PlayerSettingsService.PlayerSettings globalSettings = Core.PlayerSettings.GetGlobalSettings();
			ctx.Reply("KindredLogistics Global settings:\nSortStash: " + (globalSettings.SortStash ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + "\nPull: " + (globalSettings.Pull ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + "\nCraftPull: " + (globalSettings.CraftPull ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + "\nAutoStashMissions: " + (globalSettings.AutoStashMissions ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + "\nConveyor: " + (globalSettings.Conveyor ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + "\nSalvage: " + (globalSettings.Salvage ? "<color=green>enabled</color>" : "<color=red>disabled</color>") + "\n");
		}
	}
	public static class AdditionalCommands
	{
		[Command("stash", null, null, "Stashes all items in your inventory.", null, false)]
		public static void StashInventory(ChatCommandContext ctx)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			Core.Stash.StashCharacterInventory(ctx.Event.SenderCharacterEntity);
		}

		[Command("pull", null, null, "Pulls specified item from containers.", null, false)]
		public static void PullItem(ChatCommandContext ctx, FoundItem item, int quantity = 1)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			PullService.PullItem(ctx.Event.SenderCharacterEntity, item.prefab, quantity);
		}

		[Command("finditem", "fi", null, "Finds the specified item in containers", null, false)]
		public static void FindItem(ChatCommandContext ctx, FoundItem item)
		{
			//IL_000b: 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)
			Core.Stash.ReportWhereItemIsLocated(ctx.Event.SenderCharacterEntity, item.prefab);
		}
	}
}
namespace KindredLogistics
{
	internal class Buffs
	{
		public delegate void BuffCreated(Entity buffEntity);

		public static bool AddBuff(Entity User, Entity Character, PrefabGUID buffPrefab, float duration = 0f, bool immortal = true)
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: 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_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: 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_0038: 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_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: 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_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: 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_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0141: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_011e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_0134: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			//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_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: 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_014f: 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_0157: Unknown result type (might be due to invalid IL or missing references)
			DebugEventsSystem existingSystemManaged = Core.Server.GetExistingSystemManaged<DebugEventsSystem>();
			ApplyBuffDebugEvent val = default(ApplyBuffDebugEvent);
			val.BuffPrefabGUID = buffPrefab;
			ApplyBuffDebugEvent val2 = val;
			FromCharacter val3 = default(FromCharacter);
			val3.User = User;
			val3.Character = Character;
			FromCharacter val4 = val3;
			Entity entity = default(Entity);
			if (!BuffUtility.TryGetBuff(Core.Server.EntityManager, Character, PrefabIdentifier.op_Implicit(buffPrefab), ref entity))
			{
				existingSystemManaged.ApplyBuff(val4, val2);
				if (BuffUtility.TryGetBuff(Core.Server.EntityManager, Character, PrefabIdentifier.op_Implicit(buffPrefab), ref entity))
				{
					if (entity.Has<CreateGameplayEventsOnSpawn>())
					{
						entity.Remove<CreateGameplayEventsOnSpawn>();
					}
					if (entity.Has<GameplayEventListeners>())
					{
						entity.Remove<GameplayEventListeners>();
					}
					if (immortal)
					{
						entity.Add<Buff_Persists_Through_Death>();
						if (entity.Has<RemoveBuffOnGameplayEvent>())
						{
							entity.Remove<RemoveBuffOnGameplayEvent>();
						}
						if (entity.Has<RemoveBuffOnGameplayEventEntry>())
						{
							entity.Remove<RemoveBuffOnGameplayEventEntry>();
						}
					}
					if (duration > -1f && duration != 0f)
					{
						if (!entity.Has<LifeTime>())
						{
							entity.Add<LifeTime>();
							entity.Write<LifeTime>(new LifeTime
							{
								EndAction = (LifeTimeEndAction)2
							});
						}
						LifeTime componentData = entity.Read<LifeTime>();
						componentData.Duration = duration;
						entity.Write<LifeTime>(componentData);
					}
					else if (duration == -1f)
					{
						if (entity.Has<LifeTime>())
						{
							LifeTime componentData2 = entity.Read<LifeTime>();
							componentData2.Duration = -1f;
							componentData2.EndAction = (LifeTimeEndAction)0;
							entity.Write<LifeTime>(componentData2);
						}
						if (entity.Has<RemoveBuffOnGameplayEvent>())
						{
							entity.Remove<RemoveBuffOnGameplayEvent>();
						}
						if (entity.Has<RemoveBuffOnGameplayEventEntry>())
						{
							entity.Remove<RemoveBuffOnGameplayEventEntry>();
						}
					}
					return true;
				}
				return false;
			}
			return false;
		}

		public static void RemoveBuff(Entity Character, PrefabGUID buffPrefab)
		{
			//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_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			Entity val = default(Entity);
			if (BuffUtility.TryGetBuff(Core.EntityManager, Character, PrefabIdentifier.op_Implicit(buffPrefab), ref val))
			{
				DestroyUtility.Destroy(Core.EntityManager, val, (DestroyDebugReason)13, (string)null, 0);
			}
		}

		public static void RemoveAndAddBuff(Entity userEntity, Entity targetEntity, PrefabGUID buffPrefab, float duration = -1f, BuffCreated callback = null)
		{
			//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_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: 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)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: 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_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: 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)
			if (!BuffUtility.HasBuff(Core.EntityManager, targetEntity, PrefabIdentifier.op_Implicit(buffPrefab)))
			{
				AddBuff(userEntity, targetEntity, buffPrefab, duration);
				Entity buffEntity = default(Entity);
				if (callback != null && BuffUtility.TryGetBuff(Core.Server.EntityManager, targetEntity, PrefabIdentifier.op_Implicit(buffPrefab), ref buffEntity))
				{
					callback(buffEntity);
				}
			}
			else
			{
				Core.StartCoroutine(RemoveAndAddBuffCoroutine(userEntity, targetEntity, buffPrefab, duration, callback));
			}
		}

		private static IEnumerator RemoveAndAddBuffCoroutine(Entity userEntity, Entity targetEntity, PrefabGUID buffPrefab, float duration, BuffCreated callback)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			RemoveBuff(targetEntity, buffPrefab);
			while (BuffUtility.HasBuff(Core.EntityManager, targetEntity, PrefabIdentifier.op_Implicit(buffPrefab)))
			{
				yield return null;
			}
			AddBuff(userEntity, targetEntity, buffPrefab, duration);
			Entity buffEntity = default(Entity);
			if (callback != null && BuffUtility.TryGetBuff(Core.Server.EntityManager, targetEntity, PrefabIdentifier.op_Implicit(buffPrefab), ref buffEntity))
			{
				callback(buffEntity);
			}
		}
	}
	internal class Const
	{
		public const string RECEIVER_REGEX = "r(\\d+)";

		public const string SENDER_REGEX = "s(\\d+)";
	}
	internal static class Core
	{
		public const int MAX_REPLY_LENGTH = 509;

		private static bool hasInitialized;

		private static MonoBehaviour monoBehaviour;

		public static World Server { get; } = GetWorld("Server") ?? throw new Exception("There is no Server world (yet). Did you install a server mod on the client?");


		public static EntityManager EntityManager { get; } = Server.EntityManager;


		public static GameDataSystem GameDataSystem { get; } = Server.GetExistingSystemManaged<GameDataSystem>();


		public static PrefabCollectionSystem PrefabCollectionSystem { get; internal set; }

		public static ServerGameSettingsSystem ServerGameSettingsSystem { get; internal set; }

		public static ServerScriptMapper ServerScriptMapper { get; internal set; }

		public static DebugEventsSystem DebugEventsSystem { get; internal set; }

		public static double ServerTime => ServerGameManager.ServerTime;

		public static ServerGameManager ServerGameManager => ServerScriptMapper.GetServerGameManager();

		public static ManualLogSource Log => Plugin.LogInstance;

		public static ConveyorService ConveyorService { get; internal set; }

		public static LocalizationService Localization { get; } = new LocalizationService();


		public static PlayerSettingsService PlayerSettings { get; } = new PlayerSettingsService();


		public static RefinementStationsService RefinementStations { get; internal set; }

		public static RegionService RegionService { get; internal set; }

		public static SalvageService SalvageService { get; internal set; }

		public static StashService Stash { get; } = new StashService();


		public static TerritoryService TerritoryService { get; internal set; }

		public static void LogException(Exception e, [CallerMemberName] string caller = null)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			ManualLogSource log = Log;
			bool flag = default(bool);
			BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(51, 5, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Failure in ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(caller);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("\nMessage: ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(e.Message);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" Inner:");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(e.InnerException?.Message);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("\n\nStack: ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(e.StackTrace);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("\nInner Stack: ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(e.InnerException?.StackTrace);
			}
			log.LogError(val);
		}

		public static void Initialize()
		{
			if (!hasInitialized)
			{
				PrefabCollectionSystem = Server.GetExistingSystemManaged<PrefabCollectionSystem>();
				ServerGameSettingsSystem = Server.GetExistingSystemManaged<ServerGameSettingsSystem>();
				DebugEventsSystem = Server.GetExistingSystemManaged<DebugEventsSystem>();
				ServerScriptMapper = Server.GetExistingSystemManaged<ServerScriptMapper>();
				RefinementStations = new RefinementStationsService();
				RegionService = new RegionService();
				SalvageService = new SalvageService();
				TerritoryService = new TerritoryService();
				ConveyorService = new ConveyorService();
				FoundItemConverter.LoadItemNames();
				Log.LogInfo((object)"KindredLogistics initialized");
				hasInitialized = true;
			}
		}

		private static World GetWorld(string name)
		{
			Enumerator<World> enumerator = World.s_AllWorlds.GetEnumerator();
			while (enumerator.MoveNext())
			{
				World current = enumerator.Current;
				if (current.Name == name)
				{
					return current;
				}
			}
			return null;
		}

		public static void StartCoroutine(IEnumerator routine)
		{
			//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_0027: Expected O, but got Unknown
			if ((Object)(object)monoBehaviour == (Object)null)
			{
				GameObject val = new GameObject("KindredLogistics");
				monoBehaviour = (MonoBehaviour)(object)val.AddComponent<IgnorePhysicsDebugSystem>();
				Object.DontDestroyOnLoad((Object)val);
			}
			monoBehaviour.StartCoroutine(CollectionExtensions.WrapToIl2Cpp(routine));
		}
	}
	public static class ECSExtensions
	{
		private static EntityManager EntityManager { get; } = Core.Server.EntityManager;


		public unsafe static void Write<T>(this Entity entity, T componentData) where T : struct
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			ComponentType val = default(ComponentType);
			((ComponentType)(ref val))..ctor(Il2CppType.Of<T>(), (AccessMode)0);
			byte[] array = StructureToByteArray(componentData);
			int num = Marshal.SizeOf<T>();
			fixed (byte* ptr = array)
			{
				EntityManager entityManager = EntityManager;
				((EntityManager)(ref entityManager)).SetComponentDataRaw(entity, val.TypeIndex, (void*)ptr, num);
			}
		}

		private static byte[] StructureToByteArray<T>(T structure) where T : struct
		{
			int num = Marshal.SizeOf(structure);
			byte[] array = new byte[num];
			IntPtr intPtr = Marshal.AllocHGlobal(num);
			Marshal.StructureToPtr(structure, intPtr, fDeleteOld: true);
			Marshal.Copy(intPtr, array, 0, num);
			Marshal.FreeHGlobal(intPtr);
			return array;
		}

		public unsafe static T Read<T>(this Entity entity) where T : struct
		{
			//IL_000d: 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_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: 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)
			ComponentType val = default(ComponentType);
			((ComponentType)(ref val))..ctor(Il2CppType.Of<T>(), (AccessMode)0);
			EntityManager entityManager = EntityManager;
			return Marshal.PtrToStructure<T>(new IntPtr(((EntityManager)(ref entityManager)).GetComponentDataRawRO(entity, val.TypeIndex)));
		}

		public static DynamicBuffer<T> ReadBuffer<T>(this Entity entity) where T : struct
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			EntityManager entityManager = Core.Server.EntityManager;
			return ((EntityManager)(ref entityManager)).GetBuffer<T>(entity, false);
		}

		public static bool Has<T>(this Entity entity)
		{
			//IL_000d: 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_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			ComponentType val = default(ComponentType);
			((ComponentType)(ref val))..ctor(Il2CppType.Of<T>(), (AccessMode)0);
			EntityManager entityManager = EntityManager;
			return ((EntityManager)(ref entityManager)).HasComponent(entity, val);
		}

		public static string LookupName(this PrefabGUID prefabGuid)
		{
			//IL_0011: 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_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			PrefabCollectionSystem existingSystemManaged = Core.Server.GetExistingSystemManaged<PrefabCollectionSystem>();
			object obj;
			if (!existingSystemManaged.PrefabGuidToNameDictionary.ContainsKey(prefabGuid))
			{
				obj = "GUID Not Found";
			}
			else
			{
				string text = existingSystemManaged.PrefabGuidToNameDictionary[prefabGuid];
				PrefabGUID val = prefabGuid;
				obj = text + " " + ((object)(PrefabGUID)(ref val)).ToString();
			}
			return obj.ToString();
		}

		public static string PrefabName(this PrefabGUID prefabGuid)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			string prefabName = Core.Localization.GetPrefabName(prefabGuid);
			if (!string.IsNullOrEmpty(prefabName))
			{
				return prefabName;
			}
			return prefabGuid.LookupName();
		}

		public static string EntityName(this Entity entity)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//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_0022: 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_002b: Unknown result type (might be due to invalid IL or missing references)
			NameableInteractable val = entity.Read<NameableInteractable>();
			string text = ((object)(FixedString64Bytes)(ref val.Name)).ToString();
			if (string.IsNullOrEmpty(text) && entity.Has<PrefabGUID>())
			{
				text = entity.Read<PrefabGUID>().PrefabName();
			}
			if (string.IsNullOrEmpty(text))
			{
				text = ((object)(Entity)(ref entity)).ToString();
			}
			return text;
		}

		public static void Add<T>(this Entity entity)
		{
			//IL_000d: 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_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			ComponentType val = default(ComponentType);
			((ComponentType)(ref val))..ctor(Il2CppType.Of<T>(), (AccessMode)0);
			EntityManager entityManager = EntityManager;
			((EntityManager)(ref entityManager)).AddComponent(entity, val);
		}

		public static void Remove<T>(this Entity entity)
		{
			//IL_000d: 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_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			ComponentType val = default(ComponentType);
			((ComponentType)(ref val))..ctor(Il2CppType.Of<T>(), (AccessMode)0);
			EntityManager entityManager = EntityManager;
			((EntityManager)(ref entityManager)).RemoveComponent(entity, val);
		}
	}
	[BepInPlugin("KindredLogistics", "KindredLogistics", "1.2.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BasePlugin
	{
		private static Plugin plugin;

		private Harmony _harmony;

		public static Harmony Harmony => plugin._harmony;

		public static ManualLogSource LogInstance => ((BasePlugin)plugin).Log;

		public override void Load()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Expected O, but got Unknown
			plugin = this;
			ManualLogSource log = ((BasePlugin)this).Log;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(27, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Plugin ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("KindredLogistics");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" version ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("1.2.0");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" is loaded!");
			}
			log.LogInfo(val);
			_harmony = new Harmony("KindredLogistics");
			_harmony.PatchAll(Assembly.GetExecutingAssembly());
			CommandRegistry.RegisterAll();
		}

		public override bool Unload()
		{
			CommandRegistry.UnregisterAssembly();
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			return true;
		}
	}
	public class Utilities
	{
		public static readonly ComponentType[] StashQuery = (ComponentType[])(object)new ComponentType[4]
		{
			ComponentType.ReadOnly(Il2CppType.Of<InventoryOwner>()),
			ComponentType.ReadOnly(Il2CppType.Of<CastleHeartConnection>()),
			ComponentType.ReadOnly(Il2CppType.Of<AttachedBuffer>()),
			ComponentType.ReadOnly(Il2CppType.Of<NameableInteractable>())
		};

		public static readonly ComponentType[] RefinementStationQuery = (ComponentType[])(object)new ComponentType[4]
		{
			ComponentType.ReadOnly(Il2CppType.Of<Team>()),
			ComponentType.ReadOnly(Il2CppType.Of<CastleHeartConnection>()),
			ComponentType.ReadOnly(Il2CppType.Of<Refinementstation>()),
			ComponentType.ReadOnly(Il2CppType.Of<NameableInteractable>())
		};

		public static readonly ComponentType[] UserEntityQuery = (ComponentType[])(object)new ComponentType[1] { ComponentType.ReadOnly(Il2CppType.Of<User>()) };

		public static void StashServantInventory(Entity servant)
		{
			//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_0011: 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)
			Entity inventory = default(Entity);
			if (InventoryUtilities.TryGetInventoryEntity(Core.EntityManager, servant, ref inventory, 0))
			{
				StashInventoryEntity(servant, inventory, "spoils");
			}
		}

		public static void StashInventoryEntity(Entity entityWithTerritory, Entity inventory, string overflowStashName)
		{
			//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_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b4: Expected O, but got Unknown
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: 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_00a5: 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_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			//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_01f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0202: Unknown result type (might be due to invalid IL or missing references)
			//IL_0203: Unknown result type (might be due to invalid IL or missing references)
			//IL_020d: 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)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: 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_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_026d: 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_027a: Unknown result type (might be due to invalid IL or missing references)
			//IL_027c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0281: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0134: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_022e: 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_0232: Unknown result type (might be due to invalid IL or missing references)
			//IL_0237: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: Unknown result type (might be due to invalid IL or missing references)
			//IL_0194: Unknown result type (might be due to invalid IL or missing references)
			//IL_0199: Unknown result type (might be due to invalid IL or missing references)
			ServerGameManager serverGameManager = Core.ServerGameManager;
			Dictionary<PrefabGUID, List<(Entity, Entity)>> dictionary = new Dictionary<PrefabGUID, List<(Entity, Entity)>>(100);
			(Entity, Entity) tuple = (Entity.Null, Entity.Null);
			try
			{
				Entity val2 = default(Entity);
				DynamicBuffer<AttachedBuffer> val3 = default(DynamicBuffer<AttachedBuffer>);
				foreach (Entity stash in Core.Stash.GetAllAlliedStashesOnTerritory(entityWithTerritory))
				{
					NameableInteractable val = stash.Read<NameableInteractable>();
					if (((object)(FixedString64Bytes)(ref val.Name)).ToString().ToLower().Contains(overflowStashName) && ((Entity)(ref tuple.Item1)).Equals(Entity.Null))
					{
						if (!InventoryUtilities.TryGetInventoryEntity(Core.EntityManager, stash, ref val2, 0))
						{
							continue;
						}
						if (!((ServerGameManager)(ref serverGameManager)).HasFullInventory(val2))
						{
							tuple = (stash, val2);
							continue;
						}
					}
					if (!((ServerGameManager)(ref serverGameManager)).TryGetBuffer<AttachedBuffer>(stash, ref val3))
					{
						continue;
					}
					Enumerator<AttachedBuffer> enumerator2 = val3.GetEnumerator();
					while (enumerator2.MoveNext())
					{
						Entity entity = enumerator2.Current.Entity;
						if (!entity.Has<PrefabGUID>())
						{
							continue;
						}
						PrefabGUID val4 = entity.Read<PrefabGUID>();
						if (!((PrefabGUID)(ref val4)).Equals(StashService.ExternalInventoryPrefab))
						{
							continue;
						}
						Enumerator<InventoryBuffer> enumerator3 = entity.ReadBuffer<InventoryBuffer>().GetEnumerator();
						while (enumerator3.MoveNext())
						{
							PrefabGUID itemType = enumerator3.Current.ItemType;
							if (((PrefabGUID)(ref itemType)).GuidHash != 0)
							{
								if (!dictionary.TryGetValue(itemType, out var value))
								{
									value = (dictionary[itemType] = new List<(Entity, Entity)>());
								}
								else if (value.Any<(Entity, Entity)>(((Entity stash, Entity inventory) x) => x.stash == stash))
								{
									continue;
								}
								value.Add((stash, entity));
							}
						}
					}
				}
				DynamicBuffer<InventoryBuffer> val5 = default(DynamicBuffer<InventoryBuffer>);
				if (!((ServerGameManager)(ref serverGameManager)).TryGetBuffer<InventoryBuffer>(inventory, ref val5))
				{
					return;
				}
				for (int i = 0; i < val5.Length; i++)
				{
					PrefabGUID itemType2 = val5[i].ItemType;
					int num = ((ServerGameManager)(ref serverGameManager)).GetInventoryItemCount(inventory, itemType2);
					if (dictionary.TryGetValue(itemType2, out var value2))
					{
						foreach (var item in value2)
						{
							num -= TransferItems(serverGameManager, inventory, item.Item2, itemType2, num);
							if (num <= 0)
							{
								break;
							}
						}
					}
					if (num > 0 && !((Entity)(ref tuple.Item1)).Equals(Entity.Null))
					{
						TransferItems(serverGameManager, inventory, tuple.Item2, itemType2, num);
					}
				}
			}
			catch (Exception ex)
			{
				ManualLogSource log = Core.Log;
				bool flag = default(bool);
				BepInExErrorLogInterpolatedStringHandler val6 = new BepInExErrorLogInterpolatedStringHandler(36, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val6).AppendLiteral("Exited StashServantInventory early: ");
					((BepInExLogInterpolatedStringHandler)val6).AppendFormatted<Exception>(ex);
				}
				log.LogError(val6);
			}
		}

		public static bool TerritoryCheck(Entity character, Entity target)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: 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_0011: 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_0017: Unknown result type (might be due to invalid IL or missing references)
			//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_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: 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_0035: Unknown result type (might be due to invalid IL or missing references)
			if (!target.Has<CastleHeartConnection>())
			{
				return false;
			}
			TilePosition val = character.Read<TilePosition>();
			CastleHeartConnection val2 = target.Read<CastleHeartConnection>();
			Entity castleTerritoryEntity = ((NetworkedEntity)(ref val2.CastleHeartEntity)).GetEntityOnServer().Read<CastleHeart>().CastleTerritoryEntity;
			CastleTerritory val3 = default(CastleTerritory);
			return CastleTerritoryExtensions.IsTileInTerritory(Core.EntityManager, val.Tile, ref castleTerritoryEntity, ref val3);
		}

		public static bool SharedHeartConnection(Entity input, Entity ouput)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: 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_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: 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_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: 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_0027: 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_0034: Unknown result type (might be due to invalid IL or missing references)
			if (input.Has<CastleHeartConnection>() && ouput.Has<CastleHeartConnection>())
			{
				Entity entity = input.Read<CastleHeartConnection>().CastleHeartEntity._Entity;
				Entity entity2 = ouput.Read<CastleHeartConnection>().CastleHeartEntity._Entity;
				return ((Entity)(ref entity)).Equals(entity2);
			}
			return false;
		}

		public static bool TransferItemEntities(Entity outputInventory, Entity inputInventory, PrefabGUID itemPrefab, int transferAmount, ref int startInputSlot, out int amountTransferred)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//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_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: 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_004c: 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_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: 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_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: 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_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			DynamicBuffer<InventoryBuffer> val = outputInventory.ReadBuffer<InventoryBuffer>();
			DynamicBuffer<InventoryBuffer> val2 = inputInventory.ReadBuffer<InventoryBuffer>();
			amountTransferred = 0;
			for (int i = 0; i < val.Length; i++)
			{
				InventoryBuffer val3 = val[i];
				if (!((PrefabGUID)(ref val3.ItemType)).Equals(itemPrefab))
				{
					continue;
				}
				while (startInputSlot < val2.Length)
				{
					InventoryBuffer val4 = val2[startInputSlot];
					if (!((PrefabGUID)(ref val4.ItemType)).Equals(PrefabGUID.Empty))
					{
						startInputSlot++;
						continue;
					}
					val2[startInputSlot] = val3;
					val[i] = val4;
					Entity entityOnServer = ((NetworkedEntity)(ref val3.ItemEntity)).GetEntityOnServer();
					if (entityOnServer.Has<InventoryItem>())
					{
						InventoryItem componentData = entityOnServer.Read<InventoryItem>();
						componentData.ContainerEntity = inputInventory;
						entityOnServer.Write<InventoryItem>(componentData);
					}
					startInputSlot++;
					amountTransferred++;
					break;
				}
				if (amountTransferred >= transferAmount)
				{
					return false;
				}
				if (val2.Length <= startInputSlot)
				{
					return true;
				}
			}
			return false;
		}

		public static int TransferItems(ServerGameManager serverGameManager, Entity outputInventory, Entity inputInventory, PrefabGUID itemGuid, int transferAmount)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: 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_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: 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_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			if (((ServerGameManager)(ref serverGameManager)).TryRemoveInventoryItem(outputInventory, itemGuid, transferAmount))
			{
				AddItemResponse val = ((ServerGameManager)(ref serverGameManager)).TryAddInventoryItem(inputInventory, itemGuid, transferAmount);
				if ((int)val.Result == 0)
				{
					return transferAmount;
				}
				_ = ((ServerGameManager)(ref serverGameManager)).TryAddInventoryItem(outputInventory, itemGuid, val.RemainingAmount).Result;
				return transferAmount - val.RemainingAmount;
			}
			return 0;
		}

		public static AddItemSettings GetAddItemSettings()
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			AddItemSettings result = default(AddItemSettings);
			result.EntityManager = Core.EntityManager;
			ServerGameManager serverGameManager = Core.ServerGameManager;
			GCHandle gCHandle = GCHandle.Alloc(((ServerGameManager)(ref serverGameManager)).ItemLookupMap, GCHandleType.Pinned);
			try
			{
				IntPtr ptr = gCHandle.AddrOfPinnedObject();
				result.ItemDataMap = Marshal.ReadIntPtr(ptr);
				return result;
			}
			finally
			{
				if (gCHandle.IsAllocated)
				{
					gCHandle.Free();
				}
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "KindredLogistics";

		public const string PLUGIN_NAME = "KindredLogistics";

		public const string PLUGIN_VERSION = "1.2.0";
	}
}
namespace KindredLogistics.Services
{
	internal class ConveyorService
	{
		private readonly Random random = new Random();

		private readonly List<Entity> distributionList = new List<Entity>();

		private readonly Dictionary<Entity, int> amountReceiving = new Dictionary<Entity, int>();

		private readonly IgnorePhysicsDebugSystem conveyorMonoBehaviour;

		private const int MIN_TERRITORY_ID = 0;

		private const int MAX_TERRITORY_ID = 138;

		private EntityQuery castleHeartQuery;

		private Dictionary<int, Entity> territoryToCastleHeart = new Dictionary<int, Entity>();

		public ConveyorService()
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: 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)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			EntityQueryDesc val = new EntityQueryDesc();
			val.All = Il2CppStructArray<ComponentType>.op_Implicit((ComponentType[])(object)new ComponentType[1]
			{
				new ComponentType(Il2CppType.Of<CastleHeart>(), (AccessMode)1)
			});
			val.Options = (EntityQueryOptions)0;
			EntityQueryDesc val2 = val;
			EntityManager entityManager = Core.EntityManager;
			castleHeartQuery = ((EntityManager)(ref entityManager)).CreateEntityQuery((EntityQueryDesc[])(object)new EntityQueryDesc[1] { val2 });
			conveyorMonoBehaviour = new GameObject("ConveyorService").AddComponent<IgnorePhysicsDebugSystem>();
			((MonoBehaviour)conveyorMonoBehaviour).StartCoroutine(CollectionExtensions.WrapToIl2Cpp(UpdateLoop()));
		}

		private IEnumerator UpdateLoop()
		{
			yield return null;
			while (true)
			{
				NativeArray<Entity> val = ((EntityQuery)(ref castleHeartQuery)).ToEntityArray(AllocatorHandle.op_Implicit((Allocator)2));
				try
				{
					Enumerator<Entity> enumerator = val.GetEnumerator();
					while (enumerator.MoveNext())
					{
						Entity current = enumerator.Current;
						CastleTerritory val2 = current.Read<CastleHeart>().CastleTerritoryEntity.Read<CastleTerritory>();
						territoryToCastleHeart[val2.CastleTerritoryIndex] = current;
					}
				}
				finally
				{
					val.Dispose();
				}
				yield return null;
				for (int i = 0; i <= 138; i++)
				{
					if (Core.PlayerSettings.IsConveyorEnabled(0uL))
					{
						try
						{
							ProcessConveyors(i);
						}
						catch (Exception e)
						{
							Core.LogException(e, $"ProcessConveyors({i})");
						}
					}
					if (Core.PlayerSettings.IsSalvageEnabled(0uL))
					{
						try
						{
							ProcessSalvagers(i);
						}
						catch (Exception e2)
						{
							Core.LogException(e2, $"ProcessSalvagers({i})");
						}
					}
					yield return null;
				}
			}
		}

		private void ProcessConveyors(int territoryId)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: 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_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_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: 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_007a: 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_00ad: 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_00b3: 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_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: 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_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: 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_00f2: Unknown result type (might be due to invalid IL or missing references)
			//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_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: 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_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_0119: Unknown result type (might be due to invalid IL or missing references)
			//IL_0257: Unknown result type (might be due to invalid IL or missing references)
			//IL_025c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0260: 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)
			//IL_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0133: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: 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_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_014d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0270: Unknown result type (might be due to invalid IL or missing references)
			//IL_0275: 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_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0173: Unknown result type (might be due to invalid IL or missing references)
			//IL_0178: Unknown result type (might be due to invalid IL or missing references)
			//IL_027e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0283: Unknown result type (might be due to invalid IL or missing references)
			//IL_0288: 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_0296: Unknown result type (might be due to invalid IL or missing references)
			//IL_0298: Unknown result type (might be due to invalid IL or missing references)
			//IL_029d: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a1: 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_0183: 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_018c: 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_0192: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_02af: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_037e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0383: Unknown result type (might be due to invalid IL or missing references)
			//IL_0388: Unknown result type (might be due to invalid IL or missing references)
			//IL_0391: Unknown result type (might be due to invalid IL or missing references)
			//IL_0396: 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_01a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bc: 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_03a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01de: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0315: Unknown result type (might be due to invalid IL or missing references)
			//IL_0300: Unknown result type (might be due to invalid IL or missing references)
			//IL_0302: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0404: Unknown result type (might be due to invalid IL or missing references)
			//IL_0409: Unknown result type (might be due to invalid IL or missing references)
			//IL_040f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0414: Unknown result type (might be due to invalid IL or missing references)
			//IL_0419: Unknown result type (might be due to invalid IL or missing references)
			//IL_041b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0424: Unknown result type (might be due to invalid IL or missing references)
			//IL_0426: Unknown result type (might be due to invalid IL or missing references)
			//IL_042b: Unknown result type (might be due to invalid IL or missing references)
			//IL_042f: Unknown result type (might be due to invalid IL or missing references)
			//IL_043e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0441: Unknown result type (might be due to invalid IL or missing references)
			if (!territoryToCastleHeart.TryGetValue(territoryId, out var value))
			{
				return;
			}
			EntityManager entityManager = Core.EntityManager;
			if (!((EntityManager)(ref entityManager)).Exists(value))
			{
				territoryToCastleHeart.Remove(territoryId);
				return;
			}
			UserOwner val = value.Read<UserOwner>();
			if (((NetworkedEntity)(ref val.Owner)).GetEntityOnServer() == Entity.Null)
			{
				return;
			}
			ulong platformId = ((NetworkedEntity)(ref val.Owner)).GetEntityOnServer().Read<User>().PlatformId;
			if (!Core.PlayerSettings.IsConveyorEnabled(platformId))
			{
				return;
			}
			ServerGameManager serverGameManager = Core.ServerGameManager;
			Dictionary<(int, PrefabGUID), List<(Entity, int)>> dictionary = new Dictionary<(int, PrefabGUID), List<(Entity, int)>>();
			PrefabGUID val5;
			foreach (var allReceivingStation in Core.RefinementStations.GetAllReceivingStations(territoryId))
			{
				int item = allReceivingStation.group;
				Entity item2 = allReceivingStation.station;
				Refinementstation val2 = item2.Read<Refinementstation>();
				CastleWorkstation val3 = item2.Read<CastleWorkstation>();
				float num = (((Enum)val3.WorkstationLevel).HasFlag((Enum)(object)(WorkstationLevel)2) ? 0.75f : 1f);
				Entity entityOnServer = ((NetworkedEntity)(ref val2.InputInventoryEntity)).GetEntityOnServer();
				DynamicBuffer<InventoryBuffer> val4 = entityOnServer.ReadBuffer<InventoryBuffer>();
				Enumerator<RefinementstationRecipesBuffer> enumerator2 = item2.ReadBuffer<RefinementstationRecipesBuffer>().GetEnumerator();
				while (enumerator2.MoveNext())
				{
					RefinementstationRecipesBuffer current2 = enumerator2.Current;
					if (current2.Disabled)
					{
						continue;
					}
					Enumerator<RecipeRequirementBuffer> enumerator3 = Core.PrefabCollectionSystem._PrefabGuidToEntityMap[current2.RecipeGuid].ReadBuffer<RecipeRequirementBuffer>().GetEnumerator();
					while (enumerator3.MoveNext())
					{
						RecipeRequirementBuffer current3 = enumerator3.Current;
						int num2 = 2 * Mathf.RoundToInt((float)current3.Amount * num);
						Enumerator<InventoryBuffer> enumerator4 = val4.GetEnumerator();
						while (enumerator4.MoveNext())
						{
							InventoryBuffer current4 = enumerator4.Current;
							val5 = current4.ItemType;
							if (((PrefabGUID)(ref val5)).Equals(current3.Guid))
							{
								num2 -= current4.Amount;
							}
						}
						if (num2 > 0)
						{
							if (!dictionary.TryGetValue((item, current3.Guid), out var value2))
							{
								value2 = new List<(Entity, int)>();
								dictionary[(item, current3.Guid)] = value2;
							}
							value2.Add((entityOnServer, num2));
						}
					}
				}
			}
			DynamicBuffer<AttachedBuffer> val7 = default(DynamicBuffer<AttachedBuffer>);
			foreach (var (item3, val6) in Core.Stash.GetAllReceivingStashes(territoryId))
			{
				if (!((ServerGameManager)(ref serverGameManager)).TryGetBuffer<AttachedBuffer>(val6, ref val7))
				{
					continue;
				}
				Enumerator<AttachedBuffer> enumerator5 = val7.GetEnumerator();
				while (enumerator5.MoveNext())
				{
					Entity entity = enumerator5.Current.Entity;
					if (!entity.Has<PrefabGUID>())
					{
						continue;
					}
					val5 = entity.Read<PrefabGUID>();
					if (!((PrefabGUID)(ref val5)).Equals(StashService.ExternalInventoryPrefab))
					{
						continue;
					}
					Enumerator<InventoryBuffer> enumerator4 = entity.ReadBuffer<InventoryBuffer>().GetEnumerator();
					while (enumerator4.MoveNext())
					{
						InventoryBuffer current5 = enumerator4.Current;
						val5 = current5.ItemType;
						if (((PrefabGUID)(ref val5)).GuidHash != 0)
						{
							if (!dictionary.TryGetValue((item3, current5.ItemType), out var value3))
							{
								value3 = new List<(Entity, int)>();
								dictionary[(item3, current5.ItemType)] = value3;
							}
							value3.Add((entity, 1));
						}
					}
				}
			}
			if (dictionary.Count == 0)
			{
				return;
			}
			foreach (var allSendingStation in Core.RefinementStations.GetAllSendingStations(territoryId))
			{
				int item4 = allSendingStation.group;
				Refinementstation val8 = allSendingStation.station.Read<Refinementstation>();
				Entity entityOnServer2 = ((NetworkedEntity)(ref val8.OutputInventoryEntity)).GetEntityOnServer();
				if (!((Entity)(ref entityOnServer2)).Equals(Entity.Null))
				{
					DistributeInventory(dictionary, serverGameManager, item4, entityOnServer2);
				}
			}
			DynamicBuffer<AttachedBuffer> val10 = default(DynamicBuffer<AttachedBuffer>);
			foreach (var (group, val9) in Core.Stash.GetAllSendingStashes(territoryId))
			{
				if (!((ServerGameManager)(ref serverGameManager)).TryGetBuffer<AttachedBuffer>(val9, ref val10))
				{
					continue;
				}
				Enumerator<AttachedBuffer> enumerator5 = val10.GetEnumerator();
				while (enumerator5.MoveNext())
				{
					Entity entity2 = enumerator5.Current.Entity;
					if (entity2.Has<PrefabGUID>())
					{
						val5 = entity2.Read<PrefabGUID>();
						if (((PrefabGUID)(ref val5)).Equals(StashService.ExternalInventoryPrefab))
						{
							DistributeInventory(dictionary, serverGameManager, group, entity2, 1);
						}
					}
				}
			}
		}

		private void DistributeInventory(Dictionary<(int group, PrefabGUID item), List<(Entity receiver, int amount)>> receivingNeeds, ServerGameManager serverGameManager, int group, Entity inventoryEntity, int retain = 0)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: 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_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: 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_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: 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_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: 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)
			//IL_0098: 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_00a6: 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_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: 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_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_016f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0174: 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_019d: Unknown result type (might be due to invalid IL or missing references)
			//IL_018d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01da: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e2: 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_01e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0216: Unknown result type (might be due to invalid IL or missing references)
			//IL_0233: Unknown result type (might be due to invalid IL or missing references)
			Enumerator<InventoryBuffer> enumerator = inventoryEntity.ReadBuffer<InventoryBuffer>().GetEnumerator();
			while (enumerator.MoveNext())
			{
				InventoryBuffer current = enumerator.Current;
				PrefabGUID itemType = current.ItemType;
				if (((PrefabGUID)(ref itemType)).GuidHash == 0 || !receivingNeeds.TryGetValue((group, current.ItemType), out List<(Entity, int)> value))
				{
					continue;
				}
				int num = current.Amount - retain;
				if (num <= 0)
				{
					continue;
				}
				if (value.Sum<(Entity, int)>(((Entity receiver, int amount) x) => x.amount) < num)
				{
					foreach (var (inputInventory, transferAmount) in value)
					{
						Utilities.TransferItems(serverGameManager, inventoryEntity, inputInventory, current.ItemType, transferAmount);
					}
					value.Clear();
					continue;
				}
				distributionList.Clear();
				foreach (var item3 in value)
				{
					Entity item = item3.Item1;
					int item2 = item3.Item2;
					for (int i = 0; i < item2; i++)
					{
						distributionList.Add(item);
					}
				}
				amountReceiving.Clear();
				for (int j = 0; j < num; j++)
				{
					int index = random.Next(distributionList.Count);
					Entity key = distributionList[index];
					if (!amountReceiving.TryGetValue(key, out var value2))
					{
						amountReceiving[key] = 1;
					}
					else
					{
						amountReceiving[key] = value2 + 1;
					}
				}
				foreach (var (val2, num3) in amountReceiving)
				{
					Utilities.TransferItems(serverGameManager, inventoryEntity, val2, current.ItemType, num3);
					int num4 = num3;
					for (int num5 = value.Count - 1; num5 >= 0; num5--)
					{
						(Entity, int) tuple2 = value[num5];
						if (((Entity)(ref tuple2.Item1)).Equals(val2))
						{
							if (value[num5].Item2 > num4)
							{
								value[num5] = (val2, value[num5].Item2 - num4);
								break;
							}
							num4 -= value[num5].Item2;
							value.RemoveAt(num5);
						}
					}
				}
			}
		}

		private void ProcessSalvagers(int territoryId)
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: 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_0041: 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_0047: 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_0068: 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_009f: 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_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: 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_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0216: Unknown result type (might be due to invalid IL or missing references)
			//IL_021b: Unknown result type (might be due to invalid IL or missing references)
			//IL_021d: Unknown result type (might be due to invalid IL or missing references)
			//IL_021f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0224: Unknown result type (might be due to invalid IL or missing references)
			//IL_022d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0232: Unknown result type (might be due to invalid IL or missing references)
			//IL_0234: Unknown result type (might be due to invalid IL or missing references)
			//IL_0239: Unknown result type (might be due to invalid IL or missing references)
			//IL_023d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0240: Unknown result type (might be due to invalid IL or missing references)
			//IL_0245: Unknown result type (might be due to invalid IL or missing references)
			//IL_024a: Unknown result type (might be due to invalid IL or missing references)
			//IL_024f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0254: 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_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_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0256: 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)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: 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_026b: Unknown result type (might be due to invalid IL or missing references)
			//IL_026d: 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)
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_013c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_015c: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Unknown result type (might be due to invalid IL or missing references)
			//IL_0165: Unknown result type (might be due to invalid IL or missing references)
			//IL_016e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0173: Unknown result type (might be due to invalid IL or missing references)
			//IL_0178: Unknown result type (might be due to invalid IL or missing references)
			//IL_017a: 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_0183: Unknown result type (might be due to invalid IL or missing references)
			//IL_019b: 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_01ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
			Entity[] array = Core.SalvageService.GetAllSalvageStations(territoryId).ToArray();
			if (!territoryToCastleHeart.TryGetValue(territoryId, out var value))
			{
				return;
			}
			EntityManager entityManager = Core.EntityManager;
			if (!((EntityManager)(ref entityManager)).Exists(value))
			{
				territoryToCastleHeart.Remove(territoryId);
				return;
			}
			UserOwner val = value.Read<UserOwner>();
			if (((NetworkedEntity)(ref val.Owner)).GetEntityOnServer() == Entity.Null)
			{
				return;
			}
			ulong platformId = ((NetworkedEntity)(ref val.Owner)).GetEntityOnServer().Read<User>().PlatformId;
			if (!Core.PlayerSettings.IsSalvageEnabled(platformId))
			{
				return;
			}
			DynamicBuffer<AttachedBuffer> val2 = default(DynamicBuffer<AttachedBuffer>);
			Entity entity2 = default(Entity);
			Entity[] array2;
			foreach (Entity allSalvageStash in Core.Stash.GetAllSalvageStashes(territoryId))
			{
				ServerGameManager serverGameManager = Core.ServerGameManager;
				if (!((ServerGameManager)(ref serverGameManager)).TryGetBuffer<AttachedBuffer>(allSalvageStash, ref val2))
				{
					continue;
				}
				Enumerator<AttachedBuffer> enumerator2 = val2.GetEnumerator();
				while (enumerator2.MoveNext())
				{
					Entity entity = enumerator2.Current.Entity;
					if (!entity.Has<PrefabGUID>())
					{
						continue;
					}
					PrefabGUID val3 = entity.Read<PrefabGUID>();
					if (!((PrefabGUID)(ref val3)).Equals(StashService.ExternalInventoryPrefab))
					{
						continue;
					}
					Enumerator<InventoryBuffer> enumerator3 = entity.ReadBuffer<InventoryBuffer>().GetEnumerator();
					while (enumerator3.MoveNext())
					{
						InventoryBuffer current2 = enumerator3.Current;
						Core.PrefabCollectionSystem._PrefabGuidToEntityMap.TryGetValue(current2.ItemType, ref entity2);
						if (!entity2.Has<Salvageable>())
						{
							continue;
						}
						int num = 0;
						array2 = array;
						foreach (Entity entity3 in array2)
						{
							Salvagestation val4 = entity3.Read<Salvagestation>();
							Entity entityOnServer = ((NetworkedEntity)(ref val4.InputInventoryEntity)).GetEntityOnServer();
							int startInputSlot = 0;
							Utilities.TransferItemEntities(entity, entityOnServer, current2.ItemType, current2.Amount - num, ref startInputSlot, out var amountTransferred);
							if (amountTransferred != 0)
							{
								if (!val4.IsWorking)
								{
									val4.IsWorking = true;
									entity3.Write<Salvagestation>(val4);
								}
								num += amountTransferred;
								if (num >= current2.Amount)
								{
									break;
								}
							}
						}
					}
				}
			}
			array2 = array;
			foreach (Entity val5 in array2)
			{
				Salvagestation val6 = val5.Read<Salvagestation>();
				Entity entityOnServer2 = ((NetworkedEntity)(ref val6.OutputInventoryEntity)).GetEntityOnServer();
				entityManager = Core.EntityManager;
				NativeArray<InventoryBuffer> val7 = ((EntityManager)(ref entityManager)).GetBuffer<InventoryBuffer>(entityOnServer2, false).ToNativeArray(AllocatorHandle.op_Implicit((Allocator)2));
				try
				{
					if (InventoryUtilities.IsInventoryEmpty(val7))
					{
						continue;
					}
				}
				finally
				{
					val7.Dispose();
				}
				Utilities.StashInventoryEntity(val5, entityOnServer2, "excess");
			}
		}
	}
	internal class LocalizationService
	{
		private struct Code
		{
			public string Key { get; set; }

			public string Value { get; set; }

			public string Description { get; set; }
		}

		private struct Node
		{
			public string Guid { get; set; }

			public string Text { get; set; }
		}

		private struct LocalizationFile
		{
			public Code[] Codes { get; set; }

			public Node[] Nodes { get; set; }
		}

		private Dictionary<string, string> localization = new Dictionary<string, string>();

		private Dictionary<int, string> prefabNames = new Dictionary<int, string>();

		public LocalizationService()
		{
			LoadLocalization();
			LoadPrefabNames();
		}

		private void LoadLocalization()
		{
			string name = "KindredLogistics.Localization.English.json";
			Stream manifestResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
			if (manifestResourceStream != null)
			{
				using (StreamReader streamReader = new StreamReader(manifestResourceStream))
				{
					localization = JsonSerializer.Deserialize<LocalizationFile>(streamReader.ReadToEnd()).Nodes.ToDictionary((Node x) => x.Guid, (Node x) => x.Text);
					return;
				}
			}
			Console.WriteLine("Resource not found!");
		}

		private void LoadPrefabNames()
		{
			string name = "KindredLogistics.Data.PrefabNames.json";
			Stream manifestResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
			if (manifestResourceStream != null)
			{
				using (StreamReader streamReader = new StreamReader(manifestResourceStream))
				{
					string json = streamReader.ReadToEnd();
					prefabNames = JsonSerializer.Deserialize<Dictionary<int, string>>(json);
					return;
				}
			}
			Console.WriteLine("Resource not found!");
		}

		public string GetLocalization(string guid)
		{
			if (localization.TryGetValue(guid, out var value))
			{
				return value;
			}
			return "<Localization not found for " + guid + ">";
		}

		public string GetLocalization(LocalizationKey key)
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			Guid val = ((AssetGuid)(ref key.Key)).ToGuid();
			string guid = ((object)(Guid)(ref val)).ToString();
			return GetLocalization(guid);
		}

		public string GetPrefabName(PrefabGUID itemPrefabGUID)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//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_003f: 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_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_011f: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: 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_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Invalid comparison between Unknown and I4
			//IL_013a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: 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_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: 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_00a0: Unknown result type (might be due to invalid IL or missing references)
			if (!prefabNames.TryGetValue(itemPrefabGUID._Value, out var value))
			{
				return null;
			}
			string text = GetLocalization(value);
			if (itemPrefabGUID._Value == -1265586439)
			{
				text = "Darkmatter Pistols";
			}
			PrefabLookupMap prefabLookupMap = Core.PrefabCollectionSystem._PrefabLookupMap;
			Entity entity = default(Entity);
			if (((PrefabLookupMap)(ref prefabLookupMap)).TryGetValue(itemPrefabGUID, ref entity))
			{
				if (entity.Has<ItemData>() && (int)entity.Read<ItemData>().ItemType == 5)
				{
					text = "Book " + text;
				}
				if (entity.Has<JewelInstance>())
				{
					JewelInstance val = entity.Read<JewelInstance>();
					if (val.TierIndex != 0)
					{
						text += $" Jewel Tier {val.TierIndex + 1}";
					}
				}
				if (entity.Has<LegendaryItemInstance>())
				{
					LegendaryItemInstance val2 = entity.Read<LegendaryItemInstance>();
					text += $" Tier {val2.TierIndex + 1}";
				}
				if (entity.Has<ShatteredItem>())
				{
					text += " Shattered";
				}
			}
			if (itemPrefabGUID._Value == 1455590675 || itemPrefabGUID._Value == -651642571)
			{
				text += " Tier 1";
			}
			else if (itemPrefabGUID._Value == 1150376281 || itemPrefabGUID._Value == 686122001)
			{
				text += " Tier 2";
			}
			return text;
		}
	}
	internal class PlayerSettingsService
	{
		public struct PlayerSettings
		{
			public bool SortStash { get; set; }

			public bool Pull { get; set; }

			public bool CraftPull { get; set; }

			public bool DontPullLast { get; set; }

			public bool AutoStashMissions { get; set; }

			public bool Conveyor { get; set; }

			public bool Salvage { get; set; }

			public bool SilentPull { get; set; }

			public bool SilentStash { get; set; }

			public PlayerSettings()
			{
				SortStash = false;
				Pull = false;
				CraftPull = false;
				AutoStashMissions = false;
				Conveyor = false;
				Salvage = false;
				SilentPull = false;
				SilentStash = false;
				DontPullLast = true;
			}
		}

		private const int GLOBAL_PLAYER_ID = 0;

		private static readonly string CONFIG_PATH = Path.Combine(Paths.ConfigPath, "KindredLogistics");

		private static readonly string PLAYER_SETTINGS_PATH = Path.Combine(CONFIG_PATH, "playerSettings.json");

		private static readonly JsonSerializerOptions prettyJsonOptions = new JsonSerializerOptions
		{
			WriteIndented = true,
			IncludeFields = true
		};

		private PlayerSettings defaultSettings = new PlayerSettings();

		private Dictionary<ulong, PlayerSettings> playerSettings = new Dictionary<ulong, PlayerSettings>();

		public PlayerSettingsService()
		{
			LoadSettings();
			if (!playerSettings.ContainsKey(0uL))
			{
				playerSettings[0uL] = new PlayerSettings
				{
					SortStash = true,
					Pull = true,
					CraftPull = true,
					AutoStashMissions = true,
					Conveyor = true,
					Salvage = true
				};
				SaveSettings();
			}
		}

		private void LoadSettings()
		{
			if (!File.Exists(PLAYER_SETTINGS_PATH))
			{
				SaveSettings();
				return;
			}
			string json = File.ReadAllText(PLAYER_SETTINGS_PATH);
			playerSettings = JsonSerializer.Deserialize<Dictionary<ulong, PlayerSettings>>(json);
		}

		private void SaveSettings()
		{
			if (!Directory.Exists(CONFIG_PATH))
			{
				Directory.CreateDirectory(CONFIG_PATH);
			}
			string contents = JsonSerializer.Serialize(playerSettings, prettyJsonOptions);
			File.WriteAllText(PLAYER_SETTINGS_PATH, contents);
		}

		public bool IsSortStashEnabled(ulong playerId)
		{
			if (!playerSettings.TryGetValue(playerId, out var value))
			{
				value = defaultSettings;
			}
			if (value.SortStash)
			{
				return playerSettings[0uL].SortStash;
			}
			return false;
		}

		public bool ToggleSortStash(ulong playerId = 0uL)
		{
			if (!playerSettings.TryGetValue(playerId, out var value))
			{
				va