Decompiled source of DragonEggQualityRecipe v1.0.0

plugins/DragonEggQualityRecipe.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("DragonEggQualityRecipe")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("DragonEggQualityRecipe")]
[assembly: AssemblyTitle("DragonEggQualityRecipe")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace DragonEggQualityRecipe;

[BepInPlugin("frovix.dragoneggqualityrecipe", "Dragon Egg Quality Recipe", "1.0.0")]
public class DragonEggQualityRecipePlugin : BaseUnityPlugin
{
	public const string ModGuid = "frovix.dragoneggqualityrecipe";

	public const string ModName = "Dragon Egg Quality Recipe";

	public const string ModVersion = "1.0.0";

	internal const string RecipeName = "Frovix_DragonEggQuality2_Recipe";

	internal const string DragonEggPrefab = "DragonEgg";

	internal const int InputCost = 2;

	internal const int OutputQuality = 2;

	private static readonly Harmony Harmony = new Harmony("frovix.dragoneggqualityrecipe");

	internal static Recipe Recipe;

	internal static ItemDrop DragonEggDrop;

	internal static DragonEggQualityRecipePlugin Instance;

	private void Awake()
	{
		Instance = this;
		Harmony.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Loaded. Adds Artisan Table recipe: 2x DragonEgg -> quality 2 DragonEgg.");
	}

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

	internal static void RegisterRecipe(ObjectDB db)
	{
		//IL_013e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0143: Unknown result type (might be due to invalid IL or missing references)
		//IL_014e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0155: 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_0164: Expected O, but got Unknown
		if ((Object)(object)db == (Object)null || (Object)(object)ZNetScene.instance == (Object)null)
		{
			return;
		}
		if (db.m_recipes.Exists((Recipe recipe) => (Object)(object)recipe != (Object)null && ((Object)recipe).name == "Frovix_DragonEggQuality2_Recipe"))
		{
			Recipe = db.m_recipes.Find((Recipe recipe) => (Object)(object)recipe != (Object)null && ((Object)recipe).name == "Frovix_DragonEggQuality2_Recipe");
			return;
		}
		GameObject itemPrefab = db.GetItemPrefab("DragonEgg");
		GameObject prefab = ZNetScene.instance.GetPrefab("piece_artisanstation");
		if ((Object)(object)itemPrefab == (Object)null || (Object)(object)prefab == (Object)null)
		{
			DragonEggQualityRecipePlugin instance = Instance;
			if (instance != null)
			{
				((BaseUnityPlugin)instance).Logger.LogWarning((object)"Could not register recipe because DragonEgg or piece_artisanstation was not found.");
			}
			return;
		}
		DragonEggDrop = itemPrefab.GetComponent<ItemDrop>();
		CraftingStation component = prefab.GetComponent<CraftingStation>();
		if ((Object)(object)DragonEggDrop == (Object)null || (Object)(object)component == (Object)null)
		{
			DragonEggQualityRecipePlugin instance2 = Instance;
			if (instance2 != null)
			{
				((BaseUnityPlugin)instance2).Logger.LogWarning((object)"Could not register recipe because DragonEgg ItemDrop or Artisan Table CraftingStation was not found.");
			}
			return;
		}
		Recipe val = ScriptableObject.CreateInstance<Recipe>();
		((Object)val).name = "Frovix_DragonEggQuality2_Recipe";
		val.m_enabled = true;
		val.m_item = DragonEggDrop;
		val.m_amount = 1;
		val.m_craftingStation = component;
		val.m_minStationLevel = 1;
		val.m_resources = (Requirement[])(object)new Requirement[1]
		{
			new Requirement
			{
				m_resItem = DragonEggDrop,
				m_amount = 2,
				m_amountPerLevel = 0,
				m_recover = true
			}
		};
		db.m_recipes.Add(val);
		Recipe = val;
		DragonEggQualityRecipePlugin instance3 = Instance;
		if (instance3 != null)
		{
			((BaseUnityPlugin)instance3).Logger.LogInfo((object)"Registered Artisan Table recipe: 2x DragonEgg -> quality 2 DragonEgg.");
		}
	}

	internal static bool IsOurRecipe(Recipe recipe)
	{
		if ((Object)(object)recipe != (Object)null)
		{
			return ((Object)recipe).name == "Frovix_DragonEggQuality2_Recipe";
		}
		return false;
	}

	internal static int CountDragonEggs(Player player)
	{
		if ((Object)(object)player == (Object)null || (Object)(object)DragonEggDrop == (Object)null)
		{
			return 0;
		}
		return ((Humanoid)player).GetInventory().CountItems(DragonEggDrop.m_itemData.m_shared.m_name, -1, true);
	}
}
[HarmonyPatch(typeof(ObjectDB), "CopyOtherDB")]
internal static class ObjectDBCopyOtherDBPatch
{
	private static void Postfix(ObjectDB __instance)
	{
		DragonEggQualityRecipePlugin.RegisterRecipe(__instance);
	}
}
[HarmonyPatch(typeof(ObjectDB), "Awake")]
internal static class ObjectDBAwakePatch
{
	private static void Postfix(ObjectDB __instance)
	{
		DragonEggQualityRecipePlugin.RegisterRecipe(__instance);
	}
}
[HarmonyPatch(typeof(Player), "HaveRequirements", new Type[]
{
	typeof(Recipe),
	typeof(bool),
	typeof(int),
	typeof(int)
})]
internal static class PlayerHaveRequirementsPatch
{
	private static void Postfix(Player __instance, Recipe recipe, bool discover, int qualityLevel, int amount, ref bool __result)
	{
		if (DragonEggQualityRecipePlugin.IsOurRecipe(recipe))
		{
			if (discover)
			{
				__result = (Object)(object)DragonEggQualityRecipePlugin.DragonEggDrop == (Object)null || __instance.IsKnownMaterial(DragonEggQualityRecipePlugin.DragonEggDrop.m_itemData.m_shared.m_name);
				return;
			}
			if (!__instance.RequiredCraftingStation(recipe, qualityLevel, true))
			{
				__result = false;
				return;
			}
			int num = 2 * Math.Max(1, amount);
			__result = DragonEggQualityRecipePlugin.CountDragonEggs(__instance) >= num;
		}
	}
}
[HarmonyPatch(typeof(Player), "GetAvailableRecipes")]
internal static class PlayerGetAvailableRecipesPatch
{
	private static void Postfix(Player __instance, ref List<Recipe> available)
	{
		Recipe recipe = DragonEggQualityRecipePlugin.Recipe;
		ItemDrop dragonEggDrop = DragonEggQualityRecipePlugin.DragonEggDrop;
		if (!((Object)(object)recipe == (Object)null) && !((Object)(object)dragonEggDrop == (Object)null) && !available.Contains(recipe) && (__instance.IsKnownMaterial(dragonEggDrop.m_itemData.m_shared.m_name) || DragonEggQualityRecipePlugin.CountDragonEggs(__instance) > 0) && __instance.RequiredCraftingStation(recipe, 1, false))
		{
			available.Add(recipe);
		}
	}
}
[HarmonyPatch(typeof(InventoryGui), "DoCrafting")]
internal static class InventoryGuiDoCraftingPatch
{
	private static readonly FieldInfo CraftRecipeField = AccessTools.Field(typeof(InventoryGui), "m_craftRecipe");

	private static readonly FieldInfo MultiCraftingField = AccessTools.Field(typeof(InventoryGui), "m_multiCrafting");

	private static readonly FieldInfo MultiCraftAmountField = AccessTools.Field(typeof(InventoryGui), "m_multiCraftAmount");

	private static readonly MethodInfo UpdateCraftingPanelMethod = AccessTools.Method(typeof(InventoryGui), "UpdateCraftingPanel", (Type[])null, (Type[])null);

	private static bool Prefix(InventoryGui __instance, Player player)
	{
		//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
		object? value = CraftRecipeField.GetValue(__instance);
		if (!DragonEggQualityRecipePlugin.IsOurRecipe((Recipe)((value is Recipe) ? value : null)))
		{
			return true;
		}
		int num = ((!(bool)MultiCraftingField.GetValue(__instance)) ? 1 : Math.Max(1, (int)MultiCraftAmountField.GetValue(__instance)));
		int num2 = 2 * num;
		bool flag = player.NoCostCheat() || ZoneSystem.instance.GetGlobalKey((GlobalKeys)25);
		if (!flag && DragonEggQualityRecipePlugin.CountDragonEggs(player) < num2)
		{
			((Character)player).Message((MessageType)2, "$msg_missingrequirement", 0, (Sprite)null, false);
			return false;
		}
		Inventory inventory = ((Humanoid)player).GetInventory();
		string name = DragonEggQualityRecipePlugin.DragonEggDrop.m_itemData.m_shared.m_name;
		bool flag2 = (inventory.ItemCheated(name, -1, true) || player.NoCostCheat()) && !PlayerProfile.s_bypassCheatChecks;
		if (!flag)
		{
			inventory.RemoveItem(name, num2, -1, true);
		}
		if (inventory.AddItem("DragonEgg", num, 2, 0, player.GetPlayerID(), player.GetPlayerName(), new Vector2i(-1, -1), flag2, false, true) == null)
		{
			((Character)player).Message((MessageType)2, "$inventory_full", 0, (Sprite)null, false);
			return false;
		}
		UpdateCraftingPanelMethod.Invoke(__instance, new object[1] { false });
		return false;
	}
}