Example #1
0
        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;
            }
        }
Example #2
0
 public static bool AddEnchantment_Prefix(Tool __instance, BaseEnchantment enchantment, ref bool __result)
 {
     try
     {
         if (enchantment == null)
         {
             return(true);
         }
         if (!enchantment.IsForge() && !enchantment.IsSecondaryEnchantment())
         {
             // Enchantment is a primary enchantment.
             __instance.enchantments.Add(enchantment);
             enchantment.ApplyTo(__instance, __instance.getLastFarmerToUse());
             __result = true;
             return(false);  // don't run original logic
         }
         if (__instance is MeleeWeapon && enchantment.IsForge())
         {
             if (enchantment is DiamondEnchantment)
             {
                 // Skip adding diamond enchantments, they should result in 3 other enchantments getting added in Forge.
                 __result = true;
                 return(false);  // don't run original logic
             }
             // Enchantment is a Weapon forging or Galaxy Soul enchantment
         }
         return(true);  // run original logic
     }
     catch (Exception ex)
     {
         ModMonitor.Log($"Failed in {nameof(AddEnchantment_Prefix)}:\n{ex}", LogLevel.Error);
         return(true);  // run original logic
     }
 }