forked from danvolchek/StardewMods
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBetterFruitTreesMod.cs
84 lines (69 loc) · 3.67 KB
/
BetterFruitTreesMod.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using BetterFruitTrees.Patches;
using BetterFruitTrees.Patches.JunimoHarvester;
using BetterFruitTrees.Patches.JunimoHut;
using Harmony;
using StardewModdingAPI;
using StardewValley;
using StardewValley.Buildings;
using StardewValley.Characters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using static BetterFruitTrees.Extensions.ListExtensions;
using SObject = StardewValley.Object;
namespace BetterFruitTrees
{
public class BetterFruitTreesMod : Mod
{
internal static BetterFruitTreesMod Instance;
internal BetterFruitTreesConfig Config;
public override void Entry(IModHelper helper)
{
Utils.Reflection = helper.Reflection;
if (helper.ModRegistry.IsLoaded("cat.fruittreesanywhere"))
{
this.Monitor.Log("You have both this mod, and the old version ('Fruit Trees Anywhere') installed!", LogLevel.Error);
this.Monitor.Log("In order for this mod to work properly, you need to delete the FruitTreesAnywhere folder!", LogLevel.Error);
this.Monitor.Log("This mod does everything the old version does and fruit tree junimo harvesting, so please delete FruitTreesAnywhere!", LogLevel.Error);
helper.Events.GameLoop.SaveLoaded += this.ShowErrorMessage;
return;
}
Instance = this;
this.Config = helper.ReadConfig<BetterFruitTreesConfig>();
new GrowHelper(helper.Events);
HarmonyInstance harmony = HarmonyInstance.Create("cat.betterfruittrees");
Utils.HarvestThreeAtOnce = this.Config.Wait_To_Harvest_Fruit_Trees_Until_They_Have_Three_Fruits__Then_Harvest_All_Three_At_Once;
IList<Tuple<string, Type, Type>> replacements = new List<Tuple<string, Type, Type>>
{
{ nameof(SObject.placementAction), typeof(SObject), typeof(PlacementPatch)}
};
Type junimoHarvesterType = typeof(JunimoHarvester);
IList<Tuple<string, Type, Type>> junimoReplacements = new List<Tuple<string, Type, Type>>
{
{ nameof(JunimoHarvester.tryToHarvestHere), junimoHarvesterType, typeof(TryToHarvestHerePatch) },
{ nameof(JunimoHarvester.update), junimoHarvesterType, typeof(UpdatePatch) },
{ "areThereMatureCropsWithinRadius", typeof(JunimoHut), typeof(AreThereMatureCropsWithinRadiusPatch) }
};
if (!this.Config.Disable_Fruit_Tree_Junimo_Harvesting)
foreach (Tuple<string, Type, Type> item in junimoReplacements)
replacements.Add(item);
foreach (Tuple<string, Type, Type> replacement in replacements)
{
MethodInfo original = replacement.Item2
.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public).ToList()
.Find(m => m.Name == replacement.Item1);
MethodInfo prefix = replacement.Item3.GetMethods(BindingFlags.Static | BindingFlags.Public)
.FirstOrDefault(item => item.Name == "Prefix");
MethodInfo postfix = replacement.Item3.GetMethods(BindingFlags.Static | BindingFlags.Public)
.FirstOrDefault(item => item.Name == "Postfix");
harmony.Patch(original, prefix == null ? null : new HarmonyMethod(prefix),
postfix == null ? null : new HarmonyMethod(postfix));
}
}
private void ShowErrorMessage(object sender, EventArgs e)
{
Game1.showRedMessage("Better Fruit Trees failed to load - please see the console for how to fix this.");
}
}
}