// Lists the next several enchantments to be applied to the given tool, // up to the given limit. public static List <Prediction> ListForTool(Tool tool, uint limit, bool cumulative = false) { Utilities.CheckWorldReady(); if (!IsAvailable) { throw new UnavailableException("enchantments"); } List <Prediction> predictions = new (); Tool fakeTool = tool.getOne() as Tool; // Start from the next enchantment and continue to the limit. uint fromNumber = Game1.stats.getStat("timesEnchanted"); for (uint number = fromNumber; number < fromNumber + limit; ++number) { // Select a random available enchantment per the base game rules. Random rng = new ((int)number + (int)Game1.uniqueIDForThisGame); var candidates = BaseEnchantment.GetAvailableEnchantmentsForItem(fakeTool); var enchantment = Utility.GetRandom(candidates, rng); predictions.Add(new () { number = number, enchantment = enchantment, }); // If the enchantments are cumulative on the tool itself, // simulate applying this enchantment to be ready for the next. if (cumulative && enchantment != null) { fakeTool.AddEnchantment(enchantment); fakeTool.previousEnchantments.Insert(0, enchantment.GetName()); while (fakeTool.previousEnchantments.Count > 2) { fakeTool.previousEnchantments.RemoveAt(fakeTool.previousEnchantments.Count - 1); } } } return(predictions); }
public static bool Forge_Prefix(Tool __instance, Item item, ref bool __result) { try { BaseEnchantment enchantment = BaseEnchantment.GetEnchantmentFromItem(__instance, item); // This gets displayed twice because the game does it once to display the "Result" item in the menu. ModMonitor.Log($"Adding {(enchantment != null ? (enchantment.IsForge() ? "forge enchantment" : enchantment.GetDisplayName()) : "null enchantment")} from item {(item != null ? item.Name : "null")} to tool {__instance.Name}", LogLevel.Debug); if (enchantment != null && enchantment is DiamondEnchantment) { // Diamond Enchantment is now replaced with up to 3 other random enchantments. Still get a slight discount on the cost. if (GetValidForgeEnchantmentsForTool(__instance).Count <= 0) { __result = false; return(false); } for (int i = 0; i < 3; i++) { // Try to add 3 new enchantments. List <int> valid_forges = GetValidForgeEnchantmentsForTool(__instance); if (valid_forges.Count <= 0) { // Can't add enchantments break; } int index = Game1.random.Next(valid_forges.Count); int random_enchant = valid_forges[index]; switch (random_enchant) { case 0: ModMonitor.Log($"Adding Emerald Enchantment as part of Diamond Enchantment process", LogLevel.Trace); __instance.AddEnchantment(new EmeraldEnchantment()); break; case 1: ModMonitor.Log($"Adding Emerald Aquamarine as part of Diamond Enchantment process", LogLevel.Trace); __instance.AddEnchantment(new AquamarineEnchantment()); break; case 2: ModMonitor.Log($"Adding Ruby Enchantment as part of Diamond Enchantment process", LogLevel.Trace); __instance.AddEnchantment(new RubyEnchantment()); break; case 3: ModMonitor.Log($"Adding Amethyst Enchantment as part of Diamond Enchantment process", LogLevel.Trace); __instance.AddEnchantment(new AmethystEnchantment()); break; case 4: ModMonitor.Log($"Adding Topaz Enchantment as part of Diamond Enchantment process", LogLevel.Trace); __instance.AddEnchantment(new TopazEnchantment()); break; case 5: ModMonitor.Log($"Adding Jade Enchantment as part of Diamond Enchantment process", LogLevel.Trace); __instance.AddEnchantment(new JadeEnchantment()); break; } } __result = true; return(false); // don't run original logic } return(true); // run original logic } catch (Exception ex) { ModMonitor.Log($"Failed in {nameof(Forge_Prefix)}:\n{ex}", LogLevel.Error); return(true); // run original logic } }