public static void AddEnchantment_Postfix(Tool __instance, ref bool __result, BaseEnchantment enchantment) { if (enchantment != null && __instance is Slingshot && enchantment.IsSecondaryEnchantment()) { var lastUser = AccessTools.FieldRefAccess <Tool, Farmer>("lastUser"); //__instance.RemoveEnchantment(enchantment); __instance.enchantments.Remove(enchantment); enchantment.UnapplyTo(__instance, lastUser(__instance)); foreach (BaseEnchantment existing_enchantment in __instance.enchantments) { if (enchantment.GetType() == existing_enchantment.GetType()) { if (existing_enchantment.GetMaximumLevel() < 0 || existing_enchantment.GetLevel() < existing_enchantment.GetMaximumLevel()) { existing_enchantment.SetLevel(__instance, existing_enchantment.GetLevel() + 1); __result = true; return; } __result = false; return; } } __instance.enchantments.Add(enchantment); enchantment.ApplyTo(__instance, lastUser(__instance)); __result = true; return; } }
public static bool CanAddEnchantment_Prefix(Tool __instance, BaseEnchantment enchantment, ref bool __result) { try { if (enchantment == null) { return(true); // run original logic } if (__instance is MeleeWeapon && enchantment.IsForge()) { if (enchantment is DiamondEnchantment && GetValidForgeEnchantmentsForTool(__instance).Count <= 0) { // No more forge enchantments can be added. __result = false; return(false); } // Enchantment is a normal forge enchantment, check the existing level if there is one foreach (BaseEnchantment exisiting_enchantment in __instance.enchantments) { if (enchantment.GetType() == exisiting_enchantment.GetType()) { if (exisiting_enchantment.GetLevel() >= 3) { __result = false; return(false); } break; } } __result = true; return(false); } return(true); } catch (Exception ex) { ModMonitor.Log($"Failed in {nameof(CanAddEnchantment_Prefix)}:\n{ex}", LogLevel.Error); return(true); // run original logic } }